From 79bc0eb0f08638c8ded80b04e4d0ae6f18cbe417 Mon Sep 17 00:00:00 2001 From: jwansek Date: Fri, 19 Jun 2026 18:00:55 +0100 Subject: Started pushing SNMP POE shit to MQTT also --- .gitignore | 1 + mqtt-client/mqtt-client.py | 16 ++++++++++------ switch-snmp/mikrotik-switches.conf | 6 +++--- switch-snmp/omada-switches.conf | 4 ++-- switch-snmp/requirements.txt | 4 +++- switch-snmp/switches.py | 17 +++++++++++++++++ 6 files changed, 36 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 56d2d3f..ee91960 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ prometheus/web.yml prometheus/web.yml mqtt-client/omada.cfg venv/ +door_log.csv # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/mqtt-client/mqtt-client.py b/mqtt-client/mqtt-client.py index f45fbf1..c1a05e4 100644 --- a/mqtt-client/mqtt-client.py +++ b/mqtt-client/mqtt-client.py @@ -55,9 +55,9 @@ class MQTTClient: self.zigbee_plugs = {"MikroTikZigbeePlug"} self.zigbee_to_tasmota_transformations = { - "RMSVoltage": "Voltage", - "ActivePower": "Power", - "RMSCurrent": "Current" + "RMSVoltage": ("Voltage", int), + "ActivePower": ("Power", int), + "RMSCurrent": ("Current", float) } # print(self.send_raw_tasmota_http("192.168.5.6", os.environ["MQTT_PASSWD"], "Power")) @@ -96,9 +96,13 @@ class MQTTClient: fields2 = {} for k, v in fields.items(): if k in self.zigbee_to_tasmota_transformations.keys(): - fields2[self.zigbee_to_tasmota_transformations[k]] = v + fields2[self.zigbee_to_tasmota_transformations[k][0]] = self.zigbee_to_tasmota_transformations[k][1](v) + print("Logged the following information for the zigbee plug '%s' as a tasmota plug: %s" % (friendlyname, str(fields2))) self.append_influxdb(fields2, "tasmota_power", {"plug": friendlyname}) + + for k, v in fields2.items(): + self.tasmota_power_prom.labels(plug = friendlyname, field = k).set(v) def handle_plug(self, msg_j, location): print("'%s' is using %.1fw @ %s. %.1fkWh so far today, %.1fkWh yesterday" % (location, msg_j["ENERGY"]["Power"], msg_j["Time"], msg_j["ENERGY"]["Today"], msg_j["ENERGY"]["Yesterday"])) @@ -160,10 +164,10 @@ class MQTTClient: fields["Temperature"] = float(fields["Temperature"]) self.temperature_prom.labels(location = friendlyname).set(fields["Temperature"]) elif "ZoneStatus" in fields.keys() and "Contact" in fields.keys(): - if fields["ZoneStatus"] == 1 and fields["Contact"] == 1: + if fields["ZoneStatusChange"] == 1 and fields["ZoneStatusChangeZone"] == 1: self.doorsensor_prom.labels(location = friendlyname).state("opened") self.door_opened_counter.labels(location = friendlyname).inc() - elif fields["ZoneStatus"] == 0 and fields["Contact"] == 0: + elif fields["ZoneStatusChange"] == 0 and fields["ZoneStatusChangeZone"] == 1: self.doorsensor_prom.labels(location = friendlyname).state("closed") if "Read" not in fields.keys(): diff --git a/switch-snmp/mikrotik-switches.conf b/switch-snmp/mikrotik-switches.conf index 8f13525..9f28a32 100644 --- a/switch-snmp/mikrotik-switches.conf +++ b/switch-snmp/mikrotik-switches.conf @@ -1,8 +1,8 @@ [192.168.6.5] ether2 = 2 -ether1 = 1 -ether3 = 3 -ether4 = 4 +ether1 = Cisco 7940 +ether3 = TypeCPOENIC +ether4 = ApplePOENIC ether5 = 5 ether6 = 6 ether7 = 7 diff --git a/switch-snmp/omada-switches.conf b/switch-snmp/omada-switches.conf index be77cd2..eb112bc 100644 --- a/switch-snmp/omada-switches.conf +++ b/switch-snmp/omada-switches.conf @@ -15,11 +15,11 @@ 18 = Cluster Pi 6 17 = Cluster Pi 4 9 = Jetson Orin Nano -12 = Netgate SG-1100 +12 = POETypeC [192.168.69.27] 1 = Fibre ONT 2 = TL-RP108GE & EAP110 3 = Firestick -4 = ES205G +4 = Cisco 7942 diff --git a/switch-snmp/requirements.txt b/switch-snmp/requirements.txt index aeb5843..c6f1ed1 100644 --- a/switch-snmp/requirements.txt +++ b/switch-snmp/requirements.txt @@ -2,4 +2,6 @@ python-dotenv influxdb-client pandas fabric -prometheus-client \ No newline at end of file +prometheus-client +paho-mqtt==1.6.1 +numpyencoder==0.3.2 diff --git a/switch-snmp/switches.py b/switch-snmp/switches.py index 1bc43c2..06d7b5b 100644 --- a/switch-snmp/switches.py +++ b/switch-snmp/switches.py @@ -1,6 +1,9 @@ +import paho.mqtt.client as paho +from numpyencoder import NumpyEncoder import prometheus_client import snmpOmada import mikrotik +import json import os from influxdb_client import InfluxDBClient, Point, WritePrecision @@ -15,6 +18,11 @@ def append(points): influxc.ping() for measurement in points: + mqttc.publish( + "tele/SwitchSNMP/%s/%s/SENSOR" % (measurement["tags"]["switch_host"], str(measurement["tags"]["port"])), + json.dumps({**measurement["tags"], **measurement["fields"]}, cls = NumpyEncoder), + qos = 1 + ) for field in measurement["fields"].keys(): try: float(measurement["fields"][field]) @@ -44,11 +52,17 @@ if __name__ == "__main__": import dotenv dotenv.load_dotenv(dotenv_path = env_path) INFLUXDB_HOST = "dns.athome" + MQTT_HOST = "dns.athome" PUSHGATEWAY_HOST = "dns.athome" else: + MQTT_HOST = "mqtt" INFLUXDB_HOST = "influxdb" PUSHGATEWAY_HOST = "pushgateway" + mqttc = paho.Client("reg.reaweb.uk/switch-snmp", clean_session = True) + mqttc.username_pw_set(os.environ["MQTT_USER"], password = os.environ["MQTT_PASSWD"]) + mqttc.connect(MQTT_HOST, 1883, 60) + registry = prometheus_client.CollectorRegistry() switch_power = prometheus_client.Gauge( "switch_power", @@ -60,3 +74,6 @@ if __name__ == "__main__": points = snmpOmada.get_points() + mikrotik.get_points() mikrotik.print_points(points) append(points) + + mqttc.disconnect() + -- cgit v1.2.3