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

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

DESCRIPTION = """Checks whether the GitLab instance is ready to accept traffic by querying the
/-/readiness endpoint. Validates the status of all dependent services (database,
Redis, Gitaly, etc.) and reports each individually.
Alerts when the instance or any dependent service is not ready."""

DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_SEVERITY = 'warn'
DEFAULT_TIMEOUT = 8
DEFAULT_URL = 'http://localhost/-/readiness?all=1'


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(
        '--severity',
        help=lib.args.help('--severity') + ' Default: %(default)s',
        dest='SEVERITY',
        default=DEFAULT_SEVERITY,
        choices=['warn', 'crit'],
    )

    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='GitLab readiness URL endpoint. 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)

    if not args.URL.endswith('?all=1'):
        args.URL += '?all=1'

    # init some vars
    state = STATE_OK
    table_data = []
    issues = []

    # 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)

    if 'status' in result and 'cache_check' in result and 'error' not in result:
        for check in [
            'cache_check',
            'chat_check',
            'cluster_cache_check',
            'cluster_shared_state_check',
            'db_check',
            'db_load_balancing_check',
            'feature_flag_check',
            'gitaly_check',
            'master_check',
            'queues_check',
            'rate_limiting_check',
            'repository_cache_check',
            'sessions_check',
            'shared_state_check',
            'trace_chunks_check',
        ]:
            try:
                result.get(check)[0].get('status', '')
            except TypeError:
                continue
            if result.get(check)[0].get('status', '') != 'ok':
                check_state = lib.base.str2state(args.SEVERITY)
                state = lib.base.get_worst(state, check_state)
                check_msg = (
                    lib.base.state2str(check_state, suffix=' ')
                    + result.get(check)[0].get('message', '')[0:46]
                    + '...'
                )
                issues.append(check)
            else:
                check_state = STATE_OK
                check_msg = 'Running'
            table_data.append(
                {
                    'check': check.replace('_check', ''),
                    'msg': check_msg,
                }
            )
        if state == STATE_OK:
            msg = 'Everything is ok.'
        else:
            msg = (
                f'There are issues with '
                f'{", ".join(issues)}. '
                f'Run `curl {args.URL}` for full results.'
            )
    elif 'status' in result and 'message' in result and 'error' not in result:
        msg = result['message']
        state = lib.base.str2state(args.SEVERITY)
    elif 'error' in result:
        msg = result['error']
        state = lib.base.str2state(args.SEVERITY)
    else:
        msg = f'Unknown error fetching URL {args.URL}'
        state = STATE_UNKNOWN
    perfdata = lib.base.get_perfdata(
        'gitlab-readiness',
        state,
        _min=0,
        _max=STATE_UNKNOWN,
    )

    # build the message
    if table_data:
        msg += '\n\n' + lib.base.get_table(
            table_data,
            ['check', 'msg'],
            header=['Service', 'Message'],
        )

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


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