forked from merbanan/rtl_433
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtl_433_collectd_pipe.py
executable file
·67 lines (46 loc) · 1.67 KB
/
rtl_433_collectd_pipe.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
#!/usr/bin/env python
"""Collectd monitoring probe (i.e. no plugin) for rtl_433."""
# Needs Python collectd Network plugin, s.a. https://github.com/appliedsec/collectd
# pip install collectd
# -or-
# curl -O https://github.com/appliedsec/collectd/raw/master/collectd.py
import time
import socket
import fileinput
import json
import collectd
def send_stats(when, stats, sender, to):
for (plugin_type, plugin_inst), values in stats.items():
if not values:
continue
collectd.PLUGIN_TYPE = plugin_type
for message in collectd.messages(values, when, sender, plugin_inst):
collectd.sock.sendto(message, to)
def sanitize(text):
return text.replace(" ", "_")
def rtl_433_probe():
hostname = socket.getfqdn()
interval = 60.0 # seconds
collectd.SEND_INTERVAL = interval
collectd.PLUGIN_NAME = 'rtlsdr'
collectd_host = "localhost"
collectd_port = 25826
for line in fileinput.input():
try:
data = json.loads(line)
when = int(time.time())
label = sanitize(data["model"])
if "channel" in data:
label += ".CH" + str(data["channel"])
attributes = {}
temperatures = {}
attributes["battery"] = data["battery_ok"]
attributes["humidity"] = data["humidity"]
temperatures["sensor"] = data["temperature_C"]
stats = {('gauge', label): attributes,
('temperature', label): temperatures}
send_stats(when, stats, hostname, (collectd_host, collectd_port))
except ValueError:
pass
if __name__ == "__main__":
rtl_433_probe()