blob: ee8ce886b026536bac16234cef6510cec4de2665 [file] [log] [blame]
"""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_reachable_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.set(len(json.loads(resp.text)))
resp = requests.get(url=f"http://localhost:{self.app_port}/devices/reachable")
self.num_reachable_devices.set(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()