Skip to content

Commit 8a8f1e5

Browse files
committed
examples: vucm: snmp-eaton-ups: pythonize script
1 parent 3aedc0b commit 8a8f1e5

1 file changed

Lines changed: 51 additions & 51 deletions

File tree

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,70 @@
11
import asyncio
2+
import functools
23
import os
34

4-
import enapter
5-
6-
SNMP_HOST = os.environ["ENAPTER_SNMP_HOST"]
7-
SNMP_PORT = os.environ["ENAPTER_SNMP_PORT"]
8-
SNMP_COMMUNITY = os.environ["ENAPTER_SNMP_COMMUNITY"]
9-
10-
11-
def snmpget(oid):
12-
from pysnmp.entity.rfc3413.oneliner import cmdgen
13-
14-
global SNMP_HOST
15-
global SNMP_PORT
16-
global SNMP_COMMUNITY
5+
from pysnmp.entity.rfc3413.oneliner import cmdgen
176

18-
cmdGen = cmdgen.CommandGenerator()
19-
20-
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
21-
cmdgen.CommunityData(SNMP_COMMUNITY),
22-
cmdgen.UdpTransportTarget((SNMP_HOST, SNMP_PORT)),
23-
oid,
24-
)
25-
26-
# Check for errors and print out results
27-
if errorIndication:
28-
print(errorIndication)
29-
else:
30-
if errorStatus:
31-
print(
32-
"%s at %s"
33-
% (
34-
errorStatus.prettyPrint(),
35-
errorIndex and varBinds[int(errorIndex) - 1] or "?",
36-
)
37-
)
38-
else:
39-
for name, val in varBinds:
40-
# print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
41-
return val
7+
import enapter
428

439

4410
async def main():
45-
await enapter.vucm.run(EatonUPS)
11+
device_factory = functools.partial(
12+
EatonUPS,
13+
snmp_community=os.environ["ENAPTER_SNMP_COMMUNITY"],
14+
snmp_host=os.environ["ENAPTER_SNMP_HOST"],
15+
snmp_port=os.environ["ENAPTER_SNMP_PORT"],
16+
)
17+
await enapter.vucm.run(device_factory)
4618

4719

4820
class EatonUPS(enapter.vucm.Device):
49-
def __init__(self, **kwargs):
21+
def __init__(self, snmp_community, snmp_host, snmp_port, **kwargs):
5022
super().__init__(**kwargs)
23+
5124
self.telemetry = {}
5225
self.properties = {}
5326

27+
self.cmd_gen = cmdgen.CommandGenerator()
28+
self.auth_data = cmdgen.CommunityData(snmp_community)
29+
self.transport_target = cmdgen.UdpTransportTarget((snmp_host, snmp_port))
30+
5431
async def task_get_telemetry_data(self):
5532
while True:
56-
if (value := snmpget("1.3.6.1.4.1.534.1.6.1.0")) is not None:
33+
if (value := self.snmp_get("1.3.6.1.4.1.534.1.6.1.0")) is not None:
5734
self.telemetry["temperature"] = int(value)
5835

59-
if (value := snmpget("1.3.6.1.4.1.534.1.2.4.0")) is not None:
36+
if (value := self.snmp_get("1.3.6.1.4.1.534.1.2.4.0")) is not None:
6037
self.telemetry["capacity"] = int(value)
6138

62-
if (value := snmpget("1.3.6.1.4.1.534.1.2.5.0")) is not None:
39+
if (value := self.snmp_get("1.3.6.1.4.1.534.1.2.5.0")) is not None:
6340
self.telemetry["status"] = str(value)
6441

65-
if (value := snmpget("1.3.6.1.2.1.33.1.3.3.1.2.1")) is not None:
42+
if (value := self.snmp_get("1.3.6.1.2.1.33.1.3.3.1.2.1")) is not None:
6643
self.telemetry["grid_freq"] = int(value) * 0.1
6744

68-
if (value := snmpget("1.3.6.1.4.1.534.1.4.2.0")) is not None:
45+
if (value := self.snmp_get("1.3.6.1.4.1.534.1.4.2.0")) is not None:
6946
self.telemetry["ups_freq"] = int(value) * 0.1
7047

71-
if (value := snmpget("1.3.6.1.4.1.534.1.3.4.1.2.1")) is not None:
48+
if (value := self.snmp_get("1.3.6.1.4.1.534.1.3.4.1.2.1")) is not None:
7249
self.telemetry["grid_v"] = int(value)
7350

74-
if (value := snmpget("1.3.6.1.4.1.534.1.4.1.0")) is not None:
51+
if (value := self.snmp_get("1.3.6.1.4.1.534.1.4.1.0")) is not None:
7552
self.telemetry["out_load"] = int(value)
7653

77-
if (value := snmpget("1.3.6.1.4.1.534.1.4.4.1.4.1")) is not None:
54+
if (value := self.snmp_get("1.3.6.1.4.1.534.1.4.4.1.4.1")) is not None:
7855
self.telemetry["ac_out_active_power"] = int(value)
7956

8057
await asyncio.sleep(10)
8158

8259
async def task_get_properties_data(self):
8360
while True:
84-
if (value := snmpget("1.3.6.1.2.1.33.1.1.2.0")) is not None:
61+
if (value := self.snmp_get("1.3.6.1.2.1.33.1.1.2.0")) is not None:
8562
self.properties["model"] = str(value)
86-
if (value := snmpget("1.3.6.1.2.1.33.1.1.1.0")) is not None:
63+
if (value := self.snmp_get("1.3.6.1.2.1.33.1.1.1.0")) is not None:
8764
self.properties["manufacturer"] = str(value)
88-
if (value := snmpget("1.3.6.1.2.1.33.1.1.3.0")) is not None:
65+
if (value := self.snmp_get("1.3.6.1.2.1.33.1.1.3.0")) is not None:
8966
self.properties["fw_ver"] = str(value)
90-
if (value := snmpget("1.3.6.1.2.1.33.1.1.4.0")) is not None:
67+
if (value := self.snmp_get("1.3.6.1.2.1.33.1.1.4.0")) is not None:
9168
self.properties["agent_ver"] = str(value)
9269
await asyncio.sleep(60)
9370

@@ -101,6 +78,29 @@ async def task_properties_publisher(self):
10178
await self.send_properties(self.properties)
10279
await asyncio.sleep(10)
10380

81+
def snmp_get(self, oid):
82+
result = self.cmd_gen.getCmd(self.auth_data, self.transport_target, oid)
83+
(error_indication, error_status, error_index, var_binds) = result
84+
85+
if error_indication:
86+
await self.log.error(f"error indication: {error_indication}")
87+
return None
88+
89+
if error_status:
90+
status = error_status.prettyPrint()
91+
index = error_index and var_binds[int(error_index) - 1] or "?"
92+
await self.log.error(f"error status: {status} at {index}")
93+
return None
94+
95+
first_val = None
96+
97+
for name, val in var_binds:
98+
await self.log.debug(f"{name.prettyPrint()} = {val.prettyPrint()}")
99+
if first_val is None:
100+
first_val = val
101+
102+
return first_val
103+
104104

105105
if __name__ == "__main__":
106106
asyncio.run(main())

0 commit comments

Comments
 (0)