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 return error

Votes:

0

XML: Structural error in xml file, 1 open items. -- JSON: The returned JSON does not match the expected structure (Invalid JSON.). (code: PE231)

I am getting a strange error and not sure where to start, here is the python code, it is based on the sample script provided which runs fine and imports into PRTG. But I get the above error when I run my script. If I run from the command line I get this error:

Traceback (most recent call last):
  File "c:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\python\prtg_twilio.py", line 9, in <module>
    from prtg.sensor.result import CustomSensorResult
  File "c:\Program Files (x86)\PRTG Network Monitor\Python34\lib\site-packages\prtg\sensor\result.py", line 52
    _channels: list = []


Script:

# -*- coding: utf-8 -*-

import json
import sys
from twilio.rest import Client
import datetime
from datetime import timedelta

from prtg.sensor.result import CustomSensorResult
from prtg.sensor.units import ValueUnit

if __name__ == "__main__":

    account_sid = 'xxx'
    auth_token = 'xxx'
    client = Client(account_sid, auth_token)

    calls = client.calls.list(limit=200)
    total_calls = []
    no_answer = []

    for record in calls:
        d1 = datetime.datetime(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day, 0, 0,
                               0, 0, datetime.timezone(-timedelta(hours=7)))
        if record.date_created > d1 and record.direction == 'outbound-api':
            total_calls.append(record)
            print(record.date_created)
            if record.status == "no-answer":
                no_answer.append(record)

    try:
        data = json.loads(sys.argv[1])

        csr = CustomSensorResult(text="This sensor runs on %s" % data["host"])

        csr.add_primary_channel(name="No Answer Calls Today",
                                value=int(len(no_answer)),
                                unit=ValueUnit.COUNT,
                                is_float=False,
                                is_limit_mode=True,
                                limit_min_error=0,
                                limit_max_error=5,
                                limit_error_msg="No Answer calls too high")

        csr.add_channel(name="Total Calls Today",
                        value=int(len(total_calls)),
                        unit=ValueUnit.COUNT)

        print(csr.json_result)
    except Exception as e:
        csr = CustomSensorResult(text="Python Script execution error")
        csr.error = "Python Script execution error: %s" % str(e)
        print(csr.json_result)

json prtg python twilio

Created on Jul 7, 2020 8:59:24 PM

Last change on Jul 8, 2020 6:13:21 AM by  Moritz Heller [Paessler Support]



10 Replies

Votes:

0

Hi there,

