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

Commit e6a2f5b

Browse files
authored
feat: don't put empty values and remove trailing index numbers from m… (#173)
* feat: don't put empty values and remove trailing index numbers from metric name * fix: build fix * fix: build fix * fix: build fix * fix: PR comments * fix: build fix
1 parent b3c6f0e commit e6a2f5b

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

splunk_connect_for_snmp_poller/manager/hec_sender.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
)
2727
from splunk_connect_for_snmp_poller.manager.data.inventory_record import InventoryRecord
2828
from splunk_connect_for_snmp_poller.manager.static.mib_enricher import MibEnricher
29+
from splunk_connect_for_snmp_poller.manager.variables import (
30+
enricher_name,
31+
enricher_oid_family,
32+
)
33+
from splunk_connect_for_snmp_poller.utilities import multi_key_lookup
2934

3035
logger = get_logger(__name__)
3136

@@ -70,6 +75,7 @@ def post_data_to_splunk_hec(
7075
index,
7176
ir: InventoryRecord,
7277
additional_metric_fields,
78+
server_config,
7379
one_time_flag=False,
7480
mib_enricher=None,
7581
is_error=False,
@@ -85,6 +91,7 @@ def post_data_to_splunk_hec(
8591
index["metric_index"],
8692
ir,
8793
additional_metric_fields,
94+
server_config,
8895
mib_enricher,
8996
)
9097
else:
@@ -162,7 +169,8 @@ def _enrich_event_data(mib_enricher: MibEnricher, variables_binds: dict) -> str:
162169
logger.debug(additional_dimensions)
163170
for field_name in additional_dimensions:
164171
if field_name in metric_result:
165-
non_metric_result += f'{field_name}="{metric_result[field_name]}" '
172+
if metric_result[field_name]:
173+
non_metric_result += f'{field_name}="{metric_result[field_name]}" '
166174
return non_metric_result
167175

168176

@@ -172,6 +180,7 @@ def build_metric_data(
172180
index,
173181
ir: InventoryRecord,
174182
additional_metric_fields,
183+
server_config,
175184
mib_enricher=None,
176185
):
177186
json_val = json.loads(variables_binds)
@@ -190,11 +199,23 @@ def build_metric_data(
190199

191200
builder = init_builder_with_common_data(time.time(), host, index)
192201
builder.add(EventField.EVENT, EventType.METRIC.value)
193-
builder.add_fields(fields)
194202

203+
strip_trailing_index_number(fields, metric_name, metric_value, server_config)
204+
205+
builder.add_fields(fields)
195206
return builder.build()
196207

197208

209+
def strip_trailing_index_number(fields, metric_name, metric_value, server_config):
210+
result = multi_key_lookup(server_config, (enricher_name, enricher_oid_family))
211+
oid_families = result if result else []
212+
213+
if any(metric_name.startswith("sc4snmp." + x) for x in oid_families):
214+
stripped = metric_name[: metric_name.rindex("_")]
215+
del fields["metric_name:" + metric_name]
216+
fields["metric_name:" + stripped] = metric_value
217+
218+
198219
def build_error_data(
199220
host,
200221
variables_binds,
@@ -214,4 +235,5 @@ def _enrich_metric_data(
214235
)
215236
for field_name in additional_if_mib_dimensions:
216237
if field_name in variables_binds:
217-
fields[field_name] = variables_binds[field_name]
238+
if variables_binds[field_name]:
239+
fields[field_name] = variables_binds[field_name]

splunk_connect_for_snmp_poller/manager/task_utilities.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ async def snmp_get_handler(
229229
ir,
230230
additional_metric_fields,
231231
var_binds,
232+
server_config,
232233
):
233234
"""
234235
Perform the SNMP Get for an oid,
@@ -260,6 +261,7 @@ async def snmp_get_handler(
260261
index,
261262
ir,
262263
additional_metric_fields,
264+
server_config,
263265
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
264266
mib_enricher=mib_enricher,
265267
)
@@ -276,6 +278,7 @@ async def snmp_get_handler(
276278
index,
277279
ir,
278280
additional_metric_fields,
281+
server_config,
279282
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
280283
is_error=is_error,
281284
)
@@ -330,6 +333,7 @@ def _any_walk_failure_happened(
330333
is_metric,
331334
ir,
332335
additional_metric_fields,
336+
server_config,
333337
var_binds,
334338
):
335339
is_error, result = prepare_error_message(
@@ -345,6 +349,7 @@ def _any_walk_failure_happened(
345349
index,
346350
ir,
347351
additional_metric_fields,
352+
server_config,
348353
one_time_flag=one_time_flag,
349354
is_error=is_error,
350355
)
@@ -385,6 +390,7 @@ async def snmp_bulk_handler(
385390
ir,
386391
additional_metric_fields,
387392
var_binds,
393+
server_config,
388394
):
389395
"""
390396
Perform the SNMP Bulk for an array of oids
@@ -420,6 +426,7 @@ async def snmp_bulk_handler(
420426
index,
421427
ir,
422428
additional_metric_fields,
429+
server_config,
423430
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
424431
mib_enricher=mib_enricher,
425432
)
@@ -436,6 +443,7 @@ async def snmp_bulk_handler(
436443
index,
437444
ir,
438445
additional_metric_fields,
446+
server_config,
439447
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
440448
is_error=is_error,
441449
)
@@ -456,6 +464,7 @@ async def walk_handler(
456464
one_time_flag,
457465
ir,
458466
additional_metric_fields,
467+
server_config,
459468
):
460469
"""
461470
Perform the SNMP Walk for oid end with *,
@@ -484,6 +493,7 @@ async def walk_handler(
484493
is_metric,
485494
ir,
486495
additional_metric_fields,
496+
server_config,
487497
var_binds,
488498
):
489499
if OnetimeFlag.is_a_walk(one_time_flag):
@@ -499,6 +509,7 @@ async def walk_handler(
499509
index,
500510
ir,
501511
additional_metric_fields,
512+
server_config,
502513
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
503514
)
504515
if OnetimeFlag.is_a_walk(one_time_flag):
@@ -556,6 +567,7 @@ async def walk_handler_with_enricher(
556567
one_time_flag,
557568
ir,
558569
additional_metric_fields,
570+
server_config,
559571
):
560572
"""
561573
Perform the SNMP Walk for oid end with *,
@@ -585,6 +597,7 @@ async def walk_handler_with_enricher(
585597
is_metric,
586598
ir,
587599
additional_metric_fields,
600+
server_config,
588601
var_binds,
589602
):
590603
break
@@ -607,6 +620,7 @@ async def walk_handler_with_enricher(
607620
index,
608621
ir,
609622
additional_metric_fields,
623+
server_config,
610624
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
611625
)
612626

splunk_connect_for_snmp_poller/manager/tasks.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ async def get_snmp_data(
6767
one_time_flag,
6868
ir,
6969
additional_metric_fields,
70+
server_config,
7071
):
7172
if var_binds:
7273
try:
@@ -85,6 +86,7 @@ async def get_snmp_data(
8586
ir,
8687
additional_metric_fields,
8788
var_binds,
89+
server_config,
8890
)
8991
except Exception as e:
9092
logger.exception(f"Error occurred while calling {handler.__name__}(): {e}")
@@ -170,6 +172,7 @@ async def snmp_polling_async(
170172
one_time_flag,
171173
ir,
172174
additional_metric_fields,
175+
server_config,
173176
]
174177
get_bulk_specific_parameters = [mongo_connection, enricher_presence]
175178
try:

tests/test_hec_sender.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ def test_build_error_data_for_metric(self, mocked_hec, mocked_ir):
260260
test_index,
261261
mocked_ir,
262262
None,
263+
None,
263264
is_error=True,
264265
)
265266

@@ -297,6 +298,7 @@ def test_build_error_data_for_non_metric(self, mocked_hec, mocked_ir):
297298
test_index,
298299
mocked_ir,
299300
None,
301+
None,
300302
is_error=True,
301303
)
302304

0 commit comments

Comments
 (0)