-
Notifications
You must be signed in to change notification settings - Fork 0
/
apcupsd_influxdbV2.py
88 lines (75 loc) · 2.5 KB
/
apcupsd_influxdbV2.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
#!/usr/bin/env python3
import re
import socket
import datetime
import time
from struct import pack
from influxdb import InfluxDBClient
# put your influx server ip, port number and the name of the database
server_ip = 'xxx.xxx.xxx.xxx'
server_port = 8086
db_name = 'ups_stats'
# apcupsd server ip, port number
HOST = ('xxx.xxx.xxx.xxx')
PORT = 3551
#PORT = 3493
# iso = datetime.datetime.now()
iso = datetime.datetime.utcnow().isoformat() + 'Z'
while True:
# Open socket with remote host
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
# Send command: Packet is pad byte, size byte, and command
s.send(pack(b'xb6s', 6, b'status'))
# Ditch the header
s.recv(1024)
time.sleep(.25)
data = s.recv(4096)
s.close()
#print ('Received', repr(data))
bytes2STR = data.decode('utf-8')
values = re.findall(r'(\d+\.\d+)',bytes2STR) # find values using regex, start search at 1500th chr to remove html
names = re.findall(r'[A-Za-z-]+\w\b',bytes2STR)
#print(values) # uncomment to see regex numerical output
#print(names) # uncomment to see regex ascill output
# split results we want from "values" in to individual variables
LINEV = float(values[1])
LOAD = float(values[2])
BCHG = float(values[3])
TIMELEFT = float(values[4])
BATTV = float(values[7])
SERVER = (names[3])
UPSNAME = (names[8])
STATUS = (names[24])
# Prepare UPS variabes in JSON format for upload to Influxdb
json_ups = [
{
"measurement": "apcaccess",
"tags": {
"status": STATUS,
"upsname": UPSNAME,
"server": SERVER
},
#"time": iso, # Comment out if influxdb is not writing data points
"fields": {
"LINEV": LINEV,
"LOAD": LOAD,
"BCHG": BCHG,
"BATTV": BATTV,
"TIMELEFT": TIMELEFT,
}
}
]
# write values to influxdb
# note that the username and password are blank, put them inside the '' if you have
# implemented authentication
# retries=0 is infinite retries.
client = InfluxDBClient(server_ip, server_port, '', '', db_name, timeout=5,retries=0)
try:
client.create_database(db_name) # will create db if none exists
client.write_points(json_ups)
except ConnectionError:
print('influxdb server not responding')
#break
# Wait before repeating loop
time.sleep(5)