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

Commit 7b9f093

Browse files
committed
bug(translation): added full test from simulator
1 parent cf15434 commit 7b9f093

File tree

2 files changed

+101
-35
lines changed

2 files changed

+101
-35
lines changed

splunk_connect_for_snmp_mib_server/translator.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,26 +185,13 @@ def check_mongo(self, oid):
185185

186186
return no_mapping_mib
187187

188-
def fix_hex_string_for_walk(self, value, value_type):
189-
# We noticed this weird behavior: when running a full walk for a sub-tree, Hex-STRING values
190-
# are not an OctetString. Only for Hex-STRING types we perform a manual conversion for this
191-
# edge case:
192-
#
193-
# lstoppa@C02DL3AAMD6R ~ % snmpget -v 1 -c public localhost:1611 1.3.6.1.2.1.4.22.1.2.2.195.218.254.97
194-
# IP-MIB::ipNetToMediaPhysAddress.2.195.218.254.97 = STRING: 0:e:84:9f:9c:19
195-
# lstoppa@C02DL3AAMD6R ~ % snmpwalk -v 1 -c public localhost:1611 1.3.6.1.2.1.4.22.1.2.2.195.218.254.97
196-
# IP-MIB::ipNetToMediaPhysAddress.2.195.218.254.97 = STRING: 0:e:84:9f:9c:19
197-
# lstoppa@C02DL3AAMD6R ~ % snmpwalk -v 1 -c public localhost:1611 1.3.6.1.2.1 | grep Hex
198-
# SNMPv2-SMI::mib-2.3.1.1.2.2.1.195.218.254.97 = Hex-STRING: 00 0E 84 9F 9C 19
199-
return value.replace(" ", ":") if value_type == "Hex-STRING" else value
200188

201189
# Translate SNMP PDU varBinds into MIB objects using MIB
202190
def mib_translator(self, var_bind):
203-
204191
# Run var-binds through MIB resolver
205192
try:
206193
name = var_bind["oid"]
207-
val = self.fix_hex_string_for_walk(var_bind["val"], var_bind["val_type"])
194+
val = var_bind["val"]
208195
translated_var_bind = rfc1902.ObjectType(
209196
rfc1902.ObjectIdentity(name), val
210197
).resolveWithMib(self._mib_view_controller)

tests/test_translator.py

Lines changed: 100 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,46 @@
77
import os
88
import logging
99
import json
10+
from pysnmp.hlapi import *
1011

1112
logger = logging.getLogger(__name__)
1213

1314

15+
# This sample function was used to generate test data.
16+
# I took the content of https://raw.githubusercontent.com/etingof/snmpsim/master/data/recorded/linux-full-walk.snmprec
17+
# and sorted by data type. This should give us some real data we can use to test our MIB-server translation
18+
# process. This was the input data we use in our test "test_translate_all_snmp_simulator_data_types"
19+
def prepare_test_data():
20+
for oid in (
21+
"1.3.6.1.2.1.2.2.1.4.1",
22+
"1.3.6.1.2.1.1.6.0",
23+
"1.3.6.1.2.1.2.2.1.6.2",
24+
"1.3.6.1.2.1.1.9.1.2.7",
25+
"1.3.6.1.2.1.6.13.1.4.195.218.254.105.51684.194.67.10.226.22",
26+
"1.3.6.1.2.1.25.3.2.1.6.1025",
27+
"1.3.6.1.2.1.31.1.1.1.15.2",
28+
"1.3.6.1.2.1.1.3.0",
29+
"1.3.6.1.4.1.2021.10.1.6.1",
30+
"1.3.6.1.2.1.31.1.1.1.10.1",
31+
):
32+
g = getCmd(
33+
SnmpEngine(),
34+
CommunityData("public"),
35+
UdpTransportTarget(("localhost", 1611)),
36+
ContextData(),
37+
ObjectType(ObjectIdentity(oid)),
38+
)
39+
error_indication, error_status, error_index, var_binds = next(g)
40+
for name, value in var_binds:
41+
class_name = value.__class__.__name__
42+
# normal_value = value
43+
str_value = str(value).replace("\n", "")
44+
pretty_value = value.prettyPrint()
45+
print(
46+
f"{str(name)}|{class_name}|{str_value}|{pretty_value}|{pretty_value == str_value}"
47+
)
48+
49+
1450
class TranslatorTest(TestCase):
1551
@classmethod
1652
def setUpClass(cls):
@@ -83,7 +119,7 @@ def test_format_trap_non_existing_oid(self):
83119
value_type = input_var_binds_list[i]["val_type"]
84120
oid = input_var_binds_list[i]["oid"]
85121
value = input_var_binds_list[i]["val"]
86-
current = f'oid-type{i+1}="{oid_type}" value{i+1}-type="{value_type}" {oid}="{value}" value{i+1}="{value}"'
122+
current = f'oid-type{i + 1}="{oid_type}" value{i + 1}-type="{value_type}" {oid}="{value}" value{i + 1}="{value}"'
87123
# these two additional spaces are not an error
88124
untranslated += f"{current} "
89125
if i < len(input_var_binds_list) - 1:
@@ -161,26 +197,69 @@ def test_format_non_existing_metric(self):
161197
assert translated_dict["metric_name"] == f"sc4snmp.{untranslated_oid}"
162198

