#!/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.args  # pylint: disable=C0413
import lib.base  # pylint: disable=C0413
import lib.db_mysql  # pylint: disable=C0413
import lib.human  # 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 some metrics of the Aria Storage Engine in MySQL/MariaDB."""

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='{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(
        '--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 'aria_pagecache_buffer_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 'Aria_pagecache_reads'
            or variable_name like 'Aria_pagecache_read_requests';
          """
    return lib.base.coe(lib.db_mysql.select(conn, sql))


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

    # logic taken from mysqltuner.pl:mariadb_aria(), v2.2.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))
    engines = lib.db_mysql.get_engines(conn)

    if not engines.get('have_aria', ''):
        lib.base.cu('Aria Storage Engine not available.')
    if engines['have_aria'] != 'YES':
        lib.base.cu('Aria Storage Engine is disabled.')

    # Aria pagecache
    sql = """
        select ifnull(sum(index_length), 0) as total_aria_indexes
        from information_schema.tables
        where table_schema not in ('information_schema')
            and engine = 'aria';
          """
    result  = lib.base.coe(lib.db_mysql.select(conn, sql))
    total_aria_indexes = result[0].get('total_aria_indexes', None)
    if total_aria_indexes is None:
        lib.base.oao('Unable to calculate Aria index size on MySQL/MariaDB server.', STATE_OK)

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

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

    # calculations
    if mystat.get('Aria_pagecache_read_requests', 0) \
    and int(mystat['Aria_pagecache_read_requests']) > 0:
        pct_aria_keys_from_mem = 100 - round(
            int(mystat['Aria_pagecache_reads']) / int(mystat['Aria_pagecache_read_requests']) * 100,
            1,
        )
    else:
        pct_aria_keys_from_mem = 0

    # total_aria_indexes == 0 if there are no aria tables or user has no SELECT privileges on them
    if int(myvar['aria_pagecache_buffer_size']) < total_aria_indexes \
    and pct_aria_keys_from_mem < 95:
        aria_pagecache_buffer_size_state = STATE_WARN
        state = lib.base.get_worst(state, aria_pagecache_buffer_size_state)
        msg += ' (adjust aria_pagecache_buffer_size > {}){}'.format(
            total_aria_indexes,
            lib.base.state2str(aria_pagecache_buffer_size_state, prefix=' '),
        )
    else:
        aria_pagecache_buffer_size_state = STATE_OK
    msg = 'Total Aria indexes: {}, Aria pagecache size: {}{}; '.format(
        lib.human.bytes2human(int(total_aria_indexes)),
        lib.human.bytes2human(int(myvar['aria_pagecache_buffer_size'])),
        msg,
    )
    perfdata += lib.base.get_perfdata('mysql_aria_pagecache_buffer_size', myvar['aria_pagecache_buffer_size'], 'B', None, None, 0, None)
    perfdata += lib.base.get_perfdata('mysql_total_aria_indexes', total_aria_indexes, None, None, None, 0, None)

    if int(mystat['Aria_pagecache_read_requests']) > 0:
        msg += '{}% Aria pagecache hit rate ({} cached / {} reads)'.format(
            pct_aria_keys_from_mem,
            lib.human.number2human(mystat['Aria_pagecache_read_requests']),
            lib.human.number2human(mystat['Aria_pagecache_reads']),
        )
        # Remove WARN if `aria_pagecache_read_requests` > 0 and `pct_aria_keys_from_mem` < 95%
        # Because there's nothing we can do about it:
        # if pct_aria_keys_from_mem < 95:
        #     pct_aria_keys_from_mem_state = STATE_WARN
        #     state = lib.base.get_worst(state, pct_aria_keys_from_mem_state)
        #     msg += '{}'.format(
        #         lib.base.state2str(pct_aria_keys_from_mem_state, prefix=' '),
        #     )
        perfdata += lib.base.get_perfdata('mysql_pct_aria_keys_from_mem', pct_aria_keys_from_mem, '%', 95, None, 0, 100)
        perfdata += lib.base.get_perfdata('mysql_aria_pagecache_read_requests', mystat['Aria_pagecache_read_requests'], 'c', None, None, 0, None)
        perfdata += lib.base.get_perfdata('mysql_aria_pagecache_reads', mystat['Aria_pagecache_reads'], 'c', None, None, 0, None)

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