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

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

DESCRIPTION = """Checks the hit rate for open table cache lookups in MySQL/MariaDB. A low hit rate
indicates that table_open_cache may need to be increased.
Alerts when the hit rate drops below acceptable levels."""

DEFAULT_DEFAULTS_FILE = '/var/spool/icinga2/.my.cnf'
DEFAULT_DEFAULTS_GROUP = 'client'
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=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(
        '--defaults-file',
        help='MySQL/MariaDB cnf file to read parameters like user, host and password from (instead of specifying them on the command line). '
        'Example: `/var/spool/icinga2/.my.cnf`. '
        'Default: %(default)s',
        dest='DEFAULTS_FILE',
        default=DEFAULT_DEFAULTS_FILE,
    )

    parser.add_argument(
        '--defaults-group',
        help=lib.args.help('--defaults-group') + ' Default: %(default)s',
        dest='DEFAULTS_GROUP',
        default=DEFAULT_DEFAULTS_GROUP,
    )

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

    args, _ = parser.parse_known_args()
    return 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 'open_files_limit'
            or variable_name like 'table_open_cache'
            ;
          """
    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 'Open_tables'
            or variable_name like 'Opened_tables'
            or variable_name like 'Table_open_cache_hits'
            or variable_name like 'Table_open_cache_misses'
            ;
          """
    return lib.base.coe(lib.db_mysql.select(conn, sql))


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

    # logic taken from mysqltuner.pl:mysql_stats(), section # Table cache,
    # verified in sync with MySQLTuner v2.8.38 (the hit-rate formula, the
    # `Opened_tables > 0` calc guard, the `Open_tables > 0` check guard,
    # the `< 20%` warn threshold and the "Increase table_open_cache
    # gradually" / "open_files_limit must be greater than
    # table_open_cache" recommendation are unchanged upstream since the
    # original port).

    # parse the command line
    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
    state = STATE_OK
    perfdata = ''

    # calculations
    mycalc = {}
    if int(mystat['Opened_tables']) > 0:
        if mystat.get('Table_open_cache_hits', None) is None:
            mycalc['table_cache_hit_rate'] = round(
                int(mystat['Open_tables']) / int(mystat['Opened_tables']) * 100, 1
            )
        else:
            mycalc['table_cache_hit_rate'] = round(
                int(mystat['Table_open_cache_hits'])
                / (
                    int(mystat['Table_open_cache_hits'])
                    + int(mystat['Table_open_cache_misses'])
                )
                * 100,
                1,
            )
    else:
        mycalc['table_cache_hit_rate'] = 100

    # Table cache
    if int(mystat['Open_tables']) > 0:
        if mystat.get('Table_open_cache_hits', None) is None:
            msg = (
                f'{mycalc["table_cache_hit_rate"]}%'
                f' table cache hit rate'
                f' ({lib.human.number2human(mystat["Open_tables"])}'
                f' hits /'
                f' {lib.human.number2human(mystat["Opened_tables"])}'
                f' requests)'
            )
        else:
            msg = (
                f'{mycalc["table_cache_hit_rate"]}%'
                f' table cache hit rate'
                f' ({lib.human.number2human(mystat["Table_open_cache_hits"])}'
                f' hits /'
                f' {lib.human.number2human(int(mystat["Table_open_cache_hits"]) + int(mystat["Table_open_cache_misses"]))}'
                f' requests)'
            )
            perfdata += lib.base.get_perfdata(
                'mysql_table_open_cache_hits',
                mystat['Table_open_cache_hits'],
                uom='c',
                _min=0,
            )
            perfdata += lib.base.get_perfdata(
                'mysql_table_open_cache_misses',
                mystat['Table_open_cache_misses'],
                uom='c',
                _min=0,
            )

        if mycalc['table_cache_hit_rate'] < 20:
            state = STATE_WARN

            # build the message
            msg += (
                f'{lib.base.state2str(state, prefix=" ")}.'
                f' Set table_open_cache >'
                f' {myvar["table_open_cache"]}. '
            )
            msg += (
                f'Beware that open_files_limit'
                f' ({myvar["open_files_limit"]})'
                f' should be greater than table_open_cache'
                f' ({myvar["table_open_cache"]}). '
            )
            msg += (
                'Increase table_open_cache gradually to avoid file descriptor limits. '
            )
            msg += 'Make sure that your operating system can cope with the number of open file '
            msg += 'descriptors required by the table_open_cache setting. If table_open_cache is '
            msg += 'set too high, the DB may start to refuse connections as the operating system '
            msg += 'runs out of file descriptors. Also note that the MyISAM (and Aria?) storage '
            msg += 'engines need two file descriptors per open table.'
    else:
        msg = 'Everything is ok.'

    perfdata += lib.base.get_perfdata(
        'mysql_open_files_limit',
        myvar['open_files_limit'],
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_table_open_cache',
        myvar['table_open_cache'],
        _min=0,
    )

    perfdata += lib.base.get_perfdata(
        'mysql_open_tables',
        mystat['Open_tables'],
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_opened_tables',
        mystat['Opened_tables'],
        _min=0,
    )

    perfdata += lib.base.get_perfdata(
        'mysql_table_cache_hit_rate',
        mycalc['table_cache_hit_rate'],
        uom='%',
        _min=0,
        _max=100,
    )

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


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