#!/bin/bash

# ########################################################################
# This program is part of $PROJECT_NAME$
# License: GPL License (see COPYING)
# Authors:
#  Baron Schwartz, Roman Vynar
# ########################################################################

# ########################################################################
# 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

# ########################################################################
# 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; ;;
         -i)              shift; OPT_INTERVAL="${1}"; shift; ;;
         -l)              shift; OPT_USER="${1}"; shift; ;;
         -L)              shift; OPT_LOPA="${1}"; shift; ;;
         -p)              shift; OPT_PASS="${1}"; shift; ;;
         -P)              shift; OPT_PORT="${1}"; shift; ;;
         -S)              shift; OPT_SOCK="${1}"; shift; ;;
         -T)              shift; OPT_TABLE="${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
   OPT_INTERVAL=${OPT_INTERVAL:-0}
   OPT_TABLE="${OPT_TABLE:-percona.checksums}"
   if [ -e '/etc/nagios/mysql.cnf' ]; then
      OPT_DEFT="${OPT_DEFT:-/etc/nagios/mysql.cnf}"
   fi
   if is_not_sourced; then
      if [ -n "$1" ]; then
         echo "WARN spurious command-line options: $@"
         exit 1
      fi
   fi

   NOTE="UNK couldn't query the checksum table"

   # Set up a temp file to hold error messages from MySQL.
   TEMP=$(mktemp -t "${0##*/}.XXXXXX") || exit $?
   trap "rm -f '${TEMP}' >/dev/null 2>&1" EXIT

   # Get the query from the documentation and execute it.
   SQL=$(get_magic_query "${0}" checksum_diff_query)
   PROBLEMS=$(mysql_exec "${SQL/CHECKSUM_TABLE/${OPT_TABLE}}" 2>"${TEMP}")
   if [ $? = 0 ]; then
      if [ "${PROBLEMS}" ]; then
         NOTE="pt-table-checksum found ${PROBLEMS}"
         if [ "${OPT_CRIT}" ]; then
            NOTE="CRIT $NOTE"
         else
            NOTE="WARN $NOTE"
         fi
      else
         NOTE="OK pt-table-checksum found no out-of-sync tables"
         if [ "${OPT_INTERVAL}" -gt 0 ]; then
            RECENT_CHUNKS=$(mysql_exec "SELECT IF(COALESCE(MAX(ts), NOW()) > NOW() - INTERVAL ${OPT_INTERVAL} DAY, 1, 0) FROM ${OPT_TABLE}")
            if [ "${RECENT_CHUNKS}" = 0 ]; then
               NOTE="pt-table-checksum was not run over last ${OPT_INTERVAL} days"
               if [ "${OPT_CRIT}" ]; then
                  NOTE="CRIT $NOTE"
               else
                  NOTE="WARN $NOTE"
               fi
            fi
         fi
      fi
   else
      if grep "Table '${OPT_TABLE}' doesn't exist" "${TEMP}" >/dev/null 2>&1; then
         NOTE="UNK table '${OPT_TABLE}' doesn't exist"
      fi
   fi
   echo $NOTE
}

# ########################################################################
# Execute a MySQL command.
# ########################################################################
mysql_exec() {
   mysql ${OPT_DEFT:+--defaults-file="${OPT_DEFT}"} \
      ${OPT_LOPA:+--login-path="${OPT_LOPA}"} \
      ${OPT_HOST:+-h"${OPT_HOST}"} ${OPT_PORT:+-P"${OPT_PORT}"} \
      ${OPT_USER:+-u"${OPT_USER}"} ${OPT_PASS:+-p"${OPT_PASS}"} \
      ${OPT_SOCK:+-S"${OPT_SOCK}"} -ss -e "$1"
}

# ########################################################################
# Retrieve a paragraph from the given file, which includes MAGIC_$2 as a
# pattern.
# ########################################################################
get_magic_query() {
   perl -00 -ne"m/MAGIC_$2/ && print" "$1"
}

# ########################################################################
# Determine whether this program is being executed directly, or sourced/included
# from another file.
# ########################################################################
is_not_sourced() {
   [ "${0##*/}" = "pmp-check-pt-table-checksum" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ]
}

# ########################################################################
# Execute the program if it was not included from another file.
# This makes it possible to include without executing, and thus test.
# ########################################################################
if is_not_sourced; then
   OUTPUT=$(main "$@")
   EXITSTATUS=$STATE_UNKNOWN
   case "${OUTPUT}" in
      UNK*)  EXITSTATUS=$STATE_UNKNOWN;  ;;
      OK*)   EXITSTATUS=$STATE_OK;       ;;
      WARN*) EXITSTATUS=$STATE_WARNING;  ;;
      CRIT*) EXITSTATUS=$STATE_CRITICAL; ;;
   esac
   echo "${OUTPUT}"
   exit $EXITSTATUS
fi

# ############################################################################
# Documentation
# ############################################################################
: <<'DOCUMENTATION'
=pod

=head1 NAME

pmp-check-pt-table-checksum - Alert when pt-table-checksum finds data differences on a replica.

=head1 SYNOPSIS

  Usage: pmp-check-pt-table-checksum [OPTIONS]
  Options:
    -c CRIT         Raise a critical error instead of a warning.
    --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.
    -L LOGIN-PATH   Use login-path to access MySQL (with MySQL client 5.6).
    -p PASS         MySQL password.
    -P PORT         MySQL port.
    -S SOCKET       MySQL socket file.
    -i INTERVAL     Interval over which to ensure pt-table-checksum was run,
                    in days; default - not to check.
    -T TABLE        The checksum table; default percona.checksums
    -w WARN         Warning threshold; ignored.
    --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 MySQL replication has drifted out of sync
with the master's data, according to checks performed by the pt-table-checksum
tool in Percona Toolkit.  It uses the following query to determine whether the
server's data matches its master's:

   SELECT /* MAGIC_checksum_diff_query */
   CONCAT(
      COUNT(*),
      ' chunks differ in ',
      COUNT(DISTINCT CONCAT(db, tbl)),
      ' tables, including ',
      MIN(CONCAT(db, '.', tbl)))
   FROM CHECKSUM_TABLE
   WHERE master_cnt <> this_cnt OR master_crc <> this_crc
      OR ISNULL(master_crc) <> ISNULL(this_crc)
   HAVING COUNT(*) > 0

The word CHECKSUM_TABLE is replaced by the value of the -T option.  If the table
specified by -T does not exist, unknown is raised.

Optionally, you can specify an interval in days over which to ensure pt-table-checksum
was run. It is useful in cases when the cron job doing the checksumming suddenly
stopped working. This option will have an effect when no diffs are found and the
checksum table is not empty.

Alerts are raised at a WARNING level by default, but specifying the -c option
with any value will change this to CRITICAL instead.

=head1 PRIVILEGES

This plugin executes the following commands against MySQL:

=over

=item *

C<SELECT> against the specified table.

=back

This plugin executes no UNIX commands that may need special privileges.

=head1 COPYRIGHT, LICENSE, AND WARRANTY

This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ 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

$PROJECT_NAME$ pmp-check-pt-table-checksum $VERSION$

=cut

DOCUMENTATION
