#!/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 sys

import lib.args
import lib.base
import lib.human
import lib.url
from lib.globals import STATE_CRIT, STATE_OK, STATE_UNKNOWN, STATE_WARN

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

DESCRIPTION = """Retrieves common analytics values from a Matomo instance, including visits, unique
visitors, bounce rate, and actions. Supports one or multiple websites and any date
range or period."""

DEFAULT_URL = 'https://demo.matomo.org'
DEFAULT_IDSITE = 1
DEFAULT_PERIOD = 'day'
DEFAULT_DATE = 'today'
# Matomo's "anonymous" API default user password, not a real secret.
DEFAULT_PASSWORD = 'anonymous'  # nosec B105

DEFAULT_METRIC = []

DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_TIMEOUT = 3


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(
        '--date',
        help='Matomo REST API date parameter. '
        'Example: `last10` or `today`. '
        'Default: %(default)s',
        dest='DATE',
        default=DEFAULT_DATE,
    )

    parser.add_argument(
        '--idsite',
        help='Matomo REST API idSite parameter. '
        'Example: `1`, `1,4,5` or `all`. '
        'Default: %(default)s',
        dest='IDSITE',
        default=DEFAULT_IDSITE,
    )

    parser.add_argument(
        '--insecure',
        help=lib.args.help('--insecure'),
        dest='INSECURE',
        action='store_true',
        default=DEFAULT_INSECURE,
    )

    parser.add_argument(
        '--metric',
        help='Filter the output and optionally check against thresholds or ranges. '
        'Format: `metric_name[,warn_range][,crit_range]`. '
        'Can be specified multiple times. '
        'Example: `--metric nb_visits,100:,50:`. '
        'Default: %(default)s',
        dest='METRIC',
        type=lib.args.csv,
        action='append',
        default=None,
    )

    parser.add_argument(
        '--no-proxy',
        help=lib.args.help('--no-proxy'),
        dest='NO_PROXY',
        action='store_true',
        default=DEFAULT_NO_PROXY,
    )

    parser.add_argument(
        '--password',
        help='Matomo REST API access token. Default: %(default)s',
        dest='PASSWORD',
        default=DEFAULT_PASSWORD,
    )

    parser.add_argument(
        '--period',
        help='Matomo REST API period parameter. '
        'Example: `range` or `day`. '
        'Default: %(default)s',
        dest='PERIOD',
        default=DEFAULT_PERIOD,
    )

    parser.add_argument(
        '--timeout',
        help=lib.args.help('--timeout') + ' Default: %(default)s (seconds)',
        dest='TIMEOUT',
        type=int,
        default=DEFAULT_TIMEOUT,
    )

    parser.add_argument(
        '-u',
        '--url',
        help='Matomo URL. Default: %(default)s',
        dest='URL',
        default=DEFAULT_URL,
    )

    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)

    # set default values for append parameters that were not specified
    if args.METRIC is None:
        args.METRIC = DEFAULT_METRIC

    # fetch data
    url = (
        args.URL
        + ('/' if not args.URL.endswith('/') else '')
        + f'index.php?method=API.get&idSite={args.IDSITE}&period={args.PERIOD}&date={args.DATE}&module=API&token_auth={args.PASSWORD}&format=json&filter_limit=1'
    )
    result = lib.base.coe(
        lib.url.fetch_json(
            url, insecure=args.INSECURE, no_proxy=args.NO_PROXY, timeout=args.TIMEOUT
        )
    )
    if 'result' in result and result['result'] == 'error':
        lib.base.cu(result['message'])

    # init some vars
    msg = ''
    state = STATE_OK
    perfdata = ''

    if args.METRIC:
        # just print and check some of all those possible values
        for metric in args.METRIC:
            key = metric[0]
            try:
                value = result[key]
            except Exception:
                continue
            try:
                warn = metric[1]
            except Exception:
                warn = None
            try:
                crit = metric[2]
            except Exception:
                crit = None

            if not lib.base.coe(lib.base.match_range(value, crit)):
                sensor_state = STATE_CRIT
            elif not lib.base.coe(lib.base.match_range(value, warn)):
                sensor_state = STATE_WARN
            else:
                sensor_state = STATE_OK


            # build the message
            msg += f'{key}: {lib.human.number2human(value)}{lib.base.state2str(sensor_state, prefix=" (", suffix=")")}, '
            state = lib.base.get_worst(state, sensor_state)
            if 'time' in key:
                perfdata += lib.base.get_perfdata(
                    key,
                    value,
                    uom='s',
                    _min=0,
                )
            elif 'percent' in key or 'rate' in key:
                perfdata += lib.base.get_perfdata(
                    key,
                    value,
                    uom='%',
                    _min=0,
                    _max=100,
                )
            else:
                perfdata += lib.base.get_perfdata(
                    key,
                    value,
                    _min=0,
                )
    else:
        for key, value in lib.base.sort(result, reverse=False, sort_by_key=True):
            # print all values
            msg += f'{key}: {lib.human.number2human(value)}, '
            perfdata += lib.base.get_perfdata(
                key,
                value,
                uom='s' if 'time' in key else None,
                _min=0,
            )

    # over and out
    lib.base.oao(f'{msg[:-2]}', state, perfdata, always_ok=args.ALWAYS_OK)


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