[AETHER-3071] Export num configured and reachable devices to Prometheus
Change-Id: I8cf178e6596460433f3b82c85e3d311e6f96f032
diff --git a/exporter.py b/exporter.py
new file mode 100644
index 0000000..bb8c2d0
--- /dev/null
+++ b/exporter.py
@@ -0,0 +1,42 @@
+"""network-diag-app exporter"""
+
+import os
+import time
+from prometheus_client import start_http_server, Gauge
+import requests
+import json
+
+class Metrics:
+ def __init__(self, app_port=3333, polling_interval_seconds=5):
+ self.app_port = app_port
+ self.polling_interval_seconds = polling_interval_seconds
+
+ self.num_configured_devices = Gauge("num_configured_devices", "Number of configured devices")
+ self.num_configured_devices = Gauge("num_reachable_devices", "Number of reachable devices")
+
+ def run_metrics_loop(self):
+ while True:
+ self.fetch()
+ time.sleep(self.polling_interval_seconds)
+
+ def fetch(self):
+ resp = requests.get(url=f"http://localhost:{self.app_port}/devices")
+ self.num_configured_devices = len(json.loads(resp.text))
+
+ resp = requests.get(url=f"http://localhost:{self.app_port}/devices/reachable")
+ self.num_configured_devices = len(json.loads(resp.text))
+
+def main():
+ polling_interval_seconds = int(os.getenv("POLLING_INTERVAL_SECONDS", "5"))
+ app_port = int(os.getenv("APP_PORT", "3333"))
+ exporter_port = int(os.getenv("EXPORTER_PORT", "4444"))
+
+ app_metrics = Metrics(
+ app_port=app_port,
+ polling_interval_seconds=polling_interval_seconds
+ )
+ start_http_server(exporter_port)
+ app_metrics.run_metrics_loop()
+
+if __name__ == "__main__":
+ main()