#!/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
from lib.globals import STATE_OK, STATE_UNKNOWN

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

DESCRIPTION = """Reports MySQL/MariaDB traffic statistics: uptime, queries per second, total
connections, bytes transferred, and the SELECT-vs-write ratio. Purely informational; the plugin
always returns OK. Cumulative counters (`Bytes_received`, `Bytes_sent`, `Connections`,
`Questions`, `Com_*`) are emitted as in-plugin-computed per-second rates so the Grafana
dashboard plots them without `non_negative_difference()` workarounds."""

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

SQLITE_DB = 'linuxfabrik-monitoring-plugins-mysql-traffic.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(
        '--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_status(conn):
    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."""

    # Informational summary plugin, modelled on mysqltuner.pl's
    # "MySQL Server Statistics" section (verified in sync with MySQLTuner
    # v2.8.41). No thresholds: an admin uses this to see traffic and the
    # read/write mix at a glance. The shipped Grafana dashboard plots the
    # per-second rates over time.

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

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

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

    # init some vars
    state = STATE_OK
    perfdata = ''

    bytes_received = int(mystat['Bytes_received'])
    bytes_sent = int(mystat['Bytes_sent'])
    com_delete = int(mystat['Com_delete'])
    com_insert = int(mystat['Com_insert'])
    com_replace = int(mystat['Com_replace'])
    com_select = int(mystat['Com_select'])
    com_update = int(mystat['Com_update'])
    connections = int(mystat['Connections'])
    questions = int(mystat['Questions'])
    # 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)

    # analyze data
    qps = round(questions / uptime, 1)
    total_reads = com_select
    total_writes = com_delete + com_insert + com_update + com_replace
    if total_reads == 0 and total_writes == 0:
        # Freshly booted server with no traffic yet; mysqltuner shows 0%/0%
        # in that case rather than flipping to 0%/100%.
        pct_reads = 0.0
        pct_writes = 0.0
    else:
        pct_reads = round(total_reads / (total_reads + total_writes) * 100, 1)
        pct_writes = round(100 - pct_reads, 1)

    # build the message
    msg = (
        f'Up {lib.human.seconds2human(uptime)} '
        f'({lib.human.number2human(questions)} q '
        f'[{lib.human.number2human(qps)} qps], '
        f'{lib.human.number2human(connections)} conn, '
        f'TX: {lib.human.bytes2human(bytes_sent)}, '
        f'RX: {lib.human.bytes2human(bytes_received)}); '
        f'Read/Write: {pct_reads}%/{pct_writes}%'
    )

    perfdata += lib.base.get_perfdata(
        'mysql_pct_reads',
        pct_reads,
        uom='%',
        _min=0,
        _max=100,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_pct_writes',
        pct_writes,
        uom='%',
        _min=0,
        _max=100,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_qps',
        qps,
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'mysql_uptime',
        uptime,
        uom='s',
        _min=0,
    )

    # Per-CONTRIBUTING: emit cumulative counters as in-plugin per-second
    # deltas (computed against a local SQLite cache) instead of cumulative
    # `c` counters that force Grafana to do non_negative_difference() per
    # panel. The deltas appear from the second check run onwards.
    rates = lib.db_sqlite.per_second_deltas(
        SQLITE_DB,
        'mysql-traffic',
        {
            'bytes_received': bytes_received,
            'bytes_sent': bytes_sent,
            'com_delete': com_delete,
            'com_insert': com_insert,
            'com_replace': com_replace,
            'com_select': com_select,
            'com_update': com_update,
            'connections': connections,
            'questions': questions,
        },
    )
    if rates is not None:
        perfdata += lib.base.get_perfdata(
            'mysql_bytes_received_per_second',
            round(rates['bytes_received'], 2),
            uom='B',
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            'mysql_bytes_sent_per_second',
            round(rates['bytes_sent'], 2),
            uom='B',
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            'mysql_com_delete_per_second',
            round(rates['com_delete'], 2),
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            'mysql_com_insert_per_second',
            round(rates['com_insert'], 2),
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            'mysql_com_replace_per_second',
            round(rates['com_replace'], 2),
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            'mysql_com_select_per_second',
            round(rates['com_select'], 2),
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            'mysql_com_update_per_second',
            round(rates['com_update'], 2),
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            'mysql_connections_per_second',
            round(rates['connections'], 2),
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            'mysql_questions_per_second',
            round(rates['questions'], 2),
            _min=0,
        )

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


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