#!/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.args  # pylint: disable=C0413
import lib.base  # pylint: disable=C0413
import lib.human  # pylint: disable=C0413
import lib.shell  # pylint: disable=C0413
import lib.lftest  # 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__ = '2025021501'

DESCRIPTION = """Checks the timespan since the last package manager activity,
                 for example due to an apt 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(
        '--test',
        help='For unit tests. Needs "path-to-stdout-file,path-to-stderr-file,expected-retc".',
        dest='TEST',
        type=lib.args.csv,
    )

    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)

    # fetch data
    if args.TEST is None:
        # execute the shell command and return its result and exit code
        stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(
            "dpkg-query --show --showformat='${db-fsys:Last-Modified} ${Package}\n'"
        ))
        if stderr:
            lib.base.cu(stderr)
    else:
        # do not call the command, put in test data
        stdout, stderr, retc = lib.lftest.test(args.TEST)

    # stdout: 1680072089 adduser\n1680072121 apparmor\n1680072091 apt\n
    try:
        last_activity = lib.txt.mltext2array(stdout, skip_header=False, sort_key=0)
        last_activity = int(last_activity[-1][0])
    except ValueError:
        lib.base.oao(
            'Missing "Last-Modified" timestamp for one or more packages. Check the output of `dpkg-query --show --showformat=\'${db-fsys:Last-Modified} ${Package}\\n\'`. It is likely that some files have been deleted. The best thing to do is to reinstall these packages.', # pylint: disable=C0301
            STATE_WARN,
        )
    except:
        lib.base.cu('Unable to get dpkg-query 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()
