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

Commit 4dfa7fd

Browse files
weliaszomrozowicz-splunkgithub-actions[bot]
authored
feat: additional data extraction (#177)
* feat: additional data extraction * fix: build fix * Update tests/test_additional_data_extraction.py Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: omrozowicz-splunk <86965961+omrozowicz-splunk@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 42cb2c3 commit 4dfa7fd

File tree

2 files changed

+118
-7
lines changed

2 files changed

+118
-7
lines changed

splunk_connect_for_snmp_poller/manager/hec_sender.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515
# ########################################################################
1616
import json
17+
import re
1718
import time
1819

1920
import requests
@@ -27,6 +28,7 @@
2728
from splunk_connect_for_snmp_poller.manager.data.inventory_record import InventoryRecord
2829
from splunk_connect_for_snmp_poller.manager.static.mib_enricher import MibEnricher
2930
from splunk_connect_for_snmp_poller.manager.variables import (
31+
enricher_additional_varbinds,
3032
enricher_name,
3133
enricher_oid_family,
3234
)
@@ -189,7 +191,6 @@ def build_metric_data(
189191
fields = {
190192
"metric_name:" + metric_name: metric_value,
191193
EventField.FREQUENCY.value: ir.frequency_str,
192-
EventField.TIME.value: time.time(),
193194
}
194195
if mib_enricher:
195196
_enrich_metric_data(mib_enricher, json_val, fields)
@@ -200,20 +201,38 @@ def build_metric_data(
200201
builder = init_builder_with_common_data(time.time(), host, index)
201202
builder.add(EventField.EVENT, EventType.METRIC.value)
202203

203-
strip_trailing_index_number(fields, metric_name, metric_value, server_config)
204+
extract_additional_properties(fields, metric_name, metric_value, server_config)
204205

205206
builder.add_fields(fields)
206207
return builder.build()
207208

208209

209-
def strip_trailing_index_number(fields, metric_name, metric_value, server_config):
210+
def extract_additional_properties(fields, metric_name, metric_value, server_config):
210211
result = multi_key_lookup(server_config, (enricher_name, enricher_oid_family))
211212
oid_families = result if result else []
212213

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
214+
for family in oid_families.keys():
215+
if metric_name.startswith("sc4snmp." + family):
216+
stripped = metric_name[: metric_name.index("_")]
217+
218+
input_text = metric_name[metric_name.index("_") + 1 :] # noqa: E203
219+
220+
entries = oid_families[family][enricher_additional_varbinds]
221+
for entry in entries:
222+
if "regex" in entry and "names" in entry:
223+
regex = entry["regex"]
224+
names = entry["names"]
225+
names_list = names.split("/")
226+
227+
result = re.match(regex, input_text)
228+
if result:
229+
for index, item in enumerate(names_list):
230+
fields[item] = result.group(index + 1)
231+
del fields["metric_name:" + metric_name]
232+
fields["metric_name:" + stripped] = metric_value
233+
# TODO delete blow debug statement
234+
fields["old_metric_name:" + metric_name] = metric_value
235+
continue
217236

218237

219238
def build_error_data(
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright 2021 Splunk Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
from unittest import TestCase
17+
18+
from splunk_connect_for_snmp_poller.manager.hec_sender import (
19+
extract_additional_properties,
20+
)
21+
22+
23+
class TestAdditionalDataExtraction(TestCase):
24+
def test_data_extraction(self):
25+
server_config = {
26+
"enricher": {
27+
"oidFamily": {
28+
"TCP-MIB": {
29+
"additionalVarBinds": [
30+
{
31+
"regex": "([0-9]+_[0-9]+_[0-9]+_[0-9]+)_([0-9]+)_([0-9]+_[0-9]+_[0-9]+_[0-9]+)_([0-9]+)", # noqa: E501
32+
"names": "IP_one/port/IP_two/index_number",
33+
}
34+
]
35+
},
36+
"IF-MIB": {
37+
"existingVarBinds": [
38+
{"ifDescr": "interface_desc"},
39+
{"ifPhysAddress": "MAC_address"},
40+
],
41+
"additionalVarBinds": [{"indexNum": "index_number"}],
42+
},
43+
"UDP-MIB": {
44+
"additionalVarBinds": [
45+
{
46+
"regex": '(ipv4)_"([0-9]+_[0-9]+_[0-9]+_[0-9]+)"_([0-9]+)_(ipv4)_"([0-9]+_[0-9]+_[0-9]+_[0-9]+)"_([0-9]+)_([0-9]+)', # noqa: E501
47+
"names": "protocol_version_one/IP_one/port_one/protocol_version_two/IP_two/index_number/port_two", # noqa: E501
48+
}
49+
]
50+
},
51+
}
52+
}
53+
}
54+
55+
fields = {
56+
"metric_name:sc4snmp.TCP-MIB.tcpConnLocalPort_192_168_0_1_161_127_0_0_1_5": "1111"
57+
}
58+
fields2 = {"metric_name:sc4snmp.IF-MIB.ifInErrors_2": "173127"}
59+
fields3 = {
60+
'metric_name:sc4snmp.UDP-MIB.udpEndpointProcess_ipv4_"0_0_0_0"_111_ipv4_"0_0_0_0"_0_13348': "123"
61+
}
62+
63+
extract_additional_properties(
64+
fields,
65+
"sc4snmp.TCP-MIB.tcpConnLocalPort_192_168_0_1_161_127_0_0_1_5",
66+
"1111",
67+
server_config,
68+
)
69+
70+
extract_additional_properties(
71+
fields2, "sc4snmp.IF-MIB.ifInErrors_2", "173127", server_config
72+
)
73+
74+
extract_additional_properties(
75+
fields3,
76+
'sc4snmp.UDP-MIB.udpEndpointProcess_ipv4_"0_0_0_0"_111_ipv4_"0_0_0_0"_0_13348',
77+
"123",
78+
server_config,
79+
)
80+
81+
self.assertEqual(fields["IP_one"], "192_168_0_1")
82+
self.assertEqual(fields["port"], "161")
83+
self.assertEqual(fields["IP_two"], "127_0_0_1")
84+
self.assertEqual(fields["index_number"], "5")
85+
86+
self.assertEqual(fields3["protocol_version_one"], "ipv4")
87+
self.assertEqual(fields3["IP_one"], "0_0_0_0")
88+
self.assertEqual(fields3["port_one"], "111")
89+
self.assertEqual(fields3["protocol_version_two"], "ipv4")
90+
self.assertEqual(fields3["IP_two"], "0_0_0_0")
91+
self.assertEqual(fields3["index_number"], "0")
92+
self.assertEqual(fields3["port_two"], "13348")

0 commit comments

Comments
 (0)