Debian Codegraph @

percona-nagios-plugins/nagios/bin/pmp-check-mysql-innodb

raw symbols SCIP
1 #!/bin/sh
2
3 # ########################################################################
4 # This program is part of $PROJECT_NAME$
5 # License: GPL License (see COPYING)
6 # Authors:
7 # Baron Schwartz, Roman Vynar
8 # ########################################################################
9
10 # ########################################################################
11 # Redirect STDERR to STDOUT; Nagios doesn't handle STDERR.
12 # ########################################################################
13 exec 2>&1
14
15 # ########################################################################
16 # Set up constants, etc.
17 # ########################################################################
18 STATE_OK=0
19 STATE_WARNING=1
20 STATE_CRITICAL=2
21 STATE_UNKNOWN=3
22 STATE_DEPENDENT=4
23
24 # ########################################################################
25 # Run the program.
26 # ########################################################################
27 main() {
28 # Get options
29 for o; do
30 case "${o}" in
31 -C) shift; OPT_CHEK="${1}"; shift; ;;
32 -c) shift; OPT_CRIT="${1}"; shift; ;;
33 --defaults-file) shift; OPT_DEFT="${1}"; shift; ;;
34 -H) shift; OPT_HOST="${1}"; shift; ;;
35 -l) shift; OPT_USER="${1}"; shift; ;;
36 -L) shift; OPT_LOPA="${1}"; shift; ;;
37 -p) shift; OPT_PASS="${1}"; shift; ;;
38 -P) shift; OPT_PORT="${1}"; shift; ;;
39 -S) shift; OPT_SOCK="${1}"; shift; ;;
40 -w) shift; OPT_WARN="${1}"; shift; ;;
41 --version) grep -A2 '^=head1 VERSION' "$0" | tail -n1; exit 0 ;;
42 --help) perl -00 -ne 'm/^ Usage:/ && print' "$0"; exit 0 ;;
43 -*) echo "Unknown option ${o}. Try --help."; exit 1; ;;
44 esac
45 done
46 OPT_CHEK="${OPT_CHEK:-idle_blocker_duration}"
47 if [ -e '/etc/nagios/mysql.cnf' ]; then
48 OPT_DEFT="${OPT_DEFT:-/etc/nagios/mysql.cnf}"
49 fi
50 if is_not_sourced; then
51 if [ -n "$1" ]; then
52 echo "WARN spurious command-line options: $@"
53 exit 1
54 fi
55 fi
56 NOTE="UNK could not find information about transactions"
57
58 local TEMP=$(mktemp -t "${0##*/}.XXXXXX") || exit $?
59 trap "rm -f '${TEMP}' >/dev/null 2>&1" EXIT
60
61 case "${OPT_CHEK}" in
62 idle_blocker_duration)
63 OPT_WARN=${OPT_WARN:-60}
64 OPT_CRIT=${OPT_CRIT:-600}
65 OUTPUT=$(mysql_exec "
66 SELECT MAX(COALESCE(p.time, 0)) AS idle_in_trx,
67 p.id,
68 CONCAT(p.user, '@', p.host) AS userhost
69 FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS AS w
70 INNER JOIN INFORMATION_SCHEMA.INNODB_TRX AS b ON b.trx_id = w.blocking_trx_id
71 INNER JOIN INFORMATION_SCHEMA.INNODB_TRX AS r ON r.trx_id = w.requesting_trx_id
72 LEFT JOIN INFORMATION_SCHEMA.PROCESSLIST AS p ON p.id = b.trx_mysql_thread_id AND p.command = 'Sleep'
73 GROUP BY p.id, p.user, p.host
74 ORDER BY idle_in_trx DESC LIMIT 1" 2>"${TEMP}")
75 if [ "$?" != 0 ]; then
76 if grep "Unknown table" "${TEMP}" >/dev/null 2>&1; then
77 # The I_S tables don't exist.
78 NOTE="OK The INFORMATION_SCHEMA.INNODB_% tables don't exist."
79 else
80 cat "${TEMP}"
81 fi
82 elif [ -z "${OUTPUT}" ]; then
83 OUTPUT=0
84 fi
85 ;;
86 waiter_count)
87 OPT_WARN=${OPT_WARN:-10}
88 OPT_CRIT=${OPT_CRIT:-25}
89 OUTPUT=$(mysql_exec "
90 SELECT COUNT(DISTINCT REQUESTING_TRX_ID) AS cnt
91 FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS AS w" 2>"${TEMP}")
92 if [ "$?" != 0 ]; then
93 if grep "Unknown table" "${TEMP}" >/dev/null 2>&1; then
94 # The I_S tables don't exist.
95 mysql_exec "SHOW /*!50000 ENGINE*/ INNODB STATUS\G" > "${TEMP}" || exit $?
96 OUTPUT=$(get_waiter_count "${TEMP}")
97 else
98 cat "${TEMP}"
99 fi
100 fi
101 ;;
102 max_duration)
103 OPT_WARN=${OPT_WARN:-60}
104 OPT_CRIT=${OPT_CRIT:-600}
105 OUTPUT=$(mysql_exec "
106 SET @@time_zone='SYSTEM';
107 SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(t.trx_started),
108 p.id,
109 CONCAT(p.user, '@', p.host)
110 FROM INFORMATION_SCHEMA.INNODB_TRX AS t
111 JOIN INFORMATION_SCHEMA.PROCESSLIST AS p ON p.id = t.trx_mysql_thread_id
112 ORDER BY t.trx_started LIMIT 1" 2>"${TEMP}")
113 if [ "$?" != 0 ]; then
114 if grep "Unknown table" "${TEMP}" >/dev/null 2>&1; then
115 # The I_S tables don't exist.
116 mysql_exec "SHOW /*!50000 ENGINE*/ INNODB STATUS\G" > "${TEMP}" || exit $?
117 OUTPUT=$(get_longest_trx "${TEMP}")
118 if [ -z "${OUTPUT}" ]; then
119 OUTPUT=0
120 fi
121 else
122 cat "${TEMP}"
123 fi
124 elif [ -z "${OUTPUT}" ]; then
125 OUTPUT=0
126 fi
127 ;;
128 *)
129 echo "Unknown value for -C: '${OPT_CHEK}'. Consult the documentation.";
130 exit 1;
131 ;;
132 esac
133
134 # $OUTPUT now contains either an empty string or three words: 1) Age of
135 # oldest transaction, 2) thread ID of oldest transaction, 3) user info.
136 if [ -n "${OUTPUT}" ]; then
137 LEVEL="$(echo ${OUTPUT} | awk '{print $1}')"
138 INFO="$(echo ${OUTPUT} | awk '{print "(thread "$2" by "$3")"}')"
139
140 case "${OPT_CHEK}" in
141 idle_blocker_duration)
142 NOTE="longest blocking idle transaction sleeps for ${LEVEL:-UNKNOWN} seconds"
143 ;;
144 waiter_count)
145 NOTE="${LEVEL:-UNKNOWN} transactions in LOCK WAIT status"
146 INFO=""
147 ;;
148 max_duration)
149 NOTE="longest transaction active for ${LEVEL:-UNKNOWN} seconds"
150 ;;
151 esac
152 if [ "${LEVEL:-0}" -gt "${OPT_CRIT}" ]; then
153 NOTE="CRIT $NOTE $INFO"
154 elif [ "${LEVEL:-0}" -gt "${OPT_WARN}" ]; then
155 NOTE="WARN $NOTE $INFO"
156 else
157 NOTE="OK $NOTE"
158 fi
159 fi
160 echo $NOTE
161 }
162
163 # ########################################################################
164 # Execute a MySQL command.
165 # ########################################################################
166 mysql_exec() {
167 mysql ${OPT_DEFT:+--defaults-file="${OPT_DEFT}"} \
168 ${OPT_LOPA:+--login-path="${OPT_LOPA}"} \
169 ${OPT_HOST:+-h"${OPT_HOST}"} ${OPT_PORT:+-P"${OPT_PORT}"} \
170 ${OPT_USER:+-u"${OPT_USER}"} ${OPT_PASS:+-p"${OPT_PASS}"} \
171 ${OPT_SOCK:+-S"${OPT_SOCK}"} -ss -e "$1"
172 }
173
174 # ########################################################################
175 # Gets max txn time in SHOW INNODB STATUS. File is $1.
176 # ########################################################################
177 get_longest_trx() {
178 awk '
179 BEGIN {
180 maxtime = 0;
181 thread = 0;
182 userinfo = "nobody";
183 }
184 /^TRANSACTIONS$/ {
185 tseen = 1;
186 }
187 {
188 if ( tseen == 1 && $0 ~ /^---TRANSACTION.*[0-9] sec/ ) {
189 if ( $2 ~ /,/ ) {
190 time = $4;
191 }
192 else {
193 time = $5;
194 }
195 }
196 if ( tseen == 1 && $0 ~ /^MySQL thread id/ ) {
197 if ( time > maxtime ) {
198 maxtime = time;
199 thread = substr($4, 1, length($4) - 1);
200 match($0, /query id [0-9]+ .+/);
201 userinfo = substr($0, RSTART, RLENGTH);
202 split(userinfo, a, " ");
203 userinfo = a[5]"@"a[4];
204 }
205 }
206 }
207 END {
208 print maxtime, thread, userinfo;
209 }
210 ' "${1}"
211 }
212
213 # ########################################################################
214 # Counts the number of LOCK WAIT in SHOW INNODB STATUS. File is $1.
215 # ########################################################################
216 get_waiter_count() {
217 awk '
218 BEGIN {
219 lock_waits = 0;
220 }
221 /^TRANSACTIONS$/ {
222 tseen = 1;
223 }
224 {
225 if ( tseen == 1 && $0 ~ /TRX HAS BEEN WAITING/ ) {
226 lock_waits++;
227 }
228 }
229 END {
230 print lock_waits;
231 }
232 ' "${1}"
233 }
234
235 # ########################################################################
236 # Determine whether this program is being executed directly, or sourced/included
237 # from another file.
238 # ########################################################################
239 is_not_sourced() {
240 [ "${0##*/}" = "pmp-check-mysql-innodb" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ]
241 }
242
243 # ########################################################################
244 # Execute the program if it was not included from another file.
245 # This makes it possible to include without executing, and thus test.
246 # ########################################################################
247 if is_not_sourced; then
248 OUTPUT=$(main "$@")
249 EXITSTATUS=$STATE_UNKNOWN
250 case "${OUTPUT}" in
251 UNK*) EXITSTATUS=$STATE_UNKNOWN; ;;
252 OK*) EXITSTATUS=$STATE_OK; ;;
253 WARN*) EXITSTATUS=$STATE_WARNING; ;;
254 CRIT*) EXITSTATUS=$STATE_CRITICAL; ;;
255 esac
256 echo "${OUTPUT}"
257 exit $EXITSTATUS
258 fi
259
260 # ############################################################################
261 # Documentation
262 # ############################################################################
263 : <<'DOCUMENTATION'
264 =pod
265
266 =head1 NAME
267
268 pmp-check-mysql-innodb - Alert on problems inside InnoDB.
269
270 =head1 SYNOPSIS
271
272 Usage: pmp-check-mysql-innodb [OPTIONS]
273 Options:
274 -C CHECK What to alert on; default idle_blocker_duration.
275 Other options: waiter_count, max_duration.
276 -c CRIT Critical threshold; default varies.
277 --defaults-file FILE Only read mysql options from the given file.
278 Defaults to /etc/nagios/mysql.cnf if it exists.
279 -H HOST MySQL hostname.
280 -l USER MySQL username.
281 -L LOGIN-PATH Use login-path to access MySQL (with MySQL client 5.6).
282 -p PASS MySQL password.
283 -P PORT MySQL port.
284 -S SOCKET MySQL socket file.
285 -w WARN Warning threshold; default varies.
286 --help Print help and exit.
287 --version Print version and exit.
288 Options must be given as --option value, not --option=value or -Ovalue.
289 Use perldoc to read embedded documentation with more details.
290
291 =head1 DESCRIPTION
292
293 This Nagios plugin alerts on various aspects of InnoDB status in several ways,
294 depending on the value of the -C option:
295
296 =over
297
298 =item idle_blocker_duration
299
300 This is the default behavior. It alerts when a long-running transaction is
301 blocking another, and the blocker is idle (Sleep). The threshold is based on
302 how long the transaction has been idle. Long-running idle transactions that
303 have acquired locks but not released them are a frequent cause of application
304 downtime due to lock wait timeouts and rollbacks, especially because
305 applications are often not designed to handle such errors correctly. The
306 problem is usually due to another error that causes a transaction not to be
307 committed, such as performing very long tasks in the application while holding
308 the transaction open.
309
310 This check examines the INFORMATION_SCHEMA tables included with InnoDB version
311 1.0 and newer. The default critical level is 600, and warning is 60. If the
312 tables do not exist, the exit status is OK, with a note that the tables do not
313 exist.
314
315 =item waiter_count
316
317 Alerts if too many transactions are in LOCK WAIT status. Uses information from
318 SHOW ENGINE INNODB STATUS if the INFORMATION_SCHEMA tables are not available.
319 The default critical level is 25, and warning is 10.
320
321 =item max_duration
322
323 Alerts if any transaction is too old. Uses information from SHOW ENGINE INNODB
324 STATUS if the INFORMATION_SCHEMA tables are not available. The default critical
325 level is 600, and warning is 60.
326
327 =back
328
329 =head1 PRIVILEGES
330
331 This plugin executes the following commands against MySQL:
332
333 =over
334
335 =item *
336
337 C<SHOW ENGINE INNODB STATUS>.
338
339 =item *
340
341 C<SELECT> against the C<INFORMATION_SCHEMA> InnoDB transaction and lock tables.
342
343 =back
344
345 This plugin executes no UNIX commands that may need special privileges.
346
347 =head1 COPYRIGHT, LICENSE, AND WARRANTY
348
349 This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ Percona Inc.
350 Feedback and improvements are welcome.
351
352 THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
353 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
354 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
355
356 This program is free software; you can redistribute it and/or modify it under
357 the terms of the GNU General Public License as published by the Free Software
358 Foundation, version 2. You should have received a copy of the GNU General
359 Public License along with this program; if not, write to the Free Software
360 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
361
362 =head1 VERSION
363
364 $PROJECT_NAME$ pmp-check-mysql-innodb $VERSION$
365
366 =cut
367
368 DOCUMENTATION