This file is indexed.

/usr/lib/nagios/plugins/pmp-check-mysql-replication-delay is in nagios-plugins-contrib 21.20170222.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/sh

# ########################################################################
# 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
   OPT_ENSURE_SBM=0
   MIN_DELAY_SET=0
   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; ;;
         -L)              shift; OPT_LOPA="${1}"; shift; ;;
         -m)              shift; OPT_MIN="${1}"; MIN_DELAY_SET=1; shift; ;;
         -p)              shift; OPT_PASS="${1}"; shift; ;;
         -P)              shift; OPT_PORT="${1}"; shift; ;;
         -S)              shift; OPT_SOCK="${1}"; shift; ;;
         -s)              shift; OPT_SRVID="${1}"; shift; ;;
         -T)              shift; OPT_TABLE="${1}"; shift; ;;
         -u)              shift; OPT_UTC=1; ;;
         -w)              shift; OPT_WARN="${1}"; shift; ;;
         --master-conn)   shift; OPT_MASTERCONN="${1}"; shift; ;;
         --unconfigured)  shift; OPT_REPLNOTSET=1; ;;
         --ensure-sbm)    shift; OPT_ENSURE_SBM=1; ;;
         --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_WARN=${OPT_WARN:-300}
   OPT_CRIT=${OPT_CRIT:-600}
   OPT_MIN=${OPT_MIN:-0}
   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

   # Get replication delay from a heartbeat table or from SHOW SLAVE STATUS.
   if [ "${OPT_TABLE}" ]; then
      if [ -z "${OPT_UTC}" ]; then
         NOW_FUNC='UNIX_TIMESTAMP()'
      else
         NOW_FUNC='UNIX_TIMESTAMP(UTC_TIMESTAMP)'
      fi
      if [ "${OPT_SRVID}" == "MASTER" ]; then
        get_slave_status $1
        if [ "${MYSQL_CONN}" = 0 ]; then
          OPT_SRVID=$(awk '/Master_Server_Id/{print $2}' "${TEMP_SLAVEDATA}")
        fi
      fi
      SQL="SELECT MAX(${NOW_FUNC} - ROUND(UNIX_TIMESTAMP(ts))) AS delay
         FROM ${OPT_TABLE} WHERE (${OPT_SRVID:-0} = 0 OR server_id = ${OPT_SRVID:-0})"
      LEVEL=$(mysql_exec "${SQL}")
      MYSQL_CONN=$?
   else
      get_slave_status $1
      if [ "${MYSQL_CONN}" = 0 ]; then
         LEVEL=$(awk '/Seconds_Behind_Master/{print $2}' "${TEMP_SLAVEDATA}")
      fi
   fi

   # Check for SQL thread errors
   LAST_SLAVE_ERRNO=$(awk '/Last_SQL_Errno/{print $2}' "${TEMP_SLAVEDATA}")

   # Build the common perf data output for graph trending
   PERFDATA="replication_delay=${LEVEL:-0};${OPT_WARN};${OPT_CRIT};0;"

   # Test whether the delay is too long.
   if [ "$MYSQL_CONN" = 0 ]; then
      NOTE="${LEVEL:-0} seconds of replication delay"
      if [ "${LEVEL:-""}" = "NULL" ]; then
         test ${MIN_DELAY_SET} -eq 1 && \
         test ${LAST_SLAVE_ERRNO} -eq 0 && \
         test ${OPT_ENSURE_SBM} -eq 0 && \
            NOTE="OK NULL seconds of replication delay" || NOTE="UNK replica is stopped"
      elif [ -z "${LEVEL}" -a "${OPT_REPLNOTSET}" ]; then
         NOTE="UNK This server is not configured as a replica."
      # pt-slave-delayed slave
      elif [ "${LEVEL:-0}" -lt "${OPT_MIN}" ]; then
         NOTE="CRIT (delayed slave) $NOTE | $PERFDATA"
      elif [ "${LEVEL:-0}" -gt "${OPT_CRIT}" ]; then
         NOTE="CRIT $NOTE | $PERFDATA"
      elif [ "${LEVEL:-0}" -gt "${OPT_WARN}" ]; then
         NOTE="WARN $NOTE | $PERFDATA"
      else
         NOTE="OK $NOTE | $PERFDATA"
      fi
   else
      NOTE="UNK could not determine replication delay"
   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"
}

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

