-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_env_stats.py
More file actions
executable file
·277 lines (239 loc) · 10.8 KB
/
check_env_stats.py
File metadata and controls
executable file
·277 lines (239 loc) · 10.8 KB
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
#! /usr/bin/python
#
# Copyright (c) Brady Lamprecht
# Licensed under GPLv3
# March 2009
#
# check_env_stats plug-in for nagios
# Uses SNMP to poll for voltage, temerature, fan, and power supply statistics
#
# History:
#
# v0.1 Very basic script to poll given SNMP values (Foundry only)
# v0.2 Added functionality for temperature, fans, power supplies
# v0.3 Included Cisco support with the addition of voltage
# v0.4 Functions to set warning and critical levels were added
# v0.5 Now implements "-p" perfmon option for performance data
# v1.0 Code cleanup and a few minor bugfixes
import os
import sys
from optparse import OptionParser
scriptversion = "1.0"
errors = {
"OK": 0,
"WARNING": 1,
"CRITICAL": 2,
"UNKNOWN": 3,
}
common_options = "snmpwalk -OvQ -v 1"
# Function for Cisco equipment
def check_cisco(hostname,community,mode,verbose):
command = common_options + " -c " + community + " " + hostname + " "
ciscoEnvMonObjects = "1.3.6.1.4.1.9.9.13.1"
if mode == "volt":
ciscoVoltDescTable = ciscoEnvMonObjects + ".2.1.2"
ciscoVoltValuTable = ciscoEnvMonObjects + ".2.1.3"
desc = os.popen(command + ciscoVoltDescTable).read()[:-1].replace('\"', '').split('\n')
valu = os.popen(command + ciscoVoltValuTable).read()[:-1].replace('\"', '').split('\n')
if verbose:
print_verbose(ciscoVoltDescTable,desc,ciscoVoltValuTable,valu)
if desc[0] == '' or valu[0] == '':
fail("description / value table empty or non-existent.")
return(desc,valu)
if mode == "temp":
ciscoTempDescTable = ciscoEnvMonObjects + ".3.1.2"
ciscoTempValuTable = ciscoEnvMonObjects + ".3.1.3"
desc = os.popen(command + ciscoTempDescTable).read()[:-1].replace('\"', '').split('\n')
valu = os.popen(command + ciscoTempValuTable).read()[:-1].replace('\"', '').split('\n')
if verbose:
print_verbose(ciscoTempDescTable,desc,ciscoTempValuTable,valu)
if desc[0] == '' or valu[0] == '':
fail("description / value table empty or non-existent.")
return(desc,valu)
if mode == "fans":
# Possible values:
# 1=normal,2=warning,3=critical,4=shutdown,5=notPresent,6=notFunctioning
ciscoFansDescTable = ciscoEnvMonObjects + ".4.1.2"
ciscoFansValuTable = ciscoEnvMonObjects + ".4.1.3"
desc = os.popen(command + ciscoFansDescTable).read()[:-1].replace('\"', '').split('\n')
valu = os.popen(command + ciscoFansValuTable).read()[:-1].replace('\"', '').split('\n')
if verbose:
print_verbose(ciscoFansDescTable,desc,ciscoFansValuTable,valu)
if desc[0] == '' or valu[0] == '':
fail("description / value table empty or non-existent.")
return(desc,valu)
if mode == "power":
# Possible values:
# 1=normal,2=warning,3=critical,4=shutdown,5=notPresent,6=notFunctioning
ciscoPowrDescTable = ciscoEnvMonObjects + ".5.1.2"
ciscoPowrValuTable = ciscoEnvMonObjects + ".5.1.3"
desc = os.popen(command + ciscoPowrDescTable).read()[:-1].replace('\"', '').split('\n')
valu = os.popen(command + ciscoPowrValuTable).read()[:-1].replace('\"', '').split('\n')
if verbose:
print_verbose(ciscoPowrDescTable,desc,ciscoPowrValuTable,valu)
if desc[0] == '' or valu[0] == '':
fail("description / value table empty or non-existent.")
return(desc,valu)
# Should never get to here
sys.exit(errors['UNKNOWN'])
# Function for Foundry equipment
def check_foundry(hostname,community,mode,verbose):
command = common_options + " -c " + community + " " + hostname + " "
foundrySNAgent = "1.3.6.1.4.1.1991.1.1"
if mode == "volt":
fail("voltage table does not exist in Foundry's MIB.")
if mode == "temp":
foundryTempDescTable = foundrySNAgent + ".2.13.1.1.3"
foundryTempValuTable = foundrySNAgent + ".2.13.1.1.4"
desc = os.popen(command + foundryTempDescTable).read()[:-1].replace('\"', '').split('\n')
valu = os.popen(command + foundryTempValuTable).read()[:-1].replace('\"', '').split('\n')
if verbose:
print_verbose(foundryTempDescTable,desc,foundryTempValuTable,valu)
if desc[0] == '' or valu[0] == '':
fail("description / value table empty or non-existent.")
return(desc,valu)
if mode == "fans":
# Possible values:
# 1=other,2=normal,3=critical
foundryFansDescTable = foundrySNAgent + ".1.3.1.1.2"
foundryFansValuTable = foundrySNAgent + ".1.3.1.1.3"
desc = os.popen(command + foundryFansDescTable).read()[:-1].replace('\"', '').split('\n')
valu = os.popen(command + foundryFansValuTable).read()[:-1].replace('\"', '').split('\n')
if verbose:
print_verbose(foundryFansDescTable,desc,foundryFansValuTable,valu)
if desc[0] == '' or valu[0] == '':
fail("description / value table empty or non-existent.")
return(desc, valu)
if mode == "power":
# Possible values:
# 1=other,2=normal,3=critical
foundryPowrDescTable = foundrySNAgent + ".1.2.1.1.2"
foundryPowrValuTable = foundrySNAgent + ".1.2.1.1.3"
desc = os.popen(command + foundryPowrDescTable).read()[:-1].replace('\"', '').split('\n')
valu = os.popen(command + foundryPowrValuTable).read()[:-1].replace('\"', '').split('\n')
if verbose:
print_verbose(foundryPowrDescTable,desc,foundryPowrValuTable,valu)
if desc[0] == '' or valu[0] == '':
fail("description / value table empty or non-existent.")
return(desc,valu)
# Should never get to here
sys.exit(errors['UNKNOWN'])
# Function for HP equipment
def check_hp(hostname,community,mode,verbose):
fail("HP functions not yet implemented.")
# Function for Juniper equipment
def check_juniper(hostname,community,mode,verbose):
fail("Juniper functions not yet implemented.")
# Function to process data from SNMP tables
def process_data(description, value, warning, critical, performance):
string = ""
status = "OK"
perfstring = ""
if critical and warning:
if len(critical) != len(description):
fail("number of critical values not equal to number of table values.")
elif len(warning) != len(description):
fail("number of warning values not equal to number of table values.")
else:
# Check for integer or string values
# Check each table value against provided warning & critical values
for d, v, w, c in zip(description,value,warning,critical):
if len(string) != 0:
string += ", "
if v >= c:
status = "CRITICAL"
string += d + ": " + str(v) + " (C=" + str(c) + ")"
elif v >= w:
if status != "CRITICAL":
status = "WARNING"
string += d + ": " + str(v) + " (W=" + str(w) + ")"
else:
string += d + ": " + str(v)
# Create performance data
perfstring += d.replace(' ', '_') + "=" + str(v) + " "
# Used to provide output when no warning & critical values are provided
else:
for d, v in zip(description,value):
if len(string) != 0:
string += ", "
string += d + ": " + str(v)
# Create performance data
perfstring += d.replace(' ', '_') + "=" + str(v) + " "
# If requested, include performance data
if performance:
string += " | " + perfstring
# Print status text and return correct value.
print status + ": " + string
sys.exit(errors[status])
def print_verbose(oid_A,val_A,oid_B,val_B):
print "Description Table:\n\t" + str(oid_A) + " = \n\t" + str(val_A)
print "Value Table:\n\t" + str(oid_B) + " = \n\t" + str(val_B)
sys.exit(errors['UNKNOWN'])
def fail(message):
print "Error: " + message
sys.exit(errors['UNKNOWN'])
def main():
args = None
options = None
# Create command-line options
parser = OptionParser(version="%prog " + scriptversion)
parser.add_option("-H", action="store", type="string", dest="hostname", help="hostname or IP of device")
parser.add_option("-C", action="store", type="string", dest="community", help="community read-only string [default=%default]", default="public")
parser.add_option("-T", action="store", type="string", dest="type", help="hardware type (cisco,foundry,hp,juniper)")
parser.add_option("-M", action="store", type="string", dest="mode", help="type of statistics to gather (temp,fans,power,volt)")
parser.add_option("-w", action="store", type="string", dest="warn", help="comma-seperated list of values at which to set warning")
parser.add_option("-c", action="store", type="string", dest="crit", help="comma-seperated list of values at which to set critical")
parser.add_option("-p", action="store_true", dest="perf", help="include perfmon output")
parser.add_option("-v", action="store_true", dest="verb", help="enable verbose output")
(options, args) = parser.parse_args(args)
# Map parser values to variables
host = options.hostname
comm = options.community
type = options.type
mode = options.mode
warn = options.warn
if warn:
warn = map(int,options.warn.split(','))
crit = options.crit
if crit:
crit = map(int,options.crit.split(','))
perf = options.perf
verb = options.verb
# Check for required "-H" option
if host:
pass
else:
fail("-H is a required argument")
# Check for required "-M" option and verify value is supported
if mode:
if mode == "temp" or mode == "fans" or mode == "power" or mode == "volt":
pass
else:
fail("-M only supports modes of temp, fans, power, volt")
else:
fail("-M is a required argument")
# Check for required "-T" option
if type:
pass
else:
fail("-T is a required argument")
# Check for valid "-T" option and execute appropriate check
if type == "cisco":
(desc, value) = check_cisco(host,comm,mode,verb)
process_data(desc, map(int,value), warn, crit, perf)
if type == "foundry":
(desc, value) = check_foundry(host,comm,mode,verb)
process_data(desc, map(int,value), warn, crit, perf)
if type == "hp":
(desc, value) = check_hp(host,comm,mode,verb)
process_data(desc, map(int,value), warn, crit, perf)
if type == "juniper":
(desc, value) = check_juniper(host,commu,mode,verb)
process_data(desc, map(int,value), warn, crit, perf)
else:
fail("-T only supports types of cisco, foundry, hp, or juniper")
# Should never get here
sys.exit(errors['UNKNOWN'])
# Execute main() function
if __name__ == "__main__":
main()