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

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

DESCRIPTION = """Checks the replication status of a MySQL/MariaDB replica, including I/O thread state,
SQL thread state, seconds behind master, and replication errors. Alerts when
replication is broken or lagging."""

DEFAULT_DEFAULTS_FILE = '/var/spool/icinga2/.my.cnf'
DEFAULT_DEFAULTS_GROUP = 'client'
DEFAULT_SERVERITY = 'warn'
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(
        '--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(
        '--severity',
        help='Severity for alerts that do not depend on thresholds. '
        'One of "warn" or "crit". '
        'Default: %(default)s',
        dest='SEVERITY',
        default=DEFAULT_SERVERITY,
        choices=['warn', 'crit'],
    )

    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_format'
            or variable_name like 'innodb_support_xa'
            or variable_name like 'read_only'
            or variable_name like 'rpl_semi_sync_master_enabled'
            or variable_name like 'rpl_semi_sync_replica_enabled'
            or variable_name like 'rpl_semi_sync_slave_enabled'
            or variable_name like 'rpl_semi_sync_source_enabled'
            or variable_name like 'wsrep_on'
            or variable_name like 'wsrep_provider_options'
            ;
          """
    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:get_replication_status(), 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))
    myvar['have_galera'] = 'NO'
    if myvar.get('wsrep_provider_options', ''):
        if myvar['wsrep_provider_options'] != '' and myvar['wsrep_on'] != 'OFF':
            myvar['have_galera'] = 'YES'

    sql = 'show replica status;'
    success, myrepl = lib.db_mysql.select(conn, sql)
    if not success:
        sql = 'show slave status;'
        myrepl = lib.base.coe(lib.db_mysql.select(conn, sql))
    sql = 'show slave hosts;'
    myslaves = lib.base.coe(lib.db_mysql.select(conn, sql))

    lib.db_mysql.close(conn)

    # init some vars
    msg = ''
    state = STATE_OK

    # Replication Metrics
    msg += f'Galera Synchronous replication: {myvar["have_galera"]}. '
    if len(myslaves) != 0:

        # build the message
        msg += (
            f'This server is acting as primary for'
            f' {len(myslaves)}'
            f' {lib.txt.pluralize("server", len(myslaves))}. '
        )
    msg += (
        f'Binlog format: {myvar["binlog_format"]}'
        f', XA support enabled:'
        f' {myvar.get("innodb_support_xa", "ON")}. '
    )

    msg += 'Semi synchronous Primary: '
    if myvar.get('rpl_semi_sync_master_enabled', 0) or myvar.get(
        'rpl_semi_sync_source_enabled', 0
    ):
        msg += (
            f'{myvar.get("rpl_semi_sync_master_enabled", "")}'
            f'{myvar.get("rpl_semi_sync_source_enabled", "")}. '
        )
    else:
        msg += 'Not Activated. '
    msg += 'Semi synchronous Replica: '
    if myvar.get('rpl_semi_sync_slave_enabled', 0) or myvar.get(
        'rpl_semi_sync_replica_enabled', 0
    ):
        msg += (
            f'{myvar.get("rpl_semi_sync_slave_enabled", "")}'
            f'{myvar.get("rpl_semi_sync_replica_enabled", "")}. '
        )
    else:
        msg += 'Not Activated. '

    if len(myrepl) == 0 and len(myslaves) == 0:
        msg += 'This is a standalone server.'
        lib.base.oao(msg, state, always_ok=args.ALWAYS_OK)
    if len(myrepl) == 0:
        msg += 'No replication setup for this server, or replication not started.'
        lib.base.oao(msg, state, always_ok=args.ALWAYS_OK)

    io_running = 'yes' in [
        myrepl[0].get('Slave_IO_Running', '').lower(),
        myrepl[0].get('Replica_IO_Running', '').lower(),
    ]
    sql_running = 'yes' in [
        myrepl[0].get('Slave_SQL_Running', '').lower(),
        myrepl[0].get('Replica_SQL_Running', '').lower(),
    ]

    if not io_running or not sql_running:
        state = lib.base.str2state(args.SEVERITY)
        msg += (
            f'This Replica is not running but seems to be'
            f' configured'
            f'{lib.base.state2str(state, prefix=" ")}. '
        )
    if io_running and sql_running:
        if myvar['read_only'] == 'OFF':
            state = lib.base.str2state(args.SEVERITY)
            msg += (
                f'This Replica is running with the'
                f' read_only option disabled'
                f'{lib.base.state2str(state, prefix=" ")}. '
            )
        if (
            myrepl[0].get('Seconds_Behind_Master', 0) is None
            or myrepl[0].get('Seconds_Behind_Source', 0) is None
            or myrepl[0].get('Seconds_Behind_Master', 0) > 0
            or myrepl[0].get('Seconds_Behind_Source', 0) > 0
        ):
            state = lib.base.str2state(args.SEVERITY)
            msg += (
                f'This Replica is lagging behind Primary'
                f'{lib.base.state2str(state, prefix=" ")}. '
            )
        else:
            msg += 'This Replica is up to date with Primary. '

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


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