#!/usr/bin/python
#
#
# check_sensorprobe
# script di verifica per SensorProbe2 e SensorProbe8
# http://www.akcp.com/help/sp/OID_s.htm
#
# Version: 1.0 - angeloxx@angeloxx.it
#                Prima stesura
#
# Version: 1.1 - angeloxx@angeloxx.it
#                Aggiunta supporto nuovo pysnmp (Debian) e community variabile
#

import sys, threading, StringIO,os
from optparse import OptionParser

os.environ['PYSNMP_API_VERSION'] = "v2"
from pysnmp import asn1,v1,v2c
from pysnmp import role
import pysnmp

NAGIOS_OK = 0
NAGIOS_WARNING = 1
NAGIOS_CRITICAL = 2
NAGIOS_UNKNOWN = 3

uso = "%prog -r host -s sensor_type -i sensor_index -w warn_level -c critical_level"
parser = OptionParser(uso)
parser.add_option("-r", "--host",      dest="host",      help="sensorprobe remoto", default="TEMP")
parser.add_option("-s", "--stype",     dest="stype",     help="tipo di sensore [TEMP|WATER|SMOKE]", default="TEMP")
parser.add_option("-i", "--sindex",    dest="sindex",    help="index del sensore", default="0")
parser.add_option("-w", "--min",       dest="min",       help="soglia di warn per valore sotto il min (per temp e hum)", default='15')
parser.add_option("-c", "--max",       dest="max",       help="soglia di crit per valore oltre il max (per temp e hum)", default='25')
parser.add_option("-g", "--community", dest="community", help="community di lettura", default='public')

(options, args) = parser.parse_args()
if not options.stype:
    parser.error("option stype is mandatory")
    sys.exit(NAGIOS_UNKNOWN)

if not options.sindex:
    parser.error("option sindex is mandatory")
    sys.exit(NAGIOS_UNKNOWN)

if not options.host:
    parser.error("option host is mandatory")
    sys.exit(NAGIOS_UNKNOWN)


#if options.stype == 'TEMP':
qry = '.1.3.6.1.4.1.3854.1.2.2.1.16.1.3.' + options.sindex

if options.stype == 'HUM':
    qry = '.1.3.6.1.4.1.3854.1.2.2.1.17.1.3.' + options.sindex
if options.stype == 'WATER':
    qry = '.1.3.6.1.4.1.3854.1.2.2.1.18.1.3.' + options.sindex
if options.stype == 'SMOKE':
    qry = '.1.3.6.1.4.1.3854.1.2.2.1.18.1.3.' + options.sindex


client = role.manager((options.host, 161))
req = eval('v1').GETREQUEST()
rsp = eval('v1').GETRESPONSE()

try:
    (answer, src) = client.send_and_receive(req.encode(community=options.community, encoded_oids=map(asn1.OBJECTID().encode, [qry])))
except role.NetworkError:
    print "KO: Unable to contact or resolve name of remote SensorProbe"
    sys.exit(NAGIOS_CRITICAL)

# Decode SNMP response
rsp.decode(answer)
vals = map(lambda x: x[0](), map(asn1.decode, rsp['encoded_vals']))


## Si potrebbe comprimere e di molto il programma, qui...

if (options.stype == 'SMOKE' or options.stype == 'WATER'):
    if  vals[0] == 2:
        print "OK: Sensor " + options.stype + "/" + options.sindex
        sys.exit(NAGIOS_OK)
    else:
        print "CRIT: Sensor " + options.stype + "/" + options.sindex
        sys.exit(NAGIOS_CRITICAL)

if options.stype == 'TEMP':
    if  vals[0] < int(options.min):
        print "WARN: Sensor " + options.stype + "/" + options.sindex + " Temperature: " + str(vals[0]) + " Celsius too low"
        sys.exit(NAGIOS_WARNING)
    if  vals[0] > int(options.max):
        print "CRIT: Sensor " + options.stype + "/" + options.sindex + " Temperature: " + str(vals[0]) + " Celsius too high"
        sys.exit(NAGIOS_CRITICAL)

    print "OK: Sensor " + options.stype + "/" + options.sindex + " Temperature: " + str(vals[0]) + " Celsius"


if options.stype == 'HUM':
    if  vals[0] < int(options.min):
        print "WARN: Sensor " + options.stype + "/" + options.sindex + " Humidity: " + str(vals[0]) + " % too low"
        sys.exit(NAGIOS_WARNING)
    if  vals[0] > int(options.max):
        print "CRIT: Sensor " + options.stype + "/" + options.sindex + " Humidity: " + str(vals[0]) + " % too high"
        sys.exit(NAGIOS_CRITICAL)

    print "OK: Sensor " + options.stype + "/" + options.sindex + " Humidity: " + str(vals[0]) + " %"




#print "OK: Connected to %s (database %s)" % (options.host,options.database)
sys.exit(NAGIOS_OK)
