blob: ee8ce886b026536bac16234cef6510cec4de2665 [file] [log] [blame]
Shad Ansari467862f2022-02-08 00:40:40 +00001"""network-diag-app exporter"""
2
3import os
4import time
5from prometheus_client import start_http_server, Gauge
6import requests
7import json
8
9class Metrics:
10 def __init__(self, app_port=3333, polling_interval_seconds=5):
11 self.app_port = app_port
12 self.polling_interval_seconds = polling_interval_seconds
13
14 self.num_configured_devices = Gauge("num_configured_devices", "Number of configured devices")
Shad Ansari6292c452022-02-08 17:39:06 +000015 self.num_reachable_devices = Gauge("num_reachable_devices", "Number of reachable devices")
Shad Ansari467862f2022-02-08 00:40:40 +000016
17 def run_metrics_loop(self):
18 while True:
19 self.fetch()
20 time.sleep(self.polling_interval_seconds)
21
22 def fetch(self):
23 resp = requests.get(url=f"http://localhost:{self.app_port}/devices")
Shad Ansari6292c452022-02-08 17:39:06 +000024 self.num_configured_devices.set(len(json.loads(resp.text)))
Shad Ansari467862f2022-02-08 00:40:40 +000025
26 resp = requests.get(url=f"http://localhost:{self.app_port}/devices/reachable")
Shad Ansari6292c452022-02-08 17:39:06 +000027 self.num_reachable_devices.set(len(json.loads(resp.text)))
Shad Ansari467862f2022-02-08 00:40:40 +000028
29def main():
30 polling_interval_seconds = int(os.getenv("POLLING_INTERVAL_SECONDS", "5"))
31 app_port = int(os.getenv("APP_PORT", "3333"))
32 exporter_port = int(os.getenv("EXPORTER_PORT", "4444"))
33
34 app_metrics = Metrics(
35 app_port=app_port,
36 polling_interval_seconds=polling_interval_seconds
37 )
38 start_http_server(exporter_port)
39 app_metrics.run_metrics_loop()
40
41if __name__ == "__main__":
42 main()