| 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 | OPT_ENSURE_SBM=0 |
| 30 | MIN_DELAY_SET=0 |
| 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 | -l) shift; OPT_USER="${1}"; shift; ;; |
| 37 | -L) shift; OPT_LOPA="${1}"; shift; ;; |
| 38 | -m) shift; OPT_MIN="${1}"; MIN_DELAY_SET=1; shift; ;; |
| 39 | -p) shift; OPT_PASS="${1}"; shift; ;; |
| 40 | -P) shift; OPT_PORT="${1}"; shift; ;; |
| 41 | -S) shift; OPT_SOCK="${1}"; shift; ;; |
| 42 | -s) shift; OPT_SRVID="${1}"; shift; ;; |
| 43 | -T) shift; OPT_TABLE="${1}"; shift; ;; |
| 44 | -u) shift; OPT_UTC=1; ;; |
| 45 | -w) shift; OPT_WARN="${1}"; shift; ;; |
| 46 | --master-conn) shift; OPT_MASTERCONN="${1}"; shift; ;; |
| 47 | --channel) shift; OPT_CHANNEL="${1}"; shift; ;; |
| 48 | --unconfigured) shift; OPT_REPLNOTSET=1; ;; |
| 49 | --ensure-sbm) shift; OPT_ENSURE_SBM=1; ;; |
| 50 | --version) grep -A2 '^=head1 VERSION' "$0" | tail -n1; exit 0 ;; |
| 51 | --help) perl -00 -ne 'm/^ Usage:/ && print' "$0"; exit 0 ;; |
| 52 | -*) echo "Unknown option ${o}. Try --help."; exit 1; ;; |
| 53 | esac |
| 54 | done |
| 55 | OPT_WARN=${OPT_WARN:-300} |
| 56 | OPT_CRIT=${OPT_CRIT:-600} |
| 57 | OPT_MIN=${OPT_MIN:-0} |
| 58 | if [ -e '/etc/nagios/mysql.cnf' ]; then |
| 59 | OPT_DEFT="${OPT_DEFT:-/etc/nagios/mysql.cnf}" |
| 60 | fi |
| 61 | if is_not_sourced; then |
| 62 | if [ -n "$1" ]; then |
| 63 | echo "WARN spurious command-line options: $@" |
| 64 | exit 1 |
| 65 | fi |
| 66 | fi |
| 67 | |
| 68 | # Get replication delay from a heartbeat table or from SHOW SLAVE STATUS. |
| 69 | get_slave_status $1 |
| 70 | if [ "${OPT_TABLE}" ]; then |
| 71 | if [ -z "${OPT_UTC}" ]; then |
| 72 | NOW_FUNC='UNIX_TIMESTAMP()' |
| 73 | else |
| 74 | NOW_FUNC='UNIX_TIMESTAMP(UTC_TIMESTAMP)' |
| 75 | fi |
| 76 | if [ "${OPT_SRVID}" == "MASTER" ]; then |
| 77 | if [ "${MYSQL_CONN}" = 0 ]; then |
| 78 | OPT_SRVID=$(awk '/Master_Server_Id/{print $2}' "${TEMP_SLAVEDATA}") |
| 79 | fi |
| 80 | fi |
| 81 | SQL="SELECT MAX(${NOW_FUNC} - ROUND(UNIX_TIMESTAMP(ts))) AS delay |
| 82 | FROM ${OPT_TABLE} WHERE (${OPT_SRVID:-0} = 0 OR server_id = ${OPT_SRVID:-0})" |
| 83 | LEVEL=$(mysql_exec "${SQL}") |
| 84 | MYSQL_CONN=$? |
| 85 | else |
| 86 | if [ "${MYSQL_CONN}" = 0 ]; then |
| 87 | LEVEL=$(awk '/Seconds_Behind_Master/{print $2}' "${TEMP_SLAVEDATA}") |
| 88 | fi |
| 89 | fi |
| 90 | |
| 91 | # Check for SQL thread errors |
| 92 | LAST_SLAVE_ERRNO=$(awk '/Last_SQL_Errno/{print $2}' "${TEMP_SLAVEDATA}") |
| 93 | |
| 94 | # Build the common perf data output for graph trending |
| 95 | PERFDATA="replication_delay=${LEVEL:-0};${OPT_WARN};${OPT_CRIT};0;" |
| 96 | |
| 97 | # Test whether the delay is too long. |
| 98 | if [ "$MYSQL_CONN" = 0 ]; then |
| 99 | NOTE="${LEVEL:-0} seconds of replication delay" |
| 100 | if [ "${LEVEL:-""}" = "NULL" ]; then |
| 101 | test ${MIN_DELAY_SET} -eq 1 && \ |
| 102 | test ${LAST_SLAVE_ERRNO} -eq 0 && \ |
| 103 | test ${OPT_ENSURE_SBM} -eq 0 && \ |
| 104 | NOTE="OK NULL seconds of replication delay" || NOTE="UNK replica is stopped" |
| 105 | elif [ -z "${LEVEL}" -a "${OPT_REPLNOTSET}" ]; then |
| 106 | NOTE="UNK This server is not configured as a replica." |
| 107 | # pt-slave-delayed slave |
| 108 | elif [ ${MIN_DELAY_SET} -eq 1 ] && [ "${LEVEL:-0}" -lt "${OPT_MIN}" ]; then |
| 109 | NOTE="CRIT (delayed slave) $NOTE | $PERFDATA" |
| 110 | elif [ "${LEVEL:-0}" -gt "${OPT_CRIT}" ]; then |
| 111 | NOTE="CRIT $NOTE | $PERFDATA" |
| 112 | elif [ "${LEVEL:-0}" -gt "${OPT_WARN}" ]; then |
| 113 | NOTE="WARN $NOTE | $PERFDATA" |
| 114 | else |
| 115 | NOTE="OK $NOTE | $PERFDATA" |
| 116 | fi |
| 117 | else |
| 118 | NOTE="UNK could not determine replication delay" |
| 119 | fi |
| 120 | echo $NOTE |
| 121 | } |
| 122 | |
| 123 | # ######################################################################## |
| 124 | # Execute a MySQL command. |
| 125 | # ######################################################################## |
| 126 | mysql_exec() { |
| 127 | mysql ${OPT_DEFT:+--defaults-file="${OPT_DEFT}"} \ |
| 128 | ${OPT_LOPA:+--login-path="${OPT_LOPA}"} \ |
| 129 | ${OPT_HOST:+-h"${OPT_HOST}"} ${OPT_PORT:+-P"${OPT_PORT}"} \ |
| 130 | ${OPT_USER:+-u"${OPT_USER}"} ${OPT_PASS:+-p"${OPT_PASS}"} \ |
| 131 | ${OPT_SOCK:+-S"${OPT_SOCK}"} -ss -e "$1" |
| 132 | } |
| 133 | |
| 134 | # ######################################################################## |
| 135 | # Determine whether this program is being executed directly, or sourced/included |
| 136 | # from another file. |
| 137 | # ######################################################################## |
| 138 | is_not_sourced() { |
| 139 | [ "${0##*/}" = "pmp-check-mysql-replication-delay" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ] |
| 140 | } |
| 141 | |
| 142 | # ######################################################################## |
| 143 | # Captures the "SHOW SLAVE STATUS" output into a temp file. |
| 144 | # ######################################################################## |
| 145 | get_slave_status() { |
| 146 | TEMP_SLAVEDATA=$(mktemp -t "${0##*/}.XXXXXX") || exit $? |
| 147 | trap "rm -f '${TEMP_SLAVEDATA}' >/dev/null 2>&1" EXIT |
| 148 | if [ -z "$1" ]; then |
| 149 | if [ "${OPT_MASTERCONN}" ]; then |
| 150 | # MariaDB multi-source replication |
| 151 | mysql_exec "SHOW SLAVE '${OPT_MASTERCONN}' STATUS\G" > "${TEMP_SLAVEDATA}" |
| 152 | elif [ "${OPT_CHANNEL}" ]; then |
| 153 | mysql_exec "SHOW SLAVE STATUS FOR CHANNEL '${OPT_CHANNEL}'\G" > "${TEMP_SLAVEDATA}" |
| 154 | else |
| 155 | # Leverage lock-free SHOW SLAVE STATUS if available |
| 156 | mysql_exec "SHOW SLAVE STATUS NONBLOCKING\G" > "${TEMP_SLAVEDATA}" 2>/dev/null || |
| 157 | mysql_exec "SHOW SLAVE STATUS NOLOCK\G" > "${TEMP_SLAVEDATA}" 2>/dev/null || |
| 158 | mysql_exec "SHOW SLAVE STATUS\G" > "${TEMP_SLAVEDATA}" |
| 159 | fi |
| 160 | MYSQL_CONN=$? |
| 161 | else |
| 162 | # This is for testing only. |
| 163 | cat "$1" > "${TEMP_SLAVEDATA}" 2>/dev/null |
| 164 | MYSQL_CONN=0 |
| 165 | fi |
| 166 | } |
| 167 | |
| 168 | # ######################################################################## |
| 169 | # Execute the program if it was not included from another file. |
| 170 | # This makes it possible to include without executing, and thus test. |
| 171 | # ######################################################################## |
| 172 | if is_not_sourced; then |
| 173 | OUTPUT=$(main "$@") |
| 174 | EXITSTATUS=$STATE_UNKNOWN |
| 175 | case "${OUTPUT}" in |
| 176 | UNK*) EXITSTATUS=$STATE_UNKNOWN; ;; |
| 177 | OK*) EXITSTATUS=$STATE_OK; ;; |
| 178 | WARN*) EXITSTATUS=$STATE_WARNING; ;; |
| 179 | CRIT*) EXITSTATUS=$STATE_CRITICAL; ;; |
| 180 | esac |
| 181 | echo "${OUTPUT}" |
| 182 | exit $EXITSTATUS |
| 183 | fi |
| 184 | |
| 185 | # ############################################################################ |
| 186 | # Documentation |
| 187 | # ############################################################################ |
| 188 | : <<'DOCUMENTATION' |
| 189 | =pod |
| 190 | |
| 191 | =head1 NAME |
| 192 | |
| 193 | pmp-check-mysql-replication-delay - Alert when MySQL replication becomes delayed. |
| 194 | |
| 195 | =head1 SYNOPSIS |
| 196 | |
| 197 | Usage: pmp-check-mysql-replication-delay [OPTIONS] |
| 198 | Options: |
| 199 | -c CRIT Critical threshold; default 600. |
| 200 | --defaults-file FILE Only read mysql options from the given file. |
| 201 | Defaults to /etc/nagios/mysql.cnf if it exists. |
| 202 | -H HOST MySQL hostname. |
| 203 | -l USER MySQL username. |
| 204 | -L LOGIN-PATH Use login-path to access MySQL (with MySQL client 5.6). |
| 205 | -m CRIT Minimal threshold to ensure for delayed slaves; default 0. |
| 206 | -p PASS MySQL password. |
| 207 | -P PORT MySQL port. |
| 208 | -S SOCKET MySQL socket file. |
| 209 | -s SERVERID MySQL server ID of master, if using pt-heartbeat table. If |
| 210 | the parameter is set to "MASTER" the plugin will lookup the |
| 211 | server_id of the master |
| 212 | -T TABLE Heartbeat table used by pt-heartbeat. |
| 213 | -u Use UTC time to count the delay in case pt-heartbeat is run |
| 214 | with --utc option. |
| 215 | -w WARN Warning threshold; default 300. |
| 216 | --master-conn NAME Master connection name for MariaDB multi-source replication. |
| 217 | --channel NAME Master channel name for multi-source replication (MySQL 5.7.6+). |
| 218 | --unconfigured Alert when replica is not configured at all; default no. |
| 219 | --ensure-sbm Disallow Seconds_Behind_Master to be NULL for delayed slaves when -m is used |
| 220 | --help Print help and exit. |
| 221 | --version Print version and exit. |
| 222 | Options must be given as --option value, not --option=value or -Ovalue. |
| 223 | Use perldoc to read embedded documentation with more details. |
| 224 | |
| 225 | =head1 DESCRIPTION |
| 226 | |
| 227 | This Nagios plugin examines whether MySQL replication is delayed too much. By |
| 228 | default it uses SHOW SLAVE STATUS, but the output of the Seconds_behind_master |
| 229 | column from this command is unreliable, so it is better to use pt-heartbeat from |
| 230 | Percona Toolkit instead. Use the -T option to specify which table pt-heartbeat |
| 231 | updates. Use the -s option to specify the master's server_id to compare |
| 232 | against; otherwise the plugin reports the maximum delay from any server. Use |
| 233 | the -s options with the value "MASTER" to have plugin lookup the master's server_id |
| 234 | |
| 235 | If you want to run this check against the delayed slaves, e.g. those running |
| 236 | with pt-slave-delay tool, you may want to use -m option specifying the minimal |
| 237 | delay that should be ongoing, otherwise the plugin will alert critical. |
| 238 | |
| 239 | =head1 PRIVILEGES |
| 240 | |
| 241 | This plugin executes the following commands against MySQL: |
| 242 | |
| 243 | =over |
| 244 | |
| 245 | =item * |
| 246 | |
| 247 | C<SHOW SLAVE STATUS [NONBLOCKING|NOLOCK]> |
| 248 | |
| 249 | or |
| 250 | |
| 251 | =item * |
| 252 | |
| 253 | C<SELECT> from the C<pt-heartbeat> table. |
| 254 | |
| 255 | =back |
| 256 | |
| 257 | This plugin executes no UNIX commands that may need special privileges. |
| 258 | |
| 259 | =head1 COPYRIGHT, LICENSE, AND WARRANTY |
| 260 | |
| 261 | This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ Percona Inc. |
| 262 | Feedback and improvements are welcome. |
| 263 | |
| 264 | THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 265 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 266 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 267 | |
| 268 | This program is free software; you can redistribute it and/or modify it under |
| 269 | the terms of the GNU General Public License as published by the Free Software |
| 270 | Foundation, version 2. You should have received a copy of the GNU General |
| 271 | Public License along with this program; if not, write to the Free Software |
| 272 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 273 | |
| 274 | =head1 VERSION |
| 275 | |
| 276 | $PROJECT_NAME$ pmp-check-mysql-replication-delay $VERSION$ |
| 277 | |
| 278 | =cut |
| 279 | |
| 280 | DOCUMENTATION |