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

Commit

Permalink
[ADDON-43362] special builder for error message
Browse files Browse the repository at this point in the history
  • Loading branch information
okuzhel committed Oct 14, 2021
1 parent 3c690a5 commit 861a40e
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def is_one_time_walk(self, one_time_flag: bool) -> None:
self.data[EventField.SOURCETYPE.value] = EventType.WALK.value

def build(self) -> dict:
logger.debug("--------data------\n%s", self.data)
logger.debug("data builder result - %s", self.data)
return self.data


Expand Down
21 changes: 18 additions & 3 deletions splunk_connect_for_snmp_poller/manager/hec_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ def post_data_to_splunk_hec(
additional_metric_fields,
one_time_flag=False,
mib_enricher=None,
is_error=False,
):
if is_metric:
logger.debug(f"metric index: {index['metric_index']}")
if is_error:
logger.debug("sending error to index - %s", index["event_index"])
data = build_error_data(host, variables_binds, index["event_index"])
elif is_metric:
logger.debug("metric index: %s", index["metric_index"])
data = build_metric_data(
host,
variables_binds,
Expand All @@ -84,7 +88,7 @@ def post_data_to_splunk_hec(
mib_enricher,
)
else:
logger.debug(f"event index: {index['event_index']}")
logger.debug("event index - %s", index["event_index"])
data = build_event_data(
host,
variables_binds,
Expand Down Expand Up @@ -190,6 +194,17 @@ def build_metric_data(
return builder.build()


def build_error_data(
host,
variables_binds,
index,
):
builder = init_builder_with_common_data(time.time(), host, index)
builder.add(EventField.EVENT, str(variables_binds))
builder.add(EventField.SOURCETYPE, EventType.ERROR.value)
return builder.build()


def _enrich_metric_data(
mib_enricher: MibEnricher, variables_binds: dict, fields: dict
) -> None:
Expand Down
3 changes: 2 additions & 1 deletion splunk_connect_for_snmp_poller/manager/profile_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def assign_profiles_to_device(profiles, device_desc):


def get_profiles(server_config):
mib_profiles = yaml.safe_load(get_mib_profiles())
profiles = get_mib_profiles()
mib_profiles = profiles if not profiles else yaml.safe_load(profiles)

result = {}
merged_profiles = {}
Expand Down
6 changes: 3 additions & 3 deletions splunk_connect_for_snmp_poller/manager/task_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from typing import Tuple

from celery.utils.log import get_task_logger
from pysmi import debug as pysmi_debug
from pysnmp.hlapi import (
CommunityData,
ContextData,
Expand All @@ -47,7 +46,6 @@
)
from splunk_connect_for_snmp_poller.manager.static.mib_enricher import MibEnricher

pysmi_debug.setLogger(pysmi_debug.Debug("compiler"))
logger = get_task_logger(__name__)


Expand All @@ -56,7 +54,6 @@ def __add__(self, other):
return VarbindCollection(bulk=self.bulk + other.bulk, get=self.get + other.get)


# TODO remove the debugging statement later
# TODO analyze the code here:
# https://github.com/etingof/pysnmp/blob/becd15c79c9a6b5696928ecd50bf5cca8b1770a1/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py#L57
# to compare the performance between the runDispatcher() and the current getCmd()/nextCmd() .
Expand Down Expand Up @@ -257,6 +254,7 @@ async def snmp_get_handler(
ir,
additional_metric_fields,
one_time_flag,
is_error=is_error,
)


Expand Down Expand Up @@ -325,6 +323,7 @@ def _any_walk_failure_happened(
ir,
additional_metric_fields,
one_time_flag,
is_error=is_error,
)

return is_error
Expand Down Expand Up @@ -415,6 +414,7 @@ async def snmp_bulk_handler(
ir,
additional_metric_fields,
one_time_flag,
is_error=is_error,
)
break

Expand Down
4 changes: 3 additions & 1 deletion splunk_connect_for_snmp_poller/snmp_poller_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
def main():
logger.info("Startup Config")
args = parse_command_line_arguments()
logging.getLogger().setLevel(args.loglevel.upper())
log_level = args.loglevel.upper()
logger.info("Passed log Level is %s", log_level)
logging.getLogger().setLevel(log_level)

poller_server = Poller(args, parse_config_file(args.config))
poller_server.run()
Expand Down
76 changes: 76 additions & 0 deletions tests/test_hec_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#
import json
from unittest import TestCase
from unittest.mock import patch

import pytest as pytest
import responses
Expand All @@ -24,6 +25,7 @@
HecSender,
_enrich_event_data,
_enrich_metric_data,
post_data_to_splunk_hec,
)
from splunk_connect_for_snmp_poller.manager.static.mib_enricher import MibEnricher

Expand Down Expand Up @@ -233,3 +235,77 @@ def test_send_event_request_with_error(self):
"Connection error when sending data to HEC index - test_index",
log.output[0],
)

@patch("splunk_connect_for_snmp_poller.manager.hec_sender.HecSender")
@patch(
"splunk_connect_for_snmp_poller.manager.data.inventory_record.InventoryRecord"
)
def test_build_error_data_for_metric(self, mocked_hec, mocked_ir):
# given
test_index = {"event_index": "test_index"}
expected_data = {
"time": 1634124054.27087,
"host": "test_host",
"index": "test_index",
"event": "error message",
"sourcetype": "sc4snmp:error",
}

# when
post_data_to_splunk_hec(
mocked_hec,
"test_host",
"error message",
True,
test_index,
mocked_ir,
None,
is_error=True,
)

# then
spl = mocked_hec.send_hec_request.call_args.args
is_metric = spl[0]
data = spl[1]
self.assertTrue(is_metric)
self.assertEqual(data["host"], expected_data["host"])
self.assertEqual(data["index"], expected_data["index"])
self.assertEqual(data["event"], expected_data["event"])
self.assertEqual(data["sourcetype"], expected_data["sourcetype"])

@patch("splunk_connect_for_snmp_poller.manager.hec_sender.HecSender")
@patch(
"splunk_connect_for_snmp_poller.manager.data.inventory_record.InventoryRecord"
)
def test_build_error_data_for_non_metric(self, mocked_hec, mocked_ir):
# given
test_index = {"event_index": "test_index"}
expected_data = {
"time": 1634124054.27087,
"host": "test_host",
"index": "test_index",
"event": "error message",
"sourcetype": "sc4snmp:error",
}

# when
post_data_to_splunk_hec(
mocked_hec,
"test_host",
"error message",
False,
test_index,
mocked_ir,
None,
is_error=True,
)

# then
spl = mocked_hec.send_hec_request.call_args.args
is_metric = spl[0]
data = spl[1]
self.assertFalse(is_metric)
self.assertEqual(data["host"], expected_data["host"])
self.assertEqual(data["index"], expected_data["index"])
self.assertEqual(data["event"], expected_data["event"])
self.assertEqual(data["sourcetype"], expected_data["sourcetype"])

0 comments on commit 861a40e

Please sign in to comment.