improve stability

This commit is contained in:
Hendrik Schutter 2024-09-01 11:58:35 +02:00
parent b67a393614
commit 0617517d7c
3 changed files with 17 additions and 9 deletions

View File

@ -4,6 +4,7 @@ Export metrics of a VEGAPULS Air connected via TTN as a prometheus service.
## Install ## ## Install ##
- `zypper install python311-paho-mqtt`
- `mkdir /opt/ttn-vegapulsair-exporter/` - `mkdir /opt/ttn-vegapulsair-exporter/`
- `cd /opt/ttn-vegapulsair-exporter/` - `cd /opt/ttn-vegapulsair-exporter/`
- import `ttn-vegapulsair-exporter.py` and `config.py` - import `ttn-vegapulsair-exporter.py` and `config.py`

View File

@ -12,7 +12,6 @@ import json
import sys import sys
import config import config
scrape_healthy = True scrape_healthy = True
startTime = datetime.now() startTime = datetime.now()
lastMqttReception = datetime.now() lastMqttReception = datetime.now()
@ -132,8 +131,11 @@ def update_metrics(payload, metadata):
scrape_healthy = True scrape_healthy = True
mutex.release() mutex.release()
def on_connect(mqttc, obj, flags, rc): def on_connect(client, userdata, flags, reason_code, properties):
print("\nConnected to MQTT: rc = " + str(rc)) if reason_code == 0:
print("\nConnected to MQTT: reason_code = " + str(reason_code))
if reason_code > 0:
print("\nNot connected to MQTT: reason_code = " + str(reason_code))
def on_message(mqttc, obj, msg): def on_message(mqttc, obj, msg):
#print("\nMessage: " + msg.topic + " " + str(msg.qos)) #print("\nMessage: " + msg.topic + " " + str(msg.qos))
@ -150,20 +152,24 @@ def on_message(mqttc, obj, msg):
mutex.release() mutex.release()
print("Unable to parse uplink") print("Unable to parse uplink")
def on_subscribe(mqttc, obj, mid, granted_qos): def on_subscribe(client, userdata, mid, reason_codes, properties):
print("\nSubscribed to MQTT: " + str(mid) + " " + str(granted_qos)) print("\nSubscribed to MQTT: " + str(mid))
def poll_mqtt(mqttc): def poll_mqtt(mqttc):
while True: mqttc.loop_forever()
mqttc.loop(10) # seconds timeout
def on_disconnect(client, userdata, flags, reason_code, properties):
print("disconnect occoured")
client.reconnect()
def main(): def main():
print("starting ...") print("starting ...")
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1) mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqttc.on_connect = on_connect mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe mqttc.on_subscribe = on_subscribe
mqttc.on_message = on_message mqttc.on_message = on_message
mqttc.on_disconnect = on_disconnect
mqttc.username_pw_set(config.ttn_user, config.ttn_key) mqttc.username_pw_set(config.ttn_user, config.ttn_key)
mqttc.tls_set() mqttc.tls_set()
mqttc.connect(config.ttn_region.lower() + ".cloud.thethings.network", 8883, 60) mqttc.connect(config.ttn_region.lower() + ".cloud.thethings.network", 8883, 60)

View File

@ -4,8 +4,9 @@ After=syslog.target
After=network.target After=network.target
[Service] [Service]
Restart=on-failure
RestartSec=2s RestartSec=2s
Type=oneshot Type=simple
User=prometheus User=prometheus
Group=prometheus Group=prometheus
WorkingDirectory=/opt/ttn-vegapulsair-exporter/ WorkingDirectory=/opt/ttn-vegapulsair-exporter/