add support for static node descriptions

This commit is contained in:
Thomas Jensen 2020-01-30 11:31:19 +01:00
parent 157905283e
commit 0f4b5f6039

View File

@ -10,6 +10,12 @@ topics = [
("+/alert", 0), ("+/alert", 0),
("homelab/ats/supply/+", 0) ("homelab/ats/supply/+", 0)
] ]
nodes = {
'rack-temp': 'Office RPi temperature probes',
'powerpi': 'Homelab RPi power monitoring',
'hass': 'Home Assistant',
'exporter': 'Prometheus MQTT topic exporter'
}
node_api = NodeAPI() node_api = NodeAPI()
@ -18,6 +24,12 @@ MQTT_BROKER_CLIENTS = Gauge('mqtt_broker_clients', 'MQTT broker clients connecte
MQTT_ALERT = Gauge('mqtt_alert_state', 'MQTT alert topic states', ['origin', 'alert']) MQTT_ALERT = Gauge('mqtt_alert_state', 'MQTT alert topic states', ['origin', 'alert'])
MQTT_STATUS = Gauge('mqtt_status', 'MQTT status topic', ['status']) MQTT_STATUS = Gauge('mqtt_status', 'MQTT status topic', ['status'])
def get_desc(node):
if node in nodes:
return nodes[node]
else:
return node_api.get(node)
def on_log(client, userdata, level, buff): def on_log(client, userdata, level, buff):
print(buff) print(buff)
@ -39,7 +51,7 @@ def on_message(client, userdata, msg):
topic = msg.topic.split('/') topic = msg.topic.split('/')
if topic[0] == "$CONNECTED": if topic[0] == "$CONNECTED":
MQTT_CONNECTED.labels(topic[1], node_api.get(topic[1])).set(int(msg.payload)) MQTT_CONNECTED.labels(topic[1], get_desc(topic[1])).set(int(msg.payload))
elif msg.topic == "$SYS/broker/clients/connected": elif msg.topic == "$SYS/broker/clients/connected":
MQTT_BROKER_CLIENTS.set(int(msg.payload)) MQTT_BROKER_CLIENTS.set(int(msg.payload))