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.


Article Comments

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.


Oct, 2022 - Permalink