diff --git a/meeting/monthly-report b/meeting/monthly-report index 696d556cbb874a09a284865a1bed82b3566d5dbb..e1a665b4a994573215db99cad2024a9ad95a8cf0 100755 --- a/meeting/monthly-report +++ b/meeting/monthly-report @@ -4,6 +4,7 @@ import argparse import json +import logging import os import re import subprocess @@ -44,6 +45,50 @@ not been implemented yet.''' # whipped up quickly for a monthly report. +class LoggingAction(argparse.Action): + """change log level on the fly + + The logging system should be initialized befure this, using + `basicConfig`. + + Example usage: + + parser.add_argument( + "-v", + "--verbose", + action=LoggingAction, + const="INFO", + help="enable verbose messages", + ) + parser.add_argument( + "-d", + "--debug", + action=LoggingAction, + const="DEBUG", + help="enable debugging messages", + ) + """ + + def __init__(self, *args, **kwargs): + """setup the action parameters + + This enforces a selection of logging levels. It also checks if + const is provided, in which case we assume it's an argument + like `--verbose` or `--debug` without an argument. + """ + kwargs["choices"] = logging._nameToLevel.keys() + if "const" in kwargs: + kwargs["nargs"] = 0 + super().__init__(*args, **kwargs) + + def __call__(self, parser, ns, values, option): + """if const was specified it means argument-less parameters""" + if self.const: + logging.getLogger("").setLevel(self.const) + else: + logging.getLogger("").setLevel(values) + + def parse_args(): parser = argparse.ArgumentParser(description=__doc__, epilog=__epilog__) parser.add_argument('--puppet', default="pauli.torproject.org", @@ -52,6 +97,20 @@ def parse_args(): help='LDAP server hostname, default %(default)s') parser.add_argument('--prometheus', default="https://{HTTP_USER}:{HTTP_PASS}@prometheus.torproject.org/api/v1", # noqa: E501 help='Prometheus API endpoint, default %(default)s') + parser.add_argument( + "-v", + "--verbose", + action=LoggingAction, + const="INFO", + help="enable verbose messages", + ) + parser.add_argument( + "-d", + "--debug", + action=LoggingAction, + const="DEBUG", + help="enable debugging messages", + ) args = parser.parse_args() args.prometheus = args.prometheus.format(HTTP_USER=os.environ.get('HTTP_USER', ''), # noqa: E501 HTTP_PASS=os.environ.get('HTTP_PASS', '')) # noqa: E501 @@ -169,6 +228,7 @@ def sizeof_fmt_decimal(num, suffix='B', sep=' ', precision=2, sign=False): def main(): + logging.basicConfig() args = parse_args() print(" * hosts in Puppet: %d, LDAP: %d, Prometheus exporters: %d" % (host_count_puppet(args),