#!/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  # pylint: disable=C0413
import sys  # pylint: disable=C0413

import lib.base  # pylint: disable=C0413
import lib.human  # pylint: disable=C0413
import lib.shell  # pylint: disable=C0413
import lib.time  # pylint: disable=C0413
import lib.txt  # pylint: disable=C0413
from lib.globals import (STATE_CRIT, STATE_OK,  # pylint: disable=C0413
                          STATE_UNKNOWN, STATE_WARN)

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

DESCRIPTION = """Checks the timespan since the last package manager activity,
                 for example due to a yum/dnf install/update."""

DEFAULT_WARN  = 90   # days
DEFAULT_CRIT  = 365  # days


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

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

    parser.add_argument(
        '-c', '--critical',
        default=DEFAULT_CRIT,
        dest='CRIT',
        help='Set the critical threshold (in days). Default: %(default)s',
        type=int,
    )

    parser.add_argument(
        '-w', '--warning',
        default=DEFAULT_WARN,
        dest='WARN',
        help='Set the warning threshold (in days). Default: %(default)s',
        type=int,
    )

    return parser.parse_args()


def main():
    """The main function. Hier spielt die Musik.
    """

    # parse the command line, exit with UNKNOWN if it fails
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

    # execute the shell command and return its result and exit code
    stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(
        'rpm --query --all --queryformat "%{INSTALLTIME} %{NAME}\n"'
    ))
    if stderr:
        lib.base.cu(stderr)

    # stdout: '1662130946 unbound-libs\n1662130898 NetworkManager-libnm\n'
    try:
        last_activity = lib.txt.mltext2array(stdout, skip_header=False, sort_key=0)
        last_activity = int(last_activity[-1][0])
    except:
        lib.base.cu('Unable to get rpm info, maybe an update never happened.')

    # calculating the final check state
    delta = lib.time.now() - last_activity
    state = lib.base.get_state(delta, args.WARN * 24 * 60 * 60, args.CRIT * 24 * 60 * 60)

    # over and out
    lib.base.oao(
        'Last package manager activity is {} ago{} (thresholds {}D/{}D).'.format(
            lib.human.seconds2human(delta),
            lib.base.state2str(state, prefix=' '),
            args.WARN,
            args.CRIT,
        ),
        state,
    )


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