Skip to content

Commit 8fb8107

Browse files
author
Carl Chang
committed
add optional arguments
1 parent cb859e5 commit 8fb8107

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

check_snmp.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77

88

99
_parser = argparse.ArgumentParser()
10+
_parser.add_argument('-v', help='The SNMP version to use.', default='2c')
11+
_parser.add_argument('-c', help='The community string to use.', default='public')
1012
_parser.add_argument('-r', help='Respect properties marked as important when other results contain errors.', action='store_true')
1113
_parser.add_argument('host', help='The host to connect to.')
1214
_args = _parser.parse_args()
13-
Host = _args.host
14-
RespectImp = _args.r
15+
args_Host = _args.host
16+
args_Version = _args.v
17+
args_Community = _args.c
18+
args_RespectImp = _args.r
1519

1620

1721
# get base folder
@@ -67,7 +71,7 @@
6771

6872

6973
def run(cmd: SnmpCommand) -> SnmpResult:
70-
cmdlst = [cmd.command, '-v', '2c', '-c', 'public']
74+
cmdlst = [cmd.command, '-v', args_Version, '-c', args_Community]
7175
if cmd.value_only: cmdlst.append('-O'); cmdlst.append('qv')
7276
if cmd.mib_dir: cmdlst.append('-M'); cmdlst.append(cmd.mib_dir)
7377
if cmd.mib: cmdlst.append('-m'); cmdlst.append(cmd.mib)
@@ -129,7 +133,7 @@ def update_status_code(old_status_code: int, status_code: int) -> int:
129133

130134
# execution
131135
# get vendor
132-
VendorResult = run(SnmpCommand('snmpgetnext', Host, '1.3.6.1.4.1', '', '', False))
136+
VendorResult = run(SnmpCommand('snmpgetnext', args_Host, '1.3.6.1.4.1', '', '', False))
133137
if VendorResult.stderr:
134138
print_and_exit(VendorResult.stderr, 3)
135139

@@ -149,7 +153,7 @@ def update_status_code(old_status_code: int, status_code: int) -> int:
149153
desc = vendor['oids'][oid]['description']
150154
imp = vendor['oids'][oid].get('important') is True
151155

152-
result = run(SnmpCommand('snmpwalk', Host, oid, mib_dir, mib, True))
156+
result = run(SnmpCommand('snmpwalk', args_Host, oid, mib_dir, mib, True))
153157
cs = multi_status_converter(result.stdout)
154158
if imp: exitCodeImp = update_status_code(exitCodeImp, cs.code)
155159
exitCode = update_status_code(exitCode, cs.code)
@@ -159,7 +163,7 @@ def update_status_code(old_status_code: int, status_code: int) -> int:
159163
status=result_status['color'].format(result_status['status']),
160164
formatted=cs.formatted))
161165

162-
if RespectImp and exitCodeImp > -1:
166+
if args_RespectImp and exitCodeImp > -1:
163167
sys.exit(exitCodeImp)
164168
else:
165169
sys.exit(exitCode)

0 commit comments

Comments
 (0)