-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
183 lines (137 loc) · 5.33 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import queue
import socket
import sys
import time
import zlib
from threading import Thread
from typing import List
import influxdb
import ha
from ha import read_user_options
from util import get_logger
UDP_IP = "0.0.0.0"
logger = get_logger()
import urllib3.exceptions
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class InfluxDBWriter():
def _write_thread(self):
while True:
batch = []
while not self.Q.empty() and len(batch) < 20_000:
batch.append(self.Q.get())
if len(batch) > 0:
logger.info('Writing %d (max msg len=%d)', len(batch), max_msg_len)
try:
self.client.write(batch, protocol='line', params=dict(db=self.client._database, precision='ms'))
except Exception as e:
logger.error('Error writing batch: %s', e)
time.sleep(self.write_interval)
def __init__(self, influxdb_conf, write_interval=.5):
self.host = influxdb_conf.get('host', "homeassistant.local")
self.port = int(influxdb_conf.get('port', 8086))
self.user = influxdb_conf.get('username')
self.ssl = influxdb_conf.get('ssl', False)
self.db = influxdb_conf.get('database')
self.influxdb_conf = influxdb_conf
self.write_interval = write_interval
self.key = f'{self.host}:{self.port}/{self.db}'
def connect(self):
logger.info('Connecting InfluxDB %s@%s (port=%i ssl=%s)', self.user, self.host, self.port, self.ssl)
self.client = influxdb.InfluxDBClient(
host=self.host,
port=self.port,
username=self.user,
password=self.influxdb_conf.get('password'),
database=self.db,
ssl=self.ssl,
verify_ssl=False,
# org="" # influxdb v1 had no org
)
def _request_gzip(data, headers, **kwargs):
if headers is None:
headers = {}
if data:
headers['content-encoding'] = 'gzip'
compress = zlib.compressobj(wbits=16 + zlib.MAX_WBITS)
data = compress.compress(data) + compress.flush()
headers['Content-Length'] = str(len(data))
return self.client._session.request_(data=data, headers=headers, **kwargs)
self.client._session.request_ = self.client._session.request
self.client._session.request = _request_gzip
logger.info('Measurements: %s', ','.join(map(lambda m: m.get('name', m), self.client.get_list_measurements())))
self.Q = queue.Queue(200_000)
self.w_thread = Thread(target=self._write_thread, daemon=True)
self.w_thread.start()
max_msg_len = 0
opt = read_user_options()
addrs = set()
def receive_loop(writers: List[InfluxDBWriter]):
global max_msg_len
udp_port = int(opt.get('udp_port', 8086))
log_points = bool(opt.get('log_points', False))
logger.info("receiving on %s:%s", UDP_IP, udp_port)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, udp_port))
n_full = 0
while True:
try:
data, addr = sock.recvfrom(1024 * 2)
if len(data) > max_msg_len:
max_msg_len = len(data)
if addr not in addrs:
logger.info('received %r from %s', data[:40], addr)
addrs.add(addr)
msg = data.decode("utf-8").rstrip('\n')
lines = msg.split('\n')
for l in lines:
if log_points:
logger.info("[%s] P %s", addr, l)
for w in writers:
w.Q.put(l, block=False)
n_full = 0
except KeyboardInterrupt:
logger.info('caught KeyboardInterrupt, exiting')
break
except queue.Full:
n_full += 1
if n_full > 3:
logger.info('queue full, exiting')
time.sleep(writers[0].write_interval * 2)
break
else:
logger.info('queue full #%d', n_full)
except Exception:
logger.error('Error in rx loop:')
logger.error(sys.exc_info(), exc_info=True)
# logger.info("msg (%dB, %dL, Q=%d) from %s: %s", len(data), len(lines), Q.qsize(), addr[0], msg[:60])
def main():
# client = InfluxDBClient(
# "http://homeassistant.local:8086",
# username="home_assistant",
# password="",
# org="" # influxdb v1 had no org
# )
# write = client.write_api(write_options=SYNCHRONOUS)
try:
config = ha.read_hass_configuration_yaml()
influxdb_conf = config.get('influxdb', None)
except Exception as e:
logger.warning('Failed to load configuration: %s', str(e) or type(e))
influxdb_conf = None
writers = {}
if influxdb_conf:
w = InfluxDBWriter(influxdb_conf)
writers[w.key] = w
for influxdb_conf in opt.get('additional_servers', []):
host = influxdb_conf.get("host")
if host and host[0] != "#":
w = InfluxDBWriter(influxdb_conf)
writers[w.key] = w
if len(writers) == 0:
logger.error('No servers!')
sys.exit(1)
for w in writers.values():
w.connect()
receive_loop(list(writers.values()))
main()
sys.exit(1)