Debian Codegraph @

percona-nagios-plugins/nagios/bin/pmp-check-pt-table-checksum

raw symbols SCIP
1 #!/bin/bash
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_CRIT="${1}"; shift; ;;
32 --defaults-file) shift; OPT_DEFT="${1}"; shift; ;;
33 -H) shift; OPT_HOST="${1}"; shift; ;;
34 -i) shift; OPT_INTERVAL="${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 -T) shift; OPT_TABLE="${1}"; shift; ;;
41 -w) shift; OPT_WARN="${1}"; shift; ;;
42 --version) grep -A2 '^=head1 VERSION' "$0" | tail -n1; exit 0 ;;
43 --help) perl -00 -ne 'm/^ Usage:/ && print' "$0"; exit 0 ;;
44 -*) echo "Unknown option ${o}. Try --help."; exit 1; ;;
45 esac
46 done
47 OPT_INTERVAL=${OPT_INTERVAL:-0}
48 OPT_TABLE="${OPT_TABLE:-percona.checksums}"
49 if [ -e '/etc/nagios/mysql.cnf' ]; then
50 OPT_DEFT="${OPT_DEFT:-/etc/nagios/mysql.cnf}"
51 fi
52 if is_not_sourced; then
53 if [ -n "$1" ]; then
54 echo "WARN spurious command-line options: $@"
55 exit 1
56 fi
57 fi
58
59 NOTE="UNK couldn't query the checksum table"
60
61 # Set up a temp file to hold error messages from MySQL.
62 TEMP=$(mktemp -t "${0##*/}.XXXXXX") || exit $?
63 trap "rm -f '${TEMP}' >/dev/null 2>&1" EXIT
64
65 # Get the query from the documentation and execute it.
66 SQL=$(get_magic_query "${0}" checksum_diff_query)
67 PROBLEMS=$(mysql_exec "${SQL/CHECKSUM_TABLE/${OPT_TABLE}}" 2>"${TEMP}")
68 if [ $? = 0 ]; then
69 if [ "${PROBLEMS}" ]; then
70 NOTE="pt-table-checksum found ${PROBLEMS}"
71 if [ "${OPT_CRIT}" ]; then
72 NOTE="CRIT $NOTE"
73 else
74 NOTE="WARN $NOTE"
75 fi
76 else
77 NOTE="OK pt-table-checksum found no out-of-sync tables"
78 if [ "${OPT_INTERVAL}" -gt 0 ]; then
79 RECENT_CHUNKS=$(mysql_exec "SELECT IF(COALESCE(MAX(ts), NOW()) > NOW() - INTERVAL ${OPT_INTERVAL} DAY, 1, 0) FROM ${OPT_TABLE}")
80 if [ "${RECENT_CHUNKS}" = 0 ]; then
81 NOTE="pt-table-checksum was not run over last ${OPT_INTERVAL} days"
82 if [ "${OPT_CRIT}" ]; then
83 NOTE="CRIT $NOTE"
84 else
85 NOTE="WARN $NOTE"
86 fi
87 fi
88 fi
89 fi
90 else
91 if grep "Table '${OPT_TABLE}' doesn't exist" "${TEMP}" >/dev/null 2>&1; then
92 NOTE="UNK table '${OPT_TABLE}' doesn't exist"
93 fi
94 fi
95 echo $NOTE
96 }
97
98 # ########################################################################
99 # Execute a MySQL command.
100 # ########################################################################
101 mysql_exec() {
102 mysql ${OPT_DEFT:+--defaults-file="${OPT_DEFT}"} \
103 ${OPT_LOPA:+--login-path="${OPT_LOPA}"} \
104 ${OPT_HOST:+-h"${OPT_HOST}"} ${OPT_PORT:+-P"${OPT_PORT}"} \
105 ${OPT_USER:+-u"${OPT_USER}"} ${OPT_PASS:+-p"${OPT_PASS}"} \
106 ${OPT_SOCK:+-S"${OPT_SOCK}"} -ss -e "$1"
107 }
108
109 # ########################################################################
110 # Retrieve a paragraph from the given file, which includes MAGIC_$2 as a
111 # pattern.
112 # ########################################################################
113 get_magic_query() {
114 perl -00 -ne"m/MAGIC_$2/ && print" "$1"
115 }
116
117 # ########################################################################
118 # Determine whether this program is being executed directly, or sourced/included
119 # from another file.
120 # ########################################################################
121 is_not_sourced() {
122 [ "${0##*/}" = "pmp-check-pt-table-checksum" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ]
123 }
124
125 # ########################################################################
126 # Execute the program if it was not included from another file.
127 # This makes it possible to include without executing, and thus test.
128 # ########################################################################
129 if is_not_sourced; then
130 OUTPUT=$(main "$@")
131 EXITSTATUS=$STATE_UNKNOWN
132 case "${OUTPUT}" in
133 UNK*) EXITSTATUS=$STATE_UNKNOWN; ;;
134 OK*) EXITSTATUS=$STATE_OK; ;;
135 WARN*) EXITSTATUS=$STATE_WARNING; ;;
136 CRIT*) EXITSTATUS=$STATE_CRITICAL; ;;
137 esac
138 echo "${OUTPUT}"
139 exit $EXITSTATUS
140 fi
141
142 # ############################################################################
143 # Documentation
144 # ############################################################################
145 : <<'DOCUMENTATION'
146 =pod
147
148 =head1 NAME
149
150 pmp-check-pt-table-checksum - Alert when pt-table-checksum finds data differences on a replica.
151
152 =head1 SYNOPSIS
153
154 Usage: pmp-check-pt-table-checksum [OPTIONS]
155 Options:
156 -c CRIT Raise a critical error instead of a warning.
157 --defaults-file FILE Only read mysql options from the given file.
158 Defaults to /etc/nagios/mysql.cnf if it exists.
159 -H HOST MySQL hostname.
160 -l USER MySQL username.
161 -L LOGIN-PATH Use login-path to access MySQL (with MySQL client 5.6).
162 -p PASS MySQL password.
163 -P PORT MySQL port.
164 -S SOCKET MySQL socket file.
165 -i INTERVAL Interval over which to ensure pt-table-checksum was run,
166 in days; default - not to check.
167 -T TABLE The checksum table; default percona.checksums
168 -w WARN Warning threshold; ignored.
169 --help Print help and exit.
170 --version Print version and exit.
171 Options must be given as --option value, not --option=value or -Ovalue.
172 Use perldoc to read embedded documentation with more details.
173
174 =head1 DESCRIPTION
175
176 This Nagios plugin examines whether MySQL replication has drifted out of sync
177 with the master's data, according to checks performed by the pt-table-checksum
178 tool in Percona Toolkit. It uses the following query to determine whether the
179 server's data matches its master's:
180
181 SELECT /* MAGIC_checksum_diff_query */
182 CONCAT(
183 COUNT(*),
184 ' chunks differ in ',
185 COUNT(DISTINCT CONCAT(db, tbl)),
186 ' tables, including ',
187 MIN(CONCAT(db, '.', tbl)))
188 FROM CHECKSUM_TABLE
189 WHERE master_cnt <> this_cnt OR master_crc <> this_crc
190 OR ISNULL(master_crc) <> ISNULL(this_crc)
191 HAVING COUNT(*) > 0
192
193 The word CHECKSUM_TABLE is replaced by the value of the -T option. If the table
194 specified by -T does not exist, unknown is raised.
195
196 Optionally, you can specify an interval in days over which to ensure pt-table-checksum
197 was run. It is useful in cases when the cron job doing the checksumming suddenly
198 stopped working. This option will have an effect when no diffs are found and the
199 checksum table is not empty.
200
201 Alerts are raised at a WARNING level by default, but specifying the -c option
202 with any value will change this to CRITICAL instead.
203
204 =head1 PRIVILEGES
205
206 This plugin executes the following commands against MySQL:
207
208 =over
209
210 =item *
211
212 C<SELECT> against the specified table.
213
214 =back
215
216 This plugin executes no UNIX commands that may need special privileges.
217
218 =head1 COPYRIGHT, LICENSE, AND WARRANTY
219
220 This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ Percona Inc.
221 Feedback and improvements are welcome.
222
223 THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
224 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
225 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
226
227 This program is free software; you can redistribute it and/or modify it under
228 the terms of the GNU General Public License as published by the Free Software
229 Foundation, version 2. You should have received a copy of the GNU General
230 Public License along with this program; if not, write to the Free Software
231 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
232
233 =head1 VERSION
234
235 $PROJECT_NAME$ pmp-check-pt-table-checksum $VERSION$
236
237 =cut
238
239 DOCUMENTATION