Skip to content

Commit c041598

Browse files
author
Carl Chang
committed
add custom type for more output customization options;
move status values mapping to config file;
1 parent f7c4f60 commit c041598

File tree

4 files changed

+53
-25
lines changed

4 files changed

+53
-25
lines changed

README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,36 +76,43 @@ Fields indicated by `xxxxx` is customizable.
7676
```
7777
{
7878
"config": {
79-
"global-list-bullet": "xxxx", # bullet string prepended for each item in a category
80-
# E.g. each disk item returned. Default is " - "
79+
"status-ok": [ "xxx", ... ] # status values that indicate OK. Values should be lower case.
80+
"status-warning": [ "xxx", ... ] # status values that indicate WARNING. Values should be lower case.
81+
"status-critical": [ "xxx", ... ] # status values that indicate CRITICAL. Values should be lower case.
82+
"global-list-bullet": "xxxx", # bullet string prepended for each item in a category.
83+
# E.g. each disk item returned. Default is " - ".
8184
"global-oid-separator": "xxxx", # string to separate each sub item in an oid entry.
82-
# E.g. each piece of info of a disk. Default is ", "
85+
# E.g. each piece of info of a disk. Default is ", ".
86+
"custom-mappings": {
87+
"xxxxxxxx": { "xx": "ok", "xx": "fail" } # custom logic to convert raw value to standard status values (ok, fail, etc.).
88+
}
8389
},
8490
"dell": {
85-
"mib_dir": "xxxxxxxxxxxxxxxxxxxxxxxx", # do not change these unless you know what you are doing
86-
"mib": "xxxxxxxxxxxxxxx", # do not change these unless you know what you are doing
91+
"mib_dir": "xxxxxxxxxxxxxxxxxxxxxxxx", # do not change these unless you know what you are doing.
92+
"mib": "xxxxxxxxxxxxxxx", # do not change these unless you know what you are doing.
8793
"categories": {
88-
"xxxxxxxxx": { # category name can be any string
89-
# used in the command line for category selection
94+
"xxxxxxxxx": { # category name can be any string.
95+
# used in the command line for category selection.
9096
"description": "xxxxxxxxxxxxxxxxxx",
9197
"oids": [
9298
{
9399
"oid": "xxxxxxxxxxxxxxxx",
94-
"type": "status", # type can be "text" or "status"
95-
"prefix": "xxxxxxxxxxxx" # (optional) used to annotate this oid and customize output
96-
"suffix": "xxxxxxxxxxxx" # (optional) used to annotate this oid and customize output
100+
"type": "status", # type can be "text", "status", or "custom".
101+
"prefix": "xxxxxxxxxxxx" # (optional) used to annotate this oid and customize output.
102+
"suffix": "xxxxxxxxxxxx" # (optional) used to annotate this oid and customize output.
103+
"mapping": "xxxxxxxxxxxx" # (optional for non-custom type) specify custom mapping to use for this oid.
97104
},
98105
...
99-
"list-bullet": "xxxx" # (optional) string to override the global list bullet
100-
"oid-separator": "xxx", # (optional) string to override the global oid separator
106+
"list-bullet": "xxxx" # (optional) string to override the global list bullet.
107+
"oid-separator": "xxx", # (optional) string to override the global oid separator.
101108
],
102-
"important": true # (optional) true or false
109+
"important": true # (optional) true or false.
103110
},
104111
...
105112
}
106113
},
107114
"hpe": {
108-
... # same structure as dell
115+
... # same structure as dell.
109116
}
110117
}
111118
```

check_snmp.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
with open(args_Config, 'r') as config_file:
3535
Config = json.load(config_file, object_pairs_hook=OrderedDict)
3636

37-
# status strings need to be all lower case
38-
StatusOK = ('ok', 'true', 'yes', 'on', 'online', 'spunup', 'full', 'ready', 'enabled', 'presence', 'non-raid', 'nonraid', '0', 'notapplicable')
39-
StatusWarning = ('noncritical', 'removed', 'foreign', 'offline', 'rebuild')
40-
StatusCritical = ('fail', 'failed', 'critical', 'nonrecoverable', 'notredundant', 'lost', 'degraded', 'redundancyoffline')
37+
CustomMappings = Config['config'].get('custom-mappings') # type: OrderedDict[str, OrderedDict]
38+
39+
StatusOK = Config['config']['status-ok']
40+
StatusWarning = Config['config']['status-warning']
41+
StatusCritical = Config['config']['status-critical']
4142

