#!/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.lftest  # pylint: disable=C0413
import lib.txt  # pylint: disable=C0413
import lib.url  # pylint: disable=C0413
from lib.globals import (STATE_OK,  # pylint: disable=C0413
                          STATE_UNKNOWN, STATE_WARN)

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

DESCRIPTION = """Alerts on all monitors in down or unknown status on a given UptimeRobot status
                 page."""

DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_TIMEOUT  = 8
DEFAULT_URL = 'https://status.linuxfabrik.io'


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='Always returns OK.',
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

    parser.add_argument(
        '--insecure',
        help='This option explicitly allows to perform "insecure" SSL connections. '
             'Default: %(default)s',
        dest='INSECURE',
        action='store_true',
        default=DEFAULT_INSECURE,
    )

    parser.add_argument(
        '--no-proxy',
        help='Do not use a proxy. '
             'Default: %(default)s',
        dest='NO_PROXY',
        action='store_true',
        default=DEFAULT_NO_PROXY,
    )

    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(
        '--timeout',
        help='Network timeout in seconds. '
             'Default: %(default)s (seconds)',
        dest='TIMEOUT',
        type=int,
        default=DEFAULT_TIMEOUT,
    )

    parser.add_argument(
        '--url',
        help='UptimeRobot Status Page URL. '
             'Default: %(default)s',
        dest='URL',
        default=DEFAULT_URL,
    )

    return parser.parse_args()


def uptimerobotstate2state(utrs):
    """Convert UptimeRobot's incident level to the Nagios world.
    """
    if utrs == 'success':
        return STATE_OK
    if utrs == 'danger':
        return STATE_WARN
    # statusClass: black
    return STATE_UNKNOWN


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:
    # * in html, search for `window.pspApiPath = ...;`, and fetch the url found (returns json)
    #   e.g. `https://status.linuxfabrik.io/api/getMonitorList/yz2nGF77W2`
    if args.TEST is None:
        result = lib.base.coe(lib.url.fetch(
            args.URL,
            insecure=args.INSECURE,
            no_proxy=args.NO_PROXY,
            timeout=args.TIMEOUT,
        ))
        # search for
        # `window.pspApiPath = 'https://status.linuxfabrik.io/api/getMonitorList/yz2nGF77W2';`
        url = lib.txt.extract_str(
            result,
            "window.pspApiPath = 'https://", "';",
            include_fromto=False,
            be_tolerant=False,
        )
        if not url:
            lib.base.oao(f'Cannot parse html at {args.URL}', STATE_UNKNOWN)
        result = lib.base.coe(lib.url.fetch_json(
            f'https://{url}',
            insecure=args.INSECURE,
            no_proxy=args.NO_PROXY,
            timeout=args.TIMEOUT,
        ))
    else:
        # do not call the command, put in test data
        stdout, _, _ = lib.lftest.test(args.TEST)
        result = json.loads(stdout)

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

    # analyze data
    cnt_up = result['statistics']['counts']['up']
    cnt_down = result['statistics']['counts']['down']
    cnt_paused = result['statistics']['counts']['paused']
    if 'psp' in result:
        cnt_monitors = result['psp']['totalMonitors']
    else:
        cnt_monitors = cnt_up + cnt_down + cnt_paused
    if 'monitor' in result:
        # just one monitor
        monitors = [result['monitor']]
    else:
        # a list of monitors (even if it is one)
        monitors = result['psp']['monitors']
    for item in monitors:
        item_state = uptimerobotstate2state(item['statusClass'])
        state = lib.base.get_worst(state, item_state)
        table_data.append({
            'name': item['name'],
            'type': item['type'],
            'state_hr': lib.base.state2str(item_state, empty_ok=False),
        })

    # build the message
    msg += f'{cnt_down}/{cnt_paused}/{cnt_up} of {cnt_monitors} monitors are down/paused/up'
    msg += f'{lib.base.state2str(state, prefix=" ")}'
    if 'uptime' in result['statistics'] and result['statistics']['uptime']:
        msg += f', 24h uptime: {result["statistics"]["uptime"]["l1"]["ratio"]}%'
    if table_data:
        msg += '\n\n' + lib.base.get_table(
            table_data,
            ['name', 'type', 'state_hr'],
            header=['Name', 'Type', 'State'],
        )
    perfdata += lib.base.get_perfdata(
        'cnt_down',
        cnt_down,
        uom=None,
        warn=None,
        crit=None,
        _min=0,
        _max=cnt_monitors,
    )
    perfdata += lib.base.get_perfdata(
        'cnt_paused',
        cnt_paused,
        uom=None,
        warn=None,
        crit=None,
        _min=0,
        _max=cnt_monitors,
    )
    perfdata += lib.base.get_perfdata(
        'cnt_up',
        cnt_up,
        uom=None,
        warn=None,
        crit=None,
        _min=0,
        _max=cnt_monitors,
    )

    # over and out
    lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)


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