作者:容易 2013-04-02 9:40:37
基于python netsnmp模块的nagios思科路由器或交换机cpu性能监控插件,其中其中oid可能会因为设备型号不同需要自行定义。
#!/usr/bin/python
from optparse import OptionParser
import sys,os,netsnmp
usage = "usage_example: %prog -w 60 -c 80 -v 2 -C tiger -H 192.168.4.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 cpu used%,default value 65% ")
parser.add_option("-c", "--critical",
type="int",
dest="critical",
default=80,
help="please specify critical thresholds for cpu 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
def snmp_data(dest_host,snmp_ver,snmp_community):
oid=netsnmp.Varbind('1.3.6.1.4.1','9.2.1.57.0')
res=netsnmp.snmpget(oid,Version=snmp_ver,DestHost=dest_host,Community=snmp_community)[0]
return int(res)
if Critical <= Warning:
print "Error,Critical value must be greater than Warning."
sys.exit(3)
try:
cpu_used=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 cpu_used == None:
print "Please check device snmp oid and ensure snmp parameters right!"
print usage
sys.exit(3)
elif cpu_used > Critical:
print "CRITICAL - Cpu used %d greater than %d. | cpu_used=%d;%d;%d" % (cpu_used,Critical,cpu_used,Warning,Critical)
sys.exit(2)
elif cpu_used > Warning:
print "Warning - Cpu used %d greater than %d. | cpu_used=%d;%d;%d" % (cpu_used,Warning,cpu_used,Warning,Critical)
sys.exit(1)
elif cpu_used < Warning:
print "OK - Cpu used %d. | cpu_used=%d;%d;%d" % (cpu_used,cpu_used,Warning,Critical)
sys.exit(0)
else:
print "Unknow - Device current state is unknow."
sys.exit(3)
One Response