What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
300.000 administrators have chosen PRTG to monitor their network. 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

Your Vote:

Up

Down

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 by  nTech (0) 1

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



3 Replies

Votes:

0

Your Vote:

Up

Down

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

Your Vote:

Up

Down

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 by  nTech (0) 1



Votes:

0

Your Vote:

Up

Down

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]



Please log in or register to enter your reply.


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.