alertify/alertify.py

88 lines
2.5 KiB
Python
Raw Permalink Normal View History

2020-10-24 20:43:19 +01:00
#!/usr/bin/python3
"""
Main entrypoint to run Alertify
"""
import argparse
import logging
import os
import sys
2020-11-07 17:31:31 +00:00
from src import Alertify
2020-10-24 20:43:19 +01:00
if __name__ == '__main__':
2021-05-25 10:06:09 +01:00
def parse_cli() -> argparse.ArgumentParser:
2020-10-24 20:43:19 +01:00
"""
Function to parse the CLI
"""
2020-11-07 17:31:31 +00:00
maxlen = max([len(key) for key in Alertify.Config.defaults()])
2020-10-24 20:43:19 +01:00
defaults = [
f' * {key.upper().ljust(maxlen)} (default: {val if val != "" else "None"})'
2020-11-07 17:31:31 +00:00
for key, val in Alertify.Config.defaults().items()
2020-10-24 20:43:19 +01:00
]
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description='Bridge between Prometheus Alertmanager and Gotify\n',
epilog='The following environment variables will override any config or default:\n'
+ '\n'.join(defaults),
)
parser.add_argument(
'-c',
'--config',
default=f'{os.path.splitext(__file__)[0]}.yaml',
help=f'path to config YAML. (default: {os.path.splitext(__file__)[0]}.yaml)',
)
parser.add_argument(
'-H',
'--healthcheck',
action='store_true',
help='simply exit with 0 for healthy or 1 when unhealthy',
)
return parser.parse_args()
2021-05-25 10:06:09 +01:00
def main() -> int:
2020-10-24 20:43:19 +01:00
"""
2021-05-25 10:06:09 +01:00
Main program logic
2020-10-24 20:43:19 +01:00
"""
logging.basicConfig(
format='%(levelname)s: %(message)s',
level=logging.INFO,
)
args = parse_cli()
2020-11-07 17:31:31 +00:00
alertify = Alertify.Alertify()
alertify.configure(args.config)
2020-10-24 20:43:19 +01:00
# -----------------------------
# Calculate logging level
# -----------------------------
# Config :: Verbose: 0 = WARNING, 1 = INFO, 2 = DEBUG
# Logging :: Loglevel: 30 = WARNING, 20 = INFO, 10 = DEBUG
logger = logging.getLogger()
2020-11-07 17:31:31 +00:00
logger.setLevel(max(logging.WARNING - (alertify.config.verbose * 10), 10))
# -----------------------------
2020-10-24 20:43:19 +01:00
if args.healthcheck:
2020-11-07 17:31:31 +00:00
_, status = alertify.healthcheck()
2021-05-26 23:08:43 +01:00
# Invert the sense of 'healthy' for Unix CLI usage
return not status == 200
2020-10-24 20:43:19 +01:00
2020-11-07 17:31:31 +00:00
logging.info('Version: %s', Alertify.__version__)
2020-10-25 09:12:23 +00:00
2020-11-07 17:31:31 +00:00
if alertify.config.verbose:
2020-10-24 20:43:19 +01:00
logging.debug('Parsed config:')
2020-11-07 17:31:31 +00:00
for key, val in alertify.config.items():
2020-10-24 20:43:19 +01:00
logging.debug('%s: %s', key, val)
2020-11-07 17:31:31 +00:00
alertify.run()
2020-10-24 20:43:19 +01:00
return 0
sys.exit(main())