Files
@ 008a986903ea
Branch filter:
Location: ChaosStuff/mqtt2prometheus/mqtt2prometheus.py - annotation
008a986903ea
4.3 KiB
text/x-python
Added co2 emission handling
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 | e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 3053573247d2 e7d80ef02af2 3053573247d2 3053573247d2 3053573247d2 3053573247d2 3053573247d2 3053573247d2 3053573247d2 3053573247d2 e7d80ef02af2 3053573247d2 3053573247d2 3053573247d2 e7d80ef02af2 3053573247d2 3053573247d2 3053573247d2 3053573247d2 3053573247d2 e7d80ef02af2 2847b90c29c0 2847b90c29c0 2847b90c29c0 e7d80ef02af2 a0e920df8f3f e7d80ef02af2 bfe1467514b9 bfe1467514b9 bfe1467514b9 bfe1467514b9 d35c626db59d d35c626db59d d35c626db59d bfe1467514b9 008a986903ea 008a986903ea 008a986903ea 008a986903ea e7d80ef02af2 3053573247d2 e7d80ef02af2 3053573247d2 3053573247d2 3053573247d2 3053573247d2 2847b90c29c0 a0e920df8f3f bfe1467514b9 d35c626db59d 008a986903ea e7d80ef02af2 3053573247d2 3053573247d2 3053573247d2 3053573247d2 3053573247d2 2847b90c29c0 a0e920df8f3f bfe1467514b9 d35c626db59d 008a986903ea e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 3053573247d2 3053573247d2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 3053573247d2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 3053573247d2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 3053573247d2 3053573247d2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 2847b90c29c0 2847b90c29c0 2847b90c29c0 2847b90c29c0 2847b90c29c0 2847b90c29c0 a0e920df8f3f a0e920df8f3f a0e920df8f3f a0e920df8f3f a0e920df8f3f bfe1467514b9 bfe1467514b9 bfe1467514b9 bfe1467514b9 bfe1467514b9 bfe1467514b9 bfe1467514b9 d35c626db59d d35c626db59d d35c626db59d d35c626db59d d35c626db59d d35c626db59d 008a986903ea 008a986903ea 008a986903ea 008a986903ea 008a986903ea 008a986903ea e7d80ef02af2 e7d80ef02af2 3053573247d2 e7d80ef02af2 3053573247d2 3053573247d2 e7d80ef02af2 3053573247d2 e7d80ef02af2 3053573247d2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 e7d80ef02af2 3053573247d2 e7d80ef02af2 | import json
import time
import paho.mqtt.client as mqtt
from prometheus_client import start_http_server, Gauge
CONFIG = json.load(open("/etc/mqtt2prometheus.json", mode="r", encoding="utf-8"))
people_now_present = Gauge(
"people_now_present", "People currently present in the space"
)
space_state = Gauge("space_state", "The current space state")
space_lastchange = Gauge(
"space_lastchange", "The lastchange timestamp of the current space state"
)
member_count = Gauge(
"member_count", "The current count of members", labelnames=["name"]
)
availability = Gauge(
"availability",
"The availability status of different MQTT clients",
labelnames=["clientname"],
)
temperature = Gauge(
"temperature_celsius", "The current temperature", labelnames=["location"]
)
humidity = Gauge("humidity_percent", "The current humidity", labelnames=["location"])
barometer = Gauge(
"barometer_hectopascal", "The current air pressure", labelnames=["location"]
)
power_consumption = Gauge(
"power_consumption_watt", "The current power used", labelnames=["location"]
)
co2_emission = Gauge(
"co2_emission_kilogramm", "The CO2 emission in kilogramm", labelnames=["location"]
)
def on_connect(client, userdata, flags, rc):
client.publish("availability/mqtt2prometheus", "online", 2, True)
client.message_callback_add("sensors/people_now_present", handle_people_now_present)
client.message_callback_add("space/status", handle_space_status)
client.message_callback_add("space/member_count", handle_member_count)
client.message_callback_add("availability/#", handle_availability)
client.message_callback_add("sensors/temperature/#", handle_temperature)
client.message_callback_add("sensors/humidity/#", handle_humidity)
client.message_callback_add("sensors/barometer/#", handle_barometer)
client.message_callback_add("sensors/power_consumption/#", handle_power_consumption)
client.message_callback_add("sensors/co2_emission/#", handle_co2_emission)
client.subscribe("sensors/people_now_present")
client.subscribe("space/status")
client.subscribe("space/member_count")
client.subscribe("availability/+")
client.subscribe("availability/shutdown/+")
client.subscribe("sensors/temperature/#")
client.subscribe("sensors/humidity/#")
client.subscribe("sensors/barometer/#")
client.subscribe("sensors/power_consumption/#")
client.subscribe("sensors/co2_emission/#")
def handle_people_now_present(client, userdata, msg):
people_now_present.set(json.loads(msg.payload))
def handle_space_status(client, userdata, msg):
state = json.loads(msg.payload)
space_state.set(int(state["open"]))
space_lastchange.set(state["lastchange"])
def handle_member_count(client, userdata, msg):
count = json.loads(msg.payload)
member_count.labels(count["name"]).set(count["value"])
def handle_availability(client, userdata, msg):
t = msg.topic.split("/")
if len(t) == 2:
c = t[-1]
else:
c = "_".join(t[1:])
v = 1 if msg.payload == b"online" else 0
availability.labels(c).set(v)
def handle_temperature(client, userdata, msg):
t = msg.topic.split("/")
location = "-".join(t[2:])
temperature.labels(location).set(float(msg.payload))
def handle_humidity(client, userdata, msg):
t = msg.topic.split("/")
location = "-".join(t[2:])
humidity.labels(location).set(float(msg.payload))
def handle_barometer(client, userdata, msg):
t = msg.topic.split("/")
location = "-".join(t[2:])
barometer.labels(location).set(float(msg.payload))
def handle_power_consumption(client, userdata, msg):
t = msg.topic.split("/")
location = "-".join(t[2:])
power_consumption.labels(location).set(float(msg.payload))
def handle_co2_emission(client, userdata, msg):
t = msg.topic.split("/")
location = "-".join(t[2:])
co2_emission.labels(location).set(float(msg.payload))
def main():
client = mqtt.Client("mqtt2prometheus")
client.on_connect = on_connect
client.username_pw_set(CONFIG["mqtt"]["username"], CONFIG["mqtt"]["password"])
client.will_set("availability/mqtt2prometheus", "offline", 2, True)
client.connect(CONFIG["mqtt"]["host"])
client.loop_start()
start_http_server(CONFIG["prometheus"]["port"])
while True:
time.sleep(1)
if __name__ == "__main__":
main()
|