forked from aquaticus/esphome_snmp_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
82 lines (66 loc) · 2.16 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from pysnmp.hlapi import *
CUSTOM_OID = ".1.3.9999."
supported_oids = [
"1.3.6.1.2.1.1.1.0",
"1.3.6.1.2.1.1.2.0",
"1.3.6.1.2.1.1.3.0",
"1.3.6.1.2.1.1.4.0",
"1.3.6.1.2.1.1.5.0",
"1.3.6.1.2.1.1.6.0",
"1.3.6.1.2.1.1.7.0",
"1.3.6.1.2.1.25.1.1.0",
"1.3.6.1.2.1.25.2.2",
"1.3.6.1.2.1.25.2.3.1.1.1",
"1.3.6.1.2.1.25.2.3.1.1.2",
"1.3.6.1.2.1.25.2.3.1.3.1",
"1.3.6.1.2.1.25.2.3.1.3.2",
"1.3.6.1.2.1.25.2.3.1.4.1",
"1.3.6.1.2.1.25.2.3.1.4.2",
"1.3.6.1.2.1.25.2.3.1.5.1",
"1.3.6.1.2.1.25.2.3.1.5.2",
"1.3.6.1.2.1.25.2.3.1.6.1",
"1.3.6.1.2.1.25.2.3.1.6.2",
"1.3.9999.2.1.0",
"1.3.9999.2.2.0",
"1.3.9999.2.3.0",
"1.3.9999.2.4.0",
"1.3.9999.2.5.0",
"1.3.9999.4.1.0",
"1.3.9999.4.2.0",
"1.3.9999.4.3.0",
"1.3.9999.4.4.0",
]
esp32_oids = ["1.3.9999.32.1.0", "1.3.9999.32.2.0", "1.3.9999.32.3.0", "1.3.9999.32.4.0", ]
esp8266_oids = ["1.3.9999.8266.1.0", "1.3.9999.8266.2.0", "1.3.9999.8266.3.0", ]
def get_supported_oids(chip):
if chip == 'esp32':
all_oids = supported_oids + esp32_oids
else:
all_oids = supported_oids + esp8266_oids
return all_oids
def fetch(handler, count):
result = []
for i in range(count):
try:
error_indication, error_status, error_index, var_binds = next(handler)
if not error_indication and not error_status:
items = {}
for var_bind in var_binds:
items[str(var_bind[0])] = str(var_bind[1])
result.append(items)
else:
raise RuntimeError('SNMP error: {0}'.format(error_indication))
except StopIteration:
break
return result
def snmp_get(oid, host):
iterator = getCmd(SnmpEngine(), CommunityData('public'), UdpTransportTarget((host, 161)), ContextData(),
ObjectType(ObjectIdentity(oid)))
return fetch(iterator, 1)[0]
def construct_value_pairs(list_of_pairs):
pairs = []
for key, value in list_of_pairs.items():
pairs.append(ObjectType(ObjectIdentity(key), value))
return pairs
def get_value(oid, host):
return list(snmp_get(oid, host).values())[0]