What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Sensor Python script

Votes:

0

Hello guys.

What is the output structure of a JSON for a Python script sensor? I tried to orient myself by the example sensor and could not. Below is the JSON part of my code, it will only have 1 channel, called level, test3 is the variable of my script that has the value I want to show in the sensor.

data = json.loads(sys.argv[1])

        csr = CustomSensorResult(text="This sensor runs on %s" % data["host"])

        csr.add_primary_channel(name="Level",
                                value=test3,
                                unit=ValueUnit.PERCENT,
                                is_float=False,
                                is_limit_mode=True,
                                limit_min_error=30,
                                limit_error_msg="Percentage too high")


        print(csr.json_result)
    except Exception as e:
        csr = CustomSensorResult(text="Python Script execution error")
        csr.error = "Python Script execution error: %s" % str(e)
        print(csr.json_result)

I also noticed that in the example sensor, there is this structure:

if __name__ == "__main__":
    try:

What does she mean? Should I put it at the beginning or end of my code?

I imported the json, sys, from paesslerag_prtg_sensor_api.sensor.result import CustomSensorResult and from paesslerag_prtg_sensor_api.sensor.units import ValueUnit. But still it doesn't work.

json python script

Created on Oct 5, 2022 8:22:20 PM

Last change on Oct 6, 2022 7:10:22 AM by  Felix Wiesneth [Paessler Support]



1 Reply

Votes:

0

Hello,

the line with __name__ prevents the code from being executed if this file is used as import. Instead, it has to be the actual program.

The sensor interface itself expects a Script-Advanced result set, either as XML or JSON. In order to make the code more readable we provide a module for this. The example script shows how it is used. You need exactly one primary channel, and you can have 1 to 49 more channels as normal, non-primary channels.

Created on Oct 17, 2022 11:36:55 AM by  Arne Seifert [Paessler Support]

Last change on Oct 17, 2022 11:37:10 AM by  Arne Seifert [Paessler Support]




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.