I've wrote a PRTG custom sensor in python which is returning a json format result. But every time I set it up, faced wit this error: XML: Structural error in xml file, 1 open items. -- JSON: The returned JSON does not match the expected structure (Invalid JSON.). (code: PE231)
I've tried the sample_sensor.py
too many times and it working properly, but mine not.
Main Controller
import sys import json import requests from datetime import datetime import dicttoxml from paepy.ChannelDefinition import CustomSensorResult if __name__ == '__main__': data_file = open(sys.argv[1], 'r', encoding='iso-8859-1') data = json.load(data_file) datajs = json.dumps(data) datadict = json.loads(datajs) csr = CustomSensorResult() url = "https://" + datadict['host'] + datadict['endpoint'] payload = datadict['payload'] headers = datadict['headers'] br = datetime.now() response = requests.request("POST", url, data=payload, headers=headers) ar = datetime.now() response_time = ar - br response_time = int(float(str(response_time).split(":")[2]) * 1000) csr.add_channel(channel_name="Response Time", unit="TimeResponse", value="%d" % response_time, is_float=False, primary_channel=True) print(csr.get_json_result())
Input JSON File
{ "host": "some host", "endpoint": "/", "payload": "some data", "headers": { "content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "Cache-Control": "no-cache" }, "default_response": "<partial-response><redirect url=\"/errors/viewExpired.xhtml\"></redirect></partial-response>" }
Article Comments
{"prtg": {"text": "This sensor is added to travener.com", "result": [{"LimitMode": 1, "LimitMinError": 10, "Channel": "Percentage", "LimitMaxError": 90, "Unit": "Percent", "Value": 87, "LimitErrorMsg": "Percentage too high"}, {"Unit": "TimeResponse", "Value": 4711, "Channel": "Response Time"}]}}
Jul, 2018 - Permalink
Hi there,
Is this the output of the script via PRTG or is this the output when you manually execute the script?
Best regards.
Jul, 2018 - Permalink
{"prtg": {"text": "This sensor is added to travener.com", "result": [{"LimitMode": 1, "LimitMinError": 10, "Channel": "Percentage", "LimitMaxError": 90, "Unit": "Percent", "Value": 87, "LimitErrorMsg": "Percentage too high"}, {"Unit": "TimeResponse", "Value": 4711, "Channel": "Response Time"}]}}
Jul, 2018 - Permalink
Hello,
It is the result of manually execution. I didn't see any way to check output of PRTG-based output.
Best Regards.
Jul, 2018 - Permalink
Hi there,
Thanks for the output of the manual execution. However we will need the output of the script which PRTG sees. Therefore, please activate the "Write EXE result to disk" option in the sensor's settings and post the log files (Result of Sensor XXX.Data.txt and Result of Sensor XXX.txt) located on the corresponding probe under "C:\ProgramData\Paessler\PRTG Network Monitor\Logs (Sensors)".
Best regards.
Jul, 2018 - Permalink
Hi,
Thanks for your perfect responsibility. Here is the output of PRTG execution.
7/30/2018 7:17:56 PM Script File: flight_search_1_way.py 7/30/2018 7:17:56 PM Script Parameters: {"blockedsens":"","canlinux":"0","fastcount":"0","host":"xxx.xxx.xxx.xxx","hostv6":"","inerror":"1","interfacenumber":"","inum":"","ipversion":"0","isexesensor":"0","lastmsg":"#Y2 Structural error in xml file, 1 open items. @#O231[Invalid JSON.]","lastuptime":"0","mutexname":"","notonpod":"0","params":"\"C:\/Program Files (x86)\/PRTG Network Monitor\/Custom Sensors\/python\/data.json\"","pythonscript":"flight_search_1_way.py","reboot":"43310.8025800463","reqmsginterval":"60","sensorid":"2021","simulate":"0","timeout":"59","tlsexplicit_default":"","tlsexplicit_ftp":"","tlsexplicit_imap":"","tlsexplicit_pop3":"","tlsexplicit_port":"","tlsexplicit_smtp":"","uptimecount":"0","usednstime":"0","usewindowsauthentication":"0","writeresult":"1"} 7/30/2018 7:17:56 PM Script Parameters (escaped): {\"blockedsens\":\"\",\"canlinux\":\"0\",\"fastcount\":\"0\",\"host\":\"xxx.xxx.xxx.xxx\",\"hostv6\":\"\",\"inerror\":\"1\",\"interfacenumber\":\"\",\"inum\":\"\",\"ipversion\":\"0\",\"isexesensor\":\"0\",\"lastmsg\":\"#Y2 Structural error in xml file, 1 open items. @#O231[Invalid JSON.]\",\"lastuptime\":\"0\",\"mutexname\":\"\",\"notonpod\":\"0\",\"params\":\"\\\"C:\\/Program Files (x86)\\/PRTG Network Monitor\\/Custom Sensors\\/python\\/data.json\\\"\",\"pythonscript\":\"flight_search_1_way.py\",\"reboot\":\"43310.8025800463\",\"reqmsginterval\":\"60\",\"sensorid\":\"2021\",\"simulate\":\"0\",\"timeout\":\"59\",\"tlsexplicit_default\":\"\",\"tlsexplicit_ftp\":\"\",\"tlsexplicit_imap\":\"\",\"tlsexplicit_pop3\":\"\",\"tlsexplicit_port\":\"\",\"tlsexplicit_smtp\":\"\",\"uptimecount\":\"0\",\"usednstime\":\"0\",\"usewindowsauthentication\":\"0\",\"writeresult\":\"1\"} 7/30/2018 7:17:56 PM Script Path: C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\flight_search_1_way.py 7/30/2018 7:17:56 PM Command Line: "C:\Program Files (x86)\PRTG Network Monitor\Python34\python.exe" -E "C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\flight_search_1_way.py" "{\"blockedsens\":\"\",\"canlinux\":\"0\",\"fastcount\":\"0\",\"host\":\"xxx.xxx.xxx.xxx\",\"hostv6\":\"\",\"inerror\":\"1\",\"interfacenumber\":\"\",\"inum\":\"\",\"ipversion\":\"0\",\"isexesensor\":\"0\",\"lastmsg\":\"#Y2 Structural error in xml file, 1 open items. @#O231[Invalid JSON.]\",\"lastuptime\":\"0\",\"mutexname\":\"\",\"notonpod\":\"0\",\"params\":\"\\\"C:\\/Program Files (x86)\\/PRTG Network Monitor\\/Custom Sensors\\/python\\/data.json\\\"\",\"pythonscript\":\"flight_search_1_way.py\",\"reboot\":\"43310.8025800463\",\"reqmsginterval\":\"60\",\"sensorid\":\"2021\",\"simulate\":\"0\",\"timeout\":\"59\",\"tlsexplicit_default\":\"\",\"tlsexplicit_ftp\":\"\",\"tlsexplicit_imap\":\"\",\"tlsexplicit_pop3\":\"\",\"tlsexplicit_port\":\"\",\"tlsexplicit_smtp\":\"\",\"uptimecount\":\"0\",\"usednstime\":\"0\",\"usewindowsauthentication\":\"0\",\"writeresult\":\"1\"}" 7/30/2018 7:17:56 PM Script Output (OEMCP Encoding): Traceback (most recent call last):[CR][LF] File "C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\flight_search_1_way.py", line 13, in <module>[CR][LF] data_file = open(sys.argv[1], 'r', encoding='iso-8859-1')[CR][LF]FileNotFoundError: [Errno 2] No such file or directory: '{"blockedsens":"","canlinux":"0","fastcount":"0","host":"xxx.xxx.xxx.xxx","hostv6":"","inerror":"1","interfacenumber":"","inum":"","ipversion":"0","isexesensor":"0","lastmsg":"#Y2 Structural error in xml file, 1 open items. @#O231[Invalid JSON.]","lastuptime":"0","mutexname":"","notonpod":"0","params":"\\"C:\\\\/Program Files (x86)\\\\/PRTG Network Monitor\\\\/Custom Sensors\\\\/python\\\\/data.json\\"","pythonscript":"flight_search_1_way.py","reboot":"43310.8025800463","reqmsginterval":"60","sensorid":"2021","simulate":"0","timeout":"59","tlsexplicit_default":"","tlsexplicit_ftp":"","tlsexplicit_imap":"","tlsexplicit_pop3":"","tlsexplicit_port":"","tlsexplicit_smtp":"","uptimecount":"0","usednstime":"0","usewindowsauthentication":"0","writeresult":"1"}'[CR][LF] 7/30/2018 7:17:56 PM Exit Code: 1 7/30/2018 7:17:56 PM RawStream Size: 1041 7/30/2018 7:17:56 PM OutputStream Size: 1041 7/30/2018 7:17:56 PM Script Output (UTF8 Encoding): Traceback (most recent call last):[CR][LF] File "C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\flight_search_1_way.py", line 13, in <module>[CR][LF] data_file = open(sys.argv[1], 'r', encoding='iso-8859-1')[CR][LF]FileNotFoundError: [Errno 2] No such file or directory: '{"blockedsens":"","canlinux":"0","fastcount":"0","host":"xxx.xxx.xxx.xxx","hostv6":"","inerror":"1","interfacenumber":"","inum":"","ipversion":"0","isexesensor":"0","lastmsg":"#Y2 Structural error in xml file, 1 open items. @#O231[Invalid JSON.]","lastuptime":"0","mutexname":"","notonpod":"0","params":"\\"C:\\\\/Program Files (x86)\\\\/PRTG Network Monitor\\\\/Custom Sensors\\\\/python\\\\/data.json\\"","pythonscript":"flight_search_1_way.py","reboot":"43310.8025800463","reqmsginterval":"60","sensorid":"2021","simulate":"0","timeout":"59","tlsexplicit_default":"","tlsexplicit_ftp":"","tlsexplicit_imap":"","tlsexplicit_pop3":"","tlsexplicit_port":"","tlsexplicit_smtp":"","uptimecount":"0","usednstime":"0","usewindowsauthentication":"0","writeresult":"1"}'[CR][LF]
Best Regards.
Jul, 2018 - Permalink
Hi there,
It seems that the script throws the following error which is why there is no result:
Traceback (most recent call last):[CR][LF] File "C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\flight_search_1_way.py", line 13, in <module>[CR][LF] data_file = open(sys.argv[1], 'r', encoding='iso-8859-1')[CR][LF]FileNotFoundError: [Errno 2] No such file or directory:
Is the file "flight_search_1_way.py" available in the same folder as the python script added to PRTG?
Best regards.
Aug, 2018 - Permalink
Hi there,
Please execute the following command on the Core Server:
C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\ |
What result do you get?
Best regards.
Aug, 2018 - Permalink
Hi,
could you please set the sensor to write the result on disk and post it here please?
Jul, 2018 - Permalink