This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rtl_433_statsd_relay.py
executable file
·74 lines (53 loc) · 1.88 KB
/
rtl_433_statsd_relay.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
#!/usr/bin/env python
# Statsd monitoring relay for rtl_433.
# Uses Python statsd Network plugin, s.a. https://github.com/jsocol/pystatsd
# pip install pystatsd
# -or- get https://github.com/jsocol/pystatsd/raw/master/statsd/client.py
# (included as statsd.py for convenience)
from __future__ import print_function
import socket
import json
from statsd import StatsClient
UDP_IP = "127.0.0.1"
UDP_PORT = 1433
STATSD_HOST = "127.0.0.1"
STATSD_PORT = 8125
STATSD_PREFIX = "rtlsdr"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
def sanitize(text):
return text.replace(" ", "_")
def parse_syslog(line):
"""Try to extract the payload from a syslog line."""
line = line.decode("ascii") # also UTF-8 if BOM
if line.startswith("<"):
# fields should be "<PRI>VER", timestamp, hostname, command, pid, mid, sdata, payload
fields = line.split(None, 7)
line = fields[-1]
return line
def rtl_433_probe():
statsd = StatsClient(host=STATSD_HOST,
port=STATSD_PORT,
prefix=STATSD_PREFIX)
while True:
line, addr = sock.recvfrom(1024)
try:
line = parse_syslog(line)
data = json.loads(line)
label = sanitize(data["model"])
if "channel" in data:
label += ".CH" + str(data["channel"])
if "battery" in data:
if data["battery"] == "OK":
statsd.gauge(label + '.battery', 1)
else:
statsd.gauge(label + '.battery', 0)
if "humidity" in data:
statsd.gauge(label + '.humidity', data["humidity"])
statsd.gauge(label + '.temperature', data["temperature_C"])
except KeyError:
pass
except ValueError:
pass
if __name__ == "__main__":
rtl_433_probe()