Révision 1bd6a641
add plugin "feinstaubsensor"
| plugins/luftdaten/feinstaubsensor | ||
|---|---|---|
| 1 |
#!/usr/bin/env python3 |
|
| 2 |
""" |
|
| 3 |
|
|
| 4 |
=head1 NAME |
|
| 5 |
|
|
| 6 |
feinstaubsensor - Plugin to monitor one or more environmental sensors |
|
| 7 |
|
|
| 8 |
|
|
| 9 |
=head1 APPLICABLE SYSTEMS |
|
| 10 |
|
|
| 11 |
The "Feinstaubsensor" was developed by the OK Lab Stuttgart and is part of the |
|
| 12 |
Citizen Science Project "luftdaten.info" (http://luftdaten.info). |
|
| 13 |
|
|
| 14 |
Data is retrieved via HTTP requests from the sensors itself. |
|
| 15 |
|
|
| 16 |
|
|
| 17 |
=head1 CONFIGURATION |
|
| 18 |
|
|
| 19 |
Place a configuration entry somewhere below /etc/munin/plugin-conf.d/: |
|
| 20 |
|
|
| 21 |
[feinstaubsensor] |
|
| 22 |
env.sensor_hosts foo=192.168.1.4 [fe80::1:2:3:4%eth0] bar=sensor2.lan |
|
| 23 |
|
|
| 24 |
The <sensor_hosts> environment variable is a space separated list of <token>. |
|
| 25 |
Each <token> can be either a <host> or a combination of label and <host> (separated by the |
|
| 26 |
character "="). |
|
| 27 |
A <host> may be an IPv4 address, an IPv6 address (enclosed in square brackets) or a name to be |
|
| 28 |
resolved via DNS. |
|
| 29 |
|
|
| 30 |
Examples for <token>: |
|
| 31 |
* 192.168.1.4 |
|
| 32 |
* foo=192.168.1.4 |
|
| 33 |
* [fe80::1a:2b:3c:cafe] |
|
| 34 |
* bar=[fe80::1a:2b:3c:cafe] |
|
| 35 |
* feinstaubsensor-12345.local |
|
| 36 |
* baz=feinstaubsensor-12345.local |
|
| 37 |
|
|
| 38 |
|
|
| 39 |
=head1 AUTHOR |
|
| 40 |
|
|
| 41 |
Lars Kruse <devel@sumpfralle.de> |
|
| 42 |
|
|
| 43 |
|
|
| 44 |
=head1 LICENSE |
|
| 45 |
|
|
| 46 |
GPLv3 |
|
| 47 |
|
|
| 48 |
|
|
| 49 |
=head1 MAGIC MARKERS |
|
| 50 |
|
|
| 51 |
#%# family=manual |
|
| 52 |
|
|
| 53 |
""" |
|
| 54 |
|
|
| 55 |
import collections |
|
| 56 |
import functools |
|
| 57 |
import json |
|
| 58 |
import os |
|
| 59 |
import re |
|
| 60 |
import sys |
|
| 61 |
import urllib.request |
|
| 62 |
|
|
| 63 |
|
|
| 64 |
graphs = [ |
|
| 65 |
{
|
|
| 66 |
"name": "wireless_signal", |
|
| 67 |
"graph_title": "Feinstaub Wifi Signal", |
|
| 68 |
"graph_vlabel": "%", |
|
| 69 |
"graph_args": "-l 0", |
|
| 70 |
"graph_info": "Wifi signal strength", |
|
| 71 |
"api_value_name": "signal", |
|
| 72 |
"value_type": "GAUGE", |
|
| 73 |
}, {
|
|
| 74 |
"name": "feinstaub_samples", |
|
| 75 |
"graph_title": "Feinstaub Sample Count", |
|
| 76 |
"graph_vlabel": "#", |
|
| 77 |
"graph_info": "Number of samples since bootup", |
|
| 78 |
"api_value_name": "samples", |
|
| 79 |
"value_type": "DERIVE", |
|
| 80 |
}, {
|
|
| 81 |
"name": "feinstaub_humidity", |
|
| 82 |
"graph_title": "Feinstaub Humidity", |
|
| 83 |
"graph_vlabel": "% humidity", |
|
| 84 |
"graph_info": "Weather information: air humidity", |
|
| 85 |
"api_value_name": "humidity", |
|
| 86 |
"value_type": "GAUGE", |
|
| 87 |
}, {
|
|
| 88 |
"name": "feinstaub_temperature", |
|
| 89 |
"graph_title": "Feinstaub Temperature", |
|
| 90 |
"graph_vlabel": "°C", |
|
| 91 |
"graph_info": "Weather information: temperature", |
|
| 92 |
"api_value_name": "temperature", |
|
| 93 |
"value_type": "GAUGE", |
|
| 94 |
}, {
|
|
| 95 |
"name": "feinstaub_particles_pm10", |
|
| 96 |
"graph_title": "Feinstaub Particle Measurement P10", |
|
| 97 |
"graph_vlabel": "µg / m³", |
|
| 98 |
"graph_info": "Concentration of particles with a size between 2.5µm and 10µm", |
|
| 99 |
"api_value_name": "SDS_P1", |
|
| 100 |
"value_type": "GAUGE", |
|
| 101 |
}, {
|
|
| 102 |
"name": "feinstaub_particles_pm2_5", |
|
| 103 |
"graph_title": "Feinstaub Particle Measurement P2.5", |
|
| 104 |
"graph_vlabel": "µg / m³", |
|
| 105 |
"graph_info": "Concentration of particles with a size up to 2.5µm", |
|
| 106 |
"api_value_name": "SDS_P2", |
|
| 107 |
"value_type": "GAUGE", |
|
| 108 |
}] |
|
| 109 |
|
|
| 110 |
|
|
| 111 |
SensorHost = collections.namedtuple("SensorHost", ("host", "label", "fieldname"))
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
def clean_fieldname(text): |
|
| 115 |
if text == "root": |
|
| 116 |
# "root" is a magic (forbidden) word |
|
| 117 |
return "_root" |
|
| 118 |
else: |
|
| 119 |
return re.sub(r"(^[^A-Za-z_]|[^A-Za-z0-9_])", "_", text) |
|
| 120 |
|
|
| 121 |
|
|
| 122 |
def parse_sensor_hosts_from_description(hosts_description): |
|
| 123 |
""" parse sensor list from the environment variable 'sensor_hosts' and retrieve their data """ |
|
| 124 |
sensors = [] |
|
| 125 |
for token in hosts_description.split(): |
|
| 126 |
if "=" in token: |
|
| 127 |
label, host = token.strip().split("=", 1)
|
|
| 128 |
else: |
|
| 129 |
host = token.strip() |
|
| 130 |
label = host |
|
| 131 |
fieldname = clean_fieldname("value_" + host)
|
|
| 132 |
sensors.append(SensorHost(host, label, fieldname)) |
|
| 133 |
sensors.sort(key=lambda item: item.fieldname) |
|
| 134 |
return sensors |
|
| 135 |
|
|
| 136 |
|
|
| 137 |
@functools.lru_cache() |
|
| 138 |
def get_sensor_data(host): |
|
| 139 |
""" request the data from a sensor and return a dict (value_type -> value) |
|
| 140 |
|
|
| 141 |
The result is cached - thus we do not need to take care for efficiency. |
|
| 142 |
|
|
| 143 |
Example dataset returned by the sensor: |
|
| 144 |
{"software_version": "NRZ-2017-099", "age":"88", "sensordatavalues":[
|
|
| 145 |
{"value_type":"SDS_P1","value":"27.37"},{"value_type":"SDS_P2","value":"13.53"},
|
|
| 146 |
{"value_type":"temperature","value":"23.70"},{"value_type":"humidity","value":"69.20"},
|
|
| 147 |
{"value_type":"samples","value":"626964"},{"value_type":"min_micro","value":"225"},
|
|
| 148 |
{"value_type":"max_micro","value":"887641"},{"value_type":"signal","value":"-47"}]}
|
|
| 149 |
|
|
| 150 |
""" |
|
| 151 |
try: |
|
| 152 |
with urllib.request.urlopen("http://{}/data.json".format(host)) as request:
|
|
| 153 |
body = request.read() |
|
| 154 |
except IOError as exc: |
|
| 155 |
print("Failed to retrieve data from '{}': {}".format(host, exc), file=sys.stderr)
|
|
| 156 |
return None |
|
| 157 |
try: |
|
| 158 |
data = json.loads(body.decode("utf-8"))
|
|
| 159 |
except ValueError as exc: |
|
| 160 |
print("Failed to parse data from '{}': {}".format(host, exc), file=sys.stderr)
|
|
| 161 |
return None |
|
| 162 |
return {item["value_type"]: item["value"] for item in data["sensordatavalues"]}
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
def print_graph_section(graph_description, hosts, include_config, include_values): |
|
| 166 |
print("multigraph {}".format(graph_description["name"]))
|
|
| 167 |
if include_config: |
|
| 168 |
# graph configuration |
|
| 169 |
print("graph_category sensors")
|
|
| 170 |
for key in ("graph_title", "graph_vlabel", "graph_args", "graph_info"):
|
|
| 171 |
if key in graph_description: |
|
| 172 |
print("{} {}".format(key, graph_description[key]))
|
|
| 173 |
for host_info in hosts: |
|
| 174 |
print("{}.label {}".format(host_info.fieldname, host_info.label))
|
|
| 175 |
print("{}.type {}".format(host_info.fieldname, graph_description["value_type"]))
|
|
| 176 |
if include_values: |
|
| 177 |
for host_info in hosts: |
|
| 178 |
# We cannot distinguish between fields that are not supported by the sensor (most are |
|
| 179 |
# optional) and missing data. Thus we cannot handle online/offline sensor data fields, |
|
| 180 |
# too. |
|
| 181 |
data = get_sensor_data(host_info.host) |
|
| 182 |
if data is not None: |
|
| 183 |
value = data.get(graph_description["api_value_name"]) |
|
| 184 |
if value is not None: |
|
| 185 |
print("{}.value {}".format(host_info.fieldname, value))
|
|
| 186 |
print() |
|
| 187 |
|
|
| 188 |
|
|
| 189 |
action = sys.argv[1] if (len(sys.argv) > 1) else "" |
|
| 190 |
sensor_hosts = parse_sensor_hosts_from_description(os.getenv("sensor_hosts", ""))
|
|
| 191 |
if not sensor_hosts: |
|
| 192 |
print("ERROR: undefined or empty environment variable 'sensor_hosts'.", file=sys.stderr)
|
|
| 193 |
sys.exit(1) |
|
| 194 |
|
|
| 195 |
|
|
| 196 |
if action == "config": |
|
| 197 |
is_dirty_config = (os.getenv("MUNIN_CAP_DIRTYCONFIG") == "1")
|
|
| 198 |
for graph in graphs: |
|
| 199 |
print_graph_section(graph, sensor_hosts, True, is_dirty_config) |
|
| 200 |
elif action == "": |
|
| 201 |
for graph in graphs: |
|
| 202 |
print_graph_section(graph, sensor_hosts, False, True) |
|
| 203 |
else: |
|
| 204 |
print("ERROR: unsupported action requested ('{}')".format(action), file=sys.stderr)
|
|
| 205 |
sys.exit(2) |
|
Formats disponibles : Unified diff