| 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 | for o; do |
| 30 | case "${o}" in |
| 31 | -c) shift; OPT_CRIT="${1}"; shift; ;; |
| 32 | --defaults-file) shift; OPT_DEFT="${1}"; shift; ;; |
| 33 | -d) shift; OPT_DELD=1; shift; ;; |
| 34 | -H) shift; OPT_HOST="${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 | -w) shift; OPT_WARN="${1}"; shift; ;; |
| 41 | --master-conn) shift; OPT_MASTERCONN="${1}"; shift; ;; |
| 42 | --channel) shift; OPT_CHANNEL="${1}"; shift; ;; |
| 43 | --version) grep -A2 '^=head1 VERSION' "$0" | tail -n1; exit 0 ;; |
| 44 | --help) perl -00 -ne 'm/^ Usage:/ && print' "$0"; exit 0 ;; |
| 45 | -*) echo "Unknown option ${o}. Try --help."; exit 1; ;; |
| 46 | esac |
| 47 | done |
| 48 | if [ -e '/etc/nagios/mysql.cnf' ]; then |
| 49 | OPT_DEFT="${OPT_DEFT:-/etc/nagios/mysql.cnf}" |
| 50 | fi |
| 51 | if is_not_sourced; then |
| 52 | if [ -n "$1" ]; then |
| 53 | echo "WARN spurious command-line options: $@" |
| 54 | exit 1 |
| 55 | fi |
| 56 | fi |
| 57 | |
| 58 | # Get replication status into a temp file. TODO: move this into a subroutine |
| 59 | # and test it. But, if there is a commandline argument left over after |
| 60 | # parsing options, then use that as the file instead. |
| 61 | local TEMP=$(mktemp -t "${0##*/}.XXXXXX") || exit $? |
| 62 | trap "rm -f '${TEMP}' >/dev/null 2>&1" EXIT |
| 63 | if [ -z "$1" ]; then |
| 64 | if [ "${OPT_MASTERCONN}" ]; then |
| 65 | # MariaDB multi-source replication |
| 66 | mysql_exec "SHOW SLAVE '${OPT_MASTERCONN}' STATUS\G" > "${TEMP}" |
| 67 | elif [ "${OPT_CHANNEL}" ]; then |
| 68 | mysql_exec "SHOW SLAVE STATUS FOR CHANNEL '${OPT_CHANNEL}'\G" > "${TEMP}" |
| 69 | else |
| 70 | # Leverage lock-free SHOW SLAVE STATUS if available |
| 71 | mysql_exec "SHOW SLAVE STATUS NONBLOCKING\G" > "${TEMP}" 2>/dev/null || |
| 72 | mysql_exec "SHOW SLAVE STATUS NOLOCK\G" > "${TEMP}" 2>/dev/null || |
| 73 | mysql_exec "SHOW SLAVE STATUS\G" > "${TEMP}" |
| 74 | fi |
| 75 | else |
| 76 | # This is just for testing. /dev/null it. |
| 77 | cat "$1" > "${TEMP}" 2>/dev/null |
| 78 | fi |
| 79 | |
| 80 | if [ $? = 0 ]; then |
| 81 | # SHOW SLAVE STATUS isn't an error if the server isn't a replica. The file |
| 82 | # will be empty if that happens. |
| 83 | if [ -s "${TEMP}" ]; then |
| 84 | NOTE=$(awk '$1 ~ /_Running:|Last_Error:/{print substr($0, 1, 100)}' "${TEMP}") |
| 85 | if grep 'Last_Error: .' "${TEMP}" >/dev/null 2>&1; then |
| 86 | NOTE="CRIT $NOTE" |
| 87 | # pt-slave-delayed slave |
| 88 | elif [ -n "${OPT_DELD}" ] && |
| 89 | grep 'Slave_IO_Running: Yes' "${TEMP}" >/dev/null 2>&1 && |
| 90 | grep 'Slave_SQL_Running: No' "${TEMP}" >/dev/null 2>&1; then |
| 91 | NOTE="OK (delayed slave) $NOTE" |
| 92 | elif egrep "_Running: (No|Connecting)" "${TEMP}" >/dev/null 2>&1; then |
| 93 | if [ "${OPT_CRIT}" ]; then |
| 94 | NOTE="CRIT $NOTE" |
| 95 | else |
| 96 | NOTE="WARN $NOTE" |
| 97 | fi |
| 98 | else |
| 99 | NOTE="OK $NOTE" |
| 100 | fi |
| 101 | elif [ "${OPT_WARN}" ]; then |
| 102 | # Empty file; not a replica, but that's not supposed to happen. |
| 103 | NOTE="WARN This server is not configured as a replica." |
| 104 | else |
| 105 | # Empty file; not a replica. |
| 106 | NOTE="OK This server is not configured as a replica." |
| 107 | fi |
| 108 | else |
| 109 | NOTE="UNK could not determine replication status" |
| 110 | fi |
| 111 | |
| 112 | echo $NOTE |
| 113 | } |
| 114 | |
| 115 | # ######################################################################## |
| 116 | # Execute a MySQL command. |
| 117 | # ######################################################################## |
| 118 | mysql_exec() { |
| 119 | mysql ${OPT_DEFT:+--defaults-file="${OPT_DEFT}"} \ |
| 120 | ${OPT_LOPA:+--login-path="${OPT_LOPA}"} \ |
| 121 | ${OPT_HOST:+-h"${OPT_HOST}"} ${OPT_PORT:+-P"${OPT_PORT}"} \ |
| 122 | ${OPT_USER:+-u"${OPT_USER}"} ${OPT_PASS:+-p"${OPT_PASS}"} \ |
| 123 | ${OPT_SOCK:+-S"${OPT_SOCK}"} -ss -e "$1" |
| 124 | } |
| 125 | |
| 126 | # ######################################################################## |
| 127 | # Determine whether this program is being executed directly, or sourced/included |
| 128 | # from another file. |
| 129 | # ######################################################################## |
| 130 | is_not_sourced() { |
| 131 | [ "${0##*/}" = "pmp-check-mysql-replication-running" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ] |
| 132 | } |
| 133 | |
| 134 | # ######################################################################## |
| 135 | # Execute the program if it was not included from another file. |
| 136 | # This makes it possible to include without executing, and thus test. |
| 137 | # ######################################################################## |
| 138 | if is_not_sourced; then |
| 139 | OUTPUT=$(main "$@") |
| 140 | EXITSTATUS=$STATE_UNKNOWN |
| 141 | case "${OUTPUT}" in |
| 142 | UNK*) EXITSTATUS=$STATE_UNKNOWN; ;; |
| 143 | OK*) EXITSTATUS=$STATE_OK; ;; |
| 144 | WARN*) EXITSTATUS=$STATE_WARNING; ;; |
| 145 | CRIT*) EXITSTATUS=$STATE_CRITICAL; ;; |
| 146 | esac |
| 147 | echo "${OUTPUT}" |
| 148 | exit $EXITSTATUS |
| 149 | fi |
| 150 | |
| 151 | # ############################################################################ |
| 152 | # Documentation |
| 153 | # ############################################################################ |
| 154 | : <<'DOCUMENTATION' |
| 155 | =pod |
| 156 | |
| 157 | =head1 NAME |
| 158 | |
| 159 | pmp-check-mysql-replication-running - Alert when MySQL replication stops. |
| 160 | |
| 161 | =head1 SYNOPSIS |
| 162 | |
| 163 | Usage: pmp-check-mysql-replication-running [OPTIONS] |
| 164 | Options: |
| 165 | -c CRIT Report CRITICAL when replication is stopped with or w/o errors. |
| 166 | --defaults-file FILE Only read mysql options from the given file. |
| 167 | Defaults to /etc/nagios/mysql.cnf if it exists. |
| 168 | -d Useful for slaves delayed by pt-slave-delay. It will not alert |
| 169 | when IO thread is running, SQL one is not and no errors. |
| 170 | -H HOST MySQL hostname. |
| 171 | -l USER MySQL username. |
| 172 | -L LOGIN-PATH Use login-path to access MySQL (with MySQL client 5.6). |
| 173 | -p PASS MySQL password. |
| 174 | -P PORT MySQL port. |
| 175 | -S SOCKET MySQL socket file. |
| 176 | -w WARN Report WARNING when SHOW SLAVE STATUS output is empty. |
| 177 | --master-conn NAME Master connection name for MariaDB multi-source replication. |
| 178 | --channel NAME Master channel name for multi-source replication (MySQL 5.7.6+). |
| 179 | --help Print help and exit. |
| 180 | --version Print version and exit. |
| 181 | Options must be given as --option value, not --option=value or -Ovalue. |
| 182 | Use perldoc to read embedded documentation with more details. |
| 183 | |
| 184 | =head1 DESCRIPTION |
| 185 | |
| 186 | This Nagios plugin examines whether replication is running. It is separate from |
| 187 | the check for delay because it is confusing or impossible to handle all of the |
| 188 | combinations of replication errors and delays correctly, and provide an |
| 189 | appropriate type of alert, in a single program. |
| 190 | |
| 191 | By default, this plugin treats it as critical when the either thread stops with |
| 192 | an error, and a warning when threads are stopped with no error. You can provide |
| 193 | critical and warning thresholds with the -c and -w options, for compatibility |
| 194 | with Nagios plugin conventions, but they don't work as thresholds. Instead, if |
| 195 | you specify a critical threshold, this plugin will treat it as critical if |
| 196 | either thread is stopped, with or without an error. |
| 197 | |
| 198 | The warning threshold makes the plugin report a warning when SHOW SLAVE STATUS |
| 199 | produces no output, which means it is not configured as a replica. By default, |
| 200 | this plugin will report that replication is healthy when a server isn't |
| 201 | configured as a replica. |
| 202 | |
| 203 | If you want to run this check against the delayed slaves, e.g. those running |
| 204 | with pt-slave-delay tool, you may want to specify -d option. It will not alert |
| 205 | when Slave_IO_Running is Yes, Slave_SQL_Running is No and there are no errors. |
| 206 | |
| 207 | =head1 PRIVILEGES |
| 208 | |
| 209 | This plugin executes the following commands against MySQL: |
| 210 | |
| 211 | =over |
| 212 | |
| 213 | =item * |
| 214 | |
| 215 | C<SHOW SLAVE STATUS [NONBLOCKING|NOLOCK]> |
| 216 | |
| 217 | =back |
| 218 | |
| 219 | This plugin executes no UNIX commands that may need special privileges. |
| 220 | |
| 221 | =head1 COPYRIGHT, LICENSE, AND WARRANTY |
| 222 | |
| 223 | This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ Percona Inc. |
| 224 | Feedback and improvements are welcome. |
| 225 | |
| 226 | THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 227 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 228 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 229 | |
| 230 | This program is free software; you can redistribute it and/or modify it under |
| 231 | the terms of the GNU General Public License as published by the Free Software |
| 232 | Foundation, version 2. You should have received a copy of the GNU General |
| 233 | Public License along with this program; if not, write to the Free Software |
| 234 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 235 | |
| 236 | =head1 VERSION |
| 237 | |
| 238 | $PROJECT_NAME$ pmp-check-mysql-replication-running $VERSION$ |
| 239 | |
| 240 | =cut |
| 241 | |
| 242 | DOCUMENTATION |