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

Commit 11a42ca

Browse files
fix: onetime walk with enricher (#154)
* fix: onetime_walk with enricher * fix: delete unnecessary checking for instance type * fix: fix unit tests * fix: add logs * fix: add checking for types in sort method * fix: delete unnecessary _return_mib_enricher_for_walk method * fix: update unit tests and change metric structure when no mib server specified * fix: update logging messages
1 parent 15158e8 commit 11a42ca

File tree

3 files changed

+92
-88
lines changed

3 files changed

+92
-88
lines changed

splunk_connect_for_snmp_poller/manager/task_utilities.py

Lines changed: 45 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ async def get_translated_string(mib_server_url, var_binds, return_multimetric=Fa
8585
@return is_metric: boolean, metric data flag
8686
"""
8787
logger.debug(f"Getting translation for the following var_binds: {var_binds}")
88-
is_metric, result = await result_without_translation(var_binds)
88+
is_metric, result = await result_without_translation(var_binds, return_multimetric)
8989

9090
# Override the var_binds string with translated var_binds string
9191
try:
@@ -110,15 +110,14 @@ async def get_translated_string(mib_server_url, var_binds, return_multimetric=Fa
110110
return result, is_metric
111111

112112

113-
async def result_without_translation(var_binds):
113+
async def result_without_translation(var_binds, return_multimetric):
114114
# Get Original var_binds as backup in case the mib-server is unreachable
115115
for name, val in var_binds:
116116
# Original oid
117117
# TODO Discuss: should we return the original oid
118118
# if the mib server is unreachable
119119
# should we format it align with the format of the translated one
120120
# result = "{} = {}".format(name.prettyPrint(), val.prettyPrint())
121-
122121
# check if this is metric data
123122
is_metric = is_metric_data(val.prettyPrint())
124123
if is_metric:
@@ -131,24 +130,29 @@ async def result_without_translation(var_binds):
131130
}
132131
result = json.dumps(result)
133132
else:
134-
metric_content_dict = {
135-
name.prettyPrint(): val.prettyPrint(),
136-
InterfaceMib.METRIC_NAME_KEY: name.prettyPrint(),
137-
}
133+
if return_multimetric:
134+
metric_content_dict = {
135+
name.prettyPrint(): val.prettyPrint(),
136+
InterfaceMib.METRIC_NAME_KEY: name.prettyPrint(),
137+
}
138138

139-
metric_content = json.dumps(metric_content_dict)
139+
metric_content = json.dumps(metric_content_dict)
140140

141-
non_metric_content = '{oid}="{value}"'.format(
142-
oid=name.prettyPrint(), value=val.prettyPrint()
143-
)
141+
non_metric_content = '{oid}="{value}"'.format(
142+
oid=name.prettyPrint(), value=val.prettyPrint()
143+
)
144144

145-
result_dict = {
146-
"metric": metric_content,
147-
"non_metric": non_metric_content,
148-
"metric_name": name.prettyPrint(),
149-
}
145+
result_dict = {
146+
"metric": metric_content,
147+
"non_metric": non_metric_content,
148+
"metric_name": name.prettyPrint(),
149+
}
150150

151-
result = json.dumps(result_dict)
151+
result = json.dumps(result_dict)
152+
else:
153+
result = '{oid}="{value}"'.format(
154+
oid=name.prettyPrint(), value=val.prettyPrint()
155+
)
152156
logger.debug("Our result is_metric - %s and string - %s", is_metric, result)
153157
return is_metric, result
154158

@@ -552,48 +556,42 @@ async def walk_handler_with_enricher(
552556
errorIndex,
553557
host,
554558
index,
555-
ir,
556-
additional_metric_fields,
557559
one_time_flag,
558560
is_metric,
561+
ir,
562+
additional_metric_fields,
559563
var_binds,
560564
):
561565
break
562566
else:
563567
result, is_metric = await get_translated_string(
564568
mib_server_url, var_binds, True
565569
)
566-
_sort_walk_data(
570+
new_result = _sort_walk_data(
567571
is_metric,
568572
merged_result_metric,
569573
merged_result_non_metric,
570574
merged_result,
571575
result,
572576
)
577+
post_data_to_splunk_hec(
578+
hec_sender,
579+
host,
580+
new_result,
581+
is_metric,
582+
index,
583+
ir,
584+
additional_metric_fields,
585+
one_time_flag,
586+
)
573587

588+
logger.info(f"Walk finished for {host} profile={ir.profile}")
574589
processed_result = extract_network_interface_data_from_walk(enricher, merged_result)
575590
additional_enricher_varbinds = (
576591
extract_network_interface_data_from_additional_config(enricher)
577592
)
578-
mib_enricher = _return_mib_enricher_for_walk(
579-
mongo_connection,
580-
f"{host}:{port}",
581-
processed_result,
582-
additional_enricher_varbinds,
583-
)
584-
post_walk_data_to_splunk_arguments = [
585-
host,
586-
index,
587-
one_time_flag,
588-
ir,
589-
additional_metric_fields,
590-
mib_enricher,
591-
]
592-
_post_walk_data_to_splunk(
593-
hec_sender, merged_result_metric, True, *post_walk_data_to_splunk_arguments
594-
)
595-
_post_walk_data_to_splunk(
596-
hec_sender, merged_result_non_metric, False, *post_walk_data_to_splunk_arguments
593+
mongo_connection.update_mib_static_data_for(
594+
f"{host}:{port}", processed_result, additional_enricher_varbinds
597595
)
598596

599597

@@ -620,56 +618,17 @@ def _sort_walk_data(
620618
"""
621619
if is_metric:
622620
merged_result_metric.append(varbind)
621+
result_to_send_to_hec = varbind
623622
merged_result.append(eval(varbind))
624623
else:
625624
merged_result_non_metric.append(varbind)
626-
result = eval(varbind)
627-
merged_result.append(eval(result["metric"]))
628-
629-
630-
def _return_mib_enricher_for_walk(
631-
mongo_connection, hostname, existing_data, additional_data
632-
):
633-
"""
634-
This function works only when an enricher is specified in the config and walk is being ran.
635-
If any data was derived from walk result, then the function updates MongoDB with the result.
636-
If no data was derived from the walk, then it's being retrieved from the MongoDB.
637-
"""
638-
if existing_data or additional_data:
639-
processed_result = mongo_connection.update_mib_static_data_for(
640-
hostname, existing_data, additional_data
641-
)
642-
return MibEnricher(processed_result)
643-
else:
644-
processed_data = mongo_connection.static_data_for(hostname)
645-
if not processed_data:
646-
return None
647-
return MibEnricher(processed_data)
648-
649-
650-
def _post_walk_data_to_splunk(
651-
hec_sender,
652-
result_list,
653-
is_metric,
654-
host,
655-
index,
656-
one_time_flag,
657-
ir,
658-
additional_metric_fields,
659-
mib_enricher,
660-
):
661-
for result in result_list:
662-
post_data_to_splunk_hec(
663-
hec_sender,
664-
host,
665-
result,
666-
is_metric,
667-
index,
668-
ir,
669-
additional_metric_fields,
670-
one_time_flag,
671-
mib_enricher,
672-
)
625+
result_dict = eval(varbind)
626+
metric_part = result_dict["metric"]
627+
if isinstance(metric_part, str):
628+
metric_part = eval(metric_part)
629+
merged_result.append(metric_part)
630+
result_to_send_to_hec = result_dict["non_metric"]
631+
return result_to_send_to_hec
673632

674633

675634
def parse_port(host):

splunk_connect_for_snmp_poller/mongo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def update_mib_static_data_for(self, host, existing_data, additional_data):
187187
static_data_dictionary = self.create_mib_static_data_mongo_structure(
188188
existing_data, additional_data
189189
)
190+
logger.info(f"Updating static data for {host} with {static_data_dictionary}")
190191
self._walked_hosts.find_one_and_update(
191192
{"_id": host},
192193
{"$set": static_data_dictionary},

tests/test_task_utilities.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test__sort_walk_data_metric(self):
8181
}
8282
is_metric = True
8383
merged_result_metric, merged_result_non_metric, merged_result = [], [], []
84-
_sort_walk_data(
84+
result = _sort_walk_data(
8585
is_metric,
8686
merged_result_metric,
8787
merged_result_non_metric,
@@ -91,17 +91,19 @@ def test__sort_walk_data_metric(self):
9191
self.assertEqual(merged_result_metric, [varbind])
9292
self.assertEqual(merged_result, [varbind_dict])
9393
self.assertEqual(merged_result_non_metric, [])
94+
self.assertEqual(result, varbind)
9495

9596
def test__sort_walk_data_non_metric(self):
9697
varbind = '{"metric":"{\\"metric_name\\": \\"sc4snmp.IF-MIB.ifDescr_1\\", \\"_value\\": \\"lo\\", \\"metric_type\\": \\"OctetString\\"}","metric_name":"sc4snmp.IF-MIB.ifDescr_1","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\\" "}' # noqa: E501
98+
expected_result = """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" """ # noqa: E501
9799
varbind_metric_dict = {
98100
"metric_name": "sc4snmp.IF-MIB.ifDescr_1",
99101
"_value": "lo",
100102
"metric_type": "OctetString",
101103
}
102104
is_metric = False
103105
merged_result_metric, merged_result_non_metric, merged_result = [], [], []
104-
_sort_walk_data(
106+
result = _sort_walk_data(
105107
is_metric,
106108
merged_result_metric,
107109
merged_result_non_metric,
@@ -111,6 +113,48 @@ def test__sort_walk_data_non_metric(self):
111113
self.assertEqual(merged_result_metric, [])
112114
self.assertEqual(merged_result, [varbind_metric_dict])
113115
self.assertEqual(merged_result_non_metric, [varbind])
116+
self.assertEqual(result, expected_result)
117+
118+
def test__sort_walk_data_without_mib_server_metric(self):
119+
varbind = '{"metric_name": "sc4snmp.SNMPv2-SMI.mib-2_2_2_1_10_2", "_value": "137003294"}'
120+
varbind_metric_dict = {
121+
"metric_name": "sc4snmp.SNMPv2-SMI.mib-2_2_2_1_10_2",
122+
"_value": "137003294",
123+
}
124+
is_metric = True
125+
merged_result_metric, merged_result_non_metric, merged_result = [], [], []
126+
result = _sort_walk_data(
127+
is_metric,
128+
merged_result_metric,
129+
merged_result_non_metric,
130+
merged_result,
131+
varbind,
132+
)
133+
self.assertEqual(merged_result_metric, [varbind])
134+
self.assertEqual(merged_result, [varbind_metric_dict])
135+
self.assertEqual(merged_result_non_metric, [])
136+
self.assertEqual(result, varbind)
137+
138+
def test__sort_walk_data_without_mib_server_non_metric(self):
139+
varbind = '{"metric": {"metric_name": "SNMPv2-SMI::mib-2.2.2.1.2.1", "_value": "lo"}, "non_metric": "SNMPv2-SMI::mib-2.2.2.1.2.1=lo"}' # noqa: E501
140+
varbind_metric_dict = {
141+
"metric_name": "SNMPv2-SMI::mib-2.2.2.1.2.1",
142+
"_value": "lo",
143+
}
144+
expected_result = "SNMPv2-SMI::mib-2.2.2.1.2.1=lo"
145+
is_metric = False
146+
merged_result_metric, merged_result_non_metric, merged_result = [], [], []
147+
result = _sort_walk_data(
148+
is_metric,
149+
merged_result_metric,
150+
merged_result_non_metric,
151+
merged_result,
152+
varbind,
153+
)
154+
self.assertEqual(merged_result_metric, [])
155+
self.assertEqual(merged_result, [varbind_metric_dict])
156+
self.assertEqual(merged_result_non_metric, [varbind])
157+
self.assertEqual(result, expected_result)
114158

115159
def test_is_oid_asterisk(self):
116160
oid = "1.3.6.1.2.1.2.2.1.1.*"

0 commit comments

Comments
 (0)