4243
StatusMap = {
4344
0: {'status': 'OK', 'color': '\033[32m{}\033[00m', 'severity': 0}, # green
@@ -124,11 +125,20 @@ def status_formatter(status: int, alt_text: str = None, with_color: bool = True)
124125
return result
125126

126127

127-
def get_row_output(col_val_raw: str, col_type: str, col_prefix: str = None, col_suffix: str = None) -> str:
128+
def get_row_output(col_val_raw: str,
129+
col_type: str,
130+
col_prefix: str = None,
131+
col_suffix: str = None,
132+
col_mapping: str = None) -> str:
128133
if col_prefix is None: col_prefix = ''
129134
if col_suffix is None: col_suffix = ''
130-
if col_type == 'status':
131-
col_val = status_converter(col_val_raw)
135+
if col_type == 'custom' and col_mapping is None: col_type = 'text'
136+
137+
if col_type in ('status', 'custom'):
138+
if col_type == 'status':
139+
col_val = status_converter(col_val_raw)
140+
else: # convert to status based on custom mapping
141+
col_val = status_converter(CustomMappings[col_mapping][col_val_raw])
132142
global category_code
133143
category_code = update_status_code(category_code, col_val)
134144
result = status_formatter(col_val, StatusMap[col_val]['status'] + ' (' + col_val_raw + ')', args_MoreFormat)
@@ -184,13 +194,14 @@ def get_row_output(col_val_raw: str, col_type: str, col_prefix: str = None, col_
184194
category_result_raw.append([(l.strip('" \n'),
185195
oid['type'],
186196
oid.get('prefix'),
187-
oid.get('suffix')) for l in oid_result_raw.stdout.splitlines()])
197+
oid.get('suffix'),
198+
oid.get('mapping')) for l in oid_result_raw.stdout.splitlines()])
188199
category_result_raw = [i for i in zip(*category_result_raw)] # swap axis
189200

190201
# generate output
191202
if len(category_result_raw) == 1 and len(category_result_raw[0]) == 1: # there is only one status item without anything else
192203
col = category_result_raw[0][0]
193-
row_output = get_row_output(col[0], col[1], col[2], col[3])
204+
row_output = get_row_output(col[0], col[1], col[2], col[3], col[4])
194205
if not args_Brief:
195206
combined_status = row_output
196207
else:
@@ -200,7 +211,7 @@ def get_row_output(col_val_raw: str, col_type: str, col_prefix: str = None, col_
200211
for row in category_result_raw: # row is like (('DIMM.Socket.A1', 'text'), ('failed', 'status'))
201212
cols = []
202213
for col in row: # col is like ('DIMM.Socket.A1', 'text')
203-
cols.append(get_row_output(col[0], col[1], col[2], col[3]))
214+
cols.append(get_row_output(col[0], col[1], col[2], col[3], col[4]))
204215

205216
if not args_Brief:
206217
category_output += list_bullet + oid_separator.join(cols) + '\n'

config_basic.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"config": {
3+
"status-ok": [ "ok", "true", "yes", "on", "online", "spunup", "full", "ready", "enabled", "presence", "non-raid", "nonraid", "0", "notapplicable" ],
4+
"status-warning": [ "noncritical", "removed", "foreign", "offline", "rebuild" ],
5+
"status-critical": [ "fail", "failed", "critical", "nonrecoverable", "notredundant", "lost", "degraded", "redundancyoffline" ],
36
"global-list-bullet": " - ",
47
"global-oid-separator": ", "
58
},

config_default.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
{
22
"config": {
3+
"status-ok": [ "ok", "true", "yes", "on", "online", "spunup", "full", "ready", "enabled", "presence", "non-raid", "nonraid", "0", "notapplicable" ],
4+
"status-warning": [ "noncritical", "removed", "foreign", "offline", "rebuild" ],
5+
"status-critical": [ "fail", "failed", "critical", "nonrecoverable", "notredundant", "lost", "degraded", "redundancyoffline" ],
36
"global-list-bullet": " - ",
4-
"global-oid-separator": ", "
7+
"global-oid-separator": ", ",
8+
"custom-mappings": {
9+
"map-boolean": { "0": "ok", "1": "fail" }
10+
}
511
},
612
"dell": {
713
"mib_dir": "mibs/default:mibs/iana:mibs/ietf:mibs/dell",
@@ -35,6 +41,7 @@
3541
"oids": [
3642
{ "oid": "physicalDiskDisplayName", "type": "text" },
3743
{ "oid": "physicalDiskState", "type": "status", "prefix": " - Health: " },
44+
{ "oid": "physicalDiskSmartAlertIndication", "type": "custom", "prefix": " - Failing: ", "mapping": "map-boolean" },
3845
{ "oid": "physicalDiskOperationalState", "type": "status", "prefix": " - Operation: " },
3946
{ "oid": "physicalDiskProgress", "type": "text", "prefix": " - Progress: " }
4047
],

0 commit comments

Comments
 (0)