Skip to content

Commit

Permalink
mysql-logfile: Stop magic auto-configure if --server-log is given
Browse files Browse the repository at this point in the history
  • Loading branch information
markuslf committed Sep 8, 2023
1 parent dd2134b commit 13dd282
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions check-plugins/mysql-logfile/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
75 changes: 41 additions & 34 deletions check-plugins/mysql-logfile/mysql-logfile
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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 = '', ''
Expand All @@ -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),
Expand Down

0 comments on commit 13dd282

Please sign in to comment.