forked from inuits/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmp-check-mysql-replication-running
executable file
·189 lines (157 loc) · 7.05 KB
/
pmp-check-mysql-replication-running
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
#!/bin/sh
# ########################################################################
# This program is part of Percona Monitoring Plugins
# License: GPL License (see COPYING)
# Authors:
# Baron Schwartz
# ########################################################################
# ########################################################################
# 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
EXITSTATUS=$STATE_UNKNOWN
# ########################################################################
# Run the program.
# ########################################################################
main() {
# Get options
for o; do
case "${o}" in
-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; ;;
-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
if [ -e '/etc/nagios/mysql.cnf' ]; then
OPT_DEFT="${OPT_DEFT:-/etc/nagios/mysql.cnf}"
fi
# Get replication status into a temp file. TODO: move this into a subroutine
# and test it.
local TEMP=$(mktemp "/tmp/${0##*/}.XXXX") || exit $?
trap 'rm -rf "${TEMP}" >/dev/null 2>&1' EXIT
mysql_exec 'SHOW SLAVE STATUS\G' > "${TEMP}"
if [ $? = 0 ]; then
# SHOW SLAVE STATUS isn't an error if the server isn't a replica. The file
# will be empty if that happens.
if [ -s "${TEMP}" ]; then
NOTE=$(awk '$1 ~ /_Running:|Last_Error:/{print substr($0, 1, 100)}' "${TEMP}")
if grep 'Last_Error: .' "${TEMP}" >/dev/null 2>&1; then
EXITSTATUS=$STATE_CRITICAL
NOTE="CRIT $NOTE"
elif grep '_Running: No' "${TEMP}" >/dev/null 2>&1; then
if [ "${OPT_CRIT}" ]; then
EXITSTATUS=$STATE_CRITICAL
NOTE="CRIT $NOTE"
else
EXITSTATUS=$STATE_WARNING
NOTE="WARN $NOTE"
fi
fi
elif [ "${OPT_WARN}" ]; then
# Empty file; not a replica, but that's not supposed to happen.
NOTE="WARN This server is not configured as a replica."
EXITSTATUS=$STATE_WARNING
else
# Empty file; not a replica.
NOTE="OK This server is not configured as a replica."
EXITSTATUS=$STATE_OK
fi
else
EXITSTATUS=$STATE_UNKNOWN
NOTE="UNK could not determine replication status"
fi
echo $NOTE
exit $EXITSTATUS
}
# ########################################################################
# Execute a MySQL command.
# ########################################################################
mysql_exec() {
mysql ${OPT_DEFT:+--defaults-file="${OPT_DEFT}"} ${OPT_HOST:+-h"${OPT_HOST}"} ${OPT_USER:+-u"${OPT_USER}"} \
${OPT_PASS:+-p"${OPT_PASS}"} ${OPT_SOCK:+-S"${OPT_SOCK}"} ${OPT_PORT:+-P"${OPT_PORT}"} \
-ss -e "$1"
}
# ########################################################################
# Execute the program if it was not included from another file.
# This makes it possible to include without executing, and thus test.
# ########################################################################
if [ "${0##*/}" = "pmp-check-mysql-replication-running" ] \
|| [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then
main "$@"
fi
# ############################################################################
# Documentation
# ############################################################################
: <<'DOCUMENTATION'
=pod
=head1 NAME
pmp-check-mysql-replication-running - Alert when MySQL replication stops.
=head1 SYNOPSIS
Usage: pmp-check-mysql-replication-running [OPTIONS]
Options:
-c CRIT Report CRITICAL when replication is stopped without errors.
--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.
-p PASS MySQL password.
-P PORT MySQL port.
-S SOCKET MySQL socket file.
-w WARN Report WARNING when SHOW SLAVE STATUS output is empty.
--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 examines whether replication is running. It is separate from
the check for delay because it is confusing or impossible to handle all of the
combinations of replication errors and delays correctly, and provide an
appropriate type of alert, in a single program.
By default, this plugin treats it as critical when the either thread stops with
an error, and a warning when threads are stopped with no error. You can provide
critical and warning thresholds with the -c and -w options, for compatibility
with Nagios plugin conventions, but they don't work as thresholds. Instead, if
you specify a critical threshold, this plugin will treat it as critical if
either thread is stopped, with or without an error.
The warning threshold makes the plugin report a warning when SHOW SLAVE STATUS
produces no output, which means it is not configured as a replica. By default,
this plugin will report that replication is healthy when a server isn't
configured as a replica.
=head1 PRIVILEGES
This plugin executes the following commands against MySQL:
=over
=item *
C<SHOW SLAVE STATUS>.
=back
This plugin executes no UNIX commands that may need special privileges.
=head1 COPYRIGHT, LICENSE, AND WARRANTY
This program is copyright 2012 Baron Schwartz, 2012 Percona Inc.
Feedback and improvements are welcome.
THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, version 2. You should have received a copy of the GNU General
Public License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
=head1 VERSION
Percona Monitoring Plugins pmp-check-mysql-replication-running 0.9.0
=cut
DOCUMENTATION