Skip to content

Commit 13dd282

Browse files
committed
mysql-logfile: Stop magic auto-configure if --server-log is given
1 parent dd2134b commit 13dd282

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Monitoring Plugins:
6060
* infomaniak-events: Add filter for service categories
6161
* infomaniak-swiss-backup-devices: Improve column ordering in output
6262
* journald-query: Improve output
63+
* mysql-logfile: Stop magic auto-configure if `--server-log` is given
6364
* mysql-logfile: Returns OK instead of UNKNOWN if logfile is found but empty
6465
* openstack-nova-list: Make more robust in case of OpenStack errors
6566
* systemd-unit: Encode unit-name to text before running systemd command

check-plugins/mysql-logfile/README.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ Usage Examples
8888

8989
.. code-block:: bash
9090
91-
./mysql-logfile --ignore-pattern 'aborted connection' --ignore-pattern 'access denied'
92-
./mysql-logfile --defaults-file=/var/spool/icinga2/.my.cnf --server-log=systemd:mariadb
91+
./mysql-logfile --defaults-file=/var/spool/icinga2/.my.cnf --server-log=systemd:mariadb
92+
./mysql-logfile --ignore-pattern='aborted connection' --ignore-pattern='access denied'
9393
./mysql-logfile --server-log=docker:mariadb
9494
9595
Output:
@@ -139,14 +139,15 @@ Perfdata / Metrics
139139
Troubleshooting
140140
---------------
141141

142-
No log file set (set log_error in MySQL/MariaDB config or use the check's --server-log parameter).
142+
No log file set (set log_error in MySQL/MariaDB config or use the check's ``--server-log`` parameter).
143143
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.
144144

145145
``'proxies_priv' entry '@% root@mariadb-server' ignored in --skip-name-resolve mode.``
146146
.. code-block:: text
147147
148148
select * from mysql.proxies_priv;
149-
delete from `mysql`.`proxies_priv` where (`host` = 'mariadb-server') and (`user` = 'root') and (`proxied_host` = '') and (`proxied_user` = '');
149+
delete from `mysql`.`proxies_priv`
150+
where (`host` = 'mariadb-server') and (`user` = 'root') and (`proxied_host` = '') and (`proxied_user` = '');
150151
151152
152153
Credits, License

check-plugins/mysql-logfile/mysql-logfile

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
2727
STATE_UNKNOWN, STATE_WARN)
2828

2929
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
30-
__version__ = '2023071203'
30+
__version__ = '2023090801'
3131

3232
DESCRIPTION = """Checks MySQL/MariaDB log content the same way MySQLTuner does, but also
3333
in case the DB is down."""
@@ -192,43 +192,50 @@ def main():
192192
except SystemExit:
193193
sys.exit(STATE_UNKNOWN)
194194

195-
mysql_connection = {
196-
'defaults_file': args.DEFAULTS_FILE,
197-
'defaults_group': args.DEFAULTS_GROUP,
198-
'timeout': args.TIMEOUT,
199-
}
200-
success, conn = lib.db_mysql.connect(mysql_connection)
201-
if not success:
202-
# DB seems to be down
203-
# Try to get the "log_error" setting and save it to cache, so that we know where to get the
204-
# log info from in case the DB is down.
205-
myvar = {}
206-
log_error = lib.cache.get('{}-{}'.format(
207-
args.HOSTNAME,
208-
args.PORT
209-
), filename='linuxfabrik-monitoring-plugins-mysql-logfile.db')
195+
if args.SERVER_LOG:
196+
# user told us to use this log source, so don't try to find anything else
197+
log_error = args.SERVER_LOG
210198
else:
211-
lib.base.coe(lib.db_mysql.check_select_privileges(conn))
212-
myvar = lib.db_mysql.lod2dict(get_vars(conn))
213-
lib.db_mysql.close(conn)
214-
log_error = myvar['log_error']
215-
216-
if not log_error:
217-
if not args.SERVER_LOG:
199+
# first ask the DB where to find the logfile
200+
mysql_connection = {
201+
'defaults_file': args.DEFAULTS_FILE,
202+
'defaults_group': args.DEFAULTS_GROUP,
203+
'timeout': args.TIMEOUT,
204+
}
205+
success, conn = lib.db_mysql.connect(mysql_connection)
206+
if not success:
207+
# DB seems to be down
208+
# Try to get the "log_error" setting and save it to cache, so that we know
209+
# where to get the log info from in case the DB is down again.
210+
myvar = {}
211+
log_error = lib.cache.get('{}-{}'.format(
212+
args.HOSTNAME,
213+
args.PORT
214+
),
215+
filename='linuxfabrik-monitoring-plugins-mysql-logfile.db',
216+
)
217+
else:
218+
lib.base.coe(lib.db_mysql.check_select_privileges(conn))
219+
myvar = lib.db_mysql.lod2dict(get_vars(conn))
220+
lib.db_mysql.close(conn)
221+
log_error = myvar['log_error']
222+
if not log_error:
218223
log_error = get_log_file_real_path(
219224
myvar.get('log_error', ''),
220225
myvar.get('hostname', ''),
221226
myvar.get('datadir', ''),
222227
)
223-
else:
224-
log_error = args.SERVER_LOG
225-
if not log_error:
226-
lib.base.cu('No log file set (set log_error in MySQL/MariaDB config or use the check\'s --server-log parameter).')
228+
if not log_error:
229+
lib.base.cu('No log file set (set `log_error` in MySQL/MariaDB config or use the check\'s `--server-log` parameter).')
227230

228231
lib.cache.set('{}-{}'.format(
229-
args.HOSTNAME,
230-
args.PORT
231-
), log_error, lib.time.now() + args.CACHE_EXPIRE*60, filename='linuxfabrik-monitoring-plugins-mysql-logfile.db')
232+
args.HOSTNAME,
233+
args.PORT
234+
),
235+
log_error,
236+
lib.time.now() + args.CACHE_EXPIRE*60,
237+
filename='linuxfabrik-monitoring-plugins-mysql-logfile.db',
238+
)
232239

