Skip to content

Commit e12ddc8

Browse files
author
Carl Chang
committed
code tidy up;
remove unnecessary oid in config_basic; update readme
1 parent f77619b commit e12ddc8

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@
22
## Python script / nagios plug-in that checks components status of Dell or HP servers with snmpwalk command
33

44
```
5-
usage: check_snmp.py [-h] [-v VERSION] [-c COMMUNITY] [-r] [-f] [--config [CONFIG]]
5+
usage: check_snmp.py [-h] [-v VERSION] [-c COMMUNITY] [-r] [-f] [-b]
6+
[--config [CONFIG]]
67
host [category [category ...]]
78
89
positional arguments:
910
host The host to connect to.
10-
category One or more of the categories from the configuration separated by spaces.
11+
category One or more of the categories from the configuration
12+
separated by spaces.
1113
1214
optional arguments:
1315
-h, --help show this help message and exit
1416
-v VERSION, --version VERSION
1517
The SNMP version to use.
1618
-c COMMUNITY, --community COMMUNITY
1719
The community string to use.
18-
-r, --respect Respect properties marked as important when other results contain errors.
20+
-r, --respect Respect properties marked as important when other
21+
results contain errors.
1922
-f, --format Include additional format like colors in the output.
23+
-b, --brief Output brief information (only description and
24+
combined status).
2025
--config [CONFIG] The configuration file to load.
26+
27+
2128
```
2229

2330
For example `check_snmp.py -r -f 192.168.1.120`

check_snmp.py

100644100755
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
_parser.add_argument('-c', '--community', help='The community string to use.', default='public')
1414
_parser.add_argument('-r', '--respect', help='Respect properties marked as important when other results contain errors.', action='store_true')
1515
_parser.add_argument('-f', '--format', help='Include additional format like colors in the output.', action='store_true')
16-
_parser.add_argument('-b', '--brief', help='Output brief information (only combined status etc.).', action='store_true')
16+
_parser.add_argument('-b', '--brief', help='Output brief information (only description and combined status).', action='store_true')
1717
_parser.add_argument('host', help='The host to connect to.')
1818
_parser.add_argument('--config', nargs='?', help='The configuration file to load.', default='config_default.json')
1919
_parser.add_argument('category', nargs='*', help='One or more of the categories from the configuration separated by spaces.')
@@ -34,9 +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
3738
StatusOK = ('ok', 'true', 'yes', 'on', 'online', 'spunup', 'full', 'ready', 'enabled', 'presence', 'non-raid', 'nonraid', '0', 'notapplicable')
3839
StatusWarning = ('noncritical', 'removed', 'foreign', 'offline', 'rebuild')
3940
StatusCritical = ('fail', 'failed', 'critical', 'nonrecoverable', 'notredundant', 'lost', 'degraded', 'redundancyoffline')
41+
4042
StatusMap = {
4143
0: {'status': 'OK', 'color': '\033[32m{}\033[00m', 'severity': 0}, # green
4244
1: {'status': 'WARNING', 'color': '\033[33m{}\033[00m', 'severity': 2}, # orange
@@ -185,33 +187,29 @@ def get_row_output(col_val_raw: str, col_type: str, col_prefix: str = None, col_
185187
oid.get('suffix')) for l in oid_result_raw.stdout.splitlines()])
186188
category_result_raw = [i for i in zip(*category_result_raw)] # swap axis
187189

190+
# generate output
188191
if len(category_result_raw) == 1 and len(category_result_raw[0]) == 1: # there is only one status item without anything else
189192
col = category_result_raw[0][0]
190-
row_output = get_row_output(col[0],
191-
col[1],
192-
col[2],
193-
col[3])
193+
row_output = get_row_output(col[0], col[1], col[2], col[3])
194194
if not args_Brief:
195-
category_output = category_output.format(combined_status=row_output)
195+
combined_status = row_output
196196
else:
197-
category_output = category_output.format(combined_status=status_formatter(category_code, with_color=args_MoreFormat))
198-
197+
combined_status = status_formatter(category_code, with_color=args_MoreFormat)
198+
category_output = category_output.format(combined_status=combined_status)
199199
else:
200200
for row in category_result_raw: # row is like (('DIMM.Socket.A1', 'text'), ('failed', 'status'))
201201
cols = []
202202
for col in row: # col is like ('DIMM.Socket.A1', 'text')
203-
cols.append(get_row_output(col[0],
204-
col[1],
205-
col[2],
206-
col[3]))
203+
cols.append(get_row_output(col[0], col[1], col[2], col[3]))
207204

208205
if not args_Brief:
209206
category_output += list_bullet + oid_separator.join(cols) + '\n'
210-
211207
category_output = category_output.format(combined_status=status_formatter(category_code, with_color=args_MoreFormat))
212-
208+
209+
# write output
213210
sys.stdout.write(category_output if args_Brief else category_output + '\n')
214211

212+
# set exit code
215213
if imp: exitCodeImp = update_status_code(exitCodeImp, category_code)
216214
exitCode = update_status_code(exitCode, category_code)
217215

config_basic.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
"global": {
1111
"description": "Overall System Status",
1212
"oids": [
13-
{ "oid": "systemModelName", "type": "text" },
14-
{ "oid": "systemServiceTag", "type": "text" },
1513
{ "oid": "globalSystemStatus", "type": "status" }
1614
],
1715
"important": true
@@ -79,8 +77,6 @@
7977
"global": {
8078
"description": "Overall System Status",
8179
"oids": [
82-
{ "oid": "cpqSiProductName", "type": "text" },
83-
{ "oid": "cpqSiSysSerialNum", "type": "text" },
8480
{ "oid": "cpqHeMibCondition", "type": "status" }
8581
],
8682
"important": true

0 commit comments

Comments
 (0)