| 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 | -C) shift; OPT_COMP="${1}"; shift; ;; |
| 34 | -H) shift; OPT_HOST="${1}"; shift; ;; |
| 35 | -I) shift; OPT_INCR="${1}"; shift; ;; |
| 36 | -l) shift; OPT_USER="${1}"; shift; ;; |
| 37 | -L) shift; OPT_LOPA="${1}"; shift; ;; |
| 38 | -o) shift; OPT_OPER="${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_TRAN="${1}"; shift; ;; |
| 43 | -w) shift; OPT_WARN="${1}"; shift; ;; |
| 44 | -x) shift; OPT_VAR1="${1}"; shift; ;; |
| 45 | -y) shift; OPT_VAR2="${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 | # Set default option values |
| 52 | OPT_COMP="${OPT_COMP:->=}" |
| 53 | if [ -e '/etc/nagios/mysql.cnf' ]; then |
| 54 | OPT_DEFT="${OPT_DEFT:-/etc/nagios/mysql.cnf}" |
| 55 | fi |
| 56 | if is_not_sourced; then |
| 57 | if [ -n "$1" ]; then |
| 58 | echo "WARN spurious command-line options: $@" |
| 59 | exit 1 |
| 60 | fi |
| 61 | fi |
| 62 | |
| 63 | # Validate the options. |
| 64 | OPT_ERR="" |
| 65 | if [ -z "${OPT_CRIT}${OPT_WARN}" ]; then |
| 66 | OPT_ERR="you must specify either -c or -w" |
| 67 | elif [ -z "${OPT_VAR1}" ]; then |
| 68 | OPT_ERR="you must specify -x" |
| 69 | elif [ "${OPT_OPER}" -a -z "${OPT_VAR2}" ]; then |
| 70 | OPT_ERR="you specified -o but not -y" |
| 71 | elif [ "${OPT_VAR2}" -a -z "${OPT_OPER}" ]; then |
| 72 | OPT_ERR="you specified -y but not -o" |
| 73 | elif [ "${OPT_TRAN}" = 'pct' -a -z "${OPT_VAR2}" ]; then |
| 74 | OPT_ERR="you specified -T pct but not -y" |
| 75 | elif [ "${OPT_TRAN}" ]; then |
| 76 | case "${OPT_TRAN}" in |
| 77 | pct|str) |
| 78 | ;; |
| 79 | *) |
| 80 | OPT_ERR="-T must be one of: pct str" |
| 81 | ;; |
| 82 | esac |
| 83 | fi |
| 84 | case "${OPT_COMP}" in |
| 85 | '=='|'!='|'>='|'>'|'<'|'<=') |
| 86 | ;; |
| 87 | *) |
| 88 | OPT_ERR="-C must be one of: == != >= > < <=" |
| 89 | ;; |
| 90 | esac |
| 91 | if [ "${OPT_OPER}" ]; then |
| 92 | case "${OPT_OPER}" in |
| 93 | /|'*'|+|-) |
| 94 | : |
| 95 | ;; |
| 96 | *) |
| 97 | OPT_ERR="-o must be one of: / * + -" |
| 98 | ;; |
| 99 | esac |
| 100 | fi |
| 101 | if [ "${OPT_ERR}" ]; then |
| 102 | echo "Error: $OPT_ERR. Try --help." |
| 103 | exit 1 |
| 104 | fi |
| 105 | |
| 106 | NOTE="UNK could not evaluate the expression." |
| 107 | |
| 108 | # Set up a temporary file |
| 109 | local TEMP1=$(mktemp -t "${0##*/}.XXXXXX") || exit $? |
| 110 | local TEMP2=$(mktemp -t "${0##*/}.XXXXXX") || exit $? |
| 111 | trap "rm -f '${TEMP1}' '${TEMP2}' >/dev/null 2>&1" EXIT |
| 112 | |
| 113 | if get_status_variables "${TEMP1}" "${TEMP2}" "${OPT_INCR}"; then |
| 114 | LEVEL=$(compute_result "${TEMP1}" "${OPT_VAR1}" "${OPT_OPER}" "${OPT_VAR2}" "${OPT_TRAN}") |
| 115 | if [ $? = 0 -a -n "${LEVEL}" ]; then |
| 116 | NOTE="${OPT_VAR1}${OPT_OPER:+ ${OPT_OPER}}${OPT_VAR2:+ ${OPT_VAR2}}${OPT_TRAN:+ (${OPT_TRAN})}" |
| 117 | NOTE="${NOTE} = ${LEVEL}" |
| 118 | |
| 119 | # XXX Make sure this line and the "case" don't get separated. |
| 120 | compare_result "${LEVEL}" "${OPT_CRIT}" "${OPT_WARN}" "${OPT_COMP}" "${OPT_TRAN}" |
| 121 | case $? in |
| 122 | $STATE_OK) |
| 123 | NOTE="OK $NOTE" |
| 124 | ;; |
| 125 | $STATE_CRITICAL) |
| 126 | NOTE="CRIT $NOTE" |
| 127 | ;; |
| 128 | $STATE_WARNING) |
| 129 | NOTE="WARN $NOTE" |
| 130 | ;; |
| 131 | esac |
| 132 | |
| 133 | # Build the common perf data output for graph trending |
| 134 | if [ "${OPT_TRAN}" = 'pct' ]; then |
| 135 | PERFDATA_MAX=100 |
| 136 | fi |
| 137 | PERFDATA="${OPT_VAR1}${OPT_OPER}${OPT_VAR2}=${LEVEL};${OPT_WARN};${OPT_CRIT};0;${PERFDATA_MAX}" |
| 138 | NOTE="$NOTE | $PERFDATA" |
| 139 | fi |
| 140 | else |
| 141 | NOTE="UNK could not get MySQL status/variables." |
| 142 | fi |
| 143 | |
| 144 | echo $NOTE |
| 145 | } |
| 146 | |
| 147 | # ######################################################################## |
| 148 | # Execute a MySQL command. |
| 149 | # ######################################################################## |
| 150 | mysql_exec() { |
| 151 | mysql ${OPT_DEFT:+--defaults-file="${OPT_DEFT}"} \ |
| 152 | ${OPT_LOPA:+--login-path="${OPT_LOPA}"} \ |
| 153 | ${OPT_HOST:+-h"${OPT_HOST}"} ${OPT_PORT:+-P"${OPT_PORT}"} \ |
| 154 | ${OPT_USER:+-u"${OPT_USER}"} ${OPT_PASS:+-p"${OPT_PASS}"} \ |
| 155 | ${OPT_SOCK:+-S"${OPT_SOCK}"} -ss -e "$1" |
| 156 | } |
| 157 | |
| 158 | # ######################################################################## |
| 159 | # Compares the variable to the thresholds. Arguments: VAR CRIT WARN CMP TRAN |
| 160 | # Returns nothing; exits with OK/WARN/CRIT. |
| 161 | # ######################################################################## |
| 162 | compare_result() { |
| 163 | local VAR="${1}" |
| 164 | local CRIT="${2}" |
| 165 | local WARN="${3}" |
| 166 | local CMP="${4}" |
| 167 | local TRAN="${5}" |
| 168 | echo 1 | awk "END { |
| 169 | if ( \"${CRIT}\" != \"\" ) { |
| 170 | if ( \"${TRAN}\" == \"str\" ) { |
| 171 | if ( \"${VAR}\" ${CMP} \"${CRIT:-0}\" ) { |
| 172 | exit $STATE_CRITICAL |
| 173 | } |
| 174 | } else { |
| 175 | if ( ${VAR} ${CMP} ${CRIT:-0} ) { |
| 176 | exit $STATE_CRITICAL |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | if ( \"${WARN}\" != \"\" ) { |
| 181 | if ( \"${TRAN}\" == \"str\" ) { |
| 182 | if ( \"${VAR}\" ${CMP} \"${WARN:-0}\" ) { |
| 183 | exit $STATE_WARNING |
| 184 | } |
| 185 | } else { |
| 186 | if ( ${VAR} ${CMP} ${WARN:-0} ) { |
| 187 | exit $STATE_WARNING |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | exit $STATE_OK |
| 192 | }" |
| 193 | } |
| 194 | |
| 195 | # ######################################################################## |
| 196 | # Computes an expression against the file of variables. Returns a float. |
| 197 | # Arguments: TEMP VAR1 OPER VAR2 TRAN |
| 198 | # ######################################################################## |
| 199 | compute_result() { |
| 200 | local TEMP="$1" |
| 201 | local VAR1="$2" |
| 202 | local OPER="$3" |
| 203 | local VAR2="$4" |
| 204 | local TRAN="$5" |
| 205 | if [ "${VAR2}" ]; then |
| 206 | # Extract two variables, apply an operator, and possibly apply a |
| 207 | # transform. |
| 208 | awk -F'\t' " |
| 209 | BEGIN { |
| 210 | got1 = \"Could not find variable ${VAR1}\"; |
| 211 | got2 = \"Could not find variable ${VAR2}\"; |
| 212 | } |
| 213 | \$1 == \"${VAR1}\" { |
| 214 | var1 = \$2; |
| 215 | got1 = \"\"; |
| 216 | } |
| 217 | \$1 == \"${VAR2}\" { |
| 218 | var2 = \$2; |
| 219 | got2 = \"\"; |
| 220 | } |
| 221 | END { |
| 222 | if ( got1 == \"\" && got2 == \"\" ) { |
| 223 | if ( var2 == 0 && \"${OPER}\" == \"/\" ) { |
| 224 | # Divide-by-zero; make the result simply 0 |
| 225 | val = 0; |
| 226 | } |
| 227 | else { |
| 228 | val = var1 ${OPER} var2; |
| 229 | } |
| 230 | if ( \"${TRAN}\" == \"pct\" ) { |
| 231 | val = val * 100; |
| 232 | } |
| 233 | if ( val ~ /\.[0-9]/ ) { |
| 234 | printf \"%.6f\\n\", val; |
| 235 | } |
| 236 | else { |
| 237 | print val; |
| 238 | } |
| 239 | } |
| 240 | else { |
| 241 | print got1, got2 | \"cat 1>&2\"; |
| 242 | exit 1; |
| 243 | } |
| 244 | } |
| 245 | " "${TEMP}" |
| 246 | else |
| 247 | # This is the simplest case. We're just extracting a single variable and |
| 248 | # returning it. |
| 249 | awk -F'\t' -v var1="${VAR1}" ' |
| 250 | BEGIN { |
| 251 | got = 0; |
| 252 | } |
| 253 | $1 == var1 { |
| 254 | val = $2; |
| 255 | got = 1; |
| 256 | } |
| 257 | END { |
| 258 | if ( got == 1 ) { |
| 259 | print val; |
| 260 | } |
| 261 | else { |
| 262 | print "Unknown variable", var1 | "cat 1>&2"; |
| 263 | exit 1; |
| 264 | } |
| 265 | } |
| 266 | ' "${TEMP}" |
| 267 | fi |
| 268 | } |
| 269 | |
| 270 | # ######################################################################## |
| 271 | # Gets status variables. The first argument is the file to store the results. |
| 272 | # Optional second argument is another temp file. |
| 273 | # Optional third argument makes SHOW STATUS incremental/relative, and has the |
| 274 | # added effect of filtering out non-numeric variables. |
| 275 | # ######################################################################## |
| 276 | get_status_variables() { |
| 277 | if [ "$3" ]; then |
| 278 | mysql_exec "SHOW /*!50000 GLOBAL*/ STATUS" > "${2}" || exit 1 |
| 279 | sleep "$3" |
| 280 | # Technically we ought to use another temp file, and check the return |
| 281 | # status of this mysql_exec, but if the first one worked it's likely that |
| 282 | # this one will too. |
| 283 | mysql_exec "SHOW /*!50000 GLOBAL*/ STATUS" | cat "${2}" - \ |
| 284 | | awk -F'\t' ' |
| 285 | /Aborted_clients/ { seen++; } |
| 286 | { |
| 287 | if ( seen > 1 && $2 !~ /[^0-9.]/ ) { |
| 288 | print $1 "\t" $2 - var[$1]; |
| 289 | } |
| 290 | else { |
| 291 | var[$1] = $2; |
| 292 | } |
| 293 | } |
| 294 | ' > "${1}" |
| 295 | else |
| 296 | mysql_exec "SHOW /*!50000 GLOBAL*/ STATUS" > "${1}" || exit 1 |
| 297 | fi |
| 298 | mysql_exec "SHOW /*!40101 GLOBAL*/ VARIABLES" >> "${1}" |
| 299 | } |
| 300 | |
| 301 | # ######################################################################## |
| 302 | # Determine whether this program is being executed directly, or sourced/included |
| 303 | # from another file. |
| 304 | # ######################################################################## |
| 305 | is_not_sourced() { |
| 306 | [ "${0##*/}" = "pmp-check-mysql-status" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ] |
| 307 | } |
| 308 | |
| 309 | # ######################################################################## |
| 310 | # Execute the program if it was not included from another file. |
| 311 | # This makes it possible to include without executing, and thus test. |
| 312 | # ######################################################################## |
| 313 | if is_not_sourced; then |
| 314 | OUTPUT=$(main "$@") |
| 315 | EXITSTATUS=$STATE_UNKNOWN |
| 316 | case "${OUTPUT}" in |
| 317 | UNK*) EXITSTATUS=$STATE_UNKNOWN; ;; |
| 318 | OK*) EXITSTATUS=$STATE_OK; ;; |
| 319 | WARN*) EXITSTATUS=$STATE_WARNING; ;; |
| 320 | CRIT*) EXITSTATUS=$STATE_CRITICAL; ;; |
| 321 | esac |
| 322 | echo "${OUTPUT}" |
| 323 | exit $EXITSTATUS |
| 324 | fi |
| 325 | |
| 326 | # ############################################################################ |
| 327 | # Documentation |
| 328 | # ############################################################################ |
| 329 | : <<'DOCUMENTATION' |
| 330 | =pod |
| 331 | |
| 332 | =head1 NAME |
| 333 | |
| 334 | pmp-check-mysql-status - Check MySQL SHOW GLOBAL STATUS output. |
| 335 | |
| 336 | =head1 SYNOPSIS |
| 337 | |
| 338 | Usage: pmp-check-mysql-status [OPTIONS] |
| 339 | Options: |
| 340 | -c CRIT Critical threshold. |
| 341 | --defaults-file FILE Only read mysql options from the given file. |
| 342 | Defaults to /etc/nagios/mysql.cnf if it exists. |
| 343 | -C COMPARE Comparison operator to apply to -c and -w. |
| 344 | Possible values: == != >= > < <=. Default >=. |
| 345 | -H HOST MySQL hostname. |
| 346 | -I INCR Make SHOW STATUS incremental over this delay. |
| 347 | -l USER MySQL username. |
| 348 | -L LOGIN-PATH Use login-path to access MySQL (with MySQL client 5.6). |
| 349 | -o OPERATOR The operator to apply to -x and -y. |
| 350 | -p PASS MySQL password. |
| 351 | -P PORT MySQL port. |
| 352 | -S SOCKET MySQL socket file. |
| 353 | -T TRANS Transformation to apply before comparing to -c and -w. |
| 354 | Possible values: pct str. |
| 355 | -w WARN Warning threshold. |
| 356 | -x VAR1 Required first status or configuration variable. |
| 357 | -y VAR2 Optional second status or configuration variable. |
| 358 | --help Print help and exit. |
| 359 | --version Print version and exit. |
| 360 | Options must be given as --option value, not --option=value or -Ovalue. |
| 361 | Use perldoc to read embedded documentation with more details. |
| 362 | |
| 363 | =head1 DESCRIPTION |
| 364 | |
| 365 | This Nagios plugin captures SHOW GLOBAL STATUS and SHOW GLOBAL VARIABLES from |
| 366 | MySQL and evaluates expressions against them. The general syntax is as follows: |
| 367 | |
| 368 | VAR1 [ OPERATOR VAR2 [ TRANSFORM ] ] |
| 369 | |
| 370 | The result of evaluating this is compared against the -w and -c options as usual |
| 371 | to determine whether to raise a warning or critical alert. |
| 372 | |
| 373 | Note that all of the examples provided below are simply for illustrative |
| 374 | purposes and are not supposed to be recommendations for what to monitor. You |
| 375 | should get advice from a professional if you are not sure what you should be |
| 376 | monitoring. |
| 377 | |
| 378 | For our first example, we will raise a warning if Threads_running is 20 or over, |
| 379 | and a critical alert if it is 40 or over: |
| 380 | |
| 381 | -x Threads_running -w 20 -c 40 |
| 382 | |
| 383 | The threshold is implemented as greater-or-equals by default, not strictly |
| 384 | greater-than, so a value of 20 is a warning and a value of 40 is critical. You |
| 385 | can switch this to less-or-equals or other operators with the -C option, which |
| 386 | accepts the arithmetic comparison operators ==, !=, >, >=, <, and <=. |
| 387 | |
| 388 | You can use any variable that is present in SHOW VARIABLES or SHOW STATUS. If |
| 389 | the variable is not found, there is an error. To warn if Threads_connected |
| 390 | exceeds 80% of max_connections: |
| 391 | |
| 392 | -x Threads_connected -o / -y max_connections -T pct -w 80 |
| 393 | |
| 394 | The -T C<pct> option only works when you specify both -x and -y and implements |
| 395 | percentage transformation. The plugin uses awk to do its |
| 396 | computations and comparisons, so you can use floating-point math; you are not |
| 397 | restricted to integers for comparisons. Floating-point numbers are printed with |
| 398 | six digits of precision. The -o option accepts the arithmetic operators /, *, |
| 399 | +, and -. A division by zero results in zero, not an error. |
| 400 | |
| 401 | If you specify the -I option with an integer argument, the SHOW STATUS values |
| 402 | become incremental instead of absolute. The argument is used as a delay in |
| 403 | seconds, and instead of capturing a single sample of SHOW STATUS and using it |
| 404 | for computations, the plugin captures two samples at the specified interval and |
| 405 | subtracts the second from the first. This lets you evaluate expressions over a |
| 406 | range of time. For example, to warn when there are 10 disk-based temporary |
| 407 | tables per second, over a 5-second sampling period: |
| 408 | |
| 409 | -x Created_tmp_disk_tables -o / -y Uptime -I 5 -w 10 |
| 410 | |
| 411 | That is somewhat contrived, because it could also be written as follows: |
| 412 | |
| 413 | -x Created_tmp_disk_tables -I 5 -w 50 |
| 414 | |
| 415 | The -I option has the side effect of removing any non-numeric SHOW STATUS |
| 416 | variables. Be careful not to set the -I option too large, or Nagios will simply |
| 417 | time the plugin out, usually after about 10 seconds. |
| 418 | |
| 419 | This plugin does not support arbitrarily complex expressions, such as computing |
| 420 | the query cache hit ratio and alerting if it is less than some percentage. If |
| 421 | you are trying to do that, you might be doing it wrong. A dubious example for |
| 422 | the query cache might be to alert if the hit-to-insert ratio falls below 2:1, as |
| 423 | follows: |
| 424 | |
| 425 | -x Qcache_hits -o / -y Qcache_inserts -C '<' -w 2 |
| 426 | |
| 427 | Some people might suggest that the following is a more useful alert for the |
| 428 | query cache: |
| 429 | |
| 430 | -x query_cache_size -c 1 |
| 431 | |
| 432 | To check Percona XtraDB Cluster node status you may want to use the following |
| 433 | alert similar to what its clustercheck does: |
| 434 | |
| 435 | -x wsrep_local_state -C '!=' -w 4 |
| 436 | |
| 437 | To compare string variables use -T C<str> transformation. This is required as |
| 438 | numeric and string comparisons are handled differently. The following example |
| 439 | warns when the slave_exec_mode is IDEMPOTENT: |
| 440 | |
| 441 | -x slave_exec_mode -C '==' -T str -w IDEMPOTENT |
| 442 | |
| 443 | =head1 PRIVILEGES |
| 444 | |
| 445 | This plugin executes the following commands against MySQL: |
| 446 | |
| 447 | =over |
| 448 | |
| 449 | =item * |
| 450 | |
| 451 | C<SHOW STATUS>. |
| 452 | |
| 453 | =item * |
| 454 | |
| 455 | C<SHOW VARIABLES>. |
| 456 | |
| 457 | =back |
| 458 | |
| 459 | This plugin executes no UNIX commands that may need special privileges. |
| 460 | |
| 461 | =head1 COPYRIGHT, LICENSE, AND WARRANTY |
| 462 | |
| 463 | This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ Percona Inc. |
| 464 | Feedback and improvements are welcome. |
| 465 | |
| 466 | THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 467 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 468 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 469 | |
| 470 | This program is free software; you can redistribute it and/or modify it under |
| 471 | the terms of the GNU General Public License as published by the Free Software |
| 472 | Foundation, version 2. You should have received a copy of the GNU General |
| 473 | Public License along with this program; if not, write to the Free Software |
| 474 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 475 | |
| 476 | =head1 VERSION |
| 477 | |
| 478 | $PROJECT_NAME$ pmp-check-mysql-status $VERSION$ |
| 479 | |
| 480 | =cut |
| 481 | |
| 482 | DOCUMENTATION |