Compare commits

..

2 Commits

Author SHA1 Message Date
cb758d9bc9 Add option for custom listen port for prometheus exporter 2025-09-08 22:50:12 +02:00
9d57dab5c9 Add option to pass password as file 2025-09-08 22:46:47 +02:00
2 changed files with 14 additions and 4 deletions

View File

@@ -2,12 +2,14 @@ from . import Mu5001Tool
from .prometheus_exporter import prometheus_exporter from .prometheus_exporter import prometheus_exporter
import argparse import argparse
from pathlib import Path
from pprint import pprint from pprint import pprint
parser = argparse.ArgumentParser(prog="mu5001tool") parser = argparse.ArgumentParser(prog="mu5001tool")
parser.add_argument("--stok", dest="stok", help="Initial session token to use for commands") parser.add_argument("--stok", dest="stok", help="Initial session token to use for commands")
parser.add_argument("--password", dest="password", help="Password for authentication against the device") parser.add_argument("--password", dest="password", help="Password for authentication against the device")
parser.add_argument("--password-file", dest="password_file", type=Path, help="Password for authentication against the device, passed as path to file")
subparsers = parser.add_subparsers() subparsers = parser.add_subparsers()
@@ -28,11 +30,12 @@ def run_status(m):
sp_status = subparsers.add_parser("status", help="General modem status information") sp_status = subparsers.add_parser("status", help="General modem status information")
sp_status.set_defaults(func=run_status) sp_status.set_defaults(func=run_status)
def run_prometheus_exporter(m): def run_prometheus_exporter(m, listen_port):
prometheus_exporter(m) prometheus_exporter(m, listen_port)
sp_prometheus_exporter = subparsers.add_parser("prometheus-exporter", help="Serve metrics as prometheus exporter") sp_prometheus_exporter = subparsers.add_parser("prometheus-exporter", help="Serve metrics as prometheus exporter")
sp_prometheus_exporter.set_defaults(func=run_prometheus_exporter) sp_prometheus_exporter.set_defaults(func=run_prometheus_exporter)
sp_prometheus_exporter.add_argument("--listen-port", dest="listen_port", type=int, default=9242, help="Port for service webserver")
def main(): def main():
@@ -47,6 +50,7 @@ def main():
function_arguments.pop("func") function_arguments.pop("func")
function_arguments.pop("stok") function_arguments.pop("stok")
function_arguments.pop("password") function_arguments.pop("password")
function_arguments.pop("password_file")
m = Mu5001Tool() m = Mu5001Tool()
@@ -55,6 +59,12 @@ def main():
if args.password is not None: if args.password is not None:
m.set_password(args.password) m.set_password(args.password)
if args.password_file is not None:
password = args.password_file.read_text()
m.set_password(password)
if args.password is not None or args.password_file is not None:
m.login() m.login()
args.func(m=m, **function_arguments) args.func(m=m, **function_arguments)

View File

@@ -149,7 +149,7 @@ def make_prometheus_exporter_request_handler(m):
return PrometheusExporterRequestHandler return PrometheusExporterRequestHandler
def prometheus_exporter(m): def prometheus_exporter(m, listen_port):
with HTTPServerV6(("::1", 9242), make_prometheus_exporter_request_handler(m)) as httpd: with HTTPServerV6(("::1", listen_port), make_prometheus_exporter_request_handler(m)) as httpd:
print("Starting prometheus exporter on http://[{}]:{}".format(*httpd.socket.getsockname()[:2])) print("Starting prometheus exporter on http://[{}]:{}".format(*httpd.socket.getsockname()[:2]))
httpd.serve_forever() httpd.serve_forever()