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

Commit

Permalink
feat: don't put empty values and remove trailing index numbers from m… (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
weliasz authored Oct 28, 2021
1 parent b3c6f0e commit e6a2f5b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
28 changes: 25 additions & 3 deletions splunk_connect_for_snmp_poller/manager/hec_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
)
from splunk_connect_for_snmp_poller.manager.data.inventory_record import InventoryRecord
from splunk_connect_for_snmp_poller.manager.static.mib_enricher import MibEnricher
from splunk_connect_for_snmp_poller.manager.variables import (
enricher_name,
enricher_oid_family,
)
from splunk_connect_for_snmp_poller.utilities import multi_key_lookup

logger = get_logger(__name__)

Expand Down Expand Up @@ -70,6 +75,7 @@ def post_data_to_splunk_hec(
index,
ir: InventoryRecord,
additional_metric_fields,
server_config,
one_time_flag=False,
mib_enricher=None,
is_error=False,
Expand All @@ -85,6 +91,7 @@ def post_data_to_splunk_hec(
index["metric_index"],
ir,
additional_metric_fields,
server_config,
mib_enricher,
)
else:
Expand Down Expand Up @@ -162,7 +169,8 @@ def _enrich_event_data(mib_enricher: MibEnricher, variables_binds: dict) -> str:
logger.debug(additional_dimensions)
for field_name in additional_dimensions:
if field_name in metric_result:
non_metric_result += f'{field_name}="{metric_result[field_name]}" '
if metric_result[field_name]:
non_metric_result += f'{field_name}="{metric_result[field_name]}" '
return non_metric_result


Expand All @@ -172,6 +180,7 @@ def build_metric_data(
index,
ir: InventoryRecord,
additional_metric_fields,
server_config,
mib_enricher=None,
):
json_val = json.loads(variables_binds)
Expand All @@ -190,11 +199,23 @@ def build_metric_data(

builder = init_builder_with_common_data(time.time(), host, index)
builder.add(EventField.EVENT, EventType.METRIC.value)
builder.add_fields(fields)

strip_trailing_index_number(fields, metric_name, metric_value, server_config)

builder.add_fields(fields)
return builder.build()


def strip_trailing_index_number(fields, metric_name, metric_value, server_config):
result = multi_key_lookup(server_config, (enricher_name, enricher_oid_family))
oid_families = result if result else []

if any(metric_name.startswith("sc4snmp." + x) for x in oid_families):
stripped = metric_name[: metric_name.rindex("_")]
del fields["metric_name:" + metric_name]
fields["metric_name:" + stripped] = metric_value


def build_error_data(
host,
variables_binds,
Expand All @@ -214,4 +235,5 @@ def _enrich_metric_data(
)
for field_name in additional_if_mib_dimensions:
if field_name in variables_binds:
fields[field_name] = variables_binds[field_name]
if variables_binds[field_name]:
fields[field_name] = variables_binds[field_name]
14 changes: 14 additions & 0 deletions splunk_connect_for_snmp_poller/manager/task_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ async def snmp_get_handler(
ir,
additional_metric_fields,
var_binds,
server_config,
):
"""
Perform the SNMP Get for an oid,
Expand Down Expand Up @@ -260,6 +261,7 @@ async def snmp_get_handler(
index,
ir,
additional_metric_fields,
server_config,
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
mib_enricher=mib_enricher,
)
Expand All @@ -276,6 +278,7 @@ async def snmp_get_handler(
index,
ir,
additional_metric_fields,
server_config,
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
is_error=is_error,
)
Expand Down Expand Up @@ -330,6 +333,7 @@ def _any_walk_failure_happened(
is_metric,
ir,
additional_metric_fields,
server_config,
var_binds,
):
is_error, result = prepare_error_message(
Expand All @@ -345,6 +349,7 @@ def _any_walk_failure_happened(
index,
ir,
additional_metric_fields,
server_config,
one_time_flag=one_time_flag,
is_error=is_error,
)
Expand Down Expand Up @@ -385,6 +390,7 @@ async def snmp_bulk_handler(
ir,
additional_metric_fields,
var_binds,
server_config,
):
"""
Perform the SNMP Bulk for an array of oids
Expand Down Expand Up @@ -420,6 +426,7 @@ async def snmp_bulk_handler(
index,
ir,
additional_metric_fields,
server_config,
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
mib_enricher=mib_enricher,
)
Expand All @@ -436,6 +443,7 @@ async def snmp_bulk_handler(
index,
ir,
additional_metric_fields,
server_config,
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
is_error=is_error,
)
Expand All @@ -456,6 +464,7 @@ async def walk_handler(
one_time_flag,
ir,
additional_metric_fields,
server_config,
):
"""
Perform the SNMP Walk for oid end with *,
Expand Down Expand Up @@ -484,6 +493,7 @@ async def walk_handler(
is_metric,
ir,
additional_metric_fields,
server_config,
var_binds,
):
if OnetimeFlag.is_a_walk(one_time_flag):
Expand All @@ -499,6 +509,7 @@ async def walk_handler(
index,
ir,
additional_metric_fields,
server_config,
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
)
if OnetimeFlag.is_a_walk(one_time_flag):
Expand Down Expand Up @@ -556,6 +567,7 @@ async def walk_handler_with_enricher(
one_time_flag,
ir,
additional_metric_fields,
server_config,
):
"""
Perform the SNMP Walk for oid end with *,
Expand Down Expand Up @@ -585,6 +597,7 @@ async def walk_handler_with_enricher(
is_metric,
ir,
additional_metric_fields,
server_config,
var_binds,
):
break
Expand All @@ -607,6 +620,7 @@ async def walk_handler_with_enricher(
index,
ir,
additional_metric_fields,
server_config,
one_time_flag=OnetimeFlag.is_a_walk(one_time_flag),
)

Expand Down
3 changes: 3 additions & 0 deletions splunk_connect_for_snmp_poller/manager/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async def get_snmp_data(
one_time_flag,
ir,
additional_metric_fields,
server_config,
):
if var_binds:
try:
Expand All @@ -85,6 +86,7 @@ async def get_snmp_data(
ir,
additional_metric_fields,
var_binds,
server_config,
)
except Exception as e:
logger.exception(f"Error occurred while calling {handler.__name__}(): {e}")
Expand Down Expand Up @@ -170,6 +172,7 @@ async def snmp_polling_async(
one_time_flag,
ir,
additional_metric_fields,
server_config,
]
get_bulk_specific_parameters = [mongo_connection, enricher_presence]
try:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_hec_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def test_build_error_data_for_metric(self, mocked_hec, mocked_ir):
test_index,
mocked_ir,
None,
None,
is_error=True,
)

Expand Down Expand Up @@ -297,6 +298,7 @@ def test_build_error_data_for_non_metric(self, mocked_hec, mocked_ir):
test_index,
mocked_ir,
None,
None,
is_error=True,
)

Expand Down

0 comments on commit e6a2f5b

Please sign in to comment.