#!/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.base  # pylint: disable=C0413
import lib.db_mysql  # pylint: disable=C0413
import lib.human  # pylint: disable=C0413
import lib.txt  # pylint: disable=C0413
from lib.globals import (STATE_OK, STATE_UNKNOWN,  # pylint: disable=C0413
                          STATE_WARN)

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

DESCRIPTION = """Checks the connection usage rate, the rate of aborted connections and
                 if name resolution is active for new connections on MySQL/MariaDB."""

DEFAULT_DEFAULTS_FILE = '/var/spool/icinga2/.my.cnf'
DEFAULT_DEFAULTS_GROUP = 'client'
DEFAULT_IGNORE_NAME_RESOLUTION = False
DEFAULT_TIMEOUT = 3


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

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

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

    parser.add_argument(
        '--defaults-file',
        help='Specifies a cnf file to read parameters like user, host and password from '
             '(instead of specifying them on the command line), '
             'for example `/var/spool/icinga2/.my.cnf`. Default: %(default)s',
        dest='DEFAULTS_FILE',
        default=DEFAULT_DEFAULTS_FILE,
    )

    parser.add_argument(
        '--defaults-group',
        help='Group/section to read from in the cnf file. Default: %(default)s',
        dest='DEFAULTS_GROUP',
        default=DEFAULT_DEFAULTS_GROUP,
    )

    parser.add_argument(
        '--ignore-name-resolution',
        help='Do not check if name resolution is active. Default: %(default)s',
        dest='IGNORE_NAME_RESOLUTION',
        action='store_true',
        default=DEFAULT_IGNORE_NAME_RESOLUTION,
    )

    parser.add_argument(
        '--timeout',
        help='Network timeout in seconds. Default: %(default)s (seconds)',
        dest='TIMEOUT',
        type=int,
        default=DEFAULT_TIMEOUT,
    )

    return parser.parse_args()


def get_vars(conn):
    # Do not implement `get_all_vars()`, just fetch the ones we need for this check.
    # Without the GLOBAL modifier, SHOW VARIABLES displays the values that are used for
    # the current connection to MariaDB.
    sql = """
        show global variables
        where variable_name like 'interactive_timeout'
            or variable_name like 'max_connections'
            or variable_name like 'skip_name_resolve'
            or variable_name like 'wait_timeout'
            ;
          """
    return lib.base.coe(lib.db_mysql.select(conn, sql))


def get_status(conn):
    # Do not implement `get_all_vars()`, just fetch the ones we need for this check.
    # Without the GLOBAL modifier, SHOW STATUS displays the values that are used for
    # the current connection to MariaDB.
    sql = """
        show global status
        where variable_name like 'Aborted_connects'
            or variable_name like 'Connections'
            or variable_name like 'Max_used_connections'
            or variable_name like 'Max_used_connections_time'
            or variable_name like 'Threads_connected'
            or variable_name like 'Threads_running'
            ;
          """
    return lib.base.coe(lib.db_mysql.select(conn, sql))


