| 1 | #!/bin/sh |
| 2 | |
| 3 | # ######################################################################## |
| 4 | # This program is part of $PROJECT_NAME$ |
| 5 | # License: GPL License (see COPYING) |
| 6 | # Authors: |
| 7 | # Baron Schwartz |
| 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 | -H) shift; OPT_HOST="${1}"; shift; ;; |
| 34 | -l) shift; OPT_USER="${1}"; shift; ;; |
| 35 | -L) shift; OPT_LOPA="${1}"; shift; ;; |
| 36 | -p) shift; OPT_PASS="${1}"; shift; ;; |
| 37 | -P) shift; OPT_PORT="${1}"; shift; ;; |
| 38 | -S) shift; OPT_SOCK="${1}"; shift; ;; |
| 39 | -t) shift; OPT_TMPDIR="${1}"; shift; ;; |
| 40 | -w) shift; OPT_WARN="${1}"; shift; ;; |
| 41 | --version) grep -A2 '^=head1 VERSION' "$0" | tail -n1; exit 0 ;; |
| 42 | --help) perl -00 -ne 'm/^ Usage:/ && print' "$0"; exit 0 ;; |
| 43 | -*) echo "Unknown option ${o}. Try --help."; exit 1; ;; |
| 44 | esac |
| 45 | done |
| 46 | if [ -e '/etc/nagios/mysql.cnf' ]; then |
| 47 | OPT_DEFT="${OPT_DEFT:-/etc/nagios/mysql.cnf}" |
| 48 | fi |
| 49 | if is_not_sourced; then |
| 50 | if [ -n "$1" ]; then |
| 51 | echo "WARN spurious command-line options: $@" |
| 52 | exit 1 |
| 53 | fi |
| 54 | fi |
| 55 | |
| 56 | # If any connection option was given, then try to log in to find the server's |
| 57 | # tmpdir. |
| 58 | if [ "${OPT_DEFT}${OPT_HOST}${OPT_USER}${OPT_PASS}${OPT_PORT}${OPT_SOCK}" ]; then |
| 59 | OPT_TMPDIR=$(mysql_exec "SELECT @@tmpdir") || exit $? |
| 60 | elif [ -z "${OPT_TMPDIR}" ]; then |
| 61 | OPT_TMPDIR="${TMPDIR:-/tmp/}" |
| 62 | fi |
| 63 | |
| 64 | # TODO: We could auto-check every running instance, not just one. |
| 65 | local NOTE="OK no deleted files" |
| 66 | local PROC_ID=$(_pidof mysqld | head -n1) |
| 67 | if [ "${PROC_ID}" ]; then |
| 68 | local TEMP=$(mktemp -t "${0##*/}.XXXXXX") || exit $? |
| 69 | trap "rm -f '${TEMP}' >/dev/null 2>&1" EXIT |
| 70 | if _lsof "${PROC_ID}" > "${TEMP}" ; then |
| 71 | # If lsof exists, but you run it as non-root, you'll get a file with a |
| 72 | # bunch of this stuff: |
| 73 | # mysqld 15287 ... /proc/15287/cwd (readlink: Permission denied) |
| 74 | # We have to detect this and return UNK. |
| 75 | if grep -v -e denied -e COMMAND "${TEMP}" >/dev/null 2>&1; then |
| 76 | local FILES=$(check_deleted_files "${TEMP}" "${OPT_TMPDIR}") |
| 77 | NOTE="open but deleted files: ${FILES}" |
| 78 | if [ "${FILES}" -a -z "${OPT_WARN}" ]; then |
| 79 | NOTE="CRIT $NOTE" |
| 80 | elif [ "${FILES}" ]; then |
| 81 | NOTE="WARN $NOTE" |
| 82 | else |
| 83 | NOTE="OK no deleted files" |
| 84 | fi |
| 85 | else |
| 86 | NOTE="UNK You must execute lsof with root privileges" |
| 87 | fi |
| 88 | else |
| 89 | NOTE="UNK could not list MySQL's open files" |
| 90 | fi |
| 91 | else |
| 92 | NOTE="UNK could not find a mysqld process" |
| 93 | fi |
| 94 | echo $NOTE |
| 95 | } |
| 96 | |
| 97 | # ######################################################################## |
| 98 | # A wrapper around pidof, which might not exist. The first argument is the |
| 99 | # command name to match. |
| 100 | # ######################################################################## |
| 101 | _pidof() { |
| 102 | if ! pidof "${1}" 2>/dev/null; then |
| 103 | ps axo pid,ucomm | awk -v comm="${1}" '$2 == comm { print $1 }' |
| 104 | fi |
| 105 | } |
| 106 | |
| 107 | # ######################################################################## |
| 108 | # Execute a MySQL command. |
| 109 | # ######################################################################## |
| 110 | mysql_exec() { |
| 111 | mysql ${OPT_DEFT:+--defaults-file="${OPT_DEFT}"} \ |
| 112 | ${OPT_LOPA:+--login-path="${OPT_LOPA}"} \ |
| 113 | ${OPT_HOST:+-h"${OPT_HOST}"} ${OPT_PORT:+-P"${OPT_PORT}"} \ |
| 114 | ${OPT_USER:+-u"${OPT_USER}"} ${OPT_PASS:+-p"${OPT_PASS}"} \ |
| 115 | ${OPT_SOCK:+-S"${OPT_SOCK}"} -ss -e "$1" |
| 116 | } |
| 117 | |
| 118 | # ######################################################################## |
| 119 | # A wrapper around lsof, which might not exist. The first argument is the |
| 120 | # process ID to match. Otherwise, the fallback of listing /proc/pid/fd |
| 121 | # will probably only work on Linux. For BSD, fstat will be used. |
| 122 | # ######################################################################## |
| 123 | _lsof() { |
| 124 | PATH="$PATH:/usr/sbin:/sbin" |
| 125 | if ! lsof -p $1 2>/dev/null; then |
| 126 | if ! /bin/ls -l /proc/$1/fd 2>/dev/null; then |
| 127 | fstat -p $1 2>/dev/null |
| 128 | fi |
| 129 | fi |
| 130 | } |
| 131 | |
| 132 | # ######################################################################## |
| 133 | # Generate a list of file handles that MySQL has open, but which are deleted, |
| 134 | # and are not temp files such as /tmp/ib* files (InnoDB) or /tmp/ML* files |
| 135 | # (binary logging). The first argument is a file containing the output of lsof |
| 136 | # or ls -l for the open files. The second argument is the server's tmpdir. |
| 137 | # ######################################################################## |
| 138 | check_deleted_files() { |
| 139 | awk -v tmpdir="${2}" ' |
| 140 | /\(deleted\)/ { if ( index($0, tmpdir) == 0 ) { |
| 141 | if ( $NF ~ /deleted/ ) { |
| 142 | lf=NF-1; |
| 143 | } |
| 144 | else { |
| 145 | lf=NF; |
| 146 | } |
| 147 | print $lf; |
| 148 | }}' "${1}" |
| 149 | } |
| 150 | |
| 151 | # ######################################################################## |
| 152 | # Determine whether this program is being executed directly, or sourced/included |
| 153 | # from another file. |
| 154 | # ######################################################################## |
| 155 | is_not_sourced() { |
| 156 | [ "${0##*/}" = "pmp-check-mysql-deleted-files" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ] |
| 157 | } |
| 158 | |
| 159 | # ######################################################################## |
| 160 | # Execute the program if it was not included from another file. |
| 161 | # This makes it possible to include without executing, and thus test. |
| 162 | # ######################################################################## |
| 163 | if is_not_sourced; then |
| 164 | OUTPUT=$(main "$@") |
| 165 | EXITSTATUS=$STATE_UNKNOWN |
| 166 | case "${OUTPUT}" in |
| 167 | UNK*) EXITSTATUS=$STATE_UNKNOWN; ;; |
| 168 | OK*) EXITSTATUS=$STATE_OK; ;; |
| 169 | WARN*) EXITSTATUS=$STATE_WARNING; ;; |
| 170 | CRIT*) EXITSTATUS=$STATE_CRITICAL; ;; |
| 171 | esac |
| 172 | echo "${OUTPUT}" |
| 173 | exit $EXITSTATUS |
| 174 | fi |
| 175 | |
| 176 | # ############################################################################ |
| 177 | # Documentation |
| 178 | # ############################################################################ |
| 179 | : <<'DOCUMENTATION' |
| 180 | =pod |
| 181 | |
| 182 | =head1 NAME |
| 183 | |
| 184 | pmp-check-mysql-deleted-files - Alert when MySQL's files are deleted. |
| 185 | |
| 186 | =head1 SYNOPSIS |
| 187 | |
| 188 | Usage: pmp-check-mysql-deleted-files [OPTIONS] |
| 189 | Options: |
| 190 | -c CRIT Critical threshold; ignored. |
| 191 | --defaults-file FILE Only read mysql options from the given file. |
| 192 | Defaults to /etc/nagios/mysql.cnf if it exists. |
| 193 | -H HOST MySQL hostname. |
| 194 | -l USER MySQL username. |
| 195 | -L LOGIN-PATH Use login-path to access MySQL (with MySQL client 5.6). |
| 196 | -p PASS MySQL password. |
| 197 | -P PORT MySQL port. |
| 198 | -S SOCKET MySQL socket file. |
| 199 | -w WARN Warning threshold; changes the alert to WARN instead of CRIT. |
| 200 | --help Print help and exit. |
| 201 | --version Print version and exit. |
| 202 | Options must be given as --option value, not --option=value or -Ovalue. |
| 203 | Use perldoc to read embedded documentation with more details. |
| 204 | |
| 205 | =head1 DESCRIPTION |
| 206 | |
| 207 | This Nagios plugin looks at the files that the mysqld process has open, and |
| 208 | warns if any of them are deleted that shouldn't be. This typically happens when |
| 209 | there is a poorly written logrotate script or when a human makes a mistake at |
| 210 | the command line. This can cause several bad effects. If a table has been |
| 211 | deleted, of course, it is a serious matter. Such a file can also potentially |
| 212 | fill up the disk invisibly. If the file is the server's log, it might mean that |
| 213 | logging is effectively broken and any problems the server experiences could be |
| 214 | undiagnosable. |
| 215 | |
| 216 | The plugin accepts the -w and -c options for compatibility with standard Nagios |
| 217 | plugin conventions, but they are not based on a threshold. Instead, the plugin |
| 218 | raises a critical alert by default, and if the -w option is given, it raises a |
| 219 | warning instead, regardless of the option's value. |
| 220 | |
| 221 | This plugin doesn't alert about deleted temporary files, which are not a |
| 222 | problem. By default, this plugin assumes that the server's temporary directory |
| 223 | is either the TMPDIR environment variable, or if that is not set, then /tmp/. |
| 224 | If you specify MySQL authentication options, the value will log into the |
| 225 | specified MySQL instance and look at the C<tmpdir> variable to find the |
| 226 | temporary directory. |
| 227 | |
| 228 | This plugin looks at the first running instance of MySQL, as found in the |
| 229 | system process table, so it will not work on systems that have multiple |
| 230 | instances running. It probably works best on Linux, though it might work on |
| 231 | other operating systems. It relies on either lsof or fstat or the ability to |
| 232 | list the files in the process's /proc/pid/fd directory. |
| 233 | |
| 234 | =head1 PRIVILEGES |
| 235 | |
| 236 | This plugin executes the following commands against MySQL: |
| 237 | |
| 238 | =over |
| 239 | |
| 240 | =item * |
| 241 | |
| 242 | C<SELECT> the system variable C<@@tmpdir>. |
| 243 | |
| 244 | =back |
| 245 | |
| 246 | This plugin executes the following UNIX commands that may need special privileges: |
| 247 | |
| 248 | =over |
| 249 | |
| 250 | =item * |
| 251 | |
| 252 | ps |
| 253 | |
| 254 | =item * |
| 255 | |
| 256 | C<lsof> or C<ls /proc/$pid/fd> (Linux), C<fstat> (BSD) |
| 257 | |
| 258 | =back |
| 259 | |
| 260 | The plugin should be able to find mysqld PID using C<ps> command. |
| 261 | |
| 262 | On BSD, if C<sysctl> option C<security.bsd.see_other_uids> is set to 0, C<ps> |
| 263 | will not return mysqld PID if the plugin run from non-root user. |
| 264 | |
| 265 | =head1 COPYRIGHT, LICENSE, AND WARRANTY |
| 266 | |
| 267 | This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ Percona Inc. |
| 268 | Feedback and improvements are welcome. |
| 269 | |
| 270 | THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 271 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 272 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 273 | |
| 274 | This program is free software; you can redistribute it and/or modify it under |
| 275 | the terms of the GNU General Public License as published by the Free Software |
| 276 | Foundation, version 2. You should have received a copy of the GNU General |
| 277 | Public License along with this program; if not, write to the Free Software |
| 278 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 279 | |
| 280 | =head1 VERSION |
| 281 | |
| 282 | $PROJECT_NAME$ pmp-check-mysql-deleted-files $VERSION$ |
| 283 | |
| 284 | =cut |
| 285 | |
| 286 | DOCUMENTATION |