pythonnagiosnrpe

NRPE Python script output bug


I have been tasked with making a custom python script (since i'm bad with Bash) to run on a remote NRPE client which recursively counts the number of files in the /tmp directory. This is my script:

#!/usr/bin/python3.5
import os
import subprocess
import sys
file_count = sum([len(files) for r, d, files in os.walk("/tmp")]) #Recursive check of /tmp




if file_count < 1000:
        x = subprocess.Popen(['echo', 'OK -', str(file_count), 'files in /tmp.'], stdout=subproce$
        print(x.communicate()[0].decode("utf-8")) #Converts from byteobj to str
#       subprocess.run('exit 0', shell=True, check=True) #Service OK  - exit 0
        sys.exit(0)

elif 1000 <= file_count < 1500:
        x = subprocess.Popen(['echo', 'WARNING -', str(file_count), 'files in /tmp.'], stdout=sub$
        print(x.communicate()[0].decode("utf-8")) #Converts from byteobj to str
        sys.exit(1)
else:
        x = subprocess.Popen(['echo', 'CRITICAL -', str(file_count), 'files in /tmp.'], stdout=su$
        print(x.communicate()[0].decode("utf-8")) #Converts from byteobj to str
        sys.exit(2)

EDIT 1: I tried hardcoding file_count to 1300 and I got a WARNING: 1300 files in /tmp. It appears the issue is solely in the nagios server's ability to read files in the client machine's /tmp.

What I have done:

The output:
correct output is: OK - 3 files in /tmp

The server output for reference:

https://puu.sh/BioHW/838ba84c3e.png

(Ignore the bottom server, any issues solved with the wp-proxy will also be changed on the wpreess-gkanc1)

EDIT 2: I ran the following on the nagios server:

/usr/local/nagios/libexec/check_nrpe -H 192.168.1.59 -c check_tmp_folder 

I indeed got a 0 file return. I still don't know how this can be fixed, however.


Solution

  • SOLVED!

    Solution:

    and ignore all system.slice results

    Problem solved.

    Short explanation: The main reason for that issue is, that with debian 9.x, some processes which use systemd forced the private tmp directories by default. So if you have any other programs which have issues searching or indexing in /tmp, this solution can be tailored to fit.