blob: 7c9d7de23be81e131afdadce4fc6c4a2d753769c [file] [log] [blame]
Shad Ansari467862f2022-02-08 00:40:40 +00001"""network-diag-app exporter"""
2
3import os
4import time
Shad Ansari0a905032022-02-10 19:37:15 +00005from prometheus_client import start_http_server, Gauge, REGISTRY, PROCESS_COLLECTOR, PLATFORM_COLLECTOR
Shad Ansari467862f2022-02-08 00:40:40 +00006import requests
7import json
8
Shad Ansari0a905032022-02-10 19:37:15 +00009REGISTRY.unregister(PROCESS_COLLECTOR)
10REGISTRY.unregister(PLATFORM_COLLECTOR)
11# Unlike process and platform_collector gc_collector registers itself as three different collectors that have no corresponding public named variable.
12REGISTRY.unregister(REGISTRY._names_to_collectors['python_gc_objects_collected_total'])
13#REGISTRY.unregister(REGISTRY._names_to_collectors['python_gc_uncollectable_objects_sum'])
14#REGISTRY.unregister(REGISTRY._names_to_collectors['python_gc_collected_objects_sum'])
15
Shad Ansari467862f2022-02-08 00:40:40 +000016class Metrics:
17 def __init__(self, app_port=3333, polling_interval_seconds=5):
18 self.app_port = app_port
19 self.polling_interval_seconds = polling_interval_seconds
20
21 self.num_configured_devices = Gauge("num_configured_devices", "Number of configured devices")
Shad Ansari6292c452022-02-08 17:39:06 +000022 self.num_reachable_devices = Gauge("num_reachable_devices", "Number of reachable devices")
Shad Ansari467862f2022-02-08 00:40:40 +000023
24 def run_metrics_loop(self):
25 while True:
26 self.fetch()
27 time.sleep(self.polling_interval_seconds)
28
29 def fetch(self):
30 resp = requests.get(url=f"http://localhost:{self.app_port}/devices")
Shad Ansari6292c452022-02-08 17:39:06 +000031 self.num_configured_devices.set(len(json.loads(resp.text)))
Shad Ansari467862f2022-02-08 00:40:40 +000032
33 resp = requests.get(url=f"http://localhost:{self.app_port}/devices/reachable")
Shad Ansari6292c452022-02-08 17:39:06 +000034 self.num_reachable_devices.set(len(json.loads(resp.text)))
Shad Ansari467862f2022-02-08 00:40:40 +000035
36def main():
37 polling_interval_seconds = int(os.getenv("POLLING_INTERVAL_SECONDS", "5"))
38 app_port = int(os.getenv("APP_PORT", "3333"))
39 exporter_port = int(os.getenv("EXPORTER_PORT", "4444"))
40
41 app_metrics = Metrics(
42 app_port=app_port,
43 polling_interval_seconds=polling_interval_seconds
44 )
45 start_http_server(exporter_port)
46 app_metrics.run_metrics_loop()
47
48if __name__ == "__main__":
49 main()