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

import lib.args
import lib.base
import lib.human
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__ = '2026040801'

DESCRIPTION = """Monitors NGINX performance via the stub_status module. Reports active connections,
accepts, handled requests, and connection states (reading, writing, waiting). Alerts
when active connections exceed the configured thresholds."""

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=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(
        '-c',
        '--critical',
        help='CRIT threshold for the number of active connections. '
        'Default: >= %(default)s',
        dest='CRIT',
        type=int,
        default=DEFAULT_CRIT,
    )

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

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

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

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

    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)

    # 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 Exception:
        lib.base.cu(f'Got no valuable response from {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 += (
        f'{active_connections} active concurrent'
        f' {lib.txt.pluralize("conn", active_connections)}'
        f'{lib.base.state2str(conn_state, prefix=" ")};'
        f' {lib.human.number2human(total_connections_accepted)}'
        f' accepted'
        f' {lib.txt.pluralize("conn", total_connections_accepted)},'
        f' {lib.human.number2human(total_connections_handled)}'
        f' handled'
        f' {lib.txt.pluralize("conn", total_connections_handled)},'
        f' {lib.human.number2human(total_requests)}'
        f' {lib.txt.pluralize("req", total_requests)};'
        f' {requests_per_connection} req per conn;'
        f' currently {connections_reading}'
        f' receiving'
        f' {lib.txt.pluralize("req", connections_reading)},'
        f' {connections_writing}'
        f' sending'
        f' {lib.txt.pluralize("response", connections_writing)},'
        f' {connections_waiting}'
        f' keep-alive'
        f' {lib.txt.pluralize("conn", connections_waiting)}'
    )
    perfdata += lib.base.get_perfdata(
        'nginx_connections_accepted',
        total_connections_accepted,
        uom='c',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'nginx_connections_active',
        active_connections,
        warn=args.WARN,
        crit=args.CRIT,
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'nginx_connections_handled',
        total_connections_handled,
        uom='c',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'nginx_connections_reading',
        connections_reading,
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'nginx_connections_waiting',
        connections_waiting,
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'nginx_connections_writing',
        connections_writing,
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'nginx_http_requests_total',
        total_requests,
        uom='c',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'nginx_requests_per_connection',
        requests_per_connection,
        _min=0,
    )

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


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