#!/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__ = '2026040801'

DESCRIPTION = """Checks the InnoDB buffer pool size configuration in MySQL/MariaDB. Compares the
configured size against the recommended size based on actual data and index sizes.
Alerts if the buffer pool is significantly undersized."""

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 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 'innodb_buffer_pool_size'
            or variable_name like 'innodb_log_file_size'
            or variable_name like 'innodb_log_files_in_group'
            or variable_name like 'innodb_redo_log_capacity'
            ;
          """
    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_innodb(), section # InnoDB Buffer Pool Size, v2.2.12
    # including variable names

    # parse the command line
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

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

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

    engines = lib.db_mysql.get_engines(conn)
    if not engines.get('have_innodb', ''):
        lib.db_mysql.close(conn)
        lib.base.cu('InnoDB Storage Engine not available.')
    if engines['have_innodb'] != 'YES':
        lib.db_mysql.close(conn)
        lib.base.cu('InnoDB Storage Engine is disabled.')

    myvar = lib.db_mysql.lod2dict(get_vars(conn))
    if (
        myvar.get('innodb_log_files_in_group', None) is None
        or myvar.get('innodb_log_files_in_group', 0) == 0
    ):
        # innodb_log_files_in_group removed in MariaDB 10.6.0
        myvar['innodb_log_files_in_group'] = '1'

    sql = """
        select sum(data_length+index_length) as InnoDB
        from information_schema.tables
        where
            table_schema not in ("information_schema", "performance_schema", "mysql")
            and engine = "innodb";
    """
    enginestats = lib.base.coe(lib.db_mysql.select(conn, sql))
    if enginestats[0]['InnoDB'] is None:
        enginestats[0]['InnoDB'] = 0

    lib.db_mysql.close(conn)

    # calculations
    mycalc = {}
    mycalc['innodb_log_size_pct'] = int(
        int(myvar['innodb_log_file_size'])
        * int(myvar['innodb_log_files_in_group'])
        / int(myvar['innodb_buffer_pool_size'])
        * 100
    )

    # InnoDB Buffer Pool Size
    # Output changed compared to MySQLTuner (to be more clear).

    # handling innodb_redo_log_capacity is specific to MySQL only - not implemented here
    # MariaDB: https://mariadb.com/kb/en/innodb-redo-log/
    # MySQL: https://dev.mysql.com/doc/refman/8.0/en/innodb-parameters.html#sysvar_innodb_redo_log_capacity

    if not lib.base.X86_64 and int(myvar['innodb_buffer_pool_size']) > 4294967295:
        local_state = STATE_WARN
        state = lib.base.get_worst(state, local_state)

        # build the message
        msg += (
            f'InnoDB Buffer Pool size'
            f' ({lib.human.bytes2human(int(myvar["innodb_buffer_pool_size"]))})'
            f' limit reached for 32 bits architecture.'
            f' Limit innodb_buffer_pool_size under'
            f' {lib.human.bytes2human(4294967295)}'
            f'{lib.base.state2str(local_state)}.'
        )
    if lib.base.X86_64 and int(myvar['innodb_buffer_pool_size']) > 18446744073709551615:
        local_state = STATE_WARN
        state = lib.base.get_worst(state, local_state)
        msg += (
            f'InnoDB Buffer Pool size'
            f' ({lib.human.bytes2human(int(myvar["innodb_buffer_pool_size"]))})'
            f' limit reached for 64 bits architecture.'
            f' Limit innodb_buffer_pool_size under'
            f' {lib.human.bytes2human(18446744073709551615)}'
            f'{lib.base.state2str(local_state)}.'
        )

    msg += (
        f'Data size:'
        f' {lib.human.bytes2human(int(enginestats[0]["InnoDB"]))}'
        f', innodb_buffer_pool_size:'
        f' {lib.human.bytes2human(int(myvar["innodb_buffer_pool_size"]))}'
    )
    if int(myvar['innodb_buffer_pool_size']) <= int(enginestats[0]['InnoDB']):
        local_state = STATE_WARN
        state = lib.base.get_worst(state, local_state)
        msg += (
            f" doesn't fit"
            f' {lib.base.state2str(local_state)}.'
            f' Set innodb_buffer_pool_size >='
            f' {lib.human.bytes2human(int(enginestats[0]["InnoDB"]))}. '
        )

    msg += (
        f'\nRatio innodb_log_file_size'
        f' ({lib.human.bytes2human(int(myvar["innodb_log_file_size"]))})'
        f' * innodb_log_files_in_group'
        f' ({myvar["innodb_log_files_in_group"]})'
        f' vs. innodb_buffer_pool_size'
        f' ({lib.human.bytes2human(int(myvar["innodb_buffer_pool_size"]))})'
        f': {mycalc["innodb_log_size_pct"]}% '
    )

    # badprint
    if (
        int(mycalc['innodb_log_size_pct']) < 20
        or int(mycalc['innodb_log_size_pct']) > 30
    ):
        ratio_state = STATE_WARN
        state = lib.base.get_worst(state, ratio_state)
        msg += (
            f'{lib.base.state2str(ratio_state, prefix=" ")}'
            f' (should be 25% of innodb_buffer_pool_size). '
        )
        msg += (
            f'Set innodb_log_file_size to'
            f' {lib.human.bytes2human(int(myvar["innodb_buffer_pool_size"]) / int(myvar["innodb_log_files_in_group"]) / 4)}.'
        )

    perfdata += lib.base.get_perfdata(
        'mysql_innodb_buffer_pool_size',
        myvar['innodb_buffer_pool_size'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_innodb_log_file_size',
        myvar['innodb_log_file_size'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_innodb_log_files_in_group',
        myvar['innodb_log_files_in_group'],
        _min=0,
    )

    perfdata += lib.base.get_perfdata(
        'mysql_innodb_log_size_pct',
        mycalc['innodb_log_size_pct'],
        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()
