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

import lib.args
import lib.base
import lib.lftest
import lib.shell
import lib.version
from lib.globals import STATE_UNKNOWN

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

DESCRIPTION = """Checks the installed MySQL/MariaDB version against the endoflife.date API and alerts if
the version is end-of-life or if newer major, minor, or patch releases are available.
By default, alerts 30 days before the official EOL date. The offset is configurable."""

DEFAULT_CHECK_MAJOR = False
DEFAULT_CHECK_MINOR = False
DEFAULT_CHECK_PATCH = False
DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_OFFSET_EOL = -30  # days
DEFAULT_TIMEOUT = 8


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(
        '--check-major',
        help=lib.args.help('--check-major'),
        dest='CHECK_MAJOR',
        action='store_true',
        default=DEFAULT_CHECK_MAJOR,
    )

    parser.add_argument(
        '--check-minor',
        help=lib.args.help('--check-minor'),
        dest='CHECK_MINOR',
        action='store_true',
        default=DEFAULT_CHECK_MINOR,
    )

    parser.add_argument(
        '--check-patch',
        help=lib.args.help('--check-patch'),
        dest='CHECK_PATCH',
        action='store_true',
        default=DEFAULT_CHECK_PATCH,
    )

    parser.add_argument(
        '--insecure',
        help=lib.args.help('--insecure'),
        dest='INSECURE',
        action='store_true',
        default=DEFAULT_INSECURE,
    )

    parser.add_argument(
        '--no-proxy',
        help=lib.args.help('--no-proxy'),
        dest='NO_PROXY',
        action='store_true',
        default=DEFAULT_NO_PROXY,
    )

    parser.add_argument(
        '--offset-eol',
        help=lib.args.help('--offset-eol') + ' Default: %(default)s days',
        dest='OFFSET_EOL',
        type=int,
        default=DEFAULT_OFFSET_EOL,
    )

    parser.add_argument(
        '--test',
        help=lib.args.help('--test'),
        dest='TEST',
        type=lib.args.csv,
    )

    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


# Output formats this parser handles:
#
#   mysqld  Ver 8.0.45 for Linux on x86_64 (Source distribution)
#   mysqld  Ver 10.11.15-MariaDB for Linux on x86_64 (MariaDB Server)
#   mysql  Ver 14.14 Distrib 5.7.44, for Linux (x86_64) using EditLine wrapper
#   mariadb  Ver 15.1 Distrib 10.5.x-MariaDB, for Linux on x86_64
#   mariadb from 11.4.10-MariaDB, client 15.2 for debian-linux-gnu (x86_64) using EditLine wrapper
#
# MariaDB 11.4 dropped the `mysqld` symlink and renamed the client
# version banner from "Distrib X.Y.Z" to "from X.Y.Z", so the parser
# also tries `mariadbd` and accepts both prefixes.
_VERSION_REGEXES = (
    # `mysqld  Ver X.Y.Z[-MariaDB] for ...` (the package suffix that
    # some distros append, e.g. `-1~trusty`, is stripped)
    r'Ver (\d+\.\d+\.\d+(?:-MariaDB)?)',
    # `mysql  Ver 14.14 Distrib X.Y.Z[-MariaDB], for ...`
    # `mariadb  Ver 15.1 Distrib 10.5.x-MariaDB, for ...`
    r'Distrib (\S+?),',
    # `mariadb from X.Y.Z[-MariaDB], client ...`  (MariaDB 11.4+)
    r'from (\d+\.\d+\.\d+(?:-MariaDB)?),',
)


def _parse_version(stdout):
    """Try every known version-string regex against `stdout` and
    return the first match, or `''` if none of them apply.
    """
    for regex in _VERSION_REGEXES:
        m = re.search(regex, stdout)
        if m:
            return m.group(1).strip()
    return ''


def get_installed_version(test_arg=None):
    if test_arg is not None:
        stdout, _, _ = lib.lftest.test(test_arg)
        return _parse_version(stdout.strip())

    # Try the server binary first under both its old name (`mysqld`)
    # and its current MariaDB name (`mariadbd`), then fall back to the
    # client binary under both its current name (`mariadb`) and its
    # legacy name (`mysql`).
    for command in ('mysqld --version', 'mariadbd --version',
                    'mariadb --version', 'mysql --version'):
        success, result = lib.shell.shell_exec(command)
        if not success:
            continue
        version = _parse_version(result[0].strip())
        if version:
            return version
    return ''


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

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

    # fetch data
    installed_version = get_installed_version(args.TEST)
    if not installed_version:
        lib.base.cu('MariaDB/MySQL not found.')
    mariadb = 'MariaDB' in installed_version
    installed_version = installed_version.replace('-MariaDB', '')
    if mariadb:
        state, msg = lib.version.check_eol(
            'https://endoflife.date/api/mariadb.json',
            installed_version,
            offset_eol=args.OFFSET_EOL,
            check_major=args.CHECK_MAJOR,
            check_minor=args.CHECK_MINOR,
            check_patch=args.CHECK_PATCH,
            insecure=args.INSECURE,
            no_proxy=args.NO_PROXY,
            timeout=args.TIMEOUT,
        )
    else:
        state, msg = lib.version.check_eol(
            'https://endoflife.date/api/mysql.json',
            installed_version,
            offset_eol=args.OFFSET_EOL,
            check_major=args.CHECK_MAJOR,
            check_minor=args.CHECK_MINOR,
            check_patch=args.CHECK_PATCH,
            insecure=args.INSECURE,
            no_proxy=args.NO_PROXY,
            timeout=args.TIMEOUT,
        )

    # over and out
    lib.base.oao(
        f'{"MariaDB" if mariadb else "MySQL"} v{installed_version} ({msg})',
        state,
        lib.base.get_perfdata(
            'mysql-version',
            lib.version.version2float(installed_version),
            _min=0,
        ),
        always_ok=args.ALWAYS_OK,
    )


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