From 754de72714ceb07762e036cca6155887794f7236 Mon Sep 17 00:00:00 2001 From: Daniel Neto Date: Mon, 12 Aug 2024 21:14:01 -0300 Subject: [PATCH] Making host_exporter a Flask app that runs via mod_wsgi --- .../apache-perfsonar_host_exporter.conf | 29 +++++++-------- .../perfsonar-host-metrics/host_exporter.wsgi | 8 ++++ .../perfsonar-host-exporter.service | 12 ------ ...st_exporter => perfsonar_host_exporter.py} | 37 ++++++++----------- 4 files changed, 37 insertions(+), 49 deletions(-) create mode 100644 perfsonar-host-metrics/perfsonar-host-metrics/host_exporter.wsgi delete mode 100644 perfsonar-host-metrics/perfsonar-host-metrics/perfsonar-host-exporter.service rename perfsonar-host-metrics/perfsonar-host-metrics/{perfsonar_host_exporter => perfsonar_host_exporter.py} (91%) diff --git a/perfsonar-host-metrics/perfsonar-host-metrics/apache-perfsonar_host_exporter.conf b/perfsonar-host-metrics/perfsonar-host-metrics/apache-perfsonar_host_exporter.conf index cb57e29..75f6134 100644 --- a/perfsonar-host-metrics/perfsonar-host-metrics/apache-perfsonar_host_exporter.conf +++ b/perfsonar-host-metrics/perfsonar-host-metrics/apache-perfsonar_host_exporter.conf @@ -1,16 +1,15 @@ - - ProxyRequests Off - - = 2.4> - Require all granted - - - Order deny,allow - Allow from all - - +# WSGI settings +WSGIDaemonProcess host_exporter display-name=host_exporter user=perfsonar group=perfsonar threads=5 +WSGIScriptAlias /perfsonar_host_exporter /var/www/html/perfsonar/host_exporter/host_exporter.wsgi +WSGIApplicationGroup %{GLOBAL} - ProxyPass /perfsonar_host_exporter http://localhost:11284 status=+I - ProxyPassReverse /perfsonar_host_exporter http://localhost:11284 status=+I - ProxyPreserveHost On - + + + SSLRequireSSL + + WSGIProcessGroup host_exporter + WSGIApplicationGroup host_exporter + + Require all granted + + diff --git a/perfsonar-host-metrics/perfsonar-host-metrics/host_exporter.wsgi b/perfsonar-host-metrics/perfsonar-host-metrics/host_exporter.wsgi new file mode 100644 index 0000000..25b2697 --- /dev/null +++ b/perfsonar-host-metrics/perfsonar-host-metrics/host_exporter.wsgi @@ -0,0 +1,8 @@ +# +# WSGI File for host_exporter +# +import sys + +sys.path.insert(0, '/var/www/html/perfsonar/host_exporter') + +from host_exporter.perfsonar_host_exporter import app as application diff --git a/perfsonar-host-metrics/perfsonar-host-metrics/perfsonar-host-exporter.service b/perfsonar-host-metrics/perfsonar-host-metrics/perfsonar-host-exporter.service deleted file mode 100644 index 3b14580..0000000 --- a/perfsonar-host-metrics/perfsonar-host-metrics/perfsonar-host-exporter.service +++ /dev/null @@ -1,12 +0,0 @@ -[Unit] -Description=perfSONAR Host Exporter -After=network.target - -[Service] -Type=simple -ExecStart=/usr/lib/perfsonar/host_metrics/perfsonar_host_exporter -Restart=always -RestartSec=30s - -[Install] -WantedBy=multi-user.target \ No newline at end of file diff --git a/perfsonar-host-metrics/perfsonar-host-metrics/perfsonar_host_exporter b/perfsonar-host-metrics/perfsonar-host-metrics/perfsonar_host_exporter.py similarity index 91% rename from perfsonar-host-metrics/perfsonar-host-metrics/perfsonar_host_exporter rename to perfsonar-host-metrics/perfsonar-host-metrics/perfsonar_host_exporter.py index 4f2de32..2f637f9 100644 --- a/perfsonar-host-metrics/perfsonar-host-metrics/perfsonar_host_exporter +++ b/perfsonar-host-metrics/perfsonar-host-metrics/perfsonar_host_exporter.py @@ -6,9 +6,11 @@ import subprocess from psconfig.utilities.metrics import PSConfigMetricCalculator from psconfig.utilities.cli import PSCONFIG_CLI_AGENTS -from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from flask import Flask, Response -class PSMetricsWebHandler(BaseHTTPRequestHandler): +app = Flask(__name__) + +class PSMetricsWebHandler: LS_BASE_URL = "http://35.223.142.206:8090/lookup/records" def _read_one_liner(self, filename): @@ -111,31 +113,24 @@ def lookup_svc_metrics(self): ps_metric_output += 'perfsonar_host_registered{{uuid="{}"}} {}\n'.format(uuid, self._is_registered(uuid)) return ps_metric_output - - def do_GET(self): + + def gather_metrics(self): ps_metric_output = "" - - ## # toolkit/bundle metrics - ps_metric_output += self.bundle_metrics() - - ## + ps_metric_output += self.bundle_metrics() # pScheduler metrics ps_metric_output += self.pscheduler_metrics() - - ## # pSConfig Metrics ps_metric_output += self.psconfig_metrics() - - ## # LS client uuid ps_metric_output += self.lookup_svc_metrics() - - self.send_response(200) - self.send_header("Content-Type", "text/plain") - self.end_headers() - self.wfile.write(ps_metric_output.encode("utf-8")) + return ps_metric_output +@app.route("/") +def metrics(): + handler = PSMetricsWebHandler() + metrics_data = handler.gather_metrics() + return Response(metrics_data, status=200, mimetype="text/plain") if __name__ == "__main__": ## @@ -147,7 +142,5 @@ def do_GET(self): parser.add_argument('--host', dest='host', action='store', default='localhost', help='The host to listen for connections. 0.0.0.0 means all interfaces.') parser.add_argument('--port', dest='port', action='store', type=int, default=11284, help='The port on which to listen for connections.') args = parser.parse_args() - - #Build server - server = ThreadingHTTPServer((args.host, args.port), PSMetricsWebHandler) - server.serve_forever() \ No newline at end of file + + app.run(host=args.host, port=args.port) \ No newline at end of file