New Question
 
 
PRTG Network Monitor

Intuitive to Use.
Easy to manage.

200.000 administrators have chosen PRTG to monitor their network. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free PRTG
Download >>

 

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

 

Top Tags


View all Tags


Custom Sensor Written in Python not Working

Votes:

0

Your Vote:

Up

Down

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>"
}

custom-script-exe custom-sensor prtg python python-script-advanced-sensor sensor

Created on Jul 28, 2018 10:09:21 AM by  mojtaba (0) 1



11 Replies

Votes:

0

Your Vote:

Up

Down

Hi,

could you please set the sensor to write the result on disk and post it here please?

Created on Jul 30, 2018 6:16:48 AM by  Sven Soost (0) 1



Votes:

0

Your Vote:

Up

Down

{"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"}]}}

Created on Jul 30, 2018 7:19:08 AM by  mojtaba (0) 1

Last change on Jul 30, 2018 7:20:08 AM by  Dariusz Gorka [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Hi there,

Is this the output of the script via PRTG or is this the output when you manually execute the script?

Best regards.

Created on Jul 30, 2018 7:21:06 AM by  Dariusz Gorka [Paessler Support]



Votes:

0

Your Vote:

Up

Down

{"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"}]}}

Created on Jul 30, 2018 7:21:52 AM by  mojtaba (0) 1

Last change on Jul 30, 2018 7:24:18 AM by  Dariusz Gorka [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Hello,

It is the result of manually execution. I didn't see any way to check output of PRTG-based output.

Best Regards.

Created on Jul 30, 2018 7:23:41 AM by  mojtaba (0) 1



Votes:

0

Your Vote:

Up

Down

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.

Created on Jul 30, 2018 7:25:15 AM by  Dariusz Gorka [Paessler Support]



Votes:

0

Your Vote:

Up

Down

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.

Created on Jul 30, 2018 7:23:31 PM by  mojtaba (0) 1



Votes:

0

Your Vote:

Up

Down

Hello there,

I'm waiting for your response.

Thanks.

Created on Aug 1, 2018 7:01:52 AM by  mojtaba (0) 1



Votes:

0

Your Vote:

Up

Down

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.

Created on Aug 1, 2018 1:10:47 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Hi,

Yes they are in same folder.

Created on Aug 14, 2018 7:45:06 AM by  mojtaba (0) 1



Votes:

0

Your Vote:

Up

Down

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.

Created on Aug 14, 2018 11:31:02 AM by  Dariusz Gorka [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.