restart at failure

This commit is contained in:
Hendrik Schutter 2024-12-15 20:17:22 +01:00
parent 4723789261
commit 62da4a9f11

View File

@ -29,9 +29,33 @@ class RequestHandler(BaseHTTPRequestHandler):
self.send_response(200) self.send_response(200)
self.send_header("Content-type", "text/html") self.send_header("Content-type", "text/html")
self.end_headers() self.end_headers()
self.wfile.write(bytes(config.exporter_prefix + "expoter_duration_seconds_sum " + str(int((datetime.now() - startTime).total_seconds())) + "\n", "utf-8")) self.wfile.write(
self.wfile.write(bytes(config.exporter_prefix + "exporter_request_count " + str(request_count) + "\n", "utf-8")) bytes(
self.wfile.write(bytes(config.exporter_prefix + "exporter_scrape_healthy " + str(int(scrape_healthy)) + "\n", "utf-8")) config.exporter_prefix
+ "expoter_duration_seconds_sum "
+ str(int((datetime.now() - startTime).total_seconds()))
+ "\n",
"utf-8",
)
)
self.wfile.write(
bytes(
config.exporter_prefix
+ "exporter_request_count "
+ str(request_count)
+ "\n",
"utf-8",
)
)
self.wfile.write(
bytes(
config.exporter_prefix
+ "exporter_scrape_healthy "
+ str(int(scrape_healthy))
+ "\n",
"utf-8",
)
)
for metric in node_metrics: for metric in node_metrics:
# print(metric) # print(metric)
@ -45,7 +69,7 @@ class RequestHandler(BaseHTTPRequestHandler):
global scrape_healthy global scrape_healthy
request_count = request_count + 1 request_count = request_count + 1
# print("Request: " + self.path) # print("Request: " + self.path)
if (self.path.startswith("/metrics")): if self.path.startswith("/metrics"):
# if (datetime.now() - lastMqttReception) > timedelta(hours=7): # if (datetime.now() - lastMqttReception) > timedelta(hours=7):
# scrape_healthy = False # scrape_healthy = False
self.get_metrics() self.get_metrics()
@ -54,9 +78,16 @@ class RequestHandler(BaseHTTPRequestHandler):
self.send_header("Content-type", "text/html") self.send_header("Content-type", "text/html")
self.end_headers() self.end_headers()
self.wfile.write(bytes("<html>", "utf-8")) self.wfile.write(bytes("<html>", "utf-8"))
self.wfile.write(bytes("<head><title>VEGAPULS Air exporter</title></head>", "utf-8")) self.wfile.write(
bytes("<head><title>VEGAPULS Air exporter</title></head>", "utf-8")
)
self.wfile.write(bytes("<body>", "utf-8")) self.wfile.write(bytes("<body>", "utf-8"))
self.wfile.write(bytes('<h1>ttn-vegapulsair exporter based on data from LoRaWAN TTN node.</h1>', "utf-8")) self.wfile.write(
bytes(
"<h1>ttn-vegapulsair exporter based on data from LoRaWAN TTN node.</h1>",
"utf-8",
)
)
self.wfile.write(bytes('<p><a href="/metrics">Metrics</a></p>', "utf-8")) self.wfile.write(bytes('<p><a href="/metrics">Metrics</a></p>', "utf-8"))
self.wfile.write(bytes("</body>", "utf-8")) self.wfile.write(bytes("</body>", "utf-8"))
self.wfile.write(bytes("</html>", "utf-8")) self.wfile.write(bytes("</html>", "utf-8"))
@ -78,7 +109,9 @@ def update_metrics(payload, metadata):
if "Inclination_degree" in payload: if "Inclination_degree" in payload:
print("set Inclination_degree: " + str(int(payload["Inclination_degree"]))) print("set Inclination_degree: " + str(int(payload["Inclination_degree"])))
node_metrics.append("inclination_degree " + str(int(payload["Inclination_degree"]))) node_metrics.append(
"inclination_degree " + str(int(payload["Inclination_degree"]))
)
if "MvLinProcent" in payload: if "MvLinProcent" in payload:
print("set MvLinProcent: " + str(int(payload["MvLinProcent"]))) print("set MvLinProcent: " + str(int(payload["MvLinProcent"])))
@ -98,7 +131,9 @@ def update_metrics(payload, metadata):
if "PacketIdentifier" in payload: if "PacketIdentifier" in payload:
print("set PacketIdentifier: " + str(int(payload["PacketIdentifier"]))) print("set PacketIdentifier: " + str(int(payload["PacketIdentifier"])))
node_metrics.append("packet_identifier " + str(int(payload["PacketIdentifier"]))) node_metrics.append(
"packet_identifier " + str(int(payload["PacketIdentifier"]))
)
if "RemainingPower" in payload: if "RemainingPower" in payload:
print("set RemainingPower: " + str(int(payload["RemainingPower"]))) print("set RemainingPower: " + str(int(payload["RemainingPower"])))
@ -163,8 +198,9 @@ def on_disconnect(client, userdata, flags, reason_code, properties):
client.reconnect() client.reconnect()
def main(): def main():
while True:
try:
print("starting ...") print("starting ...")
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) 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
@ -172,7 +208,9 @@ def main():
mqttc.on_disconnect = on_disconnect 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
)
mqttc.subscribe("#", 0) # all device uplinks mqttc.subscribe("#", 0) # all device uplinks
# run mqtt in thread forever # run mqtt in thread forever
@ -190,6 +228,9 @@ def main():
webServer.server_close() webServer.server_close()
print("Server stopped.") print("Server stopped.")
poll_mqtt_thread.join() poll_mqtt_thread.join()
except Exception as e:
print(e)
time.sleep(60)
if __name__ == "__main__": if __name__ == "__main__":
main() main()