restart at failure
This commit is contained in:
		| @ -29,24 +29,48 @@ class RequestHandler(BaseHTTPRequestHandler): | ||||
|         self.send_response(200) | ||||
|         self.send_header("Content-type", "text/html") | ||||
|         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(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")) | ||||
|          | ||||
|         self.wfile.write( | ||||
|             bytes( | ||||
|                 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: | ||||
|             #print(metric) | ||||
|             # print(metric) | ||||
|             self.wfile.write(bytes(config.exporter_prefix + metric + "\n", "utf-8")) | ||||
|  | ||||
|         mutex.release() | ||||
|          | ||||
|  | ||||
|     def do_GET(self): | ||||
|         global request_count | ||||
|         global lastMqttReception | ||||
|         global scrape_healthy | ||||
|         request_count = request_count + 1 | ||||
|         #print("Request: " + self.path) | ||||
|         if (self.path.startswith("/metrics")):  | ||||
|             #if (datetime.now() - lastMqttReception) > timedelta(hours=7): | ||||
|         # print("Request: " + self.path) | ||||
|         if self.path.startswith("/metrics"): | ||||
|             # if (datetime.now() - lastMqttReception) > timedelta(hours=7): | ||||
|             #   scrape_healthy = False | ||||
|             self.get_metrics() | ||||
|         else: | ||||
| @ -54,22 +78,29 @@ class RequestHandler(BaseHTTPRequestHandler): | ||||
|             self.send_header("Content-type", "text/html") | ||||
|             self.end_headers() | ||||
|             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('<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("</body>", "utf-8")) | ||||
|             self.wfile.write(bytes("</html>", "utf-8")) | ||||
|  | ||||
| def update_metrics(payload, metadata): | ||||
|     #print("Payload: "+ str(payload)) | ||||
|     #print("Metadata: "+ str(metadata)) | ||||
|     # print("Payload: "+ str(payload)) | ||||
|     # print("Metadata: "+ str(metadata)) | ||||
|  | ||||
|     global node_metrics | ||||
|     global mutex | ||||
|     global scrape_healthy | ||||
|     mutex.acquire() | ||||
|     #scrape_healthy = False | ||||
|     # scrape_healthy = False | ||||
|     node_metrics.clear() | ||||
|  | ||||
|     if "Distance" in payload: | ||||
| @ -78,7 +109,9 @@ def update_metrics(payload, metadata): | ||||
|  | ||||
|     if "Inclination_degree" in payload: | ||||
|         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: | ||||
|         print("set MvLinProcent: " + str(int(payload["MvLinProcent"]))) | ||||
| @ -91,14 +124,16 @@ def update_metrics(payload, metadata): | ||||
|     if "MvScaled" in payload: | ||||
|         print("set MvScaled: " + str(float(payload["MvScaled"]))) | ||||
|         node_metrics.append("scaled " + str(float(payload["MvScaled"]))) | ||||
|      | ||||
|  | ||||
|     if "MvScaledUnit" in payload: | ||||
|         print("set MvScaledUnit: " + str(int(payload["MvScaledUnit"]))) | ||||
|         node_metrics.append("scaled_unit " + str(int(payload["MvScaledUnit"]))) | ||||
|  | ||||
|     if "PacketIdentifier" in payload: | ||||
|         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: | ||||
|         print("set RemainingPower: " + str(int(payload["RemainingPower"]))) | ||||
| @ -138,11 +173,11 @@ def on_connect(client, userdata, flags, reason_code, properties): | ||||
|         print("\nNot connected to MQTT: reason_code = " + str(reason_code)) | ||||
|  | ||||
| def on_message(mqttc, obj, msg): | ||||
|     #print("\nMessage: " + msg.topic + " " + str(msg.qos)) | ||||
|     # print("\nMessage: " + msg.topic + " " + str(msg.qos)) | ||||
|     parsedJSON = json.loads(msg.payload) | ||||
|     #print(json.dumps(parsedJSON, indent=4)) | ||||
|     # print(json.dumps(parsedJSON, indent=4)) | ||||
|  | ||||
|     try:  | ||||
|     try: | ||||
|         uplink_message = parsedJSON["uplink_message"] | ||||
|         update_metrics(uplink_message["decoded_payload"], uplink_message["rx_metadata"]) | ||||
|         lastMqttReception = datetime.now() | ||||
| @ -163,33 +198,39 @@ def on_disconnect(client, userdata, flags, reason_code, properties): | ||||
|     client.reconnect() | ||||
|  | ||||
| def main(): | ||||
|     print("starting ...") | ||||
|     while True: | ||||
|         try: | ||||
|             print("starting ...") | ||||
|             mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) | ||||
|             mqttc.on_connect = on_connect | ||||
|             mqttc.on_subscribe = on_subscribe | ||||
|             mqttc.on_message = on_message | ||||
|             mqttc.on_disconnect = on_disconnect | ||||
|             mqttc.username_pw_set(config.ttn_user, config.ttn_key) | ||||
|             mqttc.tls_set() | ||||
|             mqttc.connect( | ||||
|                 config.ttn_region.lower() + ".cloud.thethings.network", 8883, 60 | ||||
|             ) | ||||
|             mqttc.subscribe("#", 0)  # all device uplinks | ||||
|  | ||||
|     mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) | ||||
|     mqttc.on_connect = on_connect | ||||
|     mqttc.on_subscribe = on_subscribe | ||||
|     mqttc.on_message = on_message | ||||
|     mqttc.on_disconnect = on_disconnect | ||||
|     mqttc.username_pw_set(config.ttn_user, config.ttn_key) | ||||
|     mqttc.tls_set() | ||||
|     mqttc.connect(config.ttn_region.lower() + ".cloud.thethings.network", 8883, 60) | ||||
|     mqttc.subscribe("#", 0) # all device uplinks | ||||
|             # run mqtt in thread forever | ||||
|             poll_mqtt_thread = threading.Thread(target=poll_mqtt, args=((mqttc,))) | ||||
|             poll_mqtt_thread.start() | ||||
|  | ||||
|     # run mqtt in thread forever | ||||
|     poll_mqtt_thread = threading.Thread(target=poll_mqtt, args=((mqttc,))) | ||||
|     poll_mqtt_thread.start() | ||||
|             webServer = HTTPServer((config.hostName, config.serverPort), RequestHandler) | ||||
|             print("Server started http://%s:%s" % (config.hostName, config.serverPort)) | ||||
|  | ||||
|     webServer = HTTPServer((config.hostName, config.serverPort), RequestHandler) | ||||
|     print("Server started http://%s:%s" % (config.hostName, config.serverPort)) | ||||
|             try: | ||||
|                 webServer.serve_forever() | ||||
|             except KeyboardInterrupt: | ||||
|                 sys.exit(-1) | ||||
|  | ||||
|     try: | ||||
|         webServer.serve_forever() | ||||
|     except KeyboardInterrupt: | ||||
|             sys.exit(-1) | ||||
|  | ||||
|     webServer.server_close() | ||||
|     print("Server stopped.") | ||||
|     poll_mqtt_thread.join() | ||||
|             webServer.server_close() | ||||
|             print("Server stopped.") | ||||
|             poll_mqtt_thread.join() | ||||
|         except Exception as e: | ||||
|             print(e) | ||||
|         time.sleep(60) | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|         main() | ||||
|     main() | ||||
|  | ||||
		Reference in New Issue
	
	Block a user