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

Python JSON Script Sensor - invalid params json

Votes:

0

I'm trying to create a python script which outputs a (json) file for a sensor. As filename a script parameter should be used, this should be the only parameter. Already reading the parameter passed to the script is not working for me, it seems I make a simple mistake somewhere but can't find it.

My python script sensor points to the script output_file.py and the parameters contain the string "dhcp.txt" The output_file.py looks like this:

"""
# -*- coding: utf-8 -*-
import json
import sys
from prtg.sensor.result import CustomSensorResult

try:
    data = json.loads(sys.argv[1])
    params = json.loads(str(data["params"]))
    filename=params
    f = open("D:/ext_json_sensors/"+filename)
    cont=f.read()
    print(cont)
except Exception as e:
    print('{"prtg":{"error":1,"text":"Script Error"}}')
    with open("D:/ext_json_sensors/error.txt",'w',encoding = 'utf-8') as f:
       f.write(str(repr(e)))
    f.close()  
"""

Like this the sensor keeps falling into the "script error". The exception catched in the error.txt is: JSONDecodeError('Expecting value: line 1 column 1 (char 0)')

After some debugging the issue is in this line params = json.loads(str(data["params"]))

So the json containing the params (and coming from prtg) is not valid json? I think i made a very simple mistake somewhere

Thanks for any help

And the log oputput from PRTG:

