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

import lib.args
import lib.base
import lib.lftest
import lib.txt
import lib.url
from lib.globals import STATE_OK, STATE_UNKNOWN, STATE_WARN

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

DESCRIPTION = """Monitors all configured UptimeRobot monitors via the UptimeRobot API. Alerts on any
monitor that is in a down or unknown state."""

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=lib.args.help('--always-ok'),
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

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

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

    parser.add_argument(
        '--test',
        help=lib.args.help('--test'),
        dest='TEST',
        type=lib.args.csv,
    )

    parser.add_argument(
        '--timeout',
        help=lib.args.help('--timeout') + ' 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,
    )

    args, _ = parser.parse_known_args()
    return 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. This is where the magic happens."""

    # parse the command line
    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
    # uptimerobot returns either a single `monitor` dict (one monitor) or a
    # `psp.monitors` list (even if it contains only one entry); normalise both
    # cases to a list so the loop below is uniform
    monitors = (
        [result['monitor']] if 'monitor' in result else 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:
        lib.base.cu()
