@@ -85,7 +85,7 @@ async def get_translated_string(mib_server_url, var_binds, return_multimetric=Fa
85
85
@return is_metric: boolean, metric data flag
86
86
"""
87
87
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 )
89
89
90
90
# Override the var_binds string with translated var_binds string
91
91
try :
@@ -110,15 +110,14 @@ async def get_translated_string(mib_server_url, var_binds, return_multimetric=Fa
110
110
return result , is_metric
111
111
112
112
113
- async def result_without_translation (var_binds ):
113
+ async def result_without_translation (var_binds , return_multimetric ):
114
114
# Get Original var_binds as backup in case the mib-server is unreachable
115
115
for name , val in var_binds :
116
116
# Original oid
117
117
# TODO Discuss: should we return the original oid
118
118
# if the mib server is unreachable
119
119
# should we format it align with the format of the translated one
120
120
# result = "{} = {}".format(name.prettyPrint(), val.prettyPrint())
121
-
122
121
# check if this is metric data
123
122
is_metric = is_metric_data (val .prettyPrint ())
124
123
if is_metric :
@@ -131,24 +130,29 @@ async def result_without_translation(var_binds):
131
130
}
132
131
result = json .dumps (result )
133
132
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
+ }
138
138
139
- metric_content = json .dumps (metric_content_dict )
139
+ metric_content = json .dumps (metric_content_dict )
140
140
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
+ )
144
144
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
+ }
150
150
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
+ )
152
156
logger .debug ("Our result is_metric - %s and string - %s" , is_metric , result )
153
157
return is_metric , result
154
158
@@ -552,48 +556,42 @@ async def walk_handler_with_enricher(
552
556
errorIndex ,
553
557
host ,
554
558
index ,
555
- ir ,
556
- additional_metric_fields ,
557
559
one_time_flag ,
558
560
is_metric ,
561
+ ir ,
562
+ additional_metric_fields ,
559
563
var_binds ,
560
564
):
561
565
break
562
566
else :
563
567
result , is_metric = await get_translated_string (
564
568
mib_server_url , var_binds , True
565
569
)
566
- _sort_walk_data (
570
+ new_result = _sort_walk_data (
567
571
is_metric ,
568
572
merged_result_metric ,
569
573
merged_result_non_metric ,
570
574
merged_result ,
571
575
result ,
572
576
)
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
+ )
573
587
588
+ logger .info (f"Walk finished for { host } profile={ ir .profile } " )
574
589
processed_result = extract_network_interface_data_from_walk (enricher , merged_result )
575
590
additional_enricher_varbinds = (
576
591
extract_network_interface_data_from_additional_config (enricher )
577
592
)
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
597
595
)
598
596
599
597
@@ -620,56 +618,17 @@ def _sort_walk_data(
620
618
"""
621
619
if is_metric :
622
620
merged_result_metric .append (varbind )
621
+ result_to_send_to_hec = varbind
623
622
merged_result .append (eval (varbind ))
624
623
else :
625
624
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
673
632
674
633
675
634
def parse_port (host ):
0 commit comments