--------
07.01.2022 07:43:10 Script File: output_file.py
07.01.2022 07:43:10 Script Parameters: {"tlsexplicit_pop3":"","fastcount":"0","esxpassword":"","hostv6":"","blockedsens":"","secretaccesskeyid":"","tlsexplicit_default":"","isexesensor":"0","windowsloginpassword":"","privatekey":"","secret":"","resttoken":"","reboot":"44567.3131630324","pop3password":"","pythonscript":"output_file.py","tlsexplicit_port":"","inerror":"1","smspassword":"","params":"dhcp.txt","host":"srvvm33.vh4.local","mailpassword":"","tlsexplicit_ftp":"","timeout":"59","elevationpass":"","momopersistent":"","notonpod":"0","linuxloginpassword":"","smtppassword2":"","smtppassword":"","lastuptime":"0","writeresult":"1","reqmsginterval":"60","socksproxypass":"","prtg_linuxpassword":"","tlsexplicit_imap":"","sshelevationpass":"","uptimecount":"0","canlinux":"0","proxypassword":"","proxypass":"","mutexname":"","snmpencpass":"","authuserpwd":"","usednstime":"0","restpassword":"","httppassword":"","addomainpass":"","authtoken":"","oauthrefreshtoken":"","evapassword":"","oauthtoken":"","snmpcommv1":"","sensorid":"5168","ipversion":"0","accesskey":"","xmlhttppassword":"","tlsexplicit_smtp":"","password":"","snmpcommv2":"","snmpauthpass":"","awssk":"","dbpassword":"","pingdompassword":"","prtg_windowspassword":"","usewindowsauthentication":"0","simulate":"0","lastmsg":"Script Error","imappassword":"","ftppassword":""}
07.01.2022 07:43:10 Script Parameters (escaped): {\"tlsexplicit_pop3\":\"\",\"fastcount\":\"0\",\"esxpassword\":\"\",\"hostv6\":\"\",\"blockedsens\":\"\",\"secretaccesskeyid\":\"\",\"tlsexplicit_default\":\"\",\"isexesensor\":\"0\",\"windowsloginpassword\":\"\",\"privatekey\":\"\",\"secret\":\"\",\"resttoken\":\"\",\"reboot\":\"44567.3131630324\",\"pop3password\":\"\",\"pythonscript\":\"output_file.py\",\"tlsexplicit_port\":\"\",\"inerror\":\"1\",\"smspassword\":\"\",\"params\":\"dhcp.txt\",\"host\":\"srvvm33.vh4.local\",\"mailpassword\":\"\",\"tlsexplicit_ftp\":\"\",\"timeout\":\"59\",\"elevationpass\":\"\",\"momopersistent\":\"\",\"notonpod\":\"0\",\"linuxloginpassword\":\"\",\"smtppassword2\":\"\",\"smtppassword\":\"\",\"lastuptime\":\"0\",\"writeresult\":\"1\",\"reqmsginterval\":\"60\",\"socksproxypass\":\"\",\"prtg_linuxpassword\":\"\",\"tlsexplicit_imap\":\"\",\"sshelevationpass\":\"\",\"uptimecount\":\"0\",\"canlinux\":\"0\",\"proxypassword\":\"\",\"proxypass\":\"\",\"mutexname\":\"\",\"snmpencpass\":\"\",\"authuserpwd\":\"\",\"usednstime\":\"0\",\"restpassword\":\"\",\"httppassword\":\"\",\"addomainpass\":\"\",\"authtoken\":\"\",\"oauthrefreshtoken\":\"\",\"evapassword\":\"\",\"oauthtoken\":\"\",\"snmpcommv1\":\"\",\"sensorid\":\"5168\",\"ipversion\":\"0\",\"accesskey\":\"\",\"xmlhttppassword\":\"\",\"tlsexplicit_smtp\":\"\",\"password\":\"\",\"snmpcommv2\":\"\",\"snmpauthpass\":\"\",\"awssk\":\"\",\"dbpassword\":\"\",\"pingdompassword\":\"\",\"prtg_windowspassword\":\"\",\"usewindowsauthentication\":\"0\",\"simulate\":\"0\",\"lastmsg\":\"Script Error\",\"imappassword\":\"\",\"ftppassword\":\"\"}
07.01.2022 07:43:10 Script Path: C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\output_file.py
07.01.2022 07:43:10 Command Line: "C:\Program Files (x86)\PRTG Network Monitor\python\python.exe" -E "C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\output_file.py" "{\"tlsexplicit_pop3\":\"\",\"fastcount\":\"0\",\"esxpassword\":\"\",\"hostv6\":\"\",\"blockedsens\":\"\",\"secretaccesskeyid\":\"\",\"tlsexplicit_default\":\"\",\"isexesensor\":\"0\",\"windowsloginpassword\":\"\",\"privatekey\":\"\",\"secret\":\"\",\"resttoken\":\"\",\"reboot\":\"44567.3131630324\",\"pop3password\":\"\",\"pythonscript\":\"output_file.py\",\"tlsexplicit_port\":\"\",\"inerror\":\"1\",\"smspassword\":\"\",\"params\":\"dhcp.txt\",\"host\":\"srvvm33.vh4.local\",\"mailpassword\":\"\",\"tlsexplicit_ftp\":\"\",\"timeout\":\"59\",\"elevationpass\":\"\",\"momopersistent\":\"\",\"notonpod\":\"0\",\"linuxloginpassword\":\"\",\"smtppassword2\":\"\",\"smtppassword\":\"\",\"lastuptime\":\"0\",\"writeresult\":\"1\",\"reqmsginterval\":\"60\",\"socksproxypass\":\"\",\"prtg_linuxpassword\":\"\",\"tlsexplicit_imap\":\"\",\"sshelevationpass\":\"\",\"uptimecount\":\"0\",\"canlinux\":\"0\",\"proxypassword\":\"\",\"proxypass\":\"\",\"mutexname\":\"\",\"snmpencpass\":\"\",\"authuserpwd\":\"\",\"usednstime\":\"0\",\"restpassword\":\"\",\"httppassword\":\"\",\"addomainpass\":\"\",\"authtoken\":\"\",\"oauthrefreshtoken\":\"\",\"evapassword\":\"\",\"oauthtoken\":\"\",\"snmpcommv1\":\"\",\"sensorid\":\"5168\",\"ipversion\":\"0\",\"accesskey\":\"\",\"xmlhttppassword\":\"\",\"tlsexplicit_smtp\":\"\",\"password\":\"\",\"snmpcommv2\":\"\",\"snmpauthpass\":\"\",\"awssk\":\"\",\"dbpassword\":\"\",\"pingdompassword\":\"\",\"prtg_windowspassword\":\"\",\"usewindowsauthentication\":\"0\",\"simulate\":\"0\",\"lastmsg\":\"Script Error\",\"imappassword\":\"\",\"ftppassword\":\"\"}"
07.01.2022 07:43:10 Script Output (OEMCP Encoding): {"prtg":{"error":1,"text":"Script Error"}}[CR][LF]
07.01.2022 07:43:10 Exit Code: 0
07.01.2022 07:43:10 RawStream Size: 44
07.01.2022 07:43:10 OutputStream Size: 44
07.01.2022 07:43:10 Script Output (UTF8 Encoding): {"prtg":{"error":1,"text":"Script Error"}}[CR][LF]
-------

json prtg python

Created on Jan 7, 2022 7:52:36 AM

Last change on Jan 7, 2022 8:51:30 AM by  Felix Wiesneth [Paessler Support]



5 Replies

Votes:

0

Hello,

The Python Script Advanced sensor requires that it's output is in xml or json format. The exact format needed can be found in our Custom Sensors page. You can also find a example script for the Python sensor on your PRTG core server under "C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\python".


Kind regards,
Sasa Ignjatovic, Tech Support Team

Created on Jan 11, 2022 1:16:10 PM by  Sasa Ignjatovic [Paessler Support]



Votes:

0

Hi

The problem is not what I am passing back to the sensor but how PRTG passes the sensor parameters to my script. It seems to be an issue with escaping but I can't figure it out.

Created on Jan 11, 2022 3:26:29 PM



Votes:

0

How did you configure the parameters in PRTG?
What information does PRTG need to provide to the script?


Kind regards,
Sasa Ignjatovic, Tech Support Team

Created on Jan 13, 2022 8:57:28 AM by  Sasa Ignjatovic [Paessler Support]



Votes:

0

Any news on this case? I got the same Error since 3.11.2022 12:00. There were no updates on the sensor, prtg or windows.

Created on Nov 4, 2022 8:46:35 AM



Votes:

0

I hacked a workaround back in january and forgot about it (it still works). Since we're moving away from prtg there won't be any more troubleshooting from my side.

Here's the python code to split argv until only the param string remains (a filename in our case)

filename=(sys.argv[1].split('params')[1].split(':')[1].split(',')[0].replace('"','').replace(' ',''))

Of course I do not recommend this as a permanent solution.

Created on Nov 4, 2022 10:47:26 AM




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.