#!/usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author:  Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
#          https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.

# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.md

"""See the check's README for more details."""

import argparse
import fnmatch
import sys

import lib.args
import lib.base
import lib.lftest
import lib.shell
import lib.txt
from lib.globals import STATE_OK, STATE_UNKNOWN, STATE_WARN

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2026040801'

DESCRIPTION = """Checks for failed systemd units. Alerts when any unit is in a failed state. Specific
units can be excluded from the check via --ignore with regular expressions."""

DEFAULT_IGNORE = []


def parse_args():
    """Parse command line arguments using argparse."""
    parser = argparse.ArgumentParser(description=DESCRIPTION)

    parser.add_argument(
        '-V',
        '--version',
        action='version',
        version=f'%(prog)s: v{__version__} by {__author__}',
    )

    parser.add_argument(
        '--always-ok',
        help=lib.args.help('--always-ok'),
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

    parser.add_argument(
        '--ignore',
        help='Unit name to exclude from the check. '
        'Can be specified multiple times. '
        'Supports glob patterns according to https://docs.python.org/3/library/fnmatch.html. '
        'Example: `--ignore "dhcpd.service"`. '
        'Default: %(default)s',
        dest='IGNORE',
        default=DEFAULT_IGNORE,
        action='append',
    )

    parser.add_argument(
        '--test',
        help=lib.args.help('--test'),
        dest='TEST',
        type=lib.args.csv,
    )

    args, _ = parser.parse_known_args()
    return args


def main():
    """The main function. This is where the magic happens."""

    # parse the command line
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)


    # fetch data
    if args.TEST is None:
        # get the values the normal way
        stdout, _stderr, retc = lib.base.coe(
            lib.shell.shell_exec('systemctl --state=failed --no-pager --no-legend')
        )
    else:
        # do not call the command, put in test data
        stdout, _stderr, retc = lib.lftest.test(args.TEST)

    if retc != 0:
        lib.base.cu('systemctl was unable to return any data.')

    msg = 'Everything is ok.'
    table = ''
    state = STATE_OK
    count = 0

    failed_units = stdout.splitlines()
    if len(failed_units) > 0:
        table_data = []
        offset = 0
        for line in failed_units:
            if line.startswith('*'):
                offset = 1
            unit = line.split()
            if any(
                fnmatch.fnmatchcase(unit[0 + offset], ignore) for ignore in args.IGNORE
            ):
                continue
            count += 1
            table_data.append(
                {
                    'unit': unit[0 + offset],
                    'load': unit[1 + offset],
                    'active': unit[2 + offset],
                    'sub': unit[3 + offset],
                    'description': ' '.join(unit[4 + offset :]),
                }
            )
        if count > 0:
            state = STATE_WARN
            unit_names = ', '.join(row['unit'] for row in table_data)
            msg = f'{count} failed {lib.txt.pluralize("unit", count)}: {unit_names}\n'
            table = lib.base.get_table(
                table_data,
                ['unit', 'load', 'active', 'sub', 'description'],
                ['unit', 'load', 'active', 'sub', 'description'],
            )

    perfdata = lib.base.get_perfdata(
        'systemd-units-failed',
        count,
        warn=1,
        _min=0,
    )

    # over and out
    lib.base.oao(f'{msg}\n\n{table}', state, perfdata, always_ok=args.ALWAYS_OK)


if __name__ == '__main__':
    try:
        main()
    except Exception:
        lib.base.cu()
