Files
mu5001tool/mu5001tool/__main__.py
2025-09-11 22:41:05 +02:00

74 lines
2.1 KiB
Python

from . import Mu5001Tool
from .prometheus_exporter import prometheus_exporter
import argparse
from pathlib import Path
from pprint import pprint
parser = argparse.ArgumentParser(prog="mu5001tool")
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-file", dest="password_file", type=Path, help="Password for authentication against the device, passed as path to file")
subparsers = parser.add_subparsers()
def run_status(m):
if m.is_logged_in():
print("Is logged in")
else:
print("Is not logged in")
pprint(m.status())
pprint(m.network_information())
pprint(m.apn_info())
pprint(m.state_information())
sp_status = subparsers.add_parser("status", help="General modem status information")
sp_status.set_defaults(func=run_status)
def run_prometheus_exporter(m, listen_port):
prometheus_exporter(m, listen_port)
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.add_argument("--listen-port", dest="listen_port", type=int, default=9242, help="Port for service webserver")
def main():
args = parser.parse_args()
if "func" not in args:
parser.print_help()
exit()
function_arguments = dict(vars(args))
function_arguments.pop("func")
function_arguments.pop("stok")
function_arguments.pop("password")
function_arguments.pop("password_file")
m = Mu5001Tool()
if args.stok is not None:
m.set_stok(args.stok)
if args.password is not None:
m.set_password(args.password)
if args.password_file is not None:
password = args.password_file.read_text().strip()
m.set_password(password)
if args.password is not None or args.password_file is not None:
m.login()
args.func(m=m, **function_arguments)
if __name__ == "__main__":
main()