|
7 | 7 | import os
|
8 | 8 | import logging
|
9 | 9 | import json
|
| 10 | +from pysnmp.hlapi import * |
10 | 11 |
|
11 | 12 | logger = logging.getLogger(__name__)
|
12 | 13 |
|
13 | 14 |
|
| 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 | + |
14 | 50 | class TranslatorTest(TestCase):
|
15 | 51 | @classmethod
|
16 | 52 | def setUpClass(cls):
|
@@ -83,7 +119,7 @@ def test_format_trap_non_existing_oid(self):
|
83 | 119 | value_type = input_var_binds_list[i]["val_type"]
|
84 | 120 | oid = input_var_binds_list[i]["oid"]
|
85 | 121 | 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}"' |
87 | 123 | # these two additional spaces are not an error
|
88 | 124 | untranslated += f"{current} "
|
89 | 125 | if i < len(input_var_binds_list) - 1:
|
@@ -161,26 +197,69 @@ def test_format_non_existing_metric(self):
|
161 | 197 | assert translated_dict["metric_name"] == f"sc4snmp.{untranslated_oid}"
|
162 | 198 |
|
163 | 199 | @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", |
178 | 224 | )
|
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", |
181 | 249 | )
|
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) |
186 | 254 | )
|
| 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