Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Step

Screenshot

Import Libraries (as needed)

import requests

import sys

import JSON

import time

After Python is up, import the requests {a library for doing any URL call}, sys, JSON, and time libraries into the virtual environment.

Info

See https://apica-kb.atlassian.net/wiki/spaces/ASMDOCS/pages/2135393547/Scripted+Check#7.-Appendix for instructions on using a custom module within your script.

 

Set the URL Request

Set the URL call to be an argument.

Make a GET request against that URL.

The response will need to set a default URL to call, if we don't have an argument. Set exceptions as 'e'. and The URL will be http://google.com.

Our script returned a 200 status code. Our script will call either http://google.com or a URL that we provide.

Code Block
languagepy
  try:
        url = sys.argv[1]
except Exception as e:
       url = 'https://google.com'

response = requests.get (url)
print (response.status_code)  url = sys.argv[1)

Add JSON format

What JSON format does Apica’s back-end system expect? Apica’s back-end is based on MongoDB.

MongoDB allows us to have an expandable result format. So that's a result format that you can upload almost anything to, and it will become a part of the result.

We have the start and end times that we need. So the start and end times will be the start and end times of your check in ASM (These will show up in the check Result view in ASM).

  • Set start_time = time.time().

  • Set end_time = time.time().

Note: we're doing the start_time before our URL call and the end_time at the end of our URL call. This measures the time it takes to call this URL.

Set a message. So our message is “URL call returned status code”, then adds a string response (the returned status code) the value that you see here, in the JSON format, it will be the value of the results.

So this is the main value that you will see. Usually, it's the duration, but it could be anything. To show this, let's say that it's the status code, because this is what we're saying in the message. So we'll set it to response.status.code.
After running this, we have our JSON output; by itself, a valid result.

Code Block
languagepy
  try:
        url = sys.argv[1]
except Exception as e:
       url = 'https://google.com'

start_time = time.time()
response = requests.get (url)
end_time time. time()

json_return = (
 "returncode": 0,
 "start_time": start_time,
  "end_time": end_time,
  "message": "URL call returned status code: " + str(response.status_code),
  "unit": "ms",
  "value": response.status.code,
}
 
print(json.dumps(json_return))

 

 

 

 

 

 

 

 

 

 

Expanding the Returned values

Let's expand this a little bit; we have an expandable JSON format, so let's give ourselves more content and data.

How many headers do we have here?

What is the length of the returned content?

Add these lines below the "value" (line 16 above) to return the response header count and the size of the content.

"header_count": Len(response. headers),

"content_size": len(response.content)

 

...