def test_get_call_args(self): data = readbytes('dummy.hex') # any dump would do packet = Sequence( Integer(Version.V2C), OctetString('public'), BulkGetRequest(0, 1, 2, ObjectIdentifier(1, 2, 3), ObjectIdentifier(1, 2, 4))) with patch('puresnmp.send') as mck, \ patch('puresnmp.get_request_id') as mck2: mck2.return_value = 0 mck.return_value = data bulkget('::1', 'public', ['1.2.3'], ['1.2.4'], max_list_size=2) mck.assert_called_with('::1', 161, bytes(packet), timeout=2)
def test_bulkget(self): response = readbytes('apiv1/bulkget_response.hex') self.transport.send.return_value = response result = snmp.bulkget('192.168.1.1', 'private', scalar_oids=['1.3.6.1.2.1.1.1.0'], repeating_oids=[ '1.3.6.1.2.1.3.1.1', '1.3.6.1.2.1.4', ], max_list_size=10, port=161, timeout=1) expected = BulkResult( {'1.3.6.1.2.1.1.2.0': '1.3.6.1.4.1.8072.3.2.10'}, OrderedDict([ ('1.3.6.1.2.1.3.1.1.1.12.1.172.17.0.1', 12), ('1.3.6.1.2.1.4.1.0', 1), ('1.3.6.1.2.1.3.1.1.2.12.1.172.17.0.1', b'\x02B@j\xbf\xcd'), ('1.3.6.1.2.1.4.2.0', 64), ('1.3.6.1.2.1.3.1.1.3.12.1.172.17.0.1', b'\xac\x11\x00\x01'), ('1.3.6.1.2.1.4.3.0', 589), ('1.3.6.1.2.1.4.4.0', 0), ('1.3.6.1.2.1.4.5.0', 0), ('1.3.6.1.2.1.4.6.0', 0), ('1.3.6.1.2.1.4.7.0', 0), ('1.3.6.1.2.1.4.8.0', 0), ('1.3.6.1.2.1.4.9.0', 410), ('1.3.6.1.2.1.4.10.0', 409) ]) ) self.assertEqual(result, expected) self.assertTrue(self.transport.send.called, 'method was not called') key_types = {type(k) for k in result.listing.keys()} if six.PY3: self.assertIsInstance(result.scalars['1.3.6.1.2.1.1.2.0'], str) self.assertEqual(key_types, {str}) else: self.assertIsInstance(result.scalars['1.3.6.1.2.1.1.2.0'], unicode) self.assertEqual(key_types, {unicode}) expected_types = [ int, int, bytes, int, bytes, int, int, int, int, int, int, int, int ] for value, type_ in zip(result.listing.values(), expected_types): self.assertIsInstance(value, type_)
def _bulkget(self, scalar_oids=None, repeating_oids=None, max_list_size=1000, timeout=None, keys=None, ret_raw=False): ''' scalar_oids, repeating_oids 必须是 dict :param scalar_oids: {'1.3.6.1.2.1.1.1': 'name1', '1.3.6.1.2.1.2.1': 'name2'} -> {'name1': value1, 'name2': value2} :param repeating_oids: 会返回OrderedDict, 有max_list_size限制, 建议用bulkwalk :param max_list_size: :param timeout: :param keys: 替换snmp oid中的item为字符串,优先使用oids dict中的value {'1': 'xxx', '2': 'yyy'} :param rat_raw: 返回原始的数据 :return: ''' raw = bulkget(scalar_oids=scalar_oids.keys() if isinstance(scalar_oids, dict) else scalar_oids, repeating_oids=repeating_oids.keys() if isinstance(repeating_oids, dict) else repeating_oids, max_list_size=max_list_size, timeout=timeout or self.timeoout, **self.snmp_auth) d = raw._asdict() for k, v in d.items(): for k1, v1 in v.items(): v[k1] = bytes_decode(v1) if ret_raw: return d d1 = defaultdict(OrderedDict) if not keys: keys = {} for k, v in d.items(): if k == 'scalars' and scalar_oids: for k1, v1 in v.items(): name = scalar_oids.get(k1) or scalar_oids.get(k1[:-2]) or k1 name = keys.get(name, name) d1[k][name] = v1 elif repeating_oids: # 比较乱,待整理,用bulkwalk先 # oid中的.在replace函数中容易被误匹配, 需要escape pats = [(x + '.', '') for x in repeating_oids] for k1, v1 in v.items(): index, oid = replace(k1, pats=pats, default='', ret_with_pat=True, _any=True, escape=True) name = repeating_oids.get(oid[:-1]) or oid[:-1] key = keys.get(name, name) if index not in d1[k]: d1[k][index] = {} d1[k][index].update({key: v1}) return d1
def test_bulkget(self): data = readbytes('bulk_get_response.hex') expected = BulkResult( { '1.3.6.1.2.1.1.1.0': b'Linux 7e68e60fe303 4.4.0-28-generic ' b'#47-Ubuntu SMP Fri Jun 24 10:09:13 UTC 2016 x86_64' }, { '1.3.6.1.2.1.3.1.1.1.10.1.172.17.0.1': 10, '1.3.6.1.2.1.3.1.1.2.10.1.172.17.0.1': b'\x02B\xe2\xc5\x8d\t', '1.3.6.1.2.1.3.1.1.3.10.1.172.17.0.1': b'\xac\x11\x00\x01', '1.3.6.1.2.1.4.1.0': 1, '1.3.6.1.2.1.4.3.0': 57 }) with patch('puresnmp.send') as mck: mck.return_value = data result = bulkget('::1', 'public', ['1.3.6.1.2.1.1.1'], ['1.3.6.1.2.1.3.1'], max_list_size=5) self.assertEqual(result, expected)
def __process_methods(method, common_parameters, datatype_config): response = None if method == "get": oid = datatype_config["oid"] response = puresnmp.get(**common_parameters, oid=oid) elif method == "multiget": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) response = puresnmp.multiget(**common_parameters, oids=oids) elif method == "getnext": oid = datatype_config["oid"] master_response = puresnmp.getnext(**common_parameters, oid=oid) response = {master_response.oid: master_response.value} elif method == "multigetnext": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) master_response = puresnmp.multigetnext(**common_parameters, oids=oids) response = { binded_var.oid: binded_var.value for binded_var in master_response } elif method == "walk": oid = datatype_config["oid"] response = { binded_var.oid: binded_var.value for binded_var in list( puresnmp.walk(**common_parameters, oid=oid)) } elif method == "multiwalk": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) response = { binded_var.oid: binded_var.value for binded_var in list( puresnmp.multiwalk(**common_parameters, oids=oids)) } elif method == "set": oid = datatype_config["oid"] value = datatype_config["value"] response = puresnmp.set(**common_parameters, oid=oid, value=value) elif method == "multiset": mappings = datatype_config["mappings"] response = puresnmp.multiset(**common_parameters, mappings=mappings) elif method == "bulkget": scalar_oids = datatype_config.get("scalarOid", []) scalar_oids = scalar_oids if isinstance( scalar_oids, list) else list(scalar_oids) repeating_oids = datatype_config.get("repeatingOid", []) repeating_oids = repeating_oids if isinstance( repeating_oids, list) else list(repeating_oids) max_list_size = datatype_config.get("maxListSize", 1) response = puresnmp.bulkget(**common_parameters, scalar_oids=scalar_oids, repeating_oids=repeating_oids, max_list_size=max_list_size)._asdict() elif method == "bulkwalk": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) bulk_size = datatype_config.get("bulkSize", 10) response = { binded_var.oid: binded_var.value for binded_var in list( puresnmp.bulkwalk( **common_parameters, bulk_size=bulk_size, oids=oids)) } elif method == "table": oid = datatype_config["oid"] del common_parameters["timeout"] num_base_nodes = datatype_config.get("numBaseNodes", 0) response = puresnmp.table(**common_parameters, oid=oid, num_base_nodes=num_base_nodes) elif method == "bulktable": oid = datatype_config["oid"] num_base_nodes = datatype_config.get("numBaseNodes", 0) bulk_size = datatype_config.get("bulkSize", 10) response = puresnmp.bulktable(**common_parameters, oid=oid, num_base_nodes=num_base_nodes, bulk_size=bulk_size) else: log.error("Method \"%s\" - Not found", str(method)) return response