Get performance value from JSON output

How to extract performance value out of JSON output?

Q: the program I executed in “Script or Program” monitor returned JSON output. What is the simplest way to extract certain performance value out of it?

A: one can use “one-liner” Python script (run by bundled Python interpreter) to extract required value. Below are two examples.

Sample JSON output

Assume the program you run prints to standard output the below JSON value:

[
    {
        "key": "Value1",
        "value": 13
    },
    {
        "key": "Value2",
        "value": 165
    }
]

and you need to monitor field references by key “value” from second record of the above defined array (which will provide performance value of 165).

To extract those, add to monitor definition (most probably to “Arguments” field) the below snippet to pipe the program output the following way:

| "C:\Program Files (x86)\IPHost Network Monitor\Python-3.6.4\python.exe" -c "import sys, json; print(json.load(sys.stdin)[1]['value'])"

Pay attention to the bold part of the line: it extracts the mentioned element from JSON output. The entire line shown above should be entered without line breaks.

Extract file length of an object in AWS S3 bucket

Let’s try more real-life example. Assume we have a S3 AWS bucket (storage unit) and we would like to monitor size of certain object (file) in it. To do that, follow the below steps:

1. Install AWS command-line utility and make sure it’s in system PATH.

2. Create proper user account in IAM, and obtain its access key and secret key to interact with AWS S3. For the sake of this example, let’s assume the access key string is “AAAAA”, and secret key string is “SSSSSSSSS” (use actual keys in your case).

In IPHost GUI client, proceed to “Settings > User Credentials”, add new Windows credentials, name it “AWS S3 access” (you can use any name), and fill as following:

  • Domain: .
  • User: AAAAA
  • Password: SSSSSSSSS

Once again, use your real-life credentials instead of the above placeholders. Save the newly created credentials.

3. Create file “C:\Scripts\aws-s3-get-metadata.cmd” with contents like below:

@echo off
set AWS_ACCESS_KEY_ID=%1
set AWS_SECRET_ACCESS_KEY=%2
"C:\Program Files\Amazon\AWSCLI\bin\aws.exe" s3api head-object --bucket "%3" --key "%4" | "C:\Program Files (x86)\IPHost Network Monitor\Python-3.6.4\python.exe" -c "import sys, json; print(json.load(sys.stdin)['ContentLength'])"

Use any other script file name/path, provided they are readable by account under which IPHost monitoring service is running (by default, SYSTEM).

4. Create “Script or Program” monitor on arbitrary host (e.g., on “localhost”), and fill its parameters like below:

Monitor name: any unique name

Mode: Run program

Path: C:\Windows\System32\cmd.exe

Arguments: /c C:\Scripts\aws-s3-get-metadata.cmd $WindowsUser $WindowsPassword bucket-name path-to-object

Credentials: those created at step 2 (“AWS S3 access”).

“bucket-name” should be the S3 URL bucket name, and “path-to-object” should be the path without leading slash.

For example, if URL to object is s3://example-bucket/location/of/my/file, then replace

  • “bucket-name” with “example-bucket”
  • “path-to-object” with “location/of/my/file”
Related topics