Versions Compared

Key

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

...

Step

Screenshot

Open ASM

Navigate to New Check+

Add Script via Run Python.

The Run Python Scripted Check type icon should be displayed. If you don't have this, you may need to get it unlocked. Please ask your sales team for access because this is a more advanced check, not available for customers by default.

Creating a Run Python Check, Step 1

Enter "New Test Check." Add any description and relevant tags, and then click Next.

Run Python Step 2

Configure this check

  • Resource URL/Github URL

  • Resource Auth Type

  • Resource Auth

  • Resource Path

  • Secondary Resource

  • Script Runner 

  • Script Arguments

  • Location

Info
  • Note that the agent will delete any local files you create after running your script. Any sensitive data written to local files during script execution is deleted at the end of execution.

Resource URL/Github URL: This answers the question, "Where do we find your script?" This could be an HTTP download link, or it can be to your GitHub repository. For this example, go to your repository and copy+paste the URL here, ending with the branch (master/main). Ours is main.

Enter the URL that this script resides at. In this example, it resides in a GitHub Repo at https://github.com/[username]/NewTestRepository/main

Resource Auth Type: This type of resource authorization will be needed, GitHub or HTTP. This example uses GitHub. But if you have your file on an HTTP server, you could use HTTP as the type.

Resource Auth: Resource authorization is required. The authorization header allows you to download resources.

  • It's a basic authorization header when your resource authorization type is HTTP.

    • If you have an HTTP server with no protection, you may do it that way, but Apica does not recommend it because it's not secure.

  • If your auth type is GitHub, this form <USERNAME>:<TOKEN>.

    • Remember, the token is the Personal Access Token that we created back in the first step [it can also be empty if your repository is public].

    • Example if your username is foobar: foobar:ghp_JlvGv7PGTrAzI2LWVIQZDhRthYBBQI1TGl0J

Expand
titleResource Auth Best Practice

To set the Resource Auth, remember that it is a hidden field, so you won't be able to see anything you type here. Apica recommends taking your username as your username without the email domain and then assembling it with the colon and Personal Access Token so you can see it in another location.

For example: if your GitHub username is foobar@gmail.com, your username will be 'foobar,' without @gmail.com.

Then append the colon ':.'

Finally, add the Personal Access Token, and your resource authorization looks like this and is ready to copy into that field:

foobar:ghp_JlvGv7PGTrAzI2LWVIQZDhRthYBBQI1TGl0J

Resource Path: This is the path inside your repository to the scripts you want to run. Our example scripts are just in the base level repository, so enter main.py

Secondary Resource: If your script requires any sort of additional files, you can use this secondary resource to download another file. However, you can also start your script off by downloading the file directly: That way, you can use any sort of security you want to protect it. For example, you could have a secondary resource, like a certificate protected by OAuth: Your script could go through the whole OAuth process and then use the local file.

In this example, the secondary resource will be blank because it is unnecessary.

It is possible to reference subfolders from a base directory using the “Secondary Resource” field. For instance, if your use case requires a “/python/main.py” file and main.py depends on a module defined in /python/modules, you can specify /python, and the check runner will recognize the module because it is able to “search” the /python folder for secondary resources.

For example, if “local_module_sample.py” depends on a subfolder in /python, you can specify the project like so:

Script Runner: Python is pre-selected (as the only choice).

Script Arguments: These will be provided if we enter them on the command lines. Enter http://example.com So, we will pass this argument to our script.If using the ArgumentParser for example, you can use your arguments in this field.

Code Block
### EXAMPLE ###

from argparse import ArgumentParser

parse = ArgumentParser()
parse.add_argument('-u', '--username')
parse.add_argument('-p', '--password')
args = parse.parse_args()

myUsername = str(args.username)

In the field you would define your arguments as below

--username MyUsername --password MyPassword

or

-u MyUsername -p MyPassword

Note

Location: you must use either the “+1 Sweden, Stockholm, [amazon]” location OR a private agent with the necessary software installed to run the check! Not all check locations have the necessary software required to run the check.

Consult Apica Support or your TAM if you would like more information on running Scripted Checks on a private agent.

Step 3 Interval, Thresholds & Monitor Groups

In this example, we will be creating a manual check.

Select an interval, if needed, and check the groups you want this check to be a part of.

Click Next.

Confirm Your Check

A Confirmation Page will be displayed for you to either go Back to edit it or Create to continue.

If you are satisfied, you click Create to create the check.

Check Created

  • Uncheck Enable Failover (which is checked by default) because we don't want to have that enabled right now, as this is just for demo purposes.

  • Set the max attempts to 1 because we want the check to fail quickly for the test.

  • Click Save.

Apica generally recommends these settings for testing because what can happen is too long with the default behaviors. If Max Attempts remains at three and the Attempt Pause for each attempt is 30 seconds, this means that your test check could wait up to 90 seconds if it's failing. And so these settings don't help when trying just to debug something; it's better to have the information that your check failed from the beginning.

Click the Check Details button in the upper right as we're ready to run our check.

Check Details Page

The Check Details page has a section called "Status Last 24 Hours," and beneath that will be a "Run Check" icon. Click to run manually.

Check Results

In this example, we set the “Last Value” to the status code by assigning it to the variable “value” in the script.

 

 

Drill down

Drilling into these results, we see the Result value (ms) is 200 because, even though the typical value for a result is the number of milliseconds it took to respond, we specified in our JSON that the value would be the response status code, so the 200 is displayed in its place. The number of Attempts is shown as 1, and beneath the result, code is the JSON that we specified:

Code Block
languagepy
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,
    "headed count": len(response.headers),
    "content_size": len(response.content),
    "headers": dict(response.headers)
}

Messaging via JSON

From Returned Value Table View, note the message it says URL call return status code 200. But this is the message that we sent inside of our JSON. So message when you set it will be placed here.

So you can record any data you like, and it will show up here.

...