#!/usr/lib64/linuxfabrik-monitoring-plugins/venv/bin/python
# -*- 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.db_sqlite
import lib.human
import lib.txt
from lib.globals import STATE_OK, STATE_UNKNOWN, STATE_WARN

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

DESCRIPTION = """Checks the memory-access rate for the MySQL/MariaDB binary log cache. A low rate
(below 90%) means many transactions had to spill from the in-memory binlog cache to a
temporary disk file because they exceeded `binlog_cache_size`, and the variable should be
increased.
Alerts when the memory-access rate drops below 90%."""

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

SQLITE_DB = 'linuxfabrik-monitoring-plugins-mysql-binlog-cache.db'


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 'binlog_cache_size'
            or variable_name like 'log_bin'
            ;
          """
    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 'Binlog_cache_disk_use'
            or variable_name like 'Binlog_cache_use'
            ;
          """
    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 # Binlog cache,
    # verified in sync with MySQLTuner (the calc formula
    # (Binlog_cache_use - Binlog_cache_disk_use) / Binlog_cache_use, the
    # < 90% warn threshold and the "increase binlog_cache_size by 16M"
    # 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_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 = ''
    perfdata += lib.base.get_perfdata(
        'mysql_binlog_cache_size',
        myvar['binlog_cache_size'],
        uom='B',
        _min=0,
    )

    # Binary logging disabled is a legitimate config (standalone server without
    # replication); not UNKNOWN. mysqltuner emits the same as an infoprint.
    # Skip the SQLite cache update too: with logging off the counters stay
    # frozen and an enable-event later would surface as a spurious huge delta.
    if myvar['log_bin'] == 'OFF':
        lib.base.oao(
            'Binary logging is disabled.',
            STATE_OK,
            perfdata,
            always_ok=args.ALWAYS_OK,
        )

    # Per-CONTRIBUTING: emit the binlog cache use counters as per-second deltas
    # (computed in-plugin against a local SQLite cache) instead of cumulative
    # `c` counters that force Grafana to do non_negative_difference() per panel.
    binlog_use = int(mystat['Binlog_cache_use'])
    binlog_disk_use = int(mystat['Binlog_cache_disk_use'])
    rates = lib.db_sqlite.per_second_deltas(
        SQLITE_DB,
        'mysql-binlog-cache',
        {'binlog_cache_use': binlog_use, 'binlog_cache_disk_use': binlog_disk_use},
    )
    use_per_s = int(rates['binlog_cache_use']) if rates is not None else None
    disk_use_per_s = int(rates['binlog_cache_disk_use']) if rates is not None else None
    if use_per_s is not None:
        perfdata += lib.base.get_perfdata(
            'mysql_binlog_cache_use_per_second',
            use_per_s,
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            'mysql_binlog_cache_disk_use_per_second',
            disk_use_per_s,
            _min=0,
        )

    # No binlog cache activity yet -> the percentage would be a misleading 0%.
    # mysqltuner leaves pct_binlog_cache undefined in this case and skips the
    # display; we mirror that with an explicit info line.
    if binlog_use == 0:
        lib.base.oao(
            'Binary logging is enabled, no binlog cache activity yet.',
            STATE_OK,
            perfdata,
            always_ok=args.ALWAYS_OK,
        )

    # Binlog cache
    pct_binlog_cache = round((binlog_use - binlog_disk_use) / binlog_use * 100, 1)
    perfdata += lib.base.get_perfdata(
        'mysql_pct_binlog_cache',
        pct_binlog_cache,
        uom='%',
        _min=0,
        _max=100,
    )

    binlog_mem = lib.human.number2human(binlog_use - binlog_disk_use)
    binlog_total = lib.human.number2human(binlog_use)
    msg = (
        f'{pct_binlog_cache}% binlog cache memory access'
        f' ({binlog_mem} memory / {binlog_total} total)'
    )

    if pct_binlog_cache < 90:
        state = STATE_WARN
        new_size = lib.human.bytes2human(
            int(myvar['binlog_cache_size']) + 16 * 1024 * 1024,
        )
        msg += (
            f'{lib.base.state2str(state, prefix=" ")}.'
            f' Set `binlog_cache_size` to {new_size}.'
        )
    else:
        msg += '.'

    # mysqltuner emits a debug-only "not enough data to validate" hint when
    # Binlog_cache_use < 10. Surface it so the admin doesn't act on a hit
    # rate built from a handful of transactions.
    if binlog_use < 10:
        msg += (
            f' Note: `Binlog_cache_use` is only {binlog_use}'
            f' (i.e. {binlog_use} {lib.txt.pluralize("transaction", binlog_use)}'
            f' have used the binlog cache so far),'
            f' the hit rate may not yet be representative.'
        )

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


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