def main():
    """The main function. Hier spielt die Musik.
    """

    # logic taken from mysqltuner.pl:mysql_stats(), section # Connections, v1.9.8
    # including variable names

    # parse the command line, exit with UNKNOWN if it fails
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

    mysql_connection = {
        'defaults_file':  args.DEFAULTS_FILE,
        'defaults_group': args.DEFAULTS_GROUP,
        'timeout':        args.TIMEOUT,
    }
    conn = lib.base.coe(lib.db_mysql.connect(mysql_connection))
    lib.base.coe(lib.db_mysql.check_select_privileges(conn))

    myvar = lib.db_mysql.lod2dict(get_vars(conn))
    mystat = lib.db_mysql.lod2dict(get_status(conn))
    lib.db_mysql.close(conn)

    # init some vars
    msg = ''
    recommendations = ''
    state = STATE_OK
    perfdata = ''
    mycalc = {}

    # Aborted Connections
    aborted_connections_state = STATE_OK
    mycalc['pct_connections_aborted'] = round(int(mystat['Aborted_connects']) / int(mystat['Connections']) * 100, 1)
    if mycalc['pct_connections_aborted'] > 3:
        aborted_connections_state = STATE_WARN
        state = lib.base.get_worst(state, aborted_connections_state)
        recommendations += '* Reduce or eliminate unclosed connections and network issues.\n'
    msg += '{}% aborted connections ({}/{}){}; '.format(
        mycalc['pct_connections_aborted'],
        lib.human.number2human(mystat['Aborted_connects']),
        lib.human.number2human(mystat['Connections']),
        lib.base.state2str(aborted_connections_state, prefix=' '),
    )

    # currently used Connections
    cur_connections_state = STATE_OK
    mycalc['pct_connections_used'] = round(int(mystat['Threads_connected']) / int(myvar['max_connections']) * 100, 1)
    mycalc['pct_connections_used'] = min(100, mycalc['pct_connections_used']) # value cannot be higher than 100
    if mycalc['pct_connections_used'] > 85:
        cur_connections_state = STATE_WARN
        state = lib.base.get_worst(state, cur_connections_state)
        recommendations += '* Reduce or eliminate persistent connections to reduce connection usage (set connections > {}, wait_timeout < {} and/or interactive_timeout < {}).\n'.format(
            mystat['Connections'],
            myvar['wait_timeout'],
            myvar['interactive_timeout'],
        )
    msg += 'current {}% used ({}/{}, {} {} running){}; '.format(
        mycalc['pct_connections_used'],
        mystat['Threads_connected'],
        myvar['max_connections'],
        mystat['Threads_running'],
        lib.txt.pluralize('thread', mystat['Threads_running']),
        lib.base.state2str(cur_connections_state, prefix=' '),
    )

    # Max. used Connections
    mycalc['pct_max_connections_used'] = round(int(mystat['Max_used_connections']) / int(myvar['max_connections']) * 100, 1)
    mycalc['pct_max_connections_used'] = min(100, mycalc['pct_max_connections_used']) # value cannot be higher than 100
    msg += 'peak {}% used ({}/{}){}; '.format(
        mycalc['pct_max_connections_used'],
        mystat['Max_used_connections'],
        myvar['max_connections'],
        ' at ' + mystat['Max_used_connections_time'] if mystat.get('Max_used_connections_time') else '',
    )

    msg += 'interactive_timeout = {}, wait_timeout = {}; '.format(
        lib.human.seconds2human(myvar['interactive_timeout']),
        lib.human.seconds2human(myvar['wait_timeout']),
    )

    # name resolution
    if not args.IGNORE_NAME_RESOLUTION and myvar.get('skip_name_resolve'):
        if myvar['skip_name_resolve'] == 'OFF':
            name_resolution_state = STATE_WARN
            state = lib.base.get_worst(state, name_resolution_state)
            msg += 'Name resolution is active{}; '.format(
                lib.base.state2str(name_resolution_state, prefix=' '),
            )
            recommendations += '* A reverse name resolution is made for each new connection and can reduce performance. Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=ON.'

    perfdata += lib.base.get_perfdata('mysql_interactive_timeout', myvar['interactive_timeout'], 's', None, None, 0, None)
    perfdata += lib.base.get_perfdata('mysql_max_connections', myvar['max_connections'], None, None, None, 0, None)
    perfdata += lib.base.get_perfdata('mysql_wait_timeout', myvar['wait_timeout'], 's', None, None, 0, None)

    perfdata += lib.base.get_perfdata('mysql_aborted_connects', mystat['Aborted_connects'], 'c', None, None, 0, None)
    perfdata += lib.base.get_perfdata('mysql_connections', mystat['Connections'], 'c', None, None, 0, None)
    perfdata += lib.base.get_perfdata('mysql_max_used_connections', mystat['Max_used_connections'], None, None, None, 0, None)
    perfdata += lib.base.get_perfdata('mysql_threads_connected', mystat['Threads_connected'], None, None, None, 0, None)
    perfdata += lib.base.get_perfdata('mysql_threads_running', mystat['Threads_running'], None, None, None, 0, None)

    perfdata += lib.base.get_perfdata('mysql_pct_connections_aborted', mycalc['pct_connections_aborted'], '%', None, None, 0, 100)
    perfdata += lib.base.get_perfdata('mysql_pct_connections_used', mycalc['pct_connections_used'], '%', None, None, 0, 100)

    msg = msg[:-2] # remove trailing "; "
    if recommendations:
        msg += '\n\nRecommendations:\n'
        msg += recommendations

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