Back to index

nagios-plugins  1.4.16
check_pcpmetric.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 #
00003 # Nagios client for checking Performance Co-Pilot metrics
00004 #
00005 #
00006 
00007 from sys import argv,exit
00008 import popen2, getopt, string, types
00009 
00010 DEBUG=0
00011 
00012 nagios_pcpclient_version = 0.01
00013 PMVAL='/usr/bin/pmval'
00014 COMMANDLINE=PMVAL + " -s 1"
00015 METRIC='undefined'
00016 CRITICAL=0
00017 WARNING=0
00018 
00019 def usage():
00020        print "Usage:", argv[0], "[options]"
00021        print "Options:"
00022        print "\t[-H host]\tHostname to contact"
00023        print "\t[-m metric]\tPCP metric to check"
00024        print "\t[-i instance]\tPCP metric instance"
00025        print "\t[-w warn]\tIssue warning alert if value is larger than this"
00026        print "\t[-c critical]\tIssue critical alert value is larger than this"
00027        print "\t[-V]\t\tProgram version"
00028        print "\t[-h]\t\tThis helptext"
00029        print ""
00030        print "F.ex. to check 5 minute loadaverage, warn if the load is above 2,"
00031        print "and give critical warning if it's above 10:"
00032        print "\n\t%", argv[0], " -i 5 -m kernel.all.load -w 2 -c 10"
00033        print ""
00034        print "A list of all PCP metrics can be found with the command 'pminfo'."
00035        print "A list of all instances within a metric can be found with 'pminfo -f metric'."
00036        print "F.ex. to see all available instances of 'filesys.full' execute:"
00037        print "\n\t% pminfo -f filesys.full"
00038        print "\tfilesys.full"
00039        print """\t\tinst [0 or "/dev/root"] value 45.35514044640914"""
00040        print """\t\tinst [1 or "/dev/sda1"] value 46.74285959344712"""
00041        print """\t\tinst [2 or "/dev/sdb1"] value 0.807766570678168"""
00042        print ""
00043        print "And the command to have nagios monitor the /dev/sda1 filesystem would be:"
00044        print "\n\t", argv[0], " -i /dev/sda1 -m filesys.full -w 70 -c 90"
00045 
00046 
00047 opts, args = getopt.getopt(argv[1:],'hH:c:w:m:i:V')
00048 for opt in opts:
00049        key,value = opt
00050        if key == '-H':
00051               COMMANDLINE = COMMANDLINE + " -h " + value
00052        elif key == '-m':
00053               METRIC=value
00054        elif key == '-i':
00055               COMMANDLINE = COMMANDLINE + " -i " + value
00056        elif key == '-c':
00057               CRITICAL = value
00058        elif key == '-w':
00059               WARNING = value
00060        elif key == '-h':
00061               usage()
00062               exit(0)
00063        elif key == '-V':
00064               print "Nagios Performance CoPilot client v%.2f" % nagios_pcpclient_version
00065               print "Written by Jan-Frode Myklebust <janfrode@parallab.uib.no>"
00066               exit(0)
00067 
00068 if METRIC == 'undefined': 
00069        usage()
00070        exit(3)
00071 
00072 COMMANDLINE = COMMANDLINE + " " + METRIC
00073 if DEBUG: print COMMANDLINE
00074 p=popen2.Popen4(COMMANDLINE)
00075 exitcode=p.wait()
00076 
00077 # Get the last line of output from 'pmval':
00078 buffer = p.fromchild.readline()
00079 while (buffer != ''):
00080        output=buffer
00081        buffer = p.fromchild.readline()
00082        
00083 returndata = string.split(output)[0]
00084 
00085 
00086 # Confirm that we have gotten a float, and not
00087 # some errormessage in the returndata. If not, 
00088 # print the error, and give the UNKNOWN exit code:
00089 
00090 try:
00091        retval = string.atof(returndata)
00092 except ValueError, e:
00093        print e
00094        exit(3)
00095 
00096 if (retval < WARNING):
00097        EXITCODE=0
00098 elif (retval > CRITICAL):
00099        EXITCODE=2
00100 elif (retval > WARNING):
00101        EXITCODE=1
00102 else:
00103        EXITCODE=3
00104 
00105 print retval
00106 exit(EXITCODE)