#! /usr/bin/env python2.6
# 2013-1-28
# written by Tony.yang
#

import os
import sys
import getopt

def usage():
        print """
---------------------------------------------------------------------------------------------
| Tony-warn: This nagios receiving return status.(0:OK; 1:WARNING;  2:CRITICAL;  3:UNKNOWN) |
---------------------------------------------------------------------------------------------
Usage: check_getloadavg [-h|--help] [-m|--mode 1|2|3] [-w|--warning level] [-c|--critical level]

Mode: 1-last minute;  2-last 5 minutes;  3-last 15 minutes;
Warning level defaults to 2.0
Critical level defaults to 5.0"""
        sys.exit(3)

# obtain command args
try:
        options, args = getopt.getopt(sys.argv[1:], 'hm:w:c:', '--help --mode= --warning --critical=')
except getopt.GetoptError:
        usage()
        sys.exit(3)

#
argMode = '1'
argWarning = 2.0
argCritical = 5.0

#
for name, value in options:
        if name in ('-h', '--help'):
                usage()
        if name in ('-m', '--mode'):
                if value not in ('1', '2', '3'):
                        usage()
                argMode = value
        if name in ('-w', '--warning'):
                try:
                        argWarning = 0.0 + value
                except Exception:
                        print "Unable to convert to floating point vaule\n"
                        usage()
        if name in ('-c', '--critical'):
                try:
                        argCritical = 0.0 + value
                except Exception:
                        print "Unable to convert to floating point vaule\n"
                        usage()

try:
        (d1, d2, d3) = os.getloadavg()
except Exception:
        print "GETLOADAVG UNKNOWN: Error while getting load average"
        sys.exit(3)

if argMode == '1':
        d = d1
elif argMode == '2':
        d = d2
elif argMode == '3':
        d = d3

if d >= argCritical:
        print "GETLOADAVG CRITICAL: Load average is %.2f" % (d)
        sys.exit(2)
elif d >= argWarning:
        print "GETLOADAVG WARNING: Load average is %.2f" % (d)
        sys.exit(1)
else:
        print "GETLOADAVG OK: Load average is %.2f" % (d)
        sys.exit(0)