#!/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

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

DESCRIPTION = """Reports MySQL/MariaDB traffic statistics including uptime, queries per second,
connection rates, and bytes sent and received."""

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(
        '--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_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 'Bytes_received'
            or variable_name like 'Bytes_sent'
            or variable_name like 'Com_delete'
            or variable_name like 'Com_insert'
            or variable_name like 'Com_replace'
            or variable_name like 'Com_select'
            or variable_name like 'Com_update'
            or variable_name like 'Connections'
            or variable_name like 'Questions'
            or variable_name like 'Uptime'
            ;
          """
    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:mariadb_stats(), 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))

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

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

    # calculations
    mycalc = {}
    # Clamp Uptime to a minimum of 1 second so qps is well-defined on
    # a freshly booted server where `SHOW GLOBAL STATUS LIKE 'Uptime'`
    # can legitimately return 0 in the first second after startup.
    uptime = max(int(mystat.get('Uptime') or 0), 1)
    qps = round(float(mystat['Questions']) / uptime, 1)
    mycalc['total_reads'] = int(mystat['Com_select'])
    mycalc['total_writes'] = (
        int(mystat['Com_delete'])
        + int(mystat['Com_insert'])
        + int(mystat['Com_update'])
        + int(mystat['Com_replace'])
    )
    if mycalc['total_reads'] == 0:
        mycalc['pct_reads'] = 0
        mycalc['pct_writes'] = 100
    else:
        mycalc['pct_reads'] = (
            mycalc['total_reads']
            / (mycalc['total_reads'] + mycalc['total_writes'])
            * 100
        )
        mycalc['pct_writes'] = 100 - mycalc['pct_reads']

    # build the message
    msg += (
        f'Up {lib.human.seconds2human(int(mystat["Uptime"]))}'
        f' ({lib.human.number2human(int(mystat["Questions"]))} q'
        f' [{lib.human.number2human(qps)} qps],'
        f' {lib.human.number2human(int(mystat["Connections"]))} conn,'
        f' TX: {lib.human.number2human(int(mystat["Bytes_sent"]))},'
        f' RX: {lib.human.number2human(int(mystat["Bytes_received"]))}'
        f'); Read/Write:'
        f' {round(mycalc["pct_reads"], 1)}%'
        f'/{round(mycalc["pct_writes"], 1)}%'
    )

    perfdata += lib.base.get_perfdata(
        'mysql_bytes_received',
        mystat['Bytes_received'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_bytes_sent',
        mystat['Bytes_sent'],
        uom='B',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_com_delete',
        mystat['Com_delete'],
        uom='c',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_com_insert',
        mystat['Com_insert'],
        uom='c',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_com_replace',
        mystat['Com_replace'],
        uom='c',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_com_select',
        mystat['Com_select'],
        uom='c',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_com_update',
        mystat['Com_update'],
        uom='c',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_connections',
        mystat['Connections'],
        uom='c',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_questions',
        mystat['Questions'],
        uom='c',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_uptime',
        mystat['Uptime'],
        uom='s',
        _min=0,
    )

    perfdata += lib.base.get_perfdata(
        'mysql_qps',
        qps,
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_pct_reads',
        mycalc['pct_reads'],
        uom='%',
        _min=0,
        _max=100,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_pct_writes',
        mycalc['pct_writes'],
        uom='%',
        _min=0,
        _max=100,
    )

    # over and out
    lib.base.oao(msg, state, perfdata)


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