163199
@mongomock.patch()
164-
def test_format_hex_string_from_walk_for_root_tree(self):
165-
input_var_binds_colons = {
166-
"oid": "1.3.6.1.2.1.4.22.1.2.2.195.218.254.97",
167-
"val": "00:0E:84:9F:9C:19",
168-
"val_type": "Hex-STRING",
169-
}
170-
input_var_binds_spaces = {
171-
"oid": "1.3.6.1.2.1.4.22.1.2.2.195.218.254.97",
172-
"val": "00 0E 84 9F 9C 19",
173-
"val_type": "Hex-STRING",
174-
}
175-
176-
translated_metrics_colons = self.my_translator.format_metric_data(
177-
input_var_binds_colons
200+
def test_translate_all_snmp_simulator_data_types(self):
201+
oids = (
202+
"1.3.6.1.2.1.2.2.1.4.1",
203+
"1.3.6.1.2.1.1.6.0",
204+
"1.3.6.1.2.1.2.2.1.6.2",
205+
"1.3.6.1.2.1.1.9.1.2.7",
206+
"1.3.6.1.2.1.6.13.1.4.195.218.254.105.51684.194.67.10.226.22",
207+
"1.3.6.1.2.1.25.3.2.1.6.1025",
208+
"1.3.6.1.2.1.31.1.1.1.15.2",
209+
"1.3.6.1.2.1.1.3.0",
210+
"1.3.6.1.4.1.2021.10.1.6.1",
211+
"1.3.6.1.2.1.31.1.1.1.10.1",
212+
)
213+
str_values = (
214+
"16436",
215+
"San Francisco, California, United States",
216+
"0x00127962f940",
217+
"1.3.6.1.2.1.50",
218+
"194.67.10.226",
219+
"0",
220+
"100",
221+
"147870473",
222+
"0x9f78043eeb851f",
223+
"381059068",
178224
)
179-
translated_metrics_spaces = self.my_translator.format_metric_data(
180-
input_var_binds_spaces
225+
value_types = (
226+
"Integer",
227+
"DisplayString",
228+
"OctetString",
229+
"ObjectIdentity",
230+
"IpAddress",
231+
"Counter32",
232+
"Gauge32",
233+
"TimeTicks",
234+
"Opaque",
235+
"Counter64",
236+
)
237+
238+
expected_translations = (
239+
"sc4snmp.IF-MIB.ifMtu_1",
240+
"sc4snmp.SNMPv2-MIB.sysLocation_0",
241+
"sc4snmp.IF-MIB.ifPhysAddress_2",
242+
"sc4snmp.SNMPv2-MIB.sysORID_7",
243+
"sc4snmp.TCP-MIB.tcpConnRemAddress_195_218_254_105_51684_194_67_10_226_22",
244+
"sc4snmp.1_3_6_1_2_1_25_3_2_1_6_1025",
245+
"sc4snmp.IF-MIB.ifHighSpeed_2",
246+
"sc4snmp.SNMPv2-MIB.sysUpTime_0",
247+
"sc4snmp.1_3_6_1_4_1_2021_10_1_6_1",
248+
"sc4snmp.IF-MIB.ifHCOutOctets_1",
181249
)
182-
assert translated_metrics_colons == translated_metrics_spaces
183-
translated_dict = json.loads(translated_metrics_colons)
184-
assert translated_dict["metric_name"].startswith(
185-
"sc4snmp.IP-MIB.ipNetToMediaPhysAddress"
250+
assert (
251+
len(oids) == len(str_values)
252+
and len(str_values) == len(value_types)
253+
and len(value_types) == len(expected_translations)
186254
)
255+
for index in range(0, len(oids)):
256+
input_var_binds_colons = {
257+
"oid": oids[index],
258+
"val": str_values[index],
259+
"val_type": value_types[index],
260+
}
261+
translated_metrics = self.my_translator.format_metric_data(
262+
input_var_binds_colons
263+
)
264+
json_metrics = json.loads(translated_metrics)
265+
assert json_metrics["metric_name"] == expected_translations[index]

0 commit comments

Comments
 (0)