This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
feed.home.apcups.py
executable file
·138 lines (117 loc) · 5.22 KB
/
feed.home.apcups.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/local/bin/python3 -u
"""
Author: Oliver Ratzesberger <https://github.com/fxstein>
Copyright: Copyright (C) 2016 Oliver Ratzesberger
License: Apache License, Version 2.0
"""
# Make sure we have access to SentientHome commons
import os
import sys
try:
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
except:
exit(1)
# Sentient Home Application
from common.shapp import shApp
from common.sheventhandler import shEventHandler
from common.shutil import extract_tags, numerify, CtoF
from pysnmp.entity.rfc3413.oneliner import cmdgen
# Default settings
from cement.utils.misc import init_defaults
defaults = init_defaults('apcups', 'apcups')
defaults['apcups']['poll_interval'] = 10.0
# Feed APC UPS data
# Map intersting OIDs to names and descriptions for human readability
# Dont want to carry a multi megabyte MIB file for just a few values
# desc currently only used to make the code better documented
oids = {
'1.3.6.1.4.1.318.1.1.1.2.1.1.0': {'name': 'upsstatus',
'desc': 'UPS Status'},
'1.3.6.1.4.1.318.1.1.1.2.2.1.0': {'name': 'batcap',
'desc': 'Battery Capacity [%]'},
'1.3.6.1.4.1.318.1.1.1.2.2.2.0': {'name': 'battemp',
'desc': 'Battery Temp [C]'},
'1.3.6.1.4.1.318.1.1.1.2.2.3.0': {'name': 'runtime',
'desc': 'Runtime [ms]'},
'1.3.6.1.4.1.318.1.1.1.3.2.1.0': {'name': 'linevolt',
'desc': 'Line Voltage [V]'},
'1.3.6.1.4.1.318.1.1.1.3.2.2.0': {'name': 'linemaxvolt',
'desc': 'Line Max Voltage [V]'},
'1.3.6.1.4.1.318.1.1.1.3.2.3.0': {'name': 'lineminvolt',
'desc': 'Line Min Voltage [V]'},
'1.3.6.1.4.1.318.1.1.1.3.2.4.0': {'name': 'infreq',
'desc': 'Input Freq [Hz]'},
'1.3.6.1.4.1.318.1.1.1.4.2.1.0': {'name': 'outvolt',
'desc': 'Output Voltage [V]'},
'1.3.6.1.4.1.318.1.1.1.4.2.2.0': {'name': 'outfreq',
'desc': 'Output Freq [Hz]'},
'1.3.6.1.4.1.318.1.1.1.4.2.3.0': {'name': 'outload',
'desc': 'Output Load [%]'},
'1.3.6.1.4.1.318.1.1.1.4.2.4.0': {'name': 'outcurrent',
'desc': 'Output Current [A]'},
'1.3.6.1.4.1.318.1.1.1.7.2.3.0': {'name': 'lasttestres',
'desc': 'Last Test Result'},
'1.3.6.1.4.1.318.1.1.1.7.2.4.0': {'name': 'lasttestdate',
'desc': 'Last Test Date'},
}
with shApp('apcups', config_defaults=defaults) as app:
app.run()
handler = shEventHandler(app)
try:
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
cmdgen.CommunityData('public'),
cmdgen.UdpTransportTarget((
app.config.get('apcups', 'apcups_addr'), 161)),
cmdgen.MibVariable('SNMPv2-MIB', 'sysDescr', 0),
lookupNames=True, lookupValues=True
)
except Exception as e:
app.log.fatal('Unhandled exception: %s' % e)
app.close(1)
# Check for errors and print out results
if errorIndication:
app.log.error(errorIndication)
elif errorStatus:
app.log.error(errorStatus)
else:
for name, val in varBinds:
app.log.info('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
data = dict()
while True:
try:
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
cmdgen.CommunityData('public'),
cmdgen.UdpTransportTarget((
app.config.get('apcups', 'apcups_addr'), 161)), *oids
)
except Exception as e:
app.log.error('Unhandled exception: %s' % e)
# Check for errors and assemble results
if errorIndication:
app.log.error(errorIndication)
elif errorStatus:
app.log.error(errorStatus)
else:
for name, val in varBinds:
app.log.debug('%s = %s (%s)' % (name, val,
oids[str(name)]['desc']))
if oids[str(name)]['name'] == 'runtime':
# Convert runtime into seconds
data[oids[str(name)]['name']] = int(numerify(str(val))/100)
elif oids[str(name)]['name'] == 'battemp':
data[oids[str(name)]['name']] = numerify(str(val))
# Also provide temp in F
data['battempf'] = CtoF(data[oids[str(name)]['name']])
else:
data[oids[str(name)]['name']] = numerify(str(val))
tags = extract_tags(data, ['lasttestdate'])
event = [{
'measurement': 'apcups', # Time Series Name
'tags': tags,
'fields': data # Data points
}]
app.log.debug('Event data: %s' % event)
handler.postEvent(event)
# We reset the poll interval in case the configuration has changed
handler.sleep()