[AETHER-3071] Export num configured and reachable devices to Prometheus
Change-Id: I8cf178e6596460433f3b82c85e3d311e6f96f032
diff --git a/app.py b/app.py
index cc85ce4..3adb9dd 100644
--- a/app.py
+++ b/app.py
@@ -8,7 +8,6 @@
import time
from flask import Flask, request
-from flask_restful import Resource, Api
import logging as log
from argparse import ArgumentParser, SUPPRESS
import threading
@@ -19,40 +18,39 @@
import device
app = Flask(__name__)
-api = Api(app)
devices = {} # dict imsi:device
lock = threading.Lock()
-class Devices(Resource):
- def get(self):
- global devices, lock
- with lock:
- all = {}
- for _, device in devices.items():
- all[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
- return all
+@app.route("/devices")
+def get_devices():
+ global devices, lock
+ with lock:
+ all = {}
+ for _, device in devices.items():
+ all[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
+ return all
-class ReachableDevices(Resource):
- def get(self):
- global devices, lock
- with lock:
- reachable = {}
- for _, device in devices.items():
- if device.reachable is True:
- reachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
- return reachable
+@app.route("/devices/reachable")
+def get_devices_reachable():
+ global devices, lock
+ with lock:
+ reachable = {}
+ for _, device in devices.items():
+ if device.reachable is True:
+ reachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
+ return reachable
-class UnreachableDevices(Resource):
- def get(self):
- global devices, lock
- with lock:
- unreachable = {}
- for _, device in devices.items():
- if device.reachable is False:
- unreachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
- return unreachable
+@app.route("/devices/unreachable")
+def get_devices_unreachable():
+ global devices, lock
+ with lock:
+ unreachable = {}
+ for _, device in devices.items():
+ if device.reachable is False:
+ unreachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
+ return unreachable
@@ -116,10 +114,6 @@
datefmt='%Y-%m-%d %H:%M:%S',
stream=sys.stdout)
- api.add_resource(Devices, '/devices')
- api.add_resource(ReachableDevices, '/devices/reachable')
- api.add_resource(UnreachableDevices, '/devices/unreachable')
-
log.info("Starting network-diag-app...")
args = build_argparser().parse_args()
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()
diff --git a/requirements.txt b/requirements.txt
index c51d4df..06b8f1c 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -10,6 +10,7 @@
itsdangerous==2.0.1
Jinja2==3.0.3
MarkupSafe==2.0.1
+prometheus-client==0.13.1
pyaml==21.10.1
pytz==2021.3
PyYAML==6.0