Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 3b40ca9

Browse files
feat: refactor code
1 parent ce3dfe0 commit 3b40ca9

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

splunk_connect_for_snmp_mib_server/mib_server.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,32 @@ def translator():
6565
# when 'return_multimetric' variable is set up as 'True', mib server should return both metric and
6666
# non-metric representation of the result
6767
if return_multimetric == "True":
68-
result_dict = self._translator.format_metric_data(var_binds[0])
69-
result_string = self._translator.format_trap_event(var_binds)
70-
result = {'metric_name': result_dict['metric_name'], 'metric': json.dumps(result_dict),
71-
'non_metric': result_string}
68+
return self._return_multimetric_data(var_binds)
7269
else:
7370
result = self._translator.format_trap_event(var_binds)
7471
return result
7572

7673
return app
7774

75+
def _return_multimetric_data(self, varbinds: list) -> dict:
76+
"""
77+
If field 'return_multimetric' was set to 'True', mib_server returns dictionary containing metric structure,
78+
non-metric structure and metric name. For example:
79+
80+
{'metric_name': 'sc4snmp.IF-MIB.ifDescr_1',
81+
'metric': '{"metric_name": "sc4snmp.IF-MIB.ifDescr_1", "_value": "lo", "metric_type": "OctetString"}',
82+
'non_metric': 'oid-type1="ObjectIdentity" value1-type="OctetString" 1.3.6.1.2.1.2.2.1.2.1="lo"
83+
value1="lo" IF-MIB::ifDescr.1="lo" '}
84+
85+
:param varbinds: list of varbinds
86+
:return: dictionary of the above structure
87+
"""
88+
result_dict = self._translator.format_metric_data(varbinds[0])
89+
result_string = self._translator.format_trap_event(varbinds)
90+
result = {'metric_name': result_dict['metric_name'], 'metric': json.dumps(result_dict),
91+
'non_metric': result_string}
92+
return result
93+
7894
def run_mib_server(self):
7995
# poetry run fails when debug=True
8096
self._flask_app.run(host="0.0.0.0", port=self._args.port)

0 commit comments

Comments
 (0)