# ########################################################################
# Captures the "SHOW SLAVE STATUS" output into a temp file.
# ########################################################################
get_slave_status() {
  TEMP_SLAVEDATA=$(mktemp -t "${0##*/}.XXXXXX") || exit $?
  trap "rm -f '${TEMP_SLAVEDATA}' >/dev/null 2>&1" EXIT
  if [ -z "$1" ]; then
     if [ "${OPT_MASTERCONN}" ]; then
        # MariaDB multi-source replication
        mysql_exec "SHOW SLAVE '${OPT_MASTERCONN}' STATUS\G" > "${TEMP_SLAVEDATA}"
     else
        # Leverage lock-free SHOW SLAVE STATUS if available
        mysql_exec "SHOW SLAVE STATUS NONBLOCKING\G" > "${TEMP_SLAVEDATA}" 2>/dev/null ||
        mysql_exec "SHOW SLAVE STATUS NOLOCK\G" > "${TEMP_SLAVEDATA}" 2>/dev/null ||
        mysql_exec "SHOW SLAVE STATUS\G" > "${TEMP_SLAVEDATA}"
     fi
     MYSQL_CONN=$?
  else
     # This is for testing only.
     cat "$1" > "${TEMP_SLAVEDATA}" 2>/dev/null
     MYSQL_CONN=0
  fi
}

# ########################################################################
# 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-mysql-replication-delay - Alert when MySQL replication becomes delayed.

=head1 SYNOPSIS

  Usage: pmp-check-mysql-replication-delay [OPTIONS]
  Options:
    -c CRIT         Critical threshold; default 600.
    --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).
    -m CRIT         Minimal threshold to ensure for delayed slaves; default 0.
    -p PASS         MySQL password.
    -P PORT         MySQL port.
    -S SOCKET       MySQL socket file.
    -s SERVERID     MySQL server ID of master, if using pt-heartbeat table. If
                    the parameter is set to "MASTER" the plugin will lookup the
                    server_id of the master
    -T TABLE        Heartbeat table used by pt-heartbeat.
    -u              Use UTC time to count the delay in case pt-heartbeat is run
                    with --utc option.
    -w WARN         Warning threshold; default 300.
    --master-conn NAME  Master connection name for MariaDB multi-source replication.
    --unconfigured  Alert when replica is not configured at all; default no.
    --ensure-sbm    Disallow Seconds_Behind_Master to be NULL for delayed slaves when -m is used
    --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 is delayed too much.  By
default it uses SHOW SLAVE STATUS, but the output of the Seconds_behind_master
column from this command is unreliable, so it is better to use pt-heartbeat from
Percona Toolkit instead.  Use the -T option to specify which table pt-heartbeat
updates.  Use the -s option to specify the master's server_id to compare
against; otherwise the plugin reports the maximum delay from any server. Use
the -s options with the value "MASTER" to have plugin lookup the master's server_id

If you want to run this check against the delayed slaves, e.g. those running
with pt-slave-delay tool, you may want to use -m option specifying the minimal
delay that should be ongoing, otherwise the plugin will alert critical.

=head1 PRIVILEGES

This plugin executes the following commands against MySQL:

=over

=item *

C<SHOW SLAVE STATUS [NONBLOCKING|NOLOCK]>

or

=item *

C<SELECT> from the C<pt-heartbeat> 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-mysql-replication-delay $VERSION$

=cut

DOCUMENTATION