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] -------
Add comment