Page 1 of 1

python script return null information

Posted: Tue Oct 23, 2018 8:49 am
by denisnav
Hello,
I wrote a script in python to get the freedisk space with check_nrpe (bash command). the problem is when I want to convert a string to integer with int(), I see in nagios web interface "null" in information. When I run manually the script in a shell, it works. When I remove the function int(), nagios gets informations on the disk space (but the IF condition doesn't work of course). Below my script :

Code: Select all

#!/usr/bin/env python

import os, traceback, threading, sys
import subprocess as sp

host=sys.argv[1]
lecteur=sys.argv[2]


def main(host, lecteur):
        total=sp.Popen(["/srv/eyesofnetwork/nagios/plugins/check_nt", "-H", host, "-p", "12489", "-s", '', "-v", "USEDDISKSPACE", "-l", lecteur], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE).communicate()[0]
        total=str(total).split()[3].split(',')[0]
        total=int(total)
        #print "total: %s" % total

        if total < 2.05 :
                resultat=sp.Popen(["/srv/eyesofnetwork/nagios-3.5.1/plugins/check_nrpe", "-H", host, "-c", "check_drivesize", "-a", "drive=" + lecteur, "warning=free<0.2G", "critical=free<0.1G", "show-all", "perf-config=*(unit:G)"], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE).communicate()[0]
                res=str(resultat).split()[0]
                if 'CRITICAL' in res:
                        print resultat
                        sys.exit(2)
                elif 'WARNING' in res:
                        print resultat
                        sys.exit(1)
                else:
                        print resultat
                        sys.exit(0)
        elif total >= 2.05 and total < 99.99:
                resultat=sp.Popen(["/srv/eyesofnetwork/nagios-3.5.1/plugins/check_nrpe", "-H", host, "-c", "check_drivesize", "-a", "drive=" + lecteur, "warning=free<2G", "critical=free<1G", "show-all", "perf-config=*(unit:G)"], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE).communicate()[0]
                res=str(resultat).split()[0]
                if 'CRITICAL' in res:
                        print resultat
                        sys.exit(2)
                elif 'WARNING' in res:
                        print resultat
                        sys.exit(1)
                else:
                        print resultat
                        sys.exit(0)
        elif total >= 99.99 and total < 450.99 :
                resultat=sp.Popen(["/srv/eyesofnetwork/nagios-3.5.1/plugins/check_nrpe", "-H", host, "-c", "check_drivesize", "-a", "drive=" + lecteur, "warning=free<5G", "critical=free<2G", "show-all", "perf-config=*(unit:G)"], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE).communicate()[0]
                res=str(resultat).split()[0]
                if 'CRITICAL' in res:
                        print resultat
                        sys.exit(2)
                elif 'WARNING' in res:
                        print resultat
                        sys.exit(1)
                else:
                        print resultat
                        sys.exit(0)
        elif total >= 450.99 and total < 950.99 :
resultat=sp.Popen(["/srv/eyesofnetwork/nagios-3.5.1/plugins/check_nrpe", "-H", host, "-c", "check_drivesize", "-a", "drive=" + lecteur, "warning=free<200G", "critical=free<50G", "show-all", "perf-config=*(unit:G)"], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE).communicate()[0]
                res=str(resultat).split()[0]
                if 'CRITICAL' in res:
                        print resultat
                        sys.exit(2)
                elif 'WARNING' in res:
                        print resultat
                        sys.exit(1)
                else:
                        print resultat
                        sys.exit(0)


main(host, lecteur)
Thanks.

Re: python script return null information

Posted: Tue Oct 23, 2018 4:54 pm
by npolovenko
Hi, @denisnav. I'd like to see what the total value looks like. Try changing the line to look like this:
total=float(total)

Re: python script return null information

Posted: Wed Oct 24, 2018 2:42 am
by denisnav
Hi @npolovenko,
The result of total=float(total) is 5119.0 and now nagios gets good information in the web interface. I suppose it's was because I compared integer with float. Thank you very much.

Re: python script return null information

Posted: Wed Oct 24, 2018 3:54 pm
by npolovenko
@denisnav, Glad to hear! I will be closing this thread as resolved.