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

import lib.args
import lib.base
import lib.db_mysql
import lib.human
import lib.version
from lib.globals import STATE_OK, STATE_UNKNOWN, STATE_WARN

try:
    import psutil

    HAVE_PSUTIL = True
except ImportError:
    HAVE_PSUTIL = False


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

DESCRIPTION = """Checks memory allocation and usage metrics for MySQL/MariaDB, including global buffers,
per-thread buffers, and total potential memory consumption. Alerts if the server's
memory configuration could lead to excessive memory usage."""

DEFAULT_DEFAULTS_FILE = '/var/spool/icinga2/.my.cnf'
DEFAULT_DEFAULTS_GROUP = 'client'
DEFAULT_TIMEOUT = 3
DEFAULT_WARN = 95  # % max peak memory usage


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. '
        'Example: `--defaults-file=/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 'aria_pagecache_buffer_size'
            or variable_name like 'innodb_additional_mem_pool_size'
            or variable_name like 'innodb_buffer_pool_size'
            or variable_name like 'innodb_log_buffer_size'
            or variable_name like 'join_buffer_size'
            or variable_name like 'key_buffer_size'
            or variable_name like 'max_allowed_packet'
            or variable_name like 'max_connections'
            or variable_name like 'max_heap_table_size'
            or variable_name like 'performance_schema'
            or variable_name like 'query_cache_size'
            or variable_name like 'read_buffer_size'
            or variable_name like 'read_rnd_buffer_size'
            or variable_name like 'sort_buffer_size'
            or variable_name like 'thread_stack'
            or variable_name like 'tmp_table_size'
            ;
          """
    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 'Max_used_connections'
            ;
          """
    return lib.base.coe(lib.db_mysql.select(conn, sql))


def get_pf_memory(conn, myvar):
    # get memory usage from Performance Schema
    if not myvar.get('performance_schema', 0):
        return 0
    if myvar['performance_schema'] == 'OFF':
        return 0

    sql = 'show engine performance_schema status;'
    infoPFSMemory = lib.base.coe(lib.db_mysql.select(conn, sql))
    if len(infoPFSMemory) == 0:
        return 0
    for item in infoPFSMemory:
        if item['Type'] == 'performance_schema' and item['Name'].startswith('memory'):
            return int(item['Status'])
    return 0


