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 custom sensor incorrect output

Votes:

0

I'm trying to use a custom Python script to count some files in a directory. In the code, I use this to count said files

file_count = sum((len(f) for _, _, f in os.walk("Q:\some\location")))

Testing with the included Python build works great, I am able to get an accurate count. However, when PRTG runs the script, the output is always wrong (always at 0 files, no matter how many are there). Even when I put out the log, the output was still wrong in the log. Then, to make absolutely sure, I copied-pasted the line in the log labeled Command Line: into Powershell, and got the correct response. Somehow, only when PRTG runs it, I get the wrong values. The full script is located below:

import sys
import json
import os
# get CustomSensorResult from paepy package
from paepy.ChannelDefinition import CustomSensorResult

if __name__ == "__main__":

    # start by counting the number of files in the directory
    file_count = sum((len(f) for _, _, f in os.walk("Q:\somewhere\over\there")))

    if file_count == 0:
        result = CustomSensorResult("No error logs found")
    else:
        result = CustomSensorResult("One or more error logs detected")

    result.add_channel(channel_name="Connection Errors", is_limit_mode=True, limit_max_error=10, unit="Percent", value=file_count, is_float=False, primary_channel=True)
    print(result.get_json_result())

prtg python-script-advanced-sensor python3-6-1

Created on Jun 28, 2018 10:18:53 PM



Best Answer

Accepted Answer

Votes:

0

Got it! I changed the share path from an assigned drive letter to an absolute path:

file_count = sum((len(f) for _, _, f in os.walk("Q:\somewhere\over\there")))

became

file_count = sum((len(f) for _, _, f in os.walk("\\\\theshare.somewhere.com\somewhere\over\there")))

and it now works on all fronts.

Created on Jun 29, 2018 8:04:47 PM



4 Replies

Votes:

0

Dear EvanSteeleOSU,

please change the security context to the context of the device, and make sure you enter proper Windows credentials in the device's settings tab. Do you now get a non-zero result?

Created on Jun 29, 2018 12:29:53 PM by  Arne Seifert [Paessler Support]



Votes:

0

No luck there, it still gives a zero result. For context, here's what the command line looks like when I run it manually: PS C:\Users\SHS_steelee> & "C:\Program Files (x86)\PRTG Network Monitor\Python34\python.exe" "C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\service_sensor.py {"prtg": {"text": "Connection Errors Detected", "result": [{"Channel": "Connection Errors", "LimitMode": 1, "Value": 28, "LimitMaxError": 10, "Unit": "Percent"}]} This is the correct output. The sensor log has this as the command it runs:

Command Line: "C:\Program Files (x86)\PRTG Network Monitor\Python34\python.exe" -E "C:\Program Files (x86)\PRTG Network Monitor\custom sensors\python\service_sensor.py" "{\"blockedsens\":\"\",\"canlinux\":\"0\",\"fastcount\":\"0\",\"host\":\"thehost.somewhere.com\",\"hostv6\":\"\",\"interfacenumber\":\"\",\"inum\":\"\",\"ipversion\":\"0\",\"isexesensor\":\"0\",\"lastuptime\":\"0\",\"mutexname\":\"\",\"notonpod\":\"0\",\"params\":\"\",\"pythonscript\":\"service_sensor.py\",\"reboot\":\"43278.9046695023\",\"reqmsginterval\":\"300\",\"sensorid\":\"2779\",\"simulate\":\"0\",\"timeout\":\"299\",\"tlsexplicit_default\":\"\",\"tlsexplicit_ftp\":\"\",\"tlsexplicit_imap\":\"\",\"tlsexplicit_pop3\":\"\",\"tlsexplicit_port\":\"\",\"tlsexplicit_smtp\":\"\",\"uptimecount\":\"0\",\"usednstime\":\"0\",\"usewindowsauthentication\":\"1\",\"windowslogindomain\":\"myDomain\",\"windowsloginpassword\":********,\"windowsloginusername\":\"myusername\",\"writeresult\":\"1\"}"

Again, running this manually gets the correct answer too:

{"prtg": {"text": "Connection Errors Detected", "result": [{"Channel": "Connection Errors", "Unit": "Percent", "LimitMode": 1, "Value": 28, "LimitMaxError": 10}]}}

However, the output in the logs is still wrong:

Script Output (UTF8 Encoding): {"prtg": {"text": "No service connection errors detected", "result": [{"LimitMaxError": 10, "Value": 0, "Channel": "Connection Errors", "Unit": "Percent", "LimitMode": 1}]}}[CR][LF]

I have tried adding the suggested security context and the proper Windows credentials to no effect.

Edit: Just in case this is relevant, the location that the script is monitoring is a network share.

Created on Jun 29, 2018 7:33:39 PM

Last change on Jun 29, 2018 8:01:02 PM by  Luciano Lingnau [Paessler]



Accepted Answer

Votes:

0

Got it! I changed the share path from an assigned drive letter to an absolute path:

file_count = sum((len(f) for _, _, f in os.walk("Q:\somewhere\over\there")))

became

file_count = sum((len(f) for _, _, f in os.walk("\\\\theshare.somewhere.com\somewhere\over\there")))

and it now works on all fronts.

Created on Jun 29, 2018 8:04:47 PM



Votes:

0

Dear EvanSteeleOSU,

thank you for sharing the solution of this case.

Created on Jun 29, 2018 8:22:40 PM by  Arne Seifert [Paessler Support]




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.