git.lirion.de

Of git, get, and gud

summaryrefslogtreecommitdiffstats
path: root/nagios-plugins-contrib-24.20190301~bpo9+1/percona-nagios-plugins/nagios/bin/pmp-check-mysql-innodb
blob: 038f56497b70448a2aac2ec429629640e69da2d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/bin/sh

# ########################################################################
# This program is part of $PROJECT_NAME$
# License: GPL License (see COPYING)
# Authors:
#  Baron Schwartz, Roman Vynar
# ########################################################################

# ########################################################################
# Redirect STDERR to STDOUT; Nagios doesn't handle STDERR.
# ########################################################################
exec 2>&1

# ########################################################################
# Set up constants, etc.
# ########################################################################
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

# ########################################################################
# Run the program.
# ########################################################################
main() {
   # Get options
   for o; do
      case "${o}" in
         -C)              shift; OPT_CHEK="${1}"; shift; ;;
         -c)              shift; OPT_CRIT="${1}"; shift; ;;
         --defaults-file) shift; OPT_DEFT="${1}"; shift; ;;
         -H)              shift; OPT_HOST="${1}"; shift; ;;
         -l)              shift; OPT_USER="${1}"; shift; ;;
         -L)              shift; OPT_LOPA="${1}"; shift; ;;
         -p)              shift; OPT_PASS="${1}"; shift; ;;
         -P)              shift; OPT_PORT="${1}"; shift; ;;
         -S)              shift; OPT_SOCK="${1}"; shift; ;;
         -w)              shift; OPT_WARN="${1}"; shift; ;;
         --version)       grep -A2 '^=head1 VERSION' "$0" | tail -n1; exit 0 ;;
         --help)          perl -00 -ne 'm/^  Usage:/ && print' "$0"; exit 0 ;;
         -*)              echo "Unknown option ${o}.  Try --help."; exit 1; ;;
      esac
   done
   OPT_CHEK="${OPT_CHEK:-idle_blocker_duration}"
   if [ -e '/etc/nagios/mysql.cnf' ]; then
      OPT_DEFT="${OPT_DEFT:-/etc/nagios/mysql.cnf}"
   fi
   if is_not_sourced; then
      if [ -n "$1" ]; then
         echo "WARN spurious command-line options: $ _æ_ "
         exit 1
      fi
   fi
   NOTE="UNK could not find information about transactions"

   local TEMP=$(mktemp -t "${0##*/}.XXXXXX") || exit $?
   trap "rm -f '${TEMP}' >/dev/null 2>&1" EXIT

   case "${OPT_CHEK}" in
      idle_blocker_duration)
         OPT_WARN=${OPT_WARN:-60}
         OPT_CRIT=${OPT_CRIT:-600}
         OUTPUT=$(mysql_exec "
            SELECT MAX(COALESCE(p.time, 0)) AS idle_in_trx,
                   p.id,
                   CONCAT(p.user, ' _æ_ ', p.host) AS userhost
            FROM       INFORMATION_SCHEMA.INNODB_LOCK_WAITS AS w
            INNER JOIN INFORMATION_SCHEMA.INNODB_TRX        AS b ON  b.trx_id = w.blocking_trx_id
            INNER JOIN INFORMATION_SCHEMA.INNODB_TRX        AS r ON  r.trx_id = w.requesting_trx_id
            LEFT  JOIN INFORMATION_SCHEMA.PROCESSLIST       AS p ON  p.id     = b.trx_mysql_thread_id AND p.command = 'Sleep'
            GROUP BY p.id, p.user, p.host
            ORDER BY idle_in_trx DESC LIMIT 1" 2>"${TEMP}")
         if [ "$?" != 0 ]; then
            if grep "Unknown table" "${TEMP}" >/dev/null 2>&1; then
               # The I_S tables don't exist.
               NOTE="OK The INFORMATION_SCHEMA.INNODB_% tables don't exist."
            else
               cat "${TEMP}"
            fi
         elif [ -z "${OUTPUT}" ]; then
            OUTPUT=0
         fi
         ;;
      waiter_count)
         OPT_WARN=${OPT_WARN:-10}
         OPT_CRIT=${OPT_CRIT:-25}
         OUTPUT=$(mysql_exec "
            SELECT COUNT(DISTINCT REQUESTING_TRX_ID) AS cnt
            FROM   INFORMATION_SCHEMA.INNODB_LOCK_WAITS AS w" 2>"${TEMP}")
         if [ "$?" != 0 ]; then
            if grep "Unknown table" "${TEMP}" >/dev/null 2>&1; then
               # The I_S tables don't exist.
               mysql_exec "SHOW /*!50000 ENGINE*/ INNODB STATUS\G" > "${TEMP}" || exit $?
               OUTPUT=$(get_waiter_count "${TEMP}")
            else
               cat "${TEMP}"
            fi
         fi
         ;;
      max_duration)
         OPT_WARN=${OPT_WARN:-60}
         OPT_CRIT=${OPT_CRIT:-600}
         OUTPUT=$(mysql_exec "
            SET  _æ_  _æ_ time_zone='SYSTEM';
            SELECT   UNIX_TIMESTAMP() - UNIX_TIMESTAMP(t.trx_started),
                     p.id,
                     CONCAT(p.user, ' _æ_ ', p.host)
            FROM     INFORMATION_SCHEMA.INNODB_TRX  AS t
            JOIN     INFORMATION_SCHEMA.PROCESSLIST AS p ON p.id = t.trx_mysql_thread_id
            ORDER BY t.trx_started LIMIT 1" 2>"${TEMP}")
         if [ "$?" != 0 ]; then
            if grep "Unknown table" "${TEMP}" >/dev/null 2>&1; then
               # The I_S tables don't exist.
               mysql_exec "SHOW /*!50000 ENGINE*/ INNODB STATUS\G" > "${TEMP}" || exit $?
               OUTPUT=$(get_longest_trx "${TEMP}")
               if [ -z "${OUTPUT}" ]; then
                  OUTPUT=0
               fi
            else
               cat "${TEMP}"
            fi
         elif [ -z "${OUTPUT}" ]; then
            OUTPUT=0
         fi
         ;;
      *)
         echo "Unknown value for -C: '${OPT_CHEK}'. Consult the documentation.";
         exit 1;
         ;;
   esac

   # $OUTPUT now contains either an empty string or three words: 1) Age of
   # oldest transaction, 2) thread ID of oldest transaction, 3) user info.
   if [ -n "${OUTPUT}" ]; then
      LEVEL="$(echo ${OUTPUT} | awk '{print $1}')"
      INFO="$(echo ${OUTPUT} | awk '{print "(thread "$2" by "$3")"}')"

      case "${OPT_CHEK}" in
         idle_blocker_duration)
            NOTE="longest blocking idle transaction sleeps for ${LEVEL:-UNKNOWN} seconds"
            ;;
         waiter_count)
            NOTE="${LEVEL:-UNKNOWN} transactions in LOCK WAIT status"
            INFO=""
            ;;
         max_duration)
            NOTE="longest transaction active for ${LEVEL:-UNKNOWN} seconds"
            ;;
      esac
      if [ "${LEVEL:-0}" -gt "${OPT_CRIT}" ]; then
         NOTE="CRIT $NOTE $INFO"
      elif [ "${LEVEL:-0}" -gt "${OPT_WARN}" ]; then
         NOTE="WARN $NOTE $INFO"
      else
         NOTE="OK $NOTE"
      fi
   fi
   echo $NOTE
}

# ########################################################################
# Execute a MySQL command.
# ########################################################################
mysql_exec() {
   mysql ${OPT_DEFT:+--defaults-file="${OPT_DEFT}"} \
      ${OPT_LOPA:+--login-path="${OPT_LOPA}"} \
      ${OPT_HOST:+-h"${OPT_HOST}"} ${OPT_PORT:+-P"${OPT_PORT}"} \
      ${OPT_USER:+-u"${OPT_USER}"} ${OPT_PASS:+-p"${OPT_PASS}"} \
      ${OPT_SOCK:+-S"${OPT_SOCK}"} -ss -e "$1"
}

# ########################################################################
# Gets max txn time in SHOW INNODB STATUS. File is $1.
# ########################################################################
get_longest_trx() {
   awk '
      BEGIN {
         maxtime = 0;
         thread  = 0;
         userinfo = "nobody";
      }
      /^TRANSACTIONS$/ {
         tseen = 1;
      }
      {
         if ( tseen == 1 && $0 ~ /^---TRANSACTION.*[0-9] sec/ ) {
            if ( $2 ~ /,/ ) {
               time = $4;
            }
            else {
               time = $5;
            }
         }
         if ( tseen == 1 && $0 ~ /^MySQL thread id/ ) {
            if ( time > maxtime ) {
               maxtime = time;
               thread  = substr($4, 1, length($4) - 1);
               match($0, /query id [0-9]+ .+/);
               userinfo = substr($0, RSTART, RLENGTH);
               split(userinfo, a, " ");
               userinfo = a[5]" _æ_ "a[4];
            }
         }
      }
      END {
         print maxtime, thread, userinfo;
      }
      ' "${1}"
}

# ########################################################################
# Counts the number of LOCK WAIT in SHOW INNODB STATUS. File is $1.
# ########################################################################
get_waiter_count() {
   awk '
      BEGIN {
         lock_waits = 0;
      }
      /^TRANSACTIONS$/ {
         tseen = 1;
      }
      {
         if ( tseen == 1 && $0 ~ /TRX HAS BEEN WAITING/ ) {
            lock_waits++;
         }
      }
      END {
         print lock_waits;
      }
      ' "${1}"
}

# ########################################################################
# Determine whether this program is being executed directly, or sourced/included
# from another file.
# ########################################################################
is_not_sourced() {
   [ "${0##*/}" = "pmp-check-mysql-innodb" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ]
}

# ########################################################################
# Execute the program if it was not included from another file.
# This makes it possible to include without executing, and thus test.
# ########################################################################
if is_not_sourced; then
   OUTPUT=$(main "$ _æ_ ")
   EXITSTATUS=$STATE_UNKNOWN
   case "${OUTPUT}" in
      UNK*)  EXITSTATUS=$STATE_UNKNOWN;  ;;
      OK*)   EXITSTATUS=$STATE_OK;       ;;
      WARN*) EXITSTATUS=$STATE_WARNING;  ;;
      CRIT*) EXITSTATUS=$STATE_CRITICAL; ;;
   esac
   echo "${OUTPUT}"
   exit $EXITSTATUS
fi

# ############################################################################
# Documentation
# ############################################################################
: <<'DOCUMENTATION'
=pod

=head1 NAME

pmp-check-mysql-innodb - Alert on problems inside InnoDB.

=head1 SYNOPSIS

  Usage: pmp-check-mysql-innodb [OPTIONS]
  Options:
    -C CHECK        What to alert on; default idle_blocker_duration.
                    Other options: waiter_count, max_duration.
    -c CRIT         Critical threshold; default varies.
    --defaults-file FILE Only read mysql options from the given file.
                    Defaults to /etc/nagios/mysql.cnf if it exists.
    -H HOST         MySQL hostname.
    -l USER         MySQL username.
    -L LOGIN-PATH   Use login-path to access MySQL (with MySQL client 5.6).
    -p PASS         MySQL password.
    -P PORT         MySQL port.
    -S SOCKET       MySQL socket file.
    -w WARN         Warning threshold; default varies.
    --help          Print help and exit.
    --version       Print version and exit.
  Options must be given as --option value, not --option=value or -Ovalue.
  Use perldoc to read embedded documentation with more details.

=head1 DESCRIPTION

This Nagios plugin alerts on various aspects of InnoDB status in several ways,
depending on the value of the -C option:

=over

=item idle_blocker_duration

This is the default behavior.  It alerts when a long-running transaction is
blocking another, and the blocker is idle (Sleep).  The threshold is based on
how long the transaction has been idle.  Long-running idle transactions that
have acquired locks but not released them are a frequent cause of application
downtime due to lock wait timeouts and rollbacks, especially because
applications are often not designed to handle such errors correctly.  The
problem is usually due to another error that causes a transaction not to be
committed, such as performing very long tasks in the application while holding
the transaction open.

This check examines the INFORMATION_SCHEMA tables included with InnoDB version
1.0 and newer. The default critical level is 600, and warning is 60.  If the
tables do not exist, the exit status is OK, with a note that the tables do not
exist.

=item waiter_count

Alerts if too many transactions are in LOCK WAIT status. Uses information from
SHOW ENGINE INNODB STATUS if the INFORMATION_SCHEMA tables are not available.
The default critical level is 25, and warning is 10.

=item max_duration

Alerts if any transaction is too old.  Uses information from SHOW ENGINE INNODB
STATUS if the INFORMATION_SCHEMA tables are not available. The default critical
level is 600, and warning is 60.

=back

=head1 PRIVILEGES

This plugin executes the following commands against MySQL:

=over

=item *

C.

=item *

C