Files @ 65eb8f0a25d4
Branch filter:

Location: ChaosStuff/mqtt2prometheus/mqtt2prometheus.py - annotation

Dennis Fink
Use a custom class
e7d80ef02af2
e7d80ef02af2
e7d80ef02af2
e7d80ef02af2
65eb8f0a25d4
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
55df66067104
55df66067104
55df66067104
55df66067104
55df66067104
55df66067104
55df66067104
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
e7d80ef02af2
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
5229d5941a1b
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
e7d80ef02af2
65eb8f0a25d4
65eb8f0a25d4
e7d80ef02af2
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
e7d80ef02af2
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
e7d80ef02af2
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
e7d80ef02af2
65eb8f0a25d4
65eb8f0a25d4
e7d80ef02af2
65eb8f0a25d4
65eb8f0a25d4
e7d80ef02af2
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
65eb8f0a25d4
55df66067104
55df66067104
e7d80ef02af2
e7d80ef02af2
65eb8f0a25d4
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 Gauge, start_http_server

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"]
)

energy_consumption = Gauge(
    "energy_consumption_kilowatthour",
    "The energy consumption in kilowatthour",
    labelnames=["location"],
)


class Mqtt2Prometheus(mqtt.Client):
    def on_connect(self, client, userdata, flags, rc):
        self.publish("availability/mqtt2prometheus", "online", 2, True)

        topics_handler = {
            "availability/#": self.handle_availability,
            "space/status": self.handle_space_status,
            "space/member_count": self.handle_member_count,
            "sensors/people_now_present": self.handle_people_now_present,
            "sensors/#": self.handle_sensor,
        }

        for topic, handler in topics_handler.items():
            self.message_callback_add(topic, handler)
            self.subscribe(topic)

    def handle_people_now_present(self, client, userdata, msg):
        people_now_present.set(json.loads(msg.payload))

    def handle_space_status(self, client, userdata, msg):
        state = json.loads(msg.payload)
        space_state.set(int(state["open"]))
        space_lastchange.set(state["lastchange"])

    def handle_member_count(self, client, userdata, msg):
        count = json.loads(msg.payload)
        member_count.labels(count["name"]).set(count["value"])

    def handle_availability(self, 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_sensor(self, client, userdata, msg):
        t = msg.topic.split("/")

        if t[1] == "people_now_present":
            return

        metric = globals().get(t[1], None)
        if metric is not None:
            try:
                location = "-".join(t[2:])
                metric.labels(location).set(float(msg.payload))
            except:
                pass


def main():

    client = Mqtt2Prometheus("mqtt2prometheus")
    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()