-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_load.py
67 lines (52 loc) · 2.1 KB
/
get_load.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
# coding: utf-8
import argparse
from pysnmp.hlapi import *
# Exit statuses recognized by Nagios
UNKNOWN = -1
OK = 0
WARNING = 1
CRITICAL = 2
# Valide argument
parser = argparse.ArgumentParser(description='Get load 1,5,15 on mini-snmpd server.')
parser.add_argument('-H', '--hostname', type=str, nargs='?', required=True, help='Hostname')
parser.add_argument('-C', '--community', type=str, nargs='?', help='SNMP Community', default='public')
parser.add_argument('-P', '--port', type=str, nargs='?', help='SNMP Port', default='161')
parser.add_argument('-w', '--warning', type=float, nargs='?', help='Warning treshold', default='10')
parser.add_argument('-c', '--critical', type=float, nargs='?', help='Critical treshold', default='20')
args = parser.parse_args()
# monitor
data = (
ObjectType(ObjectIdentity('UCD-SNMP-MIB', 'laLoad', 1)),
ObjectType(ObjectIdentity('UCD-SNMP-MIB', 'laLoad', 2)),
ObjectType(ObjectIdentity('UCD-SNMP-MIB', 'laLoad', 3))
)
g = getCmd(SnmpEngine()
, CommunityData(args.community, mpModel=1)
, UdpTransportTarget((args.hostname, args.port))
, ContextData()
, *data)
errorIndication, errorStatus, errorIndex, varBinds = next(g)
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'
)
)
else:
Loada=str(varBinds[0]).split('= ')[1]
Loadb=str(varBinds[1]).split('= ')[1]
Loadc=str(varBinds[2]).split('= ')[1]
perfdata= ": Load average:"+Loada+", "+Loadb+", "+Loadc+"|Load1="+Loada+";"+str(args.warning)+";"+str(args.critical)+";; Load5="+Loadb+";"+str(args.warning)+";"+str(args.critical)+";; Load15="+Loadc+";"+str(args.warning)+";"+str(args.critical)+";; "
fl_load=float(Loada)
if fl_load >= args.critical:
print("Critical"+perfdata)
exit(CRITICAL)
elif fl_load >= args.warning:
print("Warning"+perfdata)
exit(WARNING)
else:
print("OK"+perfdata)
exit(OK)