forked from m-erhardt/check-cisco-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_cisco_memusage.py
executable file
·252 lines (218 loc) · 9.53 KB
/
check_cisco_memusage.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
#!/usr/bin/env python3
"""
###############################################################################
# check_cisco_memusage.py
# Nagios plugin that checks the system memory usage on a Cisco Switch/Router
# via SNMPv3 using the CISCO-PROCESS-MIB
#
#
# Author : Mauno Erhardt <[email protected]>
# Copyright : (c) 2021 Burkert Fluid Control Systems
# Source : https://github.com/m-erhardt/check-cisco-plugins
# License : GPLv3 (http://www.gnu.org/licenses/gpl-3.0.txt)
#
###############################################################################
"""
import sys
from argparse import ArgumentParser
from itertools import chain
from pysnmp.hlapi import bulkCmd, SnmpEngine, UsmUserData, \
UdpTransportTarget, \
ObjectType, ObjectIdentity, \
ContextData, usmHMACMD5AuthProtocol, \
usmHMACSHAAuthProtocol, \
usmHMAC128SHA224AuthProtocol, \
usmHMAC192SHA256AuthProtocol, \
usmHMAC256SHA384AuthProtocol, \
usmHMAC384SHA512AuthProtocol, usmDESPrivProtocol, \
usm3DESEDEPrivProtocol, usmAesCfb128Protocol, \
usmAesCfb192Protocol, usmAesCfb256Protocol
authprot = {
"MD5": usmHMACMD5AuthProtocol,
"SHA": usmHMACSHAAuthProtocol,
"SHA224": usmHMAC128SHA224AuthProtocol,
"SHA256": usmHMAC192SHA256AuthProtocol,
"SHA384": usmHMAC256SHA384AuthProtocol,
"SHA512": usmHMAC384SHA512AuthProtocol,
}
privprot = {
"DES": usmDESPrivProtocol,
"3DES": usm3DESEDEPrivProtocol,
"AES": usmAesCfb128Protocol,
"AES192": usmAesCfb192Protocol,
"AES256": usmAesCfb256Protocol,
}
def get_args():
""" Parse Arguments """
parser = ArgumentParser(
description="Icinga/Nagios plugin which checks system memory \
usage on Cisco switches/routers",
epilog=""
)
parser.add_argument("-H", "--host", required=True,
help="hostname or IP address", type=str, dest='host')
parser.add_argument("-p", "--port", required=False, help="SNMP port",
type=int, dest='port', default=161)
parser.add_argument("-t", "--timeout", required=False,
help="SNMP timeout", type=int, dest='timeout',
default=10)
parser.add_argument("-u", "--user", required=True,
help="SNMPv3 user name", type=str, dest='user')
parser.add_argument("-l", "--seclevel", required=False,
help="SNMPv3 security level", type=str,
dest="v3mode",
choices=["authPriv", "authNoPriv"], default="authPriv")
parser.add_argument("-A", "--authkey", required=True,
help="SNMPv3 auth key", type=str, dest='authkey')
parser.add_argument("-X", "--privkey", required=True,
help="SNMPv3 priv key", type=str, dest='privkey')
parser.add_argument("-a", "--authmode", required=False,
help="SNMPv3 auth mode", type=str, dest='authmode',
default='SHA',
choices=['MD5', 'SHA', 'SHA224', 'SHA256', 'SHA384',
'SHA512'])
parser.add_argument("-x", "--privmode", required=False,
help="SNMPv3 privacy mode", type=str, dest='privmode',
default='AES',
choices=['DES', '3DES', 'AES', 'AES192', 'AES256'])
parser.add_argument("-w", "--warn", required=False,
help="warning threshold (in percent)",
type=float, dest='warn', default="70")
parser.add_argument("-c", "--crit", required=False,
help="warning thresholds (in percent)",
type=float, dest='crit', default="80")
parser.add_argument("--mib", required=False, help="use OIDs from this MIB",
type=str, dest='mib',
default="CISCO-PROCESS-MIB",
choices=["CISCO-PROCESS-MIB", "CISCO-MEMORY-POOL-MIB"])
args = parser.parse_args()
return args
def get_snmp_table(table_oid, args):
""" get SNMP table """
# initialize empty list for return object
table = []
if args.v3mode == "authPriv":
iterator = bulkCmd(
SnmpEngine(),
UsmUserData(args.user, args.authkey, args.privkey,
authProtocol=authprot[args.authmode],
privProtocol=privprot[args.privmode]),
UdpTransportTarget((args.host, args.port), timeout=args.timeout),
ContextData(),
0, 20,
ObjectType(ObjectIdentity(table_oid)),
lexicographicMode=False,
lookupMib=False
)
elif args.v3mode == "authNoPriv":
iterator = bulkCmd(
SnmpEngine(),
UsmUserData(args.user, args.authkey,
authProtocol=authprot[args.authmode]),
UdpTransportTarget((args.host, args.port), timeout=args.timeout),
ContextData(),
0, 20,
ObjectType(ObjectIdentity(table_oid)),
lexicographicMode=False,
lookupMib=False
)
for error_indication, error_status, error_index, var_binds in iterator:
if error_indication:
exit_plugin("3", ''.join(['SNMP error: ', str(error_indication)]), "")
elif error_status:
print(f"{error_status.prettyPrint()} at "
f"{error_index and var_binds[int(error_index) - 1][0] or '?'}")
else:
# split OID and value into two fields and append to return element
table.append([str(var_binds[0][0]), str(var_binds[0][1])])
# return list with all OIDs/values from snmp table
return table
def exit_plugin(returncode, output, perfdata):
""" Check status and exit accordingly """
if returncode == "3":
print("UNKNOWN - " + str(output))
sys.exit(3)
if returncode == "2":
print("CRITICAL - " + str(output) + " | " + str(perfdata))
sys.exit(2)
if returncode == "1":
print("WARNING - " + str(output) + " | " + str(perfdata))
sys.exit(1)
elif returncode == "0":
print("OK - " + str(output) + " | " + str(perfdata))
sys.exit(0)
def main():
""" Main program code """
# Get Arguments
args = get_args()
if args.mib == "CISCO-PROCESS-MIB":
# Use revised OIDs in CISCO-PROCESS-MIB
# CISCO-PROCESS-MIB::cpmCPUMemoryUsed
# CISCO-PROCESS-MIB::cpmCPUMemoryFree
mem_used = get_snmp_table('1.3.6.1.4.1.9.9.109.1.1.1.1.12', args)
mem_free = get_snmp_table('1.3.6.1.4.1.9.9.109.1.1.1.1.13', args)
if args.mib == "CISCO-MEMORY-POOL-MIB":
# Use OIDs in CISCO-MEMORY-POOL-MIB
# CISCO-MEMORY-POOL-MIB::ciscoMemoryPoolUsed
# CISCO-MEMORY-POOL-MIB::ciscoMemoryPoolFree
mem_used = get_snmp_table('1.3.6.1.4.1.9.9.48.1.1.1.5', args)
mem_free = get_snmp_table('1.3.6.1.4.1.9.9.48.1.1.1.6', args)
if len(mem_used) == 0 or len(mem_free) == 0:
# Check if we received data via SNMP, otherwise exit with state Unknown
exit_plugin("3", "No data returned via SNMP", "NULL")
# Extract OID identifier from OID
for entry in chain(mem_used, mem_free):
entry[0] = entry[0].strip().split(".")[-1:]
entry[0] = "".join(map(str, entry[0]))
entry[1] = entry[1].strip()
# Create list with CPU identifiers
memids = []
for i in mem_free:
memids.append(i[0])
if args.mib == "CISCO-MEMORY-POOL-MIB":
# Further changes for CISCO-MEMORY-POOL-MIB
# CISCO-MEMORY-POOL-MIB gives readings in B instead of KB,
# convert accordingly
for i in mem_used:
i[1] = round(int(i[1]) / 1024, 2)
for i in mem_free:
i[1] = round(int(i[1]) / 1024, 2)
# Set return code and generate output and perfdata strings
returncode = "0"
perfdata = ""
output = ""
for i in memids:
# loop through memory id's
memid = i
for entry in mem_used:
# loop through "mempory used" values and extract reading
# for this memory ID
if str(entry[0]) == str(memid):
used = float(entry[1])
for entry in mem_free:
# loop throug "memory free" values and extract reading for this
# CPU ID
if str(entry[0]) == str(memid):
free = float(entry[1])
# Calculate total memory and thresholds (all in KB)
total = free + used
warn_b = round(total * (args.warn / 100))
crit_b = round(total * (args.crit / 100))
# Calculate percentages
used_pct = round((used / total) * 100, 2)
# Append to perfdata and output string
perfdata += ''.join(["\'mem_used_", str(memid), "\'=", str(used),
"KB;", str(warn_b), ";", str(crit_b), ";0;",
str(total), " "])
output += ''.join(["Memory (", str(memid), "): ", str(used_pct),
"%, "])
# Evaluate against thresholds
if used >= crit_b:
returncode = "2"
if returncode != "2" and used >= warn_b:
returncode = "1"
# Remove last comma from output string
output = output.rstrip(', ')
exit_plugin(returncode, output, perfdata)
if __name__ == "__main__":
main()