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

Commit

Permalink
feat: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
omrozowicz-splunk committed Aug 18, 2021
1 parent ce3dfe0 commit 3b40ca9
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions splunk_connect_for_snmp_mib_server/mib_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,32 @@ def translator():
# when 'return_multimetric' variable is set up as 'True', mib server should return both metric and
# non-metric representation of the result
if return_multimetric == "True":
result_dict = self._translator.format_metric_data(var_binds[0])
result_string = self._translator.format_trap_event(var_binds)
result = {'metric_name': result_dict['metric_name'], 'metric': json.dumps(result_dict),
'non_metric': result_string}
return self._return_multimetric_data(var_binds)
else:
result = self._translator.format_trap_event(var_binds)
return result

return app

def _return_multimetric_data(self, varbinds: list) -> dict:
"""
If field 'return_multimetric' was set to 'True', mib_server returns dictionary containing metric structure,
non-metric structure and metric name. For example:
{'metric_name': 'sc4snmp.IF-MIB.ifDescr_1',
'metric': '{"metric_name": "sc4snmp.IF-MIB.ifDescr_1", "_value": "lo", "metric_type": "OctetString"}',
'non_metric': 'oid-type1="ObjectIdentity" value1-type="OctetString" 1.3.6.1.2.1.2.2.1.2.1="lo"
value1="lo" IF-MIB::ifDescr.1="lo" '}
:param varbinds: list of varbinds
:return: dictionary of the above structure
"""
result_dict = self._translator.format_metric_data(varbinds[0])
result_string = self._translator.format_trap_event(varbinds)
result = {'metric_name': result_dict['metric_name'], 'metric': json.dumps(result_dict),
'non_metric': result_string}
return result

def run_mib_server(self):
# poetry run fails when debug=True
self._flask_app.run(host="0.0.0.0", port=self._args.port)

0 comments on commit 3b40ca9

Please sign in to comment.