#!/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.shell
import lib.time
import lib.url
from lib.globals import STATE_CRIT, STATE_OK, STATE_UNKNOWN, STATE_WARN

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

DESCRIPTION = """Monitors a Statuspal status page, including overall status, service states, active
incidents, and scheduled maintenances. Alerts on degraded services, ongoing incidents,
or emergency maintenance windows."""

DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_TIMEOUT = 8
DEFAULT_URL = 'https://statuspal.eu/api/v2/status_pages/exoscalestatus/summary'


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='Statuspal API URL pointing to the summary endpoint. Default: %(default)s',
        dest='URL',
        default=DEFAULT_URL,
    )

    args, _ = parser.parse_known_args()
    return args


def flatten_services(services, parent_name=''):
    """Recursively flatten the services tree into a list of dicts with
    'name' (dotted hierarchy) and 'current_incident_type'.

    >>> services = [
    ...     {
    ...         'name': 'Global',
    ...         'current_incident_type': None,
    ...         'children': [
    ...             {'name': 'DNS', 'current_incident_type': None, 'children': []}
    ...         ],
    ...     }
    ... ]
    >>> flatten_services(services)
    [{'name': 'Global', 'current_incident_type': None},
     {'name': 'Global.DNS', 'current_incident_type': None}]
    """
    result = []
    for service in services:
        if not service.get('name'):
            continue
        full_name = (
            f'{parent_name}.{service["name"]}' if parent_name else service['name']
        )
        result.append(
            {
                'name': full_name,
                'current_incident_type': service.get('current_incident_type'),
            }
        )
        for child in service.get('children') or []:
            result.extend(flatten_services([child], full_name))
    return result


def statuspalstate2state(sps):
    """Convert Statuspal's incident level to the Nagios world."""
    if sps is None:
        return STATE_OK
    sps = sps.lower()
    if sps == 'scheduled' or sps == 'info':
        return STATE_OK
    if sps == 'minor' or sps == 'performance':
        return STATE_WARN
    if sps == 'major' or sps == 'emergency-maintenance':
        return STATE_CRIT
    return STATE_UNKNOWN


def main():
    """The main function. This is where the magic happens."""

    # https://www.statuspal.io/api-docs/v2

    # parse the command line
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

    # fetch data
    if args.TEST is None:
        result = lib.base.coe(
            lib.url.fetch_json(
                args.URL,
                insecure=args.INSECURE,
                no_proxy=args.NO_PROXY,
                timeout=args.TIMEOUT,
            )
        )
    else:
        # do not call the command, put in test data
        stdout, _stderr, _retc = lib.lftest.test(args.TEST)
        result = json.loads(stdout)

    try:
        result['status_page']['current_incident_type']
    except Exception:
        lib.base.cu(f'Unexpected response from {args.URL}')

    # init some vars
    msg = ''
    state = statuspalstate2state(result['status_page']['current_incident_type'])
    perfdata = ''
    table_data = []
    cnt_warn, cnt_crit = 0, 0

    # analyze data and build the message

    # status_page
    if state == STATE_OK:

        # build the message
        msg += 'All systems operational @ '
    if state == STATE_WARN:
        msg += 'Minor incidents @ '
    if state == STATE_CRIT:
        msg += 'Major incidents @ '
    if state == STATE_UNKNOWN:
        msg += (
            f'Got state "'
            f'{result["status_page"]["current_incident_type"]}'
            f'" from {args.URL} for '
        )
    msg += (
        f'{result["status_page"]["name"]} '
        f'({result["status_page"]["url"]}, '
        f'TZ {result["status_page"]["time_zone"]})'
    )

    # incidents - get newest incident message only and print it on top
    if result['incidents']:
        msg += f': {result["incidents"][0]["title"].strip()}'
        if result['incidents'][0]['updates']:
            description = result['incidents'][0]['updates'][0]['description'].strip()
            updated_at = (
                result['incidents'][0]['updates'][0]['updated_at']
                .replace('T', ' ')
                .strip()
            )
            msg += f' / {description} ({updated_at})'
        msg += (
            f' (see {result["incidents"][0]["url"]})'
            if result['incidents'][0]['url']
            else ''
        )

    # services - search for any incidents in services
    for service in flatten_services(result['services']):
        service_state = statuspalstate2state(service['current_incident_type'])
        if service_state == STATE_WARN:
            cnt_warn += 1
        if service_state == STATE_CRIT:
            cnt_crit += 1
        table_data.append(
            {
                'name': service['name'],
                'state': lib.base.state2str(service_state, empty_ok=False),
            }
        )
    if table_data:
        msg += '\n\n'
        msg += lib.base.get_table(
            table_data,
            ['name', 'state'],
            header=['Service', 'State'],
        )

    # maintenance
    table_data = []
    for maint in result['maintenances']:
        table_data.append(
            {
                'type': maint['type'],
                'title': maint['title'],
                'starts_at': f'{lib.time.timestr2datetime(maint["starts_at"], pattern="%Y-%m-%dT%H:%M:%S")}',
                # in case of an ongoing maint, ends_at is "None"
                'ends_at': (
                    f'{lib.time.timestr2datetime(maint["ends_at"], pattern="%Y-%m-%dT%H:%M:%S")}'
                    if maint.get('ends_at') is not None
                    else 'in progress'
                ),
            }
        )
    if table_data:
        msg += '\n'
        msg += lib.base.get_table(
            table_data,
            ['title', 'type', 'starts_at', 'ends_at'],
            header=['Maintenance', 'Type', 'Start', 'End'],
        )
        # get newest maintenance task
        if table_data[0]['starts_at'] <= lib.time.now(as_type='iso'):
            msg = (
                f'Ongoing maintenance since '
                f'{table_data[0]["starts_at"]}: '
                f'{table_data[0]["title"]}\n'
                f'{msg}'
            )

    # upcoming_maintenances (just fyi)
    table_data = []
    for maint in result['upcoming_maintenances']:
        table_data.append(
            {
                'type': maint['type'],
                'title': maint['title'],
                'starts_at': f'{lib.time.timestr2datetime(maint["starts_at"], pattern="%Y-%m-%dT%H:%M:%S")}',
                # in case of an ongoing maint, ends_at is "None"
                'ends_at': (
                    f'{lib.time.timestr2datetime(maint["ends_at"], pattern="%Y-%m-%dT%H:%M:%S")}'
                    if maint.get('ends_at') is not None
                    else 'open end'
                ),
            }
        )
    if table_data:
        msg += '\n'
        msg += lib.base.get_table(
            table_data,
            ['title', 'type', 'starts_at', 'ends_at'],
            header=['Upcoming Maintenance', 'Type', 'Start', 'End'],
        )
    perfdata += lib.base.get_perfdata(
        'cnt_warn',
        cnt_warn,
        uom=None,
        warn=None,
        crit=None,
        _min=0,
        _max=None,
    )
    perfdata += lib.base.get_perfdata(
        'cnt_crit',
        cnt_crit,
        uom=None,
        warn=None,
        crit=None,
        _min=0,
        _max=None,
    )

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


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