-
Notifications
You must be signed in to change notification settings - Fork 23
/
getUpsData.py
33 lines (24 loc) · 1.22 KB
/
getUpsData.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
from __future__ import print_function
import subprocess,sys,re
if (len(sys.argv) == 2):
arg = sys.argv[1]
cmd="upsc "+arg+" > /dev/stdout 2> /dev/null"
output=""
string_measurements=["battery.mfr.date", "battery.type", "device.mfr", "device.model","device.serial","device.type",
"driver.name", "driver.paramter.port", "driver.parameter.synchronous", "driver.version", "driver.version.data",
"ups.beeper.status", "ups.mfr","ups.model", "ups.serial", "ups.status", "ups.test.result", "driver.parameter.port",
"driver.parameter.syncronous"]
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
for line in p.stdout.readlines(): #read and store result in log file
line = line.decode("utf-8").rstrip()
key = line[:line.find(":")]
value = line[line.find(":")+2:]
if key in string_measurements:
if not re.match(r'^(-?)(\d+|\d+\.\d+)$',value):
value = '"' + value + '"'
measurement = key + "=" + value
if output != "":
measurement = "," + measurement
output += measurement
output = "ups " + output.rstrip()
print(output, end='')