#!/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 json  # 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 = """Check the age of the newest restic repository snapshot."""

DEFAULT_CRIT = None
DEFAULT_GROUP_BY = 'host,paths'
DEFAULT_LATEST = 3  # count
DEFAULT_LENGTHY = False
DEFAULT_WARN = 24   # hours


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

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

    parser.add_argument(
        '-c', '--critical',
        help='Set the critical threshold for the time difference to the start of the last backup '
             '(in each group) (in hours). Default: %(default)s',
        dest='CRIT',
        type=int,
        default=DEFAULT_CRIT,
    )

    parser.add_argument(
        '--group-by',
        help='String for grouping snapshots by host,paths,tags. Default: %(default)s',
        dest='GROUP_BY',
        default=DEFAULT_GROUP_BY,
    )

    parser.add_argument(
        '--host',
        help='Only consider snapshots for this host (can be specified multiple times).',
        dest='HOST',
        action='append',
        default=None,
    )

    parser.add_argument(
        '--latest',
        help='Only show the last n snapshots for each host and path. Default: %(default)s',
        dest='LATEST',
        type=int,
        default=DEFAULT_LATEST,
    )

    parser.add_argument(
        '--lengthy',
        help='Extended reporting.',
        dest='LENGTHY',
        action='store_true',
        default=DEFAULT_LENGTHY,
    )

    parser.add_argument(
        '--password-file',
        help='File to read the repository password from.',
        dest='PASSWORD_FILE',
    )

    parser.add_argument(
        '--path',
        help='Only consider snapshots for this path (can be specified multiple times).',
        dest='PATH',
        action='append',
        default=None,
    )

    parser.add_argument(
        '--repo',
        help='Repository location',
        dest='REPO',
        required=True,
    )

    parser.add_argument(
        '--tag',
        help='Only consider snapshots which include this taglist in the format `tag[,tag,...]` '
             '(can be specified multiple times).',
        dest='TAG',
        action='append',
        default=None,
    )

    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',
        help='Set the warning threshold for the time difference to the start of the last backup '
             '(in each group) (in hours). Default: %(default)s',
        dest='WARN',
        type=int,
        default=DEFAULT_WARN,
    )

    return parser.parse_args()


def get_snapshot_data(snapshot, args):
    """Enrich data returned from retic's snapshot command.
    """
    snapshot['ts'] = snapshot['time'][0:19].replace('T', ' ')   # 2022-11-28T16:22:43.896818692Z
    snapshot['age'] = lib.time.timestrdiff(lib.time.now('iso'), snapshot['ts'])
    snapshot['age_hr'] = lib.human.seconds2human(snapshot['age']) # human-readable
    snapshot['p'] = ' '.join(snapshot['paths'])
    snapshot['tags'] = ','.join(snapshot['tags']) if 'tags' in snapshot.keys() else ''
    snapshot['state'] = lib.base.get_state(int(snapshot['age'] / 3600), args.WARN, args.CRIT)

    return snapshot


def get_message(table_data, args):
    """Build the message. Show latest/newest snapshot on top, which is the last one in the list.
    """
    msg = '\n\nLatest snapshot {} ago{} ({}@{}:{}, ID {}); {} {} found\n\n'.format(
        table_data[-1]['age_hr'],
        lib.base.state2str(table_data[-1]['state'], prefix=' '),
        table_data[-1]['ts'],
        table_data[-1]['hostname'],
        table_data[-1]['p'],
        table_data[-1]['short_id'],
        len(table_data),
        lib.txt.pluralize('snapshot', len(table_data)),
    )
    table_data[-1]['age_hr'] += lib.base.state2str(table_data[-1]['state'], prefix=' ')
    if table_data:
        if not args.LENGTHY:
            keys = [
                'short_id',
                'ts',
                'age_hr',
                'hostname',
                'p',
                #'username',
            ]
            headers = [
                'Short ID',
                'Timestamp',
                'Age',
                'Host',
                'Paths',
                #'User',
            ]
        else:
            keys = [
                'short_id',
                'ts',
                'age_hr',
                'hostname',
                'p',
                'tags',
                #'username',
            ]
            headers = [
                'Short ID',
                'Timestamp',
                'Age',
                'Host',
                'Paths',
                'Tags',
                #'User',
            ]
        msg += lib.base.get_table(
            table_data,
            keys,
            header=headers,
            sort_by_key='ts',
            sort_order_reverse=True,
        )

    return msg, table_data[-1]['state']


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:
        cmd = 'restic --json --repo={} --password-file={} --latest={}'.format(
            args.REPO,
            args.PASSWORD_FILE,
            args.LATEST,
        )
        if args.HOST:
            cmd += ' --host=' + ' --host='.join(args.HOST)
        if args.PATH:
            cmd += ' --path=' + ' --path='.join(args.PATH)
        if args.TAG:
            cmd += ' --tag=' + ' --tag='.join(args.TAG)
        if args.GROUP_BY:
            cmd += ' --group-by={}'.format(args.GROUP_BY)
        cmd += ' snapshots'
        stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(cmd)) # pylint: disable=W0612
        if stderr:
            lib.base.cu(stderr)
    else:
        # do not call the command, put in test data
        stdout, stderr, retc = lib.lftest.test(args.TEST)

    try:
        snapshots = json.loads(stdout)
    except ValueError:
        lib.base.cu('No JSON object could be decoded.')

    # init some vars
    msg = ''
    state = STATE_OK
    perfdata = ''
    table_data = []

    if not snapshots:
        lib.base.oao(
            'No snapshots match your filter criteria. Called `{}`'.format(cmd),
            STATE_UNKNOWN,
        )

    # analyze data and build the message
    snapshot_count = 0
    if 'group_key' in snapshots[0]:
        # --group-by was used, so check every group of snapshots
        for snapshot in snapshots:
            table_data = []
            for s in snapshot['snapshots']:
                table_data.append(get_snapshot_data(s, args))
            snapshot_msg, snapshot_state = get_message(table_data, args)
            snapshot_count += len(table_data)
            msg += snapshot_msg
            state = lib.base.get_worst(snapshot_state, state)
    else:
        for snapshot in snapshots:
            table_data.append(get_snapshot_data(snapshot, args))
        msg, state = get_message(table_data, args)
        snapshot_count = len(table_data)

    perfdata += lib.base.get_perfdata('snapshots', snapshot_count, '', None, None, 0, None)

    if state == STATE_CRIT:
        msg = 'There are critical errors.' + msg
    elif state == STATE_WARN:
        msg = 'There are warnings.' + msg
    else:
        msg = 'Everything is ok.' + msg

    # over and out
    lib.base.oao(msg, state, perfdata)


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