From 13dd2829c6efe1616e653687d4d9982544d695e5 Mon Sep 17 00:00:00 2001 From: Markus Frei Date: Fri, 8 Sep 2023 11:29:40 +0200 Subject: [PATCH] mysql-logfile: Stop magic auto-configure if `--server-log` is given --- CHANGELOG.md | 1 + check-plugins/mysql-logfile/README.rst | 9 +-- check-plugins/mysql-logfile/mysql-logfile | 75 +++++++++++++---------- 3 files changed, 47 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53e52dbb..be929ba6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ Monitoring Plugins: * infomaniak-events: Add filter for service categories * infomaniak-swiss-backup-devices: Improve column ordering in output * journald-query: Improve output +* mysql-logfile: Stop magic auto-configure if `--server-log` is given * mysql-logfile: Returns OK instead of UNKNOWN if logfile is found but empty * openstack-nova-list: Make more robust in case of OpenStack errors * systemd-unit: Encode unit-name to text before running systemd command diff --git a/check-plugins/mysql-logfile/README.rst b/check-plugins/mysql-logfile/README.rst index aa5c3e11..223b0d5b 100644 --- a/check-plugins/mysql-logfile/README.rst +++ b/check-plugins/mysql-logfile/README.rst @@ -88,8 +88,8 @@ Usage Examples .. code-block:: bash - ./mysql-logfile --ignore-pattern 'aborted connection' --ignore-pattern 'access denied' - ./mysql-logfile --defaults-file=/var/spool/icinga2/.my.cnf --server-log=systemd:mariadb + ./mysql-logfile --defaults-file=/var/spool/icinga2/.my.cnf --server-log=systemd:mariadb + ./mysql-logfile --ignore-pattern='aborted connection' --ignore-pattern='access denied' ./mysql-logfile --server-log=docker:mariadb Output: @@ -139,14 +139,15 @@ Perfdata / Metrics Troubleshooting --------------- -No log file set (set log_error in MySQL/MariaDB config or use the check's --server-log parameter). +No log file set (set log_error in MySQL/MariaDB config or use the check's ``--server-log`` parameter). The check tried to get information from an error logfile, but was unable to do so. All possible error logfile locations were tried, but no logfile was found. You have to help by configuring the MySQL/MariaDB system variable ``log_error`` accordingly, or by providing the ``--server-log`` parameter to the check. ``'proxies_priv' entry '@% root@mariadb-server' ignored in --skip-name-resolve mode.`` .. code-block:: text select * from mysql.proxies_priv; - delete from `mysql`.`proxies_priv` where (`host` = 'mariadb-server') and (`user` = 'root') and (`proxied_host` = '') and (`proxied_user` = ''); + delete from `mysql`.`proxies_priv` + where (`host` = 'mariadb-server') and (`user` = 'root') and (`proxied_host` = '') and (`proxied_user` = ''); Credits, License diff --git a/check-plugins/mysql-logfile/mysql-logfile b/check-plugins/mysql-logfile/mysql-logfile index e33016bb..bd6b1ef9 100755 --- a/check-plugins/mysql-logfile/mysql-logfile +++ b/check-plugins/mysql-logfile/mysql-logfile @@ -27,7 +27,7 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413 STATE_UNKNOWN, STATE_WARN) __author__ = 'Linuxfabrik GmbH, Zurich/Switzerland' -__version__ = '2023071203' +__version__ = '2023090801' DESCRIPTION = """Checks MySQL/MariaDB log content the same way MySQLTuner does, but also in case the DB is down.""" @@ -192,43 +192,50 @@ def main(): except SystemExit: sys.exit(STATE_UNKNOWN) - mysql_connection = { - 'defaults_file': args.DEFAULTS_FILE, - 'defaults_group': args.DEFAULTS_GROUP, - 'timeout': args.TIMEOUT, - } - success, conn = lib.db_mysql.connect(mysql_connection) - if not success: - # DB seems to be down - # Try to get the "log_error" setting and save it to cache, so that we know where to get the - # log info from in case the DB is down. - myvar = {} - log_error = lib.cache.get('{}-{}'.format( - args.HOSTNAME, - args.PORT - ), filename='linuxfabrik-monitoring-plugins-mysql-logfile.db') + if args.SERVER_LOG: + # user told us to use this log source, so don't try to find anything else + log_error = args.SERVER_LOG else: - lib.base.coe(lib.db_mysql.check_select_privileges(conn)) - myvar = lib.db_mysql.lod2dict(get_vars(conn)) - lib.db_mysql.close(conn) - log_error = myvar['log_error'] - - if not log_error: - if not args.SERVER_LOG: + # first ask the DB where to find the logfile + mysql_connection = { + 'defaults_file': args.DEFAULTS_FILE, + 'defaults_group': args.DEFAULTS_GROUP, + 'timeout': args.TIMEOUT, + } + success, conn = lib.db_mysql.connect(mysql_connection) + if not success: + # DB seems to be down + # Try to get the "log_error" setting and save it to cache, so that we know + # where to get the log info from in case the DB is down again. + myvar = {} + log_error = lib.cache.get('{}-{}'.format( + args.HOSTNAME, + args.PORT + ), + filename='linuxfabrik-monitoring-plugins-mysql-logfile.db', + ) + else: + lib.base.coe(lib.db_mysql.check_select_privileges(conn)) + myvar = lib.db_mysql.lod2dict(get_vars(conn)) + lib.db_mysql.close(conn) + log_error = myvar['log_error'] + if not log_error: log_error = get_log_file_real_path( myvar.get('log_error', ''), myvar.get('hostname', ''), myvar.get('datadir', ''), ) - else: - log_error = args.SERVER_LOG - if not log_error: - lib.base.cu('No log file set (set log_error in MySQL/MariaDB config or use the check\'s --server-log parameter).') + if not log_error: + lib.base.cu('No log file set (set `log_error` in MySQL/MariaDB config or use the check\'s `--server-log` parameter).') lib.cache.set('{}-{}'.format( - args.HOSTNAME, - args.PORT - ), log_error, lib.time.now() + args.CACHE_EXPIRE*60, filename='linuxfabrik-monitoring-plugins-mysql-logfile.db') + args.HOSTNAME, + args.PORT + ), + log_error, + lib.time.now() + args.CACHE_EXPIRE*60, + filename='linuxfabrik-monitoring-plugins-mysql-logfile.db', + ) # init some vars msg, msg_body = '', '' @@ -251,19 +258,19 @@ def main(): cmd = "journalctl -n {} -b -u '{}'".format(MAXLINES, match.group(1).strip()) logfile, stderr, retc = lib.base.coe(lib.shell.shell_exec(cmd)) if retc != 0: - lib.base.cu('{} exited with error ({}, {}).'.format(cmd, retc, stderr.strip())) + lib.base.cu('`{}` exited with error ({}, {}).'.format(cmd, retc, stderr.strip())) msg += 'Src: {}, '.format(log_error) else: # good old logfile if not os.path.isabs(log_error): log_error = os.path.join(myvar.get('datadir', ''), log_error) if not os.path.isfile(log_error): - msg = 'Logging seems to be configured, but {} does not seem to be an existing regular file. '.format(log_error) - msg += 'Check the path and file permissions, or provide the --server-log parameter.' + msg = 'Logging seems to be configured, but `{}` does not seem to be an existing regular file. '.format(log_error) + msg += 'Check the path and file permissions, or provide the `--server-log` parameter.' lib.base.oao(msg, STATE_WARN) size = os.path.getsize(log_error) if size == 0: - lib.base.oao('Empty log file {} found. Assuming log-rotation. Use `--server-log` for explicit file.'.format(log_error), STATE_OK) + lib.base.oao('Empty log file `{}` found. Assuming log-rotation. Use `--server-log` for explicit file.'.format(log_error), STATE_OK) msg += 'Src: Log file {} (size: {}), '.format( log_error, lib.human.bytes2human(size),