#!/usr/bin/python # Nagios script : checking APC USV # Copyright 2010 by Joerg Stephan johe DOT stephan AT googlemail.com # from pysnmp.entity.rfc3413.oneliner import cmdgen, ntforg from pysnmp.proto.api import v2c import sys import getopt STATE_OK = 0 STATE_WARNING = 1 STATE_CRIT = 2 CurrentState = 0 RetString = '' service = 1,3,6,1,4,1,318,1,1,1,1,1,1,0 capacity = 1,3,6,1,4,1,318,1,1,1,2,2,1,0 outload = 1,3,6,1,4,1,318,1,1,1,4,2,3,0 outstatus = 1,3,6,1,4,1,318,1,1,1,4,1,1,0 batstatus = 1,3,6,1,4,1,318,1,1,1,2,1,1,0 tobat = 1,3,6,1,4,1,318,1,1,1,2,1,2,0 VarList = [service, capacity, outload, outstatus, batstatus, tobat] OutStatusList = ['Unknown','On Line','On Battery','On Smart Boost','Timed Sleeping','Software Bypass','Off','Rebooting', 'Switched Bypass','Hardware Failure Bypass','Sleeping Until Power Returns','On Smart Trim'] BatteryStatus =['Unknown', 'Battery Normal', 'Battery Low'] count = 0 Communitystring = '' if (len(Communitystring) < 1) : print "warning: community string may not be empty" if (len(sys.argv) < 2) : print "Usage ./check_apc.py " sys.exit(-1) testHost = sys.argv[1] for Var in VarList : errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd( cmdgen.CommunityData('test', Communitystring, 0), cmdgen.UdpTransportTarget((testHost, 161)), (Var)) if (len(varBinds) == 0) : print "Status is CRITICAL - Host not replying" sys.exit(2) Var = varBinds[0][1] if (count == 0) : RetString = 'APC Model: ' + Var if (count == 1) : RetString += ', Capacity: ' + str(Var) + '%' if (int(Var) < 90) : CurrentState = 2 if (int(Var) < 100) : CurrentState = 1 if (count == 2) : RetString += ', OutputLoad: ' + str(Var) + '%' if (int(Var) < 30) : CurrentState = 2 if (count == 3) : RetString += ', Status(Out): ' + OutStatusList[int(Var) - 1] if (int(Var) != 2): CurrentState = 2 if (count == 4) : RetString += ', Status(Battery): ' + BatteryStatus[int(Var) - 1] if (int(Var) != 2): CurrentState = 2 if (count == 5) : RetString += ', TimeOnBattery: ' + str(Var) if (int(Var) != 0): CurrentState = 2 count = count +1 if (CurrentState == 0) : print 'Status is OK - ' + RetString sys.exit(0) if (CurrentState == 1) : print 'Status is WARNING - ' + RetString sys.exit(1) if (CurrentState == 0) : print 'Status is CRITICAL - ' + RetString sys.exit(2)