#!/usr/bin/env python
# Aleksandrov Artyom <a.aleksandrov@westcall.spb.ru>
# Script get information from temptraxe drive. And return to nagios result
# ver 0.1.1
import urllib
from optparse import OptionParser
import sys 

def get_status(opts):

    u = urllib.urlopen("http://"+opts.host+"/temp")
    data = u.read ()
    data = data.split ("|")
    temp = float(data[opts.probe * 2 - 1 ])
    temp = (temp -32) *5/9
    result = data [opts.probe*2 -2] + ' ' + str("%2.2f"% temp) + ' C'
    msg = result + '| temp=' + str("%2.2f"% temp) + ';' + str(opts.warn) +  ';' + str(opts.crit)
    if temp > opts.crit:	
    	print 'CRITICAL ' + msg
    	sys.exit(2)
    elif temp > opts.warn:
    	print 'WARNING ' + msg 
    	sys.exit(1)
    elif temp <= opts.warn:
    	print 'OK ' + msg 
    	sys.exit(0)
    else:
	print 'UNKNOWN'
	sys.exit(3)


def main():
    p = OptionParser()
    p.add_option("-H","--host", dest="host", help="Hostname or IP address")
    p.add_option("-P","--probe", type="int", dest="probe", help="Probe")
    p.add_option("-c","--critical", type="int", dest="crit", help="Critical value")
    p.add_option("-w","--warning", type="int", dest="warn", help="Warning value")
    (opts, args) = p.parse_args()

    if opts.crit < opts.warn:
	print "Critical value < Warning value. Check your config"
	sys.exit(1)

    if not opts.host:
        print "\nUsage: %s -H <host> [<options>]\n"%sys.argv[0]
        print "For full usage instructions please invoke with -h option\n"
        sys.exit(1)

    get_status(opts)

if __name__ == '__main__':
    main()