It seems like that your script has an issue with the module "CustomSensorResult". Please ensure that this module is available on the corresponding Probe.
In addition, you can enable the option " Write Results To Disk" in your Sensor settings. This will create additional log files in the "Logs > Sensors" folder in the PRTG Data Directory of the corresponding Probe (https://kb.paessler.com/en/topic/463).

Created on Jul 8, 2020 6:16:40 AM by  Moritz Heller [Paessler Support]



Votes:

0

The library is available we only have the one probe and server on all on the same box. It seemed to find the file but not like the code. Which is odd. The example script runs fine. I will post a saved output shortly

Created on Jul 8, 2020 1:53:06 PM



Votes:

0

Hi there,

Please note that this script will executed in 32bit mode. This is due to the Probe service process which is also a 32bit process. We made the experience that some modules are not available in 32bit.

Created on Jul 9, 2020 5:37:13 AM by  Moritz Heller [Paessler Support]



Votes:

0

Here is the log, but it looks like it is running python from the /python dir, not the /python34 dir: 7/9/2020 5:11:03 PM Script File: prtg_twilio.py 7/9/2020 5:11:03 PM Script Parameters: {"tlsexplicit_pop3":"","fastcount":"0","esxpassword":"","hostv6":"","blockedsens":"","secretaccesskeyid":"","tlsexplicit_default":"","isexesensor":"0","windowsloginpassword":"","privatekey":"","secret":"","resttoken":"","reboot":"44020.7072764931","pop3password":"","pythonscript":"prtg_twilio.py","tlsexplicit_port":"","inerror":"1","smspassword":"","params":"","host":"www.twilio.com","mailpassword":"","tlsexplicit_ftp":"","timeout":"299","elevationpass":"","momopersistent":"","notonpod":"0","linuxloginpassword":"","smtppassword2":"","smtppassword":"","lastuptime":"0","writeresult":"1","reqmsginterval":"300","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":"7374","ipversion":"0","accesskey":"","xmlhttppassword":"","tlsexplicit_smtp":"","password":"","snmpcommv2":"","snmpauthpass":"","awssk":"","dbpassword":"","pingdompassword":"","prtg_windowspassword":"","usewindowsauthentication":"0","simulate":"0","lastmsg":"#Y2 Structural error in xml file, 3 open items. @#O231[Invalid JSON.]","imappassword":"","ftppassword":""} 7/9/2020 5:11:03 PM Script Parameters (escaped): {\"tlsexplicit_pop3\":\"\",\"fastcount\":\"0\",\"esxpassword\":\"\",\"hostv6\":\"\",\"blockedsens\":\"\",\"secretaccesskeyid\":\"\",\"tlsexplicit_default\":\"\",\"isexesensor\":\"0\",\"windowsloginpassword\":\"\",\"privatekey\":\"\",\"secret\":\"\",\"resttoken\":\"\",\"reboot\":\"44020.7072764931\",\"pop3password\":\"\",\"pythonscript\":\"prtg_twilio.py\",\"tlsexplicit_port\":\"\",\"inerror\":\"1\",\"smspassword\":\"\",\"params\":\"\",\"host\":\"www.twilio.com\",\"mailpassword\":\"\",\"tlsexplicit_ftp\":\"\",\"timeout\":\"299\",\"elevationpass\":\"\",\"momopersistent\":\"\",\"notonpod\":\"0\",\"linuxloginpassword\":\"\",\"smtppassword2\":\"\",\"smtppassword\":\"\",\"lastuptime\":\"0\",\"writeresult\":\"1\",\"reqmsginterval\":\"300\",\"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\":\"7374\",\"ipversion\":\"0\",\"accesskey\":\"\",\"xmlhttppassword\":\"\",\"tlsexplicit_smtp\":\"\",\"password\":\"\",\"snmpcommv2\":\"\",\"snmpauthpass\":\"\",\"awssk\":\"\",\"dbpassword\":\"\",\"pingdompassword\":\"\",\"prtg_windowspassword\":\"\",\"usewindowsauthentication\":\"0\",\"simulate\":\"0\",\"lastmsg\":\"#Y2 Structural error in xml file, 3 open items. @#O231[Invalid JSON.]\",\"imappassword\":\"\",\"ftppassword\":\"\"} 7/9/2020 5:11:03 PM Script Path: C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\prtg_twilio.py 7/9/2020 5:11:03 PM Command Line: "C:\Program Files (x86)\PRTG Network Monitor\python\python.exe" -E "C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\prtg_twilio.py" "{\"tlsexplicit_pop3\":\"\",\"fastcount\":\"0\",\"esxpassword\":\"\",\"hostv6\":\"\",\"blockedsens\":\"\",\"secretaccesskeyid\":\"\",\"tlsexplicit_default\":\"\",\"isexesensor\":\"0\",\"windowsloginpassword\":\"\",\"privatekey\":\"\",\"secret\":\"\",\"resttoken\":\"\",\"reboot\":\"44020.7072764931\",\"pop3password\":\"\",\"pythonscript\":\"prtg_twilio.py\",\"tlsexplicit_port\":\"\",\"inerror\":\"1\",\"smspassword\":\"\",\"params\":\"\",\"host\":\"www.twilio.com\",\"mailpassword\":\"\",\"tlsexplicit_ftp\":\"\",\"timeout\":\"299\",\"elevationpass\":\"\",\"momopersistent\":\"\",\"notonpod\":\"0\",\"linuxloginpassword\":\"\",\"smtppassword2\":\"\",\"smtppassword\":\"\",\"lastuptime\":\"0\",\"writeresult\":\"1\",\"reqmsginterval\":\"300\",\"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\":\"7374\",\"ipversion\":\"0\",\"accesskey\":\"\",\"xmlhttppassword\":\"\",\"tlsexplicit_smtp\":\"\",\"password\":\"\",\"snmpcommv2\":\"\",\"snmpauthpass\":\"\",\"awssk\":\"\",\"dbpassword\":\"\",\"pingdompassword\":\"\",\"prtg_windowspassword\":\"\",\"usewindowsauthentication\":\"0\",\"simulate\":\"0\",\"lastmsg\":\"#Y2 Structural error in xml file, 3 open items. @#O231[Invalid JSON.]\",\"imappassword\":\"\",\"ftppassword\":\"\"}" 7/9/2020 5:11:03 PM Script Output (OEMCP Encoding): Traceback (most recent call last):[CR][LF] File "C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\prtg_twilio.py", line 5, in <module>[CR][LF] from twilio.rest import Client[CR][LF] File "C:\Program Files (x86)\PRTG Network Monitor\python\lib\twilio\rest\__init__.py", line 18, in <module>[CR][LF] from twilio.http.http_client import TwilioHttpClient[CR][LF] File "C:\Program Files (x86)\PRTG Network Monitor\python\lib\twilio\http\http_client.py", line 1, in <module>[CR][LF] from requests import Request, Session, hooks[CR][LF]ModuleNotFoundError: No module named 'requests'[CR][LF] 7/9/2020 5:11:03 PM Exit Code: 1 7/9/2020 5:11:03 PM RawStream Size: 565 7/9/2020 5:11:03 PM OutputStream Size: 565 7/9/2020 5:11:03 PM Script Output (UTF8 Encoding): Traceback (most recent call last):[CR][LF] File "C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\prtg_twilio.py", line 5, in <module>[CR][LF] from twilio.rest import Client[CR][LF] File "C:\Program Files (x86)\PRTG Network Monitor\python\lib\twilio\rest\__init__.py", line 18, in <module>[CR][LF] from twilio.http.http_client import TwilioHttpClient[CR][LF] File "C:\Program Files (x86)\PRTG Network Monitor\python\lib\twilio\http\http_client.py", line 1, in <module>[CR][LF] from requests import Request, Session, hooks[CR][LF]ModuleNotFoundError: No module named 'requests'[CR][LF]

Created on Jul 9, 2020 5:15:07 PM

Last change on Jul 10, 2020 5:47:07 AM by  Moritz Heller [Paessler Support]



Votes:

0

Hi there,

It's still a module which causes the issue:

ModuleNotFoundError: No module named 'requests'

Unfortunately, we don't assist by coding custom scripts. For more information, why we don't offer any customization help for PRTG, please follow this link.

Created on Jul 10, 2020 5:54:57 AM by  Moritz Heller [Paessler Support]



Votes:

0

But that same module works fine if outside prtg. The only thing I did different was import the prtg library and format the return by replacing some values in the CSR field.

Created on Jul 11, 2020 12:55:46 PM



Votes:

0

To add modules to the PRTG integrated Python Interpreter they have to reside in the interpreter's site-packages directory. The same can be found at <PRTG Install Directory>\Python34\Lib\site-packages.
After adding the package there, you should be able to import the same in a script run by PRTG.

Created on Jul 13, 2020 6:23:20 AM by  Moritz Heller [Paessler Support]



Votes:

0

Hello there.

As of PRTG version 22.1.74 it is possible to install/add modules to PRTG's python. If my script needs the requests module for example, I can do so by heading to C:\Program Files (x86)\PRTG Network Monitor\python\ (PRTG's install path) and issuing:

python.exe -m pip install requests

Afterwards requests can be used in scripts. This has to be done on a per-probe basis.

Best regards,
Luciano Lingnau, Paessler Team

Created on Feb 23, 2022 8:42:48 AM by  Luciano Lingnau [Paessler]

Last change on Feb 23, 2022 9:16:12 AM by  Luciano Lingnau [Paessler]



Votes:

1

Hi Luciano,

We're running 22.2.77.2204 x64 here and I'm still getting ModuleNotFoundError: No module named 'requests' in my logs, even after following your install step.

(BTW the script runs fine when executed directly in a PowerShell window).

I've performed the ./python.exe -m pip install requests step from both a user and admin-level P$ window:

PS C:\Program Files (x86)\PRTG Network Monitor\python> ./python.exe -m pip install requests
Requirement already satisfied: requests in c:\users\myusername\appdata\roaming\python\python39\site-packages (2.28.1)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\myusername\appdata\roaming\python\python39\site-packages (from requests) (2022.6.15)
Requirement already satisfied: charset-normalizer<3,>=2 in c:\users\myusername\appdata\roaming\python\python39\site-packages (from requests) (2.1.1)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\myusername\appdata\roaming\python\python39\site-packages (from requests) (1.26.11)
Requirement already satisfied: idna<4,>=2.5 in c:\users\myusername\appdata\roaming\python\python39\site-packages (from requests) (3.3)

[notice] A new release of pip available: 22.2 -> 22.2.2
[notice] To update, run: python.exe -m pip install --upgrade pip
PS C:\Program Files (x86)\PRTG Network Monitor\python> ./python.exe -m pip3 install requests
C:\Program Files (x86)\PRTG Network Monitor\python\python.exe: No module named pip3
PS C:\Program Files (x86)\PRTG Network Monitor\python>


You wrote "This has to be done on a per-probe basis", but installing requests in this manner appears to have a global effect. How do I make 'requests' available to this sensor?


- Greig.

Created on Aug 21, 2022 11:48:48 PM



Votes:

1

Aha!

For the benefit of anyone who finds this thread in future, the solution is that you need to be logged into Windows as THE Administrator account to install the required modules. Being logged in as AN administrator is insufficient.

Oh, and you also need to be on a release later than 22.2.77.2204:

PRTG Version 22.2.77.2204 broke compatibility with pip.exe install (to install Python packages). With this PRTG release we have removed support for this command to prevent unexpected errors. As of now the only supported way of installing a Python package to use Python Script Advanced sensor is python.exe -m pip install. Please also see our Knowledge Base article for more information about Python installation: https://kb.paessler.com/en/topic/90686.


- Greig.

Created on Sep 6, 2022 5:11:02 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.