233240
# init some vars
234241
msg, msg_body = '', ''
@@ -251,19 +258,19 @@ def main():
251258
cmd = "journalctl -n {} -b -u '{}'".format(MAXLINES, match.group(1).strip())
252259
logfile, stderr, retc = lib.base.coe(lib.shell.shell_exec(cmd))
253260
if retc != 0:
254-
lib.base.cu('{} exited with error ({}, {}).'.format(cmd, retc, stderr.strip()))
261+
lib.base.cu('`{}` exited with error ({}, {}).'.format(cmd, retc, stderr.strip()))
255262
msg += 'Src: {}, '.format(log_error)
256263
else:
257264
# good old logfile
258265
if not os.path.isabs(log_error):
259266
log_error = os.path.join(myvar.get('datadir', ''), log_error)
260267
if not os.path.isfile(log_error):
261-
msg = 'Logging seems to be configured, but {} does not seem to be an existing regular file. '.format(log_error)
262-
msg += 'Check the path and file permissions, or provide the --server-log parameter.'
268+
msg = 'Logging seems to be configured, but `{}` does not seem to be an existing regular file. '.format(log_error)
269+
msg += 'Check the path and file permissions, or provide the `--server-log` parameter.'
263270
lib.base.oao(msg, STATE_WARN)
264271
size = os.path.getsize(log_error)
265272
if size == 0:
266-
lib.base.oao('Empty log file {} found. Assuming log-rotation. Use `--server-log` for explicit file.'.format(log_error), STATE_OK)
273+
lib.base.oao('Empty log file `{}` found. Assuming log-rotation. Use `--server-log` for explicit file.'.format(log_error), STATE_OK)
267274
msg += 'Src: Log file {} (size: {}), '.format(
268275
log_error,
269276
lib.human.bytes2human(size),

0 commit comments

Comments
 (0)