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())


Article Comments

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?


Jun, 2018 - Permalink

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.


Jun, 2018 - Permalink

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.


Jun, 2018 - Permalink

Dear EvanSteeleOSU,

thank you for sharing the solution of this case.


Jun, 2018 - Permalink