This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_snmp_win-2.py
285 lines (258 loc) · 11.9 KB
/
check_snmp_win-2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python
# based on check_snmp_win.pl from http://nagios.manubulon.com/
# $Revision$
# $Date$
# $Author$
# Description:
# this version uses memcahced caching for the result of snmpwalk
# Depends:
# netsnmp python bindings, requires netsnmp 5.4.1
import os,sys,re
from optparse import OptionParser
from pprint import pprint
import syslog
import pkg_resources
from pkg_resources import require
from time import time
from stat import ST_MTIME
import cPickle as pickle
import memcache,md5
nagios_states={'OK':0,'WARNING':1,'CRITICAL':2,'UNKNOWN':3,'DEPENDENT':4}
invert_nagios_states= dict([(v, k) for k, v in nagios_states.iteritems()])
my_state='UNKNOWN'
response_text="NO OUTPUT"
debug=False
class NagiosError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return repr(self.msg)
def xor_state(a='UNKNOWN',b='UNKNOWN'):
na=nagios_states[a]
nb=nagios_states[b]
state=max(na,nb)
return invert_nagios_states[state]
def arg_parse():
#"Usage: $Name [-v] -H <host> -C <snmp_community> [-2]
# [-p <port>] -n <name>[,<name2] [-T=service] [-r] [-s]
# [-N=<n>] [-t <timeout>] [-V]\n";
usage="%prog [options] arg"
parser = OptionParser(usage)
parser.add_option("-H", "--hostname", dest="hostname",help="name or IP address of host to check",default='localhost')
parser.add_option("-v", "--verbose", dest="verbose",action="store_true",help="print extra debugging information (and lists all services)",default=False)
parser.add_option("-C", "--community", dest="community",help="community name for the host's SNMP agent (implies SNMP v1 or v2c with option)",default='public')
parser.add_option("-2", "--v2c", dest="version",action="store_true",help="Use snmp v2c",default=False)
parser.add_option("-p", "--port", dest="port",type="int",help="SNMP port (Default 161)",default=161)
parser.add_option("-n", "--name", dest="name",help="Comma separated names of services (regular expressions can be used for every one). By default, it is not case sensitive.",default=None)
parser.add_option("-N", "--number", dest="number",help="Compare matching services with <n> instead of the number of names provided.",type="int",default=1)
parser.add_option("-s", "--showall", dest="showall",help="Show all services in the output, instead of only the non-active ones.",action="store_true",default=False)
parser.add_option("-r", "--noregexp", dest="noregexp",help="Do not use regexp to match NAME in service description..",action="store_true",default=False)
parser.add_option("--nocache", dest="nocache",help="Do not use caching of snmpwalk",action="store_true",default=False)
parser.add_option("-t", "--timeout", dest="timeout",type="int",help="timeout for SNMP in seconds (Default: 5)",default=5)
### version
(options, args) = parser.parse_args(args=sys.argv[1:])
if len(args)!=0:
parser.error("Error in command line options")
if options.name is None : raise NagiosError,"Option name not defined."
if options.number<0: raise NagiosError,"Invalid number of services."
options.timeout=options.timeout*100000
if options.version:
options.version=2
else:
options.version=1
return options
########################################################################################################
# Plugin specific stuff goes here
########################################################################################################
try:
cache=pkg_resources.get_default_cache()
if 'root' in cache:
os.environ['PYTHON_EGG_CACHE']=os.path.expanduser("~")+"/.python-eggs"
pkg_resources.cleanup_resources()
pkg_resources.set_extraction_path(os.path.expanduser("~")+"/.python-eggs")
pkg_resources.cleanup_resources()
#import netsnmp
from pkg_resources import require
import pkg_resources
require("netsnmp-python")
import netsnmp
except:
print 'ERROR: netsnmp python library not installed'
syslog.syslog(str(sys.exc_info()[0]))
syslog.syslog(str(sys.exc_info()[1]))
syslog.syslog(str(sys.path))
syslog.syslog(pkg_resources.get_default_cache())
sys.exit(nagios_states[my_state])
snmp_oids={
"process_table":'.1.3.6.1.2.1.25.4.2.1',
"index_table":'.1.3.6.1.2.1.25.4.2.1.1',
"run_name_table":'.1.3.6.1.2.1.25.4.2.1.2',
"run_path_table":'.1.3.6.1.2.1.25.4.2.1.4',
"proc_mem_table":'.1.3.6.1.2.1.25.5.1.1.2', # Kbytes
"proc_cpu_table":'.1.3.6.1.2.1.25.5.1.1.1', # Centi sec of CPU
"proc_run_state":'.1.3.6.1.2.1.25.4.2.1.7',
# Windows SNMP DATA
"win_serv_table":'1.3.6.1.4.1.77.1.2.3.1', # Windows services table
"win_serv_name":'.1.3.6.1.4.1.77.1.2.3.1.1', # Name of the service
# Install state : uninstalled(1), install-pending(2), uninstall-pending(3), installed(4)
"win_serv_inst":'.1.3.6.1.4.1.77.1.2.3.1.2',
# Operating state : active(1), continue-pending(2), pause-pending(3), paused(4)
"win_serv_state":'.1.3.6.1.4.1.77.1.2.3.1.3',
#"win_serv_state_label": ['active', 'continue-pending', 'pause-pending', 'paused'],
# Can be uninstalled : cannot-be-uninstalled(1), can-be-uninstalled(2)
"win_serv_uninst":'.1.3.6.1.4.1.77.1.2.3.1.4'
}
win_serv_state_label= { 1 : 'active', 2:'continue-pending', 3:'pause-pending', 4: 'paused', 99: 'UNAVAILABLE'}
# SNMP Datas for processes (MIB II)
########################################################################################################
if __name__=="__main__":
#syslog.syslog(pprint.saferepr(sys.path))
try:
import psyco
psyco.full()
except ImportError:
pass
try:
options=arg_parse()
if options.verbose: debug=True
match_list=[]
name_list=options.name.split(',')
except NagiosError, msg:
print my_state,':',msg
sys.exit(nagios_states[my_state])
except:
response_text="Errors in parameters submitted to command."
my_state='UNKNOWN'
if debug: raise
else:
if debug:
pprint(options)
try:
netsnmp.verbose=0
snmp_session=netsnmp.Session(
DestHost=options.hostname,
Community=options.community,
Version=options.version,
RemotePort=options.port,
Timeout=options.timeout,
Retries=2
)
snmp_session.UseNumeric=1
except:
response_text='SNMP session establishing failed.'
print my_state,':',response_text
sys.exit(nagios_states[my_state])
try:
varoids=netsnmp.VarList(netsnmp.Varbind(snmp_oids["win_serv_name"]))
try:
import memcache
cmemcache = memcache.Client(['127.0.0.1:11211'], debug=0)
except:
cmemcache=None
match_list=[]
if options.nocache or cmemcache is None:
res=snmp_session.walk(varoids)
else:
key="%s-%s" % (options.hostname,snmp_oids["win_serv_name"])
md5_key=md5.md5(key).hexdigest()
if debug: print "MD5 key",md5_key
value=cmemcache.get(md5_key)
if value:
if debug: print "Found in cache"
varoids=pickle.loads(value)
res='cached'
else:
if debug: print "NOT Found in cache"
res=snmp_session.walk(varoids)
cmemcache.set(md5_key,pickle.dumps(varoids),time=300)
if res is None or len(res)==0:
if debug:
print varoids
print snmp_session.ErrorStr
print snmp_session.ErrorNum
my_state='CRITICAL'
raise NagiosError,"SNMP query error: no value returned."
if options.noregexp:
for k in varoids:
if debug: print k.val
for n in name_list:
if n in k.val:
match_list.append(k)
if debug:
print "Found ",k.val
if len(match_list)!=len(name_list):
my_state='CRITICAL'
raise NagiosError,"Some values were not found."
else:
name_list_re=map(lambda x: (x,re.compile(x)),name_list)
for k in varoids:
if debug: print k.val
for on,n in name_list_re:
#if n in k.val:
if n.search(k.val):
match_list.append(k)
if debug:
print "Found ",k.val
if len(match_list)!=len(name_list):
my_state='CRITICAL'
if debug:
print "matches:",len(match_list)
print "requested:",len(name_list)
raise NagiosError,"Incorrect number of services running."
pass
# found a match?
services_states={}
if len(match_list)>0:
#test_oid=match.tag.replace(snmp_oids["win_serv_name"],snmp_oids["win_serv_state"])
for match in match_list:
test_oid = netsnmp.Varbind(match.tag.replace(snmp_oids["win_serv_name"],snmp_oids["win_serv_state"]))
test_oid.iid=match.iid
res=netsnmp.snmpget(test_oid,
DestHost=options.hostname,
Community=options.community,
Version=options.version,
RemotePort=options.port,
Timeout=options.timeout,
Retries=2
)
if res is None or None in res:
if debug:
print test_oid.val
print test_oid.tag
services_states[match.val]=99
else:
services_states[match.val]=int(test_oid.val)
else:
# no match
my_state='CRITICAL'
raise NagiosError,"SNMP query error: no value returned."
# generate the output
response_text=''
my_state='OK'
for service in services_states:
if services_states[service]!=1:
this_state='CRITICAL'
else:
this_state='OK'
my_state=xor_state(my_state,this_state)
if options.showall:
response_text=response_text +' '+ service+': '+win_serv_state_label[services_states[service]]
else:
if services_states[service]!=1:
response_text=response_text +' '+ service+':'+win_serv_state_label[services_states[service]]
if response_text=='':
if len(services_states)==1:
response_text='Service running'
else:
response_text='Services running'
if debug: pprint(services_states)
except NagiosError, msg:
print my_state,':',msg
sys.exit(nagios_states[my_state])
except:
raise
response_text='SNMP request failed.'
print my_state,':',response_text
sys.exit(nagios_states[my_state])
print my_state,':',response_text
sys.exit(nagios_states[my_state])