#!/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 sys  # pylint: disable=C0413

import lib.args  # pylint: disable=C0413
import lib.base  # pylint: disable=C0413
import lib.human  # 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_CRIT, STATE_OK,  # pylint: disable=C0413
                          STATE_UNKNOWN, STATE_WARN)

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

DESCRIPTION = 'This check provides NGINX basic status information.'

DEFAULT_CRIT = 486       # active connections
DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_TIMEOUT = 8
DEFAULT_URL = 'http://localhost/server-status'
DEFAULT_WARN = 460       # active connections


def parse_args():
    """Parse command line arguments using argparse.
    """
    parser = argparse.ArgumentParser(description=DESCRIPTION)

    parser.add_argument(
        '-V', '--version',
        action='version',
        version='%(prog)s: v{} by {}'.format(__version__, __author__)
    )

    parser.add_argument('--always-ok',
        help='Always returns OK.',
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

    parser.add_argument('-c', '--critical',
        help='Set the CRIT threshold for the number of active connections. '
             'Default: >= %(default)s',
        dest='CRIT',
        type=int,
        default=DEFAULT_CRIT,
    )

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

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

    parser.add_argument('-w', '--warning',
        help='Set the WARN threshold for the number of active connections. '
             'Default: >= %(default)s',
        dest='WARN',
        type=int,
        default=DEFAULT_WARN,
    )

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

    return parser.parse_args()


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
    if args.TEST is None:
        URL = args.URL
        if URL[0:4] != 'http':
            lib.base.cu('--url parameter has to start with "http://" or https://".')
        # fetch the URL
        result = lib.base.coe(lib.url.fetch(
            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 = stdout

    result = result.strip().split('\n')

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

    # analyze data
    #   Active connections: 2
    #   server accepts handled requests
    #    2 2 3
    #   Reading: 0 Writing: 1 Waiting: 1
    try:
        tmp, active_connections = result[0].strip().split(': ')
        total_connections_accepted, total_connections_handled, total_requests = result[2].strip().split(' ')
        requests_per_connection = round(float(total_requests) / float(total_connections_handled), 1)
        tmp, connections_reading, tmp, connections_writing, tmp, connections_waiting = result[3].strip().split(' ')
    except:
        lib.base.cu('Got no valuable response from {}.'.format(args.URL))

    # identify any alerts
    if total_connections_handled != total_connections_accepted:
        state = STATE_WARN
        msg += 'Some resource limits have been reached (for example, the worker_connections limit). '
    conn_state = lib.base.get_state(active_connections, args.WARN, args.CRIT, _operator='ge')
    state = lib.base.get_worst(conn_state, state)

    # build the message
    msg += '{} active concurrent {}{}; {} accepted {}, {} handled {}, {} {}; {} req per conn; currently {} receiving {}, {} sending {}, {} keep-alive {}'.format(
        active_connections, lib.txt.pluralize('conn', active_connections), lib.base.state2str(conn_state, prefix=' '),
        lib.human.number2human(total_connections_accepted), lib.txt.pluralize('conn', total_connections_accepted),
        lib.human.number2human(total_connections_handled), lib.txt.pluralize('conn', total_connections_handled),
        lib.human.number2human(total_requests), lib.txt.pluralize('req', total_requests),
        requests_per_connection,
        connections_reading, lib.txt.pluralize('req', connections_reading),
        connections_writing, lib.txt.pluralize('response', connections_writing),
        connections_waiting, lib.txt.pluralize('conn', connections_waiting),
        )
    perfdata += lib.base.get_perfdata('nginx_connections_accepted', total_connections_accepted, 'c', None, None, 0, None)
    perfdata += lib.base.get_perfdata('nginx_connections_active', active_connections, None, args.WARN, args.CRIT, 0, None)
    perfdata += lib.base.get_perfdata('nginx_connections_handled', total_connections_handled, 'c', None, None, 0, None)
    perfdata += lib.base.get_perfdata('nginx_connections_reading', connections_reading, None, None, None, 0, None)
    perfdata += lib.base.get_perfdata('nginx_connections_waiting', connections_waiting, None, None, None, 0, None)
    perfdata += lib.base.get_perfdata('nginx_connections_writing', connections_writing, None, None, None, 0, None)
    perfdata += lib.base.get_perfdata('nginx_http_requests_total', total_requests, 'c', None, None, 0, None)
    perfdata += lib.base.get_perfdata('nginx_requests_per_connection', requests_per_connection, None, None, None, 0, None)

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