-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_cisco_stack.py
executable file
·341 lines (317 loc) · 11.7 KB
/
check_cisco_stack.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python
###############################################################
# ========================= INFO ==============================
# NAME: check_cisco_stack.py
# AUTHOR: Jeffrey Wolak
# LICENSE: MIT
# ======================= SUMMARY ============================
# Python rewrite of check_snmp_cisco_stack.pl
#
# https://exchange.nagios.org/directory/Plugins/Hardware/Network-Gear/Cisco/Check-cisco-3750-stack-status/details
#
# It looks like the perl version wasn't maintained and had some
# bugs working with newer switch models
#
# =================== SUPPORTED DEVICES =======================
# Lab testing with:
# 3750G
# 3750X
# 3850X
#
# !!! WARNING !!!
# See relevant bug reports before using in your environment
#
# Bug CSCsg18188 - Major
# Desc: May cause memory leak
# Effects: 12.2(25)SEE1
# Fixed: 12.2(35)SE
#
# Bug CSCse53528 - Minor
# Desc: May report the wrong status
# Effects: 12.2(25)SEE
# Fixed: 12.2(25)SEE3, 12.2(35)SE (and Later)
#
# ========================= NOTES =============================
# 11-27-2015: Version 1.0 released (Moving to PROD)
# 12-04-2015: Now marking all states other than "ready" as critical
# TODO: Add SNMP version 2 support
#
# ======================= LICENSE =============================
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# ###############################################################
import netsnmp # Requires net-snmp compiled with python bindings
import sys # exit
import getopt # for parsing options
import logging # for debug option
# Global program variables
__program_name__ = 'Cisco Stack'
__version__ = 1.1
###############################################################
#
# Exit codes and status messages
#
###############################################################
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
def exit_status(x):
return {
0: 'OK',
1: 'WARNING',
2: 'CRITICAL',
3: 'UNKNOWN'
}.get(x, 'UNKNOWN')
###############################################################
#
# usage() - Prints out the usage and options help
#
###############################################################
def usage():
print """
\t-h --help\t\t- Prints out this help message
\t-v --version\t\t- Prints the version number
\t-H --host <ip_address>\t- IP address of the cisco stack
\t-c --community <string>\t- SNMP community string
\t-d --debug\t\t- Verbose mode for debugging
"""
sys.exit(UNKNOWN)
###############################################################
#
# parse_args() - parses command line args and returns options dict
#
###############################################################
def parse_args():
options = dict([
('remote_ip', None),
('community', 'Public')
])
try:
opts, args = getopt.getopt(sys.argv[1:], "hvH:c:d", ["help", "host=", "version", "community=", "debug"])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
for o, a in opts:
if o in ("-d", "--debug"):
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(funcName)s - %(message)s'
)
logging.debug('*** Debug mode started ***')
elif o in ("-v", "--version"):
print "{0} plugin version {1}".format(__program_name__, __version__)
sys.exit(0)
elif o in ("-h", "--help"):
usage()
elif o in ("-H", "--host"):
options['remote_ip'] = a
elif o in ("-c", "--community"):
options['community'] = a
else:
assert False, "unhandled option"
logging.debug('Printing initial variables')
logging.debug('remote_ip: {0}'.format(options['remote_ip']))
logging.debug('community: {0}'.format(options['community']))
if options['remote_ip'] is None:
print "Requires host to check"
usage()
return options
###############################################################
#
# plugin_exit() - Prints value and exits
# :param exitcode: Numerical or constant value
# :param message: Message to print
#
###############################################################
def plugin_exit(exitcode, message=''):
logging.debug('Exiting with status {0}. Message: {1}'.format(exitcode, message))
status = exit_status(exitcode)
print '{0} {1} - {2}'.format(__program_name__, status, message)
sys.exit(exitcode)
###############################################################
#
# get_stack_info() - Acquire info about the stack status
# :param remote_ip: IP address of the system
# :param community: SNMP read community
# :return member_table: dict of dict of stack status
#
# -- member_table example:
# {'4001': {'status': 'ready', 'index': '4001', 'number': '4', 'status_num': '4'},
# '2001': {'status': 'ready', 'index': '2001', 'number': '2', 'status_num': '4'},
# '3001': {'status': 'ready', 'index': '3001', 'number': '3', 'status_num': '4'},
# '1001': {'status': 'ready', 'index': '1001', 'number': '1', 'status_num': '4'}}
#
# -- OID definitions:
# OID: 1.3.6.1.4.1.9.9.500.1.2.1.1.1
# "This object contains the current switch identification number.
# This number should match any logical labeling on the switch.
# For example, a switch whose interfaces are labeled
# 'interface #3' this value should be 3."
#
# OID: 1.3.6.1.4.1.9.9.500.1.2.1.1.6
# "The current state of a switch"
# See stack_state() documentation for all states
#
###############################################################
def get_stack_info(remote_ip, community):
member_table = {}
stack_table_oid = netsnmp.VarList(netsnmp.Varbind('.1.3.6.1.4.1.9.9.500.1.2.1.1.1'))
logging.debug('Walking stack table -- ')
netsnmp.snmpwalk(stack_table_oid, DestHost=remote_ip, Version=1, Community=community)
if not stack_table_oid:
plugin_exit(CRITICAL, 'Unable to retrieve SNMP stack table')
for member in stack_table_oid:
logging.debug('Member info: {0}'.format(member.print_str()))
a = {'number': member.val, 'index': member.tag.rsplit('.').pop()}
member_table[a['index']] = a
stack_status_oid = netsnmp.VarList(netsnmp.Varbind('.1.3.6.1.4.1.9.9.500.1.2.1.1.6'))
logging.debug('Walking stack status -- ')
netsnmp.snmpwalk(stack_status_oid, DestHost=remote_ip, Version=1, Community=community)
if not stack_status_oid:
plugin_exit(CRITICAL, 'Unable to retrieve SNMP stack status')
for member in stack_status_oid:
logging.debug('Member info: {0}'.format(member.print_str()))
index = member.tag.rsplit('.').pop()
member_table[index]['status_num'] = member.val
member_table[index]['status'] = stack_state(member.val)
logging.debug('Stack info table to return: {0}'.format(member_table))
return member_table
# -- STACK STATES --
#
# Defined by Cisco:
#
# http://tools.cisco.com/Support/SNMP/do/BrowseOID.do?
# objectInput=1.3.6.1.4.1.9.9.500.1.2.1.1.6&translate=Translate&submitValue=SUBMIT
#
#
# "The current state of a switch:
#
# waiting - Waiting for a limited time on other
# switches in the stack to come online.
#
# progressing - Master election or mismatch checks in
# progress.
#
# added - The switch is added to the stack.
#
# ready - The switch is operational.
#
# sdmMismatch - The SDM template configured on the master
# is not supported by the new member.
#
# verMismatch - The operating system version running on the
# master is different from the operating
# system version running on this member.
#
# featureMismatch - Some of the features configured on the
# master are not supported on this member.
#
# newMasterInit - Waiting for the new master to finish
# initialization after master switchover
# (Master Re-Init).
#
# provisioned - The switch is not an active member of the
# stack.
#
# invalid - The switch's state machine is in an
# invalid state.
#
# removed - The switch is removed from the stack."
def stack_state(x):
return {
'1': 'waiting',
'2': 'progressing',
'3': 'added',
'4': 'ready',
'5': 'sdmMismatch',
'6': 'verMismatch',
'7': 'featureMismatch',
'8': 'newMasterInit',
'9': 'provisioned',
'10': 'invalid',
'11': 'removed',
}.get(x, 'UNKNOWN')
###############################################################
#
# get_ring_status() - Acquire info about the stack status
# :param remote_ip: IP address of the system
# :param community: SNMP read community
# :return stack_ring_status: status of the stack ring
#
# OID: 1.3.6.1.4.1.9.9.500.1.1.3
# "A value of 'true' is returned when the stackports are
# connected in such a way that it forms a redundant ring."
#
###############################################################
def get_ring_status(remote_ip, community):
ring_status_oid = netsnmp.Varbind('.1.3.6.1.4.1.9.9.500.1.1.3.0')
logging.debug('Getting stack ring redundancy status -- ')
netsnmp.snmpget(ring_status_oid, DestHost=remote_ip, Version=1, Community=community)
if not ring_status_oid:
plugin_exit(CRITICAL, 'Unable to retrieve SNMP ring status')
logging.debug('Ring status: {0}'.format(ring_status_oid.print_str()))
stack_ring_status = ring_status_oid.val
return stack_ring_status
###############################################################
#
# evaluate_results() - Evaluate status of stack and ring
# :param stack: stack info dict
# :param ring: ring status
# :return result: result for exit code
# :return message: status message string for exit
#
###############################################################
def evaluate_results(stack, ring):
message = ["Members: "]
result = OK
logging.debug('Checking each stack member')
for i, member in stack.iteritems():
logging.debug('Member {0} is {1}'.format(member['number'], member['status']))
message.append("{0}: {1}, ".format(member['number'], member['status']))
if member['status_num'] is not '4':
result = CRITICAL
logging.debug('Status changed to CRITICAL')
if ring == '1':
message.append("Stack Ring is redundant")
else:
message.append("Stack Ring is non-redundant")
if result == OK:
result = WARNING
logging.debug('Status changed to WARNING')
message = ''.join(message)
return result, message
###############################################################
#
# main() - Main function
#
###############################################################
def main():
options = parse_args()
stack = get_stack_info(options['remote_ip'], options['community'])
ring = get_ring_status(options['remote_ip'], options['community'])
result, message = evaluate_results(stack, ring)
plugin_exit(result, message)
if __name__ == "__main__":
main()