def get_other_process_memory():
    if not HAVE_PSUTIL:
        return 0
    totalMemOther = 0
    if lib.version.version(psutil.__version__) >= lib.version.version('5.3.0'):
        try:
            for p in psutil.process_iter(attrs=['name', 'cmdline', 'memory_info']):
                if not p.info['cmdline']:
                    continue
                cmdline = ' '.join(p.info['cmdline'])
                if not cmdline.strip():
                    continue
                if 'PID' in cmdline:
                    continue
                if 'mysqld' in cmdline:
                    continue
                if 'mariadbd' in cmdline:
                    continue
                if 'systemd' in cmdline:
                    continue
                if '[' in cmdline or ']' in cmdline:
                    continue
                if cmdline.startswith(' '):
                    continue
                totalMemOther += p.info['memory_info'].rss
        except psutil.NoSuchProcess:
            pass
    else:
        try:
            for p in [
                x.as_dict(attrs=['name', 'cmdline', 'memory_info'])
                for x in psutil.process_iter()
            ]:
                if not p.get('cmdline'):
                    continue
                cmdline = ' '.join(p['cmdline'])
                if not cmdline.strip():
                    continue
                if 'PID' in cmdline:
                    continue
                if 'mysqld' in cmdline:
                    continue
                if 'mariadbd' in cmdline:
                    continue
                if 'systemd' in cmdline:
                    continue
                if '[' in cmdline or ']' in cmdline:
                    continue
                if cmdline.startswith(' '):
                    continue
                totalMemOther += p['memory_info'].rss
        except psutil.NoSuchProcess:
            pass
    return totalMemOther


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

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

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

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

    # calculations
    mycalc = {}
    mycalc['max_tmp_table_size'] = max(
        int(myvar['max_heap_table_size']), int(myvar['tmp_table_size'])
    )
    mycalc['server_buffers'] = (
        int(myvar['key_buffer_size']) + mycalc['max_tmp_table_size']
    )
    mycalc['server_buffers'] += int(myvar.get('innodb_buffer_pool_size', 0))
    mycalc['server_buffers'] += int(myvar.get('innodb_additional_mem_pool_size', 0))
    mycalc['server_buffers'] += int(myvar.get('innodb_log_buffer_size', 0))
    mycalc['server_buffers'] += int(myvar.get('query_cache_size', 0))
    mycalc['server_buffers'] += int(myvar.get('aria_pagecache_buffer_size', 0))
    mycalc['per_thread_buffers'] = int(myvar['read_buffer_size'])
    mycalc['per_thread_buffers'] += int(myvar['read_rnd_buffer_size'])
    mycalc['per_thread_buffers'] += int(myvar['sort_buffer_size'])
    mycalc['per_thread_buffers'] += int(myvar['thread_stack'])
    mycalc['per_thread_buffers'] += int(myvar['max_allowed_packet'])
    mycalc['per_thread_buffers'] += int(myvar['join_buffer_size'])
    mycalc['max_total_per_thread_buffers'] = mycalc['per_thread_buffers'] * int(
        mystat['Max_used_connections']
    )
    mycalc['total_per_thread_buffers'] = mycalc['per_thread_buffers'] * int(
        myvar['max_connections']
    )

    # Global memory
    # Max used memory is memory used by MySQL based on Max_used_connections
    # This is the max memory used theoretically calculated with the max concurrent connection number reached by mysql
    mycalc['pf_memory'] = get_pf_memory(conn, myvar)
    mycalc['max_used_memory'] = (
        mycalc['server_buffers']
        + mycalc['max_total_per_thread_buffers']
        + mycalc['pf_memory']
    )
    mycalc['max_peak_memory'] = (
        mycalc['server_buffers']
        + mycalc['total_per_thread_buffers']
        + mycalc['pf_memory']
    )
    physical_memory = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
    mycalc['pct_max_used_memory'] = round(
        mycalc['max_used_memory'] / physical_memory * 100, 1
    )
    mycalc['pct_max_physical_memory'] = round(
        mycalc['max_peak_memory'] / physical_memory * 100, 1
    )

    lib.db_mysql.close(conn)

    if not lib.base.X86_64 and mycalc['max_used_memory'] > 2 * 1024 * 1024 * 1024:
        mem1_state = STATE_WARN

        # build the message
        msg += 'Allocating > 2GB RAM on 32-bit systems can cause system instability. '
    elif mycalc['pct_max_used_memory'] > DEFAULT_WARN:
        mem1_state = STATE_WARN
    else:
        mem1_state = STATE_OK
    state = lib.base.get_worst(state, mem1_state)
    msg += (
        f'{mycalc["pct_max_used_memory"]}%'
        f' - total: {lib.human.bytes2human(physical_memory)}'
        f', used: {lib.human.bytes2human(mycalc["max_used_memory"])}'
        f'{lib.base.state2str(mem1_state, prefix=" ")}. '
    )

    msg += (
        f'Maximum possible memory usage is'
        f' {mycalc["pct_max_physical_memory"]}%'
        f' (possible peak:'
        f' {lib.human.bytes2human(mycalc["max_peak_memory"])}). '
    )
    other_process_memory = get_other_process_memory()
    if physical_memory < mycalc['max_peak_memory'] + other_process_memory:
        mem2_state = STATE_WARN
        state = lib.base.get_worst(state, mem2_state)
        msg += (
            f'Overall possible memory usage'
            f' ({lib.human.bytes2human(mycalc["max_peak_memory"])})'
            f' with other processes'
            f' ({lib.human.bytes2human(other_process_memory)})'
            f' will exceed physical memory'
            f' ({lib.human.bytes2human(physical_memory)}).'
            f'{lib.base.state2str(mem2_state, prefix=" ")}'
        )

    if mycalc['pct_max_physical_memory'] > DEFAULT_WARN:
        msg += '\n\nRecommendations:\n'
        msg += '* Reduce your overall MySQL memory footprint for system stability.'

    # be nice and print the calculation
    msg += '\n\nCalculations:\n'
    msg += (
        f'* Memory usage according to Performance Schema:'
        f' pfm = {lib.human.bytes2human(mycalc["pf_memory"])}\n'
    )

    msg += f'* Server Buffers: sb = {lib.human.bytes2human(mycalc["server_buffers"])}\n'
    msg += f'      {lib.human.bytes2human(int(myvar["key_buffer_size"]))} key_buffer_size\n'
    msg += f'    + {lib.human.bytes2human(mycalc["max_tmp_table_size"])} max_tmp_table_size\n'
    msg += (
        f'    +'
        f' {lib.human.bytes2human(int(myvar.get("innodb_buffer_pool_size", 0)))}'
        f' innodb_buffer_pool_size\n'
    )
    msg += (
        f'    +'
        f' {lib.human.bytes2human(int(myvar.get("innodb_additional_mem_pool_size", 0)))}'
        f' innodb_additional_mem_pool_size\n'
    )
    msg += (
        f'    +'
        f' {lib.human.bytes2human(int(myvar.get("innodb_log_buffer_size", 0)))}'
        f' innodb_log_buffer_size\n'
    )
    msg += (
        f'    +'
        f' {lib.human.bytes2human(int(myvar.get("query_cache_size", 0)))}'
        f' query_cache_size\n'
    )
    msg += (
        f'    +'
        f' {lib.human.bytes2human(int(myvar.get("aria_pagecache_buffer_size", 0)))}'
        f' aria_pagecache_buffer_size\n'
    )

    msg += f'* Per Thread Buffers: ptb = {lib.human.bytes2human(mycalc["per_thread_buffers"])}\n'
    msg += f'      {lib.human.bytes2human(int(myvar["read_buffer_size"]))} read_buffer_size\n'
    msg += (
        f'    +'
        f' {lib.human.bytes2human(int(myvar["read_rnd_buffer_size"]))}'
        f' read_rnd_buffer_size\n'
    )
    msg += f'    + {lib.human.bytes2human(int(myvar["sort_buffer_size"]))} sort_buffer_size\n'
    msg += f'    + {lib.human.bytes2human(int(myvar["thread_stack"]))} thread_stack\n'
    msg += f'    + {lib.human.bytes2human(int(myvar["max_allowed_packet"]))} max_allowed_packet\n'
    msg += f'    + {lib.human.bytes2human(int(myvar["join_buffer_size"]))} join_buffer_size\n'

    msg += (
        f'* Max Total per Thread Buffers:'
        f' mtptb = ptb * max_used_conns'
        f' = {lib.human.bytes2human(mycalc["per_thread_buffers"])}'
        f' * {int(mystat["Max_used_connections"])}'
        f' = {lib.human.bytes2human(mycalc["max_total_per_thread_buffers"])}\n'
    )
    msg += (
        f'* Total per Thread Buffers:'
        f' tptb = ptb * max_conns'
        f' = {lib.human.bytes2human(mycalc["per_thread_buffers"])}'
        f' * {int(myvar["max_connections"])}'
        f' = {lib.human.bytes2human(mycalc["total_per_thread_buffers"])}\n'
    )

    msg += (
        f'* Max Used Memory: mum = sb + mtptb + pfm'
        f' = {lib.human.bytes2human(mycalc["server_buffers"])}'
        f' + {lib.human.bytes2human(mycalc["max_total_per_thread_buffers"])}'
        f' + {lib.human.bytes2human(mycalc["pf_memory"])}'
        f' = {lib.human.bytes2human(mycalc["max_used_memory"])}\n'
    )
    msg += (
        f'* Possible Peak Memory: ppm = sb + tptb + pfm'
        f' = {lib.human.bytes2human(mycalc["server_buffers"])}'
        f' + {lib.human.bytes2human(mycalc["total_per_thread_buffers"])}'
        f' + {lib.human.bytes2human(mycalc["pf_memory"])}'
        f' = {lib.human.bytes2human(mycalc["max_peak_memory"])}\n'
    )
    msg += f'* Other Process Memory: {lib.human.bytes2human(other_process_memory)}\n'
    msg += f'* Physical Memory: pm = {lib.human.bytes2human(physical_memory)}\n'
    msg += (
        f'* Max Used Memory %: mump = mum / pm * 100'
        f' = {lib.human.bytes2human(mycalc["max_used_memory"])}'
        f' / {lib.human.bytes2human(physical_memory)}'
        f' * 100 = {mycalc["pct_max_used_memory"]}%\n'
    )
    msg += (
        f'* Max Possible Memory Usage %:'
        f' mpmu = ppm / pm * 100'
        f' = {lib.human.bytes2human(mycalc["max_peak_memory"])}'
        f' / {lib.human.bytes2human(physical_memory)}'
        f' * 100 = {mycalc["pct_max_physical_memory"]}%\n'
    )

    # perfdata
    perfdata += lib.base.get_perfdata(
        'mysql_aria_pagecache_buffer_size',
        myvar.get('aria_pagecache_buffer_size', 0),
        uom='B',
        _min=0,
    )
    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_buffer_size',
        myvar['innodb_log_buffer_size'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_join_buffer_size',
        myvar['join_buffer_size'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_key_buffer_size',
        myvar['key_buffer_size'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_max_allowed_packet',
        myvar['max_allowed_packet'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_max_connections',
        myvar['max_connections'],
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_max_heap_table_size',
        myvar['max_heap_table_size'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_query_cache_size',
        myvar.get('query_cache_size', 0),
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_read_buffer_size',
        myvar['read_buffer_size'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_read_rnd_buffer_size',
        myvar['read_rnd_buffer_size'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_sort_buffer_size',
        myvar['sort_buffer_size'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_thread_stack',
        myvar['thread_stack'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_tmp_table_size',
        myvar['tmp_table_size'],
        uom='B',
        _min=0,
    )

    perfdata += lib.base.get_perfdata(
        'mysql_max_used_connections',
        mystat['Max_used_connections'],
        _min=0,
    )

    perfdata += lib.base.get_perfdata(
        'mysql_physical_memory',
        physical_memory,
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_max_peak_memory',
        mycalc['max_peak_memory'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_max_tmp_table_size',
        mycalc['max_tmp_table_size'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_max_total_per_thread_buffers',
        mycalc['max_total_per_thread_buffers'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_max_used_memory',
        mycalc['max_used_memory'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_pct_max_physical_memory',
        mycalc['pct_max_physical_memory'],
        uom='%',
        _min=0,
        _max=100,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_pct_max_used_memory',
        mycalc['pct_max_used_memory'],
        uom='%',
        _min=0,
        _max=100,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_per_thread_buffers',
        mycalc['per_thread_buffers'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_server_buffers',
        mycalc['server_buffers'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_total_per_thread_buffers',
        mycalc['total_per_thread_buffers'],
        uom='B',
        _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()
