forked from NETWAYS/check_hp_msa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_hp_msa.py
451 lines (330 loc) · 12.7 KB
/
check_hp_msa.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/env python3
"""
Icinga check plugin for HP MSA storages
TODO
---
Copyright (C) 2021 NETWAYS GmbH <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
import argparse
import logging
import requests
import hashlib
from xml.etree import ElementTree
from urllib.parse import urljoin
VERSION = '0.1.0'
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
STATES = {
OK: "OK",
WARNING: "WARNING",
CRITICAL: "CRITICAL",
UNKNOWN: "UNKNOWN",
}
def worst_state(*states):
overall = -1
for state in states:
if state == CRITICAL:
overall = CRITICAL
elif state == UNKNOWN:
if overall != CRITICAL:
overall = UNKNOWN
elif state > overall:
overall = state
if overall < 0 or overall > 3:
overall = UNKNOWN
return overall
def getPct(usage, size):
return round((100/float(size) * float(usage)),2)
class CriticalException(Exception):
"""
Provide an exception that will cause the check to exit critically with an error
"""
pass
class Client:
"""
Simple API client for MSA WBI
"""
API_PREFIX = '/api/'
def __init__(self, api, username, password, logger=None, insecure=False):
self.api = api
self.username = username
self.password = password
self.session = requests.Session()
self.session_key = None
self.insecure = insecure
# TODO: allow debug output
self.debug_outdir = 'tmp/'
#self.debug_outdir = None
if insecure:
self.session.verify = not insecure
import urllib3
requests.packages.urllib3.disable_warnings()
urllib3.disable_warnings()
if logger is None:
logger = logging.getLogger()
self.logger = logger
def credential_hash(self):
"""
Build a MD5 hashed credential built from username_password
"""
cred = "%s_%s" % (self.username, self.password)
return hashlib.md5(cred.encode()).hexdigest()
def login(self):
try:
cred = self.credential_hash()
xml, response = self.request('login/'+cred)
responseType, responseText = self.get_response_status(xml)
# TODO: we either receive sessionkey in responseText or a cookie in newer API releases
# both should be supported
self.session_key = responseText
except Exception as e:
raise CriticalException('login failed: ' + str(e))
def get_response_status(self, xml):
"""
Parse and return status information from the XML response
Will raise an Exception when not successful
"""
status = xml.find("./OBJECT[@name='status']")
responseType = status.find("./PROPERTY[@name='response-type']").text
responseText = status.find("./PROPERTY[@name='response']").text
self.logger.debug('XML response: %s - %s' % (responseType, responseText))
if responseType != "Success":
raise Exception("%s: %s" % (responseType, responseText))
return responseType, responseText
def request(self, url, method='GET', **kwargs):
"""
Basic XML API request handling
Returns the XML result when successful
"""
base_url = urljoin(self.api, self.API_PREFIX)
request_url = urljoin(base_url, url)
self.logger.debug("starting API %s request from: %s", method, url)
try:
headers = {}
if self.session_key:
headers['sessionKey'] = self.session_key
response = self.session.request(method, request_url, headers=headers)
except requests.exceptions.RequestException as e:
raise CriticalException(e)
if response.status_code != 200:
raise CriticalException('Request to %s was not successful: %s' % (request_url, response.status_code))
try:
# debug:
# print(response.text)
return ElementTree.fromstring(response.text), response
except Exception as e:
raise CriticalException('Could not decode API XML: ' + str(e))
def get_component(self, name, api_type, outputList, perfDataList=None):
"""
GET and initialize a class with a certain type
"""
xml, response = self.request('show/' + name)
status = self.get_response_status(xml)
# TODO: debug response in file
if self.debug_outdir is not None:
if not os.path.isdir(self.debug_outdir):
os.mkdir(self.debug_outdir)
with open(os.path.join(self.debug_outdir, "show-%s.xml" % name), 'w') as fh:
fh.write(response.text)
objects = []
for child in xml:
if child.tag != 'OBJECT':
continue
base_type = child.attrib['basetype']
if base_type == 'status':
continue
objects.append(ApiObject(child))
return CheckResult(objects,name, outputList, perfDataList)
class ApiObject:
def __init__(self, element):
"""
Build a Python object from an XML API object element
:type element: ElementTree.Element
"""
assert element.tag == 'OBJECT'
attrib = element.attrib
self.name = attrib['name']
self.base_type = attrib['basetype']
self.oid = attrib['oid']
self.properties = {}
# store all properties in a dict
for child in element:
if child.tag != 'PROPERTY':
continue
# TODO: display-name might be interesting at some point
value = child.text
value_type = child.attrib['type']
# cast to numeric when API passes that type
if value_type.startswith('int') or value_type.startswith('uint'):
value = int(value)
self.properties[child.attrib['name']] = value
class CheckResult:
def __init__(self, objects, objectName, outputList, perfDataList=None):
super().__init__()
self.objects = objects
self.state = -1
self.summary = []
self.output = []
self.perfdata = []
self.objectName = objectName
self.outputList = outputList
self.perfDataList = perfDataList
def get_output(self):
if len(self.summary) == 0:
self.build_output()
if self.state < 0:
self.build_status()
output = ' - '.join(self.summary)
if len(self.output) > 0:
output += "\n\n" + "\n".join(self.output)
if len(self.perfdata) > 0:
output += "\n| " + " ".join(self.perfdata)
try:
state = STATES[self.state]
except KeyError:
state = "UNKNOWN"
return "[%s] " % state + output
def get_status(self):
if self.state < 0:
self.build_status()
if self.state < 0:
return UNKNOWN
return self.state
def print_and_return(self):
print(self.get_output())
return self.get_status()
def build_output(self):
states = {
'objectctn': 0,
'unhealthy': 0,
}
for obj in self.objects:
p = obj.properties
states['objectctn'] += 1
# States
if p['health'] != "OK":
states['unhealthy'] += 1
# Build health summary
health = p['health']
if p['health-reason']:
health += " (%s)" % p['health-reason']
if p['health-recommendation']:
health += " (%s)" % p['health-recommendation']
# Handling output
outputLine=f"[{health}]"
for output in self.outputList:
outputLine+=f" {p[output]}"
self.output.append(outputLine)
# Handling PerfData
if self.perfDataList is not None:
for perfDataLine in self.perfDataList:
if isinstance(perfDataLine, str):
self.perfdata.append(f"{self.objectName}_{perfDataLine}={p[perfDataLine]}")
elif isinstance(perfDataLine, list):
usage=perfDataLine[0]
total=perfDataLine[1]
name=perfDataLine[2]
pct=getPct(p[usage],p[total])
self.perfdata.append(f"{self.objectName}_pct_{name}={pct}")
else:
raise NotImplemented("handling performance data of type {type(perfDataLine)} is not implemented")
for state in states:
if states[state] != 0:
self.summary.append("%d %s" % (states[state], str(self.objectName)))
if len(self.summary) == 0:
self.summary = [f"no {objectName}"]
def build_status(self):
states = []
for obj in self.objects:
p = obj.properties
if p['health'] != "OK":
states.append(CRITICAL)
else:
states.append(OK)
if len(states) == 0:
# no disks
self.state = OK
else:
self.state = worst_state(*states)
def parse_args():
args = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
args.add_argument('--api', '-A', required=True,
help='HP MSA host url (e.g. https://msa1.local)')
args.add_argument('--username', '-u', help='Username for login', required=True)
args.add_argument('--password', '-p', help='Password for login', required=True)
args.add_argument('--mode', '-m', help='Check mode', required=True)
args.add_argument('--insecure', help='Do not check certificates', action='store_true')
args.add_argument('--version', '-V', help='Print version', action='store_true')
return args.parse_args()
def main():
args = parse_args()
if args.version:
print("check_hp_msa version %s" % VERSION)
return 0
client = Client(args.api, args.username, args.password, insecure=args.insecure)
client.login()
mode = None
if args.mode == 'disks':
output=['location','vendor','model','size','serial-number','status']
mode = client.get_component( "disks", "drives",output)
elif args.mode == 'volumes':
output=['volume-name','volume-type','capabilities','size','serial-number']
perfdata=[
['allocated-size-numeric','total-size-numeric','allocation'],
'allocated-size'
]
mode = client.get_component("volumes","volumes",output,perfdata)
elif args.mode == 'enclosures':
output=['durable-id','type','board-model','slots','status']
perfdata=[
['number-of-disks','slots','diskallocation']
]
mode = client.get_component("enclosures", "enclosures",output,perfdata)
elif args.mode == 'ports':
output=['port','port-type','media','actual-speed','status']
perfdata=[
['configured-speed-numeric','actual-speed-numeric','speedallocation']
]
mode = client.get_component("ports", "port",output,perfdata)
elif args.mode == 'fans':
output=['name','location','status-ses','status']
perfdata=[
'speed'
]
mode = client.get_component("fans", "fan-details",output,perfdata)
elif args.mode == 'controllers':
output=['durable-id','ip-address','disks','description','status']
mode = client.get_component("controllers", "controllers",output)
else:
print("[UNKNOWN] unknown mode %s" % args.mode)
return UNKNOWN
return mode.print_and_return()
if __package__ == '__main__' or __package__ is None:
try:
sys.exit(main())
except CriticalException as e:
print("[CRITICAL] " + str(e))
sys.exit(CRITICAL)
except Exception:
exception = sys.exc_info()
print("[UNKNOWN] Unexpected Python error: %s %s" % (exception[0], exception[1]))
try:
import traceback
traceback.print_tb(exception[2])
except:
pass
sys.exit(UNKNOWN)