| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# ######################################################################## |
| 4 |
# This program is part of $PROJECT_NAME$ |
| 5 |
# License: GPL License (see COPYING) |
| 6 |
# Authors: |
| 7 |
# Baron Schwartz, Ben Mildren |
| 8 |
# Depends-on: table with ts column, e.g. table written by pt-deadlock-logger |
| 9 |
# ######################################################################## |
| 10 |
|
| 11 |
# ######################################################################## |
| 12 |
# Redirect STDERR to STDOUT; Nagios doesn't handle STDERR. |
| 13 |
# ######################################################################## |
| 14 |
exec 2>&1 |
| 15 |
|
| 16 |
# ######################################################################## |
| 17 |
# Set up constants, etc. |
| 18 |
# ######################################################################## |
| 19 |
STATE_OK=0 |
| 20 |
STATE_OK=0 |
| 21 |
STATE_WARNING=1 |
| 22 |
STATE_CRITICAL=2 |
| 23 |
STATE_UNKNOWN=3 |
| 24 |
STATE_DEPENDENT=4 |
| 25 |
|
| 26 |
# ######################################################################## |
| 27 |
# Run the program. |
| 28 |
# ######################################################################## |
| 29 |
main() { |
| 30 |
# Get options |
| 31 |
for o; do |
| 32 |
case "${o}" in |
| 33 |
-c) shift; OPT_CRIT="${1}"; shift; ;; |
| 34 |
--defaults-file) shift; OPT_DEFT="${1}"; shift; ;; |
| 35 |
-H) shift; OPT_HOST="${1}"; shift; ;; |
| 36 |
-i) shift; OPT_INTERVAL="${1}"; shift; ;; |
| 37 |
-l) shift; OPT_USER="${1}"; shift; ;; |
| 38 |
-L) shift; OPT_LOPA="${1}"; shift; ;; |
| 39 |
-p) shift; OPT_PASS="${1}"; shift; ;; |
| 40 |
-P) shift; OPT_PORT="${1}"; shift; ;; |
| 41 |
-S) shift; OPT_SOCK="${1}"; shift; ;; |
| 42 |
-t) shift; OPT_TIMESTAMP="${1}"; shift; ;; |
| 43 |
-T) shift; OPT_TABLE="${1}"; shift; ;; |
| 44 |
-w) shift; OPT_WARN="${1}"; shift; ;; |
| 45 |
-x) shift; OPT_TARGET="${1}"; shift; ;; |
| 46 |
--version) grep -A2 '^=head1 VERSION' "$0" | tail -n1; exit 0 ;; |
| 47 |
--help) perl -00 -ne 'm/^ Usage:/ && print' "$0"; exit 0 ;; |
| 48 |
-*) echo "Unknown option ${o}. Try --help."; exit 1; ;; |
| 49 |
esac |
| 50 |
done |
| 51 |
OPT_WARN=${OPT_WARN:-12} |
| 52 |
OPT_CRIT=${OPT_CRIT:-60} |
| 53 |
OPT_INTERVAL=${OPT_INTERVAL:-1} |
| 54 |
OPT_TARGET="${OPT_TARGET:-deadlocks}" |
| 55 |
if [ -e '/etc/nagios/mysql.cnf' ]; then |
| 56 |
OPT_DEFT="${OPT_DEFT:-/etc/nagios/mysql.cnf}" |
| 57 |
fi |
| 58 |
if is_not_sourced; then |
| 59 |
if [ -n "$1" ]; then |
| 60 |
echo "WARN spurious command-line options: $@" |
| 61 |
exit 1 |
| 62 |
fi |
| 63 |
fi |
| 64 |
if [ "${OPT_TARGET}" == "kills" ]; then |
| 65 |
OPT_TABLE="${OPT_TABLE:-percona.kill_log}" |
| 66 |
OPT_TIMESTAMP="${OPT_TIMESTAMP:-timestamp}" |
| 67 |
elif [ "${OPT_TARGET}" == "fkerrors" ]; then |
| 68 |
OPT_TABLE="${OPT_TABLE:-percona.foreign_key_errors}" |
| 69 |
OPT_TIMESTAMP="${OPT_TIMESTAMP:-ts}" |
| 70 |
else |
| 71 |
OPT_TABLE="${OPT_TABLE:-percona.deadlocks}" |
| 72 |
OPT_TIMESTAMP="${OPT_TIMESTAMP:-ts}" |
| 73 |
fi |
| 74 |
|
| 75 |
LEVEL=$(mysql_exec "SELECT COUNT(*) FROM ${OPT_TABLE} WHERE ${OPT_TIMESTAMP} >= NOW() - INTERVAL ${OPT_INTERVAL}*60 SECOND") |
| 76 |
if [ $? = 0 ]; then |
| 77 |
NOTE="${LEVEL:-UNKNOWN} ${OPT_TARGET} in last ${OPT_INTERVAL} minutes" |
| 78 |
if [ "${LEVEL:-0}" -gt "${OPT_CRIT}" ]; then |
| 79 |
NOTE="CRIT $NOTE" |
| 80 |
elif [ "${LEVEL:-0}" -gt "${OPT_WARN}" ]; then |
| 81 |
NOTE="WARN $NOTE" |
| 82 |
else |
| 83 |
NOTE="OK $NOTE" |
| 84 |
fi |
| 85 |
|
| 86 |
# Build the common perf data output for graph trending |
| 87 |
PERFDATA="${OPT_TARGET}=${LEVEL:-0};${OPT_WARN};${OPT_CRIT};0;" |
| 88 |
NOTE="$NOTE | $PERFDATA" |
| 89 |
else |
| 90 |
NOTE="UNK could not count ${OPT_TARGET}" |
| 91 |
fi |
| 92 |
echo $NOTE |
| 93 |
} |
| 94 |
|
| 95 |
# ######################################################################## |
| 96 |
# Execute a MySQL command. |
| 97 |
# ######################################################################## |
| 98 |
mysql_exec() { |
| 99 |
mysql ${OPT_DEFT:+--defaults-file="${OPT_DEFT}"} \ |
| 100 |
${OPT_LOPA:+--login-path="${OPT_LOPA}"} \ |
| 101 |
${OPT_HOST:+-h"${OPT_HOST}"} ${OPT_PORT:+-P"${OPT_PORT}"} \ |
| 102 |
${OPT_USER:+-u"${OPT_USER}"} ${OPT_PASS:+-p"${OPT_PASS}"} \ |
| 103 |
${OPT_SOCK:+-S"${OPT_SOCK}"} -ss -e "$1" |
| 104 |
} |
| 105 |
|
| 106 |
# ######################################################################## |
| 107 |
# Determine whether this program is being executed directly, or sourced/included |
| 108 |
# from another file. |
| 109 |
# ######################################################################## |
| 110 |
is_not_sourced() { |
| 111 |
[ "${0##*/}" = "pmp-check-mysql-ts-count" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ] |
| 112 |
} |
| 113 |
|
| 114 |
# ######################################################################## |
| 115 |
# Execute the program if it was not included from another file. |
| 116 |
# This makes it possible to include without executing, and thus test. |
| 117 |
# ######################################################################## |
| 118 |
if is_not_sourced; then |
| 119 |
OUTPUT=$(main "$@") |
| 120 |
EXITSTATUS=$STATE_UNKNOWN |
| 121 |
case "${OUTPUT}" in |
| 122 |
UNK*) EXITSTATUS=$STATE_UNKNOWN; ;; |
| 123 |
OK*) EXITSTATUS=$STATE_OK; ;; |
| 124 |
WARN*) EXITSTATUS=$STATE_WARNING; ;; |
| 125 |
CRIT*) EXITSTATUS=$STATE_CRITICAL; ;; |
| 126 |
esac |
| 127 |
echo "${OUTPUT}" |
| 128 |
exit $EXITSTATUS |
| 129 |
fi |
| 130 |
|
| 131 |
# ############################################################################ |
| 132 |
# Documentation |
| 133 |
# ############################################################################ |
| 134 |
: <<'DOCUMENTATION' |
| 135 |
=pod |
| 136 |
|
| 137 |
=head1 NAME |
| 138 |
|
| 139 |
pmp-check-mysql-ts-count - Generic alert based on pmp-check-mysql-deadlocks to count number of rows written in the last interval. |
| 140 |
|
| 141 |
=head1 SYNOPSIS |
| 142 |
|
| 143 |
Usage: pmp-check-mysql-ts-count [OPTIONS] |
| 144 |
Options: |
| 145 |
-c CRIT Critical threshold; default 60. |
| 146 |
--defaults-file FILE Only read mysql options from the given file. |
| 147 |
Defaults to /etc/nagios/mysql.cnf if it exists. |
| 148 |
-H HOST MySQL hostname. |
| 149 |
-i INTERVAL Interval over which to count, in minutes; default 1. |
| 150 |
-l USER MySQL username. |
| 151 |
-L LOGIN-PATH Use login-path to access MySQL (with MySQL client 5.6). |
| 152 |
-p PASS MySQL password. |
| 153 |
-P PORT MySQL port. |
| 154 |
-S SOCKET MySQL socket file. |
| 155 |
-t TIMESTAMP The name of the timestamp column to be monitored; default ts. |
| 156 |
-T TABLE The database.table to be monitored; default percona.deadlocks. |
| 157 |
-w WARN Warning threshold; default 12. |
| 158 |
-x TARGET Metric monitored; default deadlocks. |
| 159 |
Other options: kills, fkerrors. |
| 160 |
--help Print help and exit. |
| 161 |
--version Print version and exit. |
| 162 |
Options must be given as --option value, not --option=value or -Ovalue. |
| 163 |
Use perldoc to read embedded documentation with more details. |
| 164 |
|
| 165 |
=head1 DESCRIPTION |
| 166 |
|
| 167 |
This Nagios plugin looks at a table and counts the number of rows since the |
| 168 |
last interval, and alerts if this exceeds the threshold. This could be the |
| 169 |
table referenced by pt-deadlock-logger, pt-kill, pt-fk-error-logger, or a |
| 170 |
custom table supplied. |
| 171 |
Default behaviour is the same as pmp-check-mysql-deadlocks, can also specify |
| 172 |
target to be C<kills> or C<fkerrors> to monitor default tables created by pt-kill |
| 173 |
or pt-fk-error-logger respectively, or supply custom metric and table. |
| 174 |
|
| 175 |
=head1 PRIVILEGES |
| 176 |
|
| 177 |
This plugin executes the following commands against MySQL: |
| 178 |
|
| 179 |
=over |
| 180 |
|
| 181 |
=item * |
| 182 |
|
| 183 |
C<SELECT> from the supplied table. |
| 184 |
|
| 185 |
=back |
| 186 |
|
| 187 |
This plugin executes no UNIX commands that may need special privileges. |
| 188 |
|
| 189 |
=head1 COPYRIGHT, LICENSE, AND WARRANTY |
| 190 |
|
| 191 |
This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ Percona Inc. |
| 192 |
Feedback and improvements are welcome. |
| 193 |
|
| 194 |
THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 195 |
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 196 |
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 197 |
|
| 198 |
This program is free software; you can redistribute it and/or modify it under |
| 199 |
the terms of the GNU General Public License as published by the Free Software |
| 200 |
Foundation, version 2. You should have received a copy of the GNU General |
| 201 |
Public License along with this program; if not, write to the Free Software |
| 202 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 203 |
|
| 204 |
=head1 VERSION |
| 205 |
|
| 206 |
$PROJECT_NAME$ pmp-check-mysql-ts-count $VERSION$ |
| 207 |
|
| 208 |
=cut |
| 209 |
|
| 210 |
DOCUMENTATION |