Skip to content

Commit faa43ab

Browse files
author
Carl Chang
committed
add more options for customized output;
1 parent c22f735 commit faa43ab

File tree

3 files changed

+127
-170
lines changed

3 files changed

+127
-170
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,53 @@ Exit Code | Status
5959
2 | CRITICAL
6060
3 | UNKNOWN
6161

62+
### Config File Structure
63+
Fields indicated by `xxxxx` is customizable.
64+
65+
```
66+
{
67+
"config": {
68+
"global-list-bullet": "xxxx", # bullet string before each item in a category
69+
# E.g. each disk item returned. Default is " - "
70+
"global-oid-separator": "xxxx", # string to separate each sub item in an oid entry.
71+
# E.g. each piece of info of a disk. Default is ", "
72+
},
73+
"dell": {
74+
"mib_dir": "xxxxxxxxxxxxxxxxxxxxxxxx", # do not change these unless you know what you are doing
75+
"mib": "xxxxxxxxxxxxxxx", # do not change these unless you know what you are doing
76+
"categories": {
77+
"xxxxxxxxx": { # category name can be any string
78+
"description": "xxxxxxxxxxxxxxxxxx",
79+
"oids": [
80+
{
81+
"oid": "xxxxxxxxxxxxxxxx",
82+
"type": "text" # type can be "text" or "status"
83+
},
84+
{
85+
"oid": "xxxxxxxxxxxxxxxx",
86+
"type": "status",
87+
"prefix": "xxxxxxxxxxxx" # (optional) used to annotate this oid and customize output
88+
"suffix": "xxxxxxxxxxxx" # (optional) used to annotate this oid and customize output
89+
},
90+
{
91+
"oid": "xxxxxxxxxxxxxxxx",
92+
"type": "status"
93+
},
94+
...
95+
"list-bullet": "xxxx" # (optional) string to override the global list bullet
96+
"oid-separator": "xxx", # (optional) string to override the global oid separator
97+
],
98+
"important": true # (optional) true or false
99+
},
100+
...
101+
}
102+
},
103+
"hpe": {
104+
... # same structure as dell
105+
}
106+
}
107+
```
108+
62109
### Loading MIB
63110
For Dell only one MIB module `IDRAC-MIB-SMIv2` needs to be specified in the snmpwalk command with `-m` option.
64111

@@ -154,3 +201,4 @@ OID (numeric format) | OID Translation | Description
154201

155202
[CPQHLTH-MIB treeview](http://www.oidview.com/mibs/232/CPQHLTH-MIB.html)
156203

204+
[Perl check_hp](https://github.com/lairsdragon/check_hp/blob/master/check_hp)

check_snmp.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@
191191
with open(args_Config, 'r') as config_file:
192192
Config = json.load(config_file, object_pairs_hook=OrderedDict)
193193

194-
StatusOK = ('ok', 'true', 'yes', 'on', 'online', 'spunup', 'full', 'ready', 'enabled', 'presence', 'non-raid', 'nonraid', '0')
195-
StatusWarning = ('noncritical', 'removed', 'foreign', 'offline')
194+
StatusOK = ('ok', 'true', 'yes', 'on', 'online', 'spunup', 'full', 'ready', 'enabled', 'presence', 'non-raid', 'nonraid', '0', 'notapplicable')
195+
StatusWarning = ('noncritical', 'removed', 'foreign', 'offline', 'rebuild')
196196
StatusCritical = ('fail', 'failed', 'critical', 'nonrecoverable', 'notredundant', 'lost', 'degraded', 'redundancyoffline')
197197
StatusMap = {
198198
0: {'status': 'OK', 'color': '\033[32m{}\033[00m', 'severity': 0}, # green
@@ -279,14 +279,17 @@ def status_formatter(status: int, alt_text: str = None, with_color: bool = True)
279279
return result
280280

281281

282-
def get_row_output(col_val_raw: str, col_type: str) -> str:
282+
def get_row_output(col_val_raw: str, col_type: str, col_prefix: str = None, col_suffix: str = None) -> str:
283+
if col_prefix is None: col_prefix = ''
284+
if col_suffix is None: col_suffix = ''
283285
if col_type == 'status':
284286
col_val = status_converter(col_val_raw)
285287
global category_code
286288
category_code = update_status_code(category_code, col_val)
287-
return status_formatter(col_val, StatusMap[col_val]['status'] + ' (' + col_val_raw + ')', args_MoreFormat)
289+
result = status_formatter(col_val, StatusMap[col_val]['status'] + ' (' + col_val_raw + ')', args_MoreFormat)
288290
else:
289-
return col_val_raw
291+
result = col_val_raw
292+
return col_prefix + result + col_suffix
290293

291294

292295
# execution
@@ -303,10 +306,13 @@ def get_row_output(col_val_raw: str, col_type: str) -> str:
303306
print_and_exit('Unknown vendor information.', 3)
304307

305308

309+
global_list_bullet = Config['config']['global-list-bullet']
310+
global_oid_separator = Config['config']['global-oid-separator']
306311
exitCode, exitCodeImp = -1, -1
307312
vendor = Config[Vendor]
308313
mib_dir = vendor['mib_dir']
309314
mib = vendor['mib']
315+
310316
for category_key in vendor['categories']:
311317
# only show specified categories when applicable
312318
if len(args_Category) > 0 and category_key not in args_Category: continue
@@ -315,6 +321,11 @@ def get_row_output(col_val_raw: str, col_type: str) -> str:
315321
description = category['description']
316322
imp = category.get('important') is True
317323

324+
list_bullet = category.get('list-bullet')
325+
if list_bullet is None: list_bullet = global_list_bullet
326+
oid_separator = category.get('oid-separator')
327+
if oid_separator is None: oid_separator = global_oid_separator
328+
318329
oids = category['oids']
319330
if len(oids) == 0: continue
320331

@@ -325,20 +336,29 @@ def get_row_output(col_val_raw: str, col_type: str) -> str:
325336
for oid in oids:
326337
oid_result_raw = run(SnmpCommand('snmpwalk', args_Host, oid['oid'], mib_dir, mib, True))
327338
if len(oid_result_raw.stderr) > 0: print_and_exit(oid_result_raw.stderr, 3)
328-
category_result_raw.append([(l.strip('" \n'), oid['type']) for l in oid_result_raw.stdout.splitlines()])
339+
category_result_raw.append([(l.strip('" \n'),
340+
oid['type'],
341+
oid.get('prefix'),
342+
oid.get('suffix')) for l in oid_result_raw.stdout.splitlines()])
329343
category_result_raw = [i for i in zip(*category_result_raw)] # swap axis
330344

331345
if len(category_result_raw) == 1 and len(category_result_raw[0]) == 1: # there is only one status item without anything else
332346
col = category_result_raw[0][0]
333-
category_output = category_output.format(combined_status=get_row_output(col[0], col[1]))
347+
category_output = category_output.format(combined_status=get_row_output(col[0],
348+
col[1],
349+
col[2],
350+
col[3]))
334351

335352
else:
336353
for row in category_result_raw: # row is like (('DIMM.Socket.A1', 'text'), ('failed', 'status'))
337354
cols = []
338355
for col in row: # col is like ('DIMM.Socket.A1', 'text')
339-
cols.append(get_row_output(col[0], col[1]))
356+
cols.append(get_row_output(col[0],
357+
col[1],
358+
col[2],
359+
col[3]))
340360

341-
category_output += ' - ' + ', '.join(cols) + '\n'
361+
category_output += list_bullet + oid_separator.join(cols) + '\n'
342362
category_output = category_output.format(combined_status=status_formatter(category_code, with_color=args_MoreFormat))
343363

344364
print(category_output)

0 commit comments

Comments
 (0)