作者:容易 2013-05-14 8:40:55
#!/usr/bin/python
from optparse import OptionParser
import sys,netsnmp
usage = "usage_example: %prog -w 60 -c 80 -v 2 -C gopass -h 192.168.0.3"
parser = OptionParser(usage=usage)
parser.add_option("-v", "--snmp_ver",
type="int",
dest="snmp_ver",
default=2,
help="please specify snmp version")
parser.add_option("-C", "--snmp_community",
type="string",
dest="snmp_community",
default='public',
help="please specify snmp community")
parser.add_option("-H", "--dest_host",
type="string",
dest="dest_host",
default="127.0.0.1",
help="please specify destination host")
parser.add_option("-w", "--warning",
type="int",
dest="warning",
default=65,
help="please specify warning thresholds for memory used%,default value 65% ")
parser.add_option("-c", "--critical",
type="int",
dest="critical",
default=80,
help="please specify critical thresholds for memory used%,default value 80%")
(options, args) = parser.parse_args()
Warning=options.warning
Critical=options.critical
snmp_ver=options.snmp_ver
snmp_community=options.snmp_community
dest_host=options.dest_host
if Critical <= Warning:
print "Error,Critical value must be greater than Warning."
sys.exit(3)
def snmp_data(dest_host,snmp_ver,snmp_community):
oid=netsnmp.Varbind('1.3.6.1.4.1','9.9.48.1.1.1.6')
res=netsnmp.snmpwalk(oid,Version=snmp_ver,DestHost=dest_host,Community=snmp_community)
print res
return res
try:
mem_info=snmp_data(dest_host,snmp_ver,snmp_community)
except:
print "Please check device snmp oid and ensure snmp parameters right!"
print usage
sys.exit(3)
if mem_info[0] == None or mem_info[1] == None:
print "Please check device snmp oid and ensure snmp parameters right!"
print usage
sys.exit(3)
mem_free=int(mem_info[0])
mem_used=int(mem_info[1])
#mem_per=round((mem_used/(mem_free+mem_used)*100),1)
mem_per=round((mem_used/float(mem_free+mem_used)*100),1)
print mem_per
if mem_per > Critical:
print "CRITICAL - memory used %d greater than %d. | mem_per=%d;%d;%d" % (mem_per,Critical,mem_per,Warning,Critical)
sys.exit(2)
elif mem_per > Warning:
print "Warning - memory used %d greater than %d. | mem_per=%d;%d;%d" % (mem_per,Warning,mem_per,Warning,Critical)
sys.exit(1)
elif mem_per < Warning:
print "OK - memory used %d. | mem_per=%d;%d;%d" % (mem_per,mem_per,Warning,Critical)
sys.exit(0)
else:
print "Unknow - Device current state is unknow."
sys.exit(3)
One Response