| 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 | # Print the name and fullness of every LVM snapshot that is open and |
| 26 | # nearly full. The input is the file with 'lvs', and the allowable fullness. |
| 27 | # In many cases lvs will report "File descriptor %d (...) leaked" and we ignore |
| 28 | # this, as it's only a warning that usually happens from a shell. |
| 29 | # ######################################################################## |
| 30 | check_lvm_snapshot_fullness() { |
| 31 | local FILE="$1" |
| 32 | local FULL="$2" |
| 33 | awk -v full="$FULL" ' |
| 34 | $1 != "LV" && $1 != "File" && $6 !~ /[^0-9.]/ && $6 > full { |
| 35 | print $2 "/" $1 "[" $5 "]=" $6 "%" |
| 36 | }' "${FILE}" |
| 37 | } |
| 38 | |
| 39 | # ######################################################################## |
| 40 | # Run the program. |
| 41 | # ######################################################################## |
| 42 | main() { |
| 43 | |
| 44 | # Get options |
| 45 | for o; do |
| 46 | case "${o}" in |
| 47 | -w) shift; OPT_WARN="${1}"; shift; ;; |
| 48 | -c) shift; OPT_CRIT="${1}"; shift; ;; |
| 49 | --version) grep -A2 '^=head1 VERSION' "$0" | tail -n1; exit 0 ;; |
| 50 | --help) perl -00 -ne 'm/^ Usage:/ && print' "$0"; exit 0 ;; |
| 51 | -*) echo "Unknown option ${o}. Try --help."; exit 1; ;; |
| 52 | esac |
| 53 | done |
| 54 | OPT_WARN=${OPT_WARN:-90} |
| 55 | OPT_CRIT=${OPT_CRIT:-95} |
| 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 | local NOTE="OK no full LVM snapshot volumes" |
| 64 | local TEMP=$(mktemp -t "${0##*/}.XXXXXX") || exit $? |
| 65 | trap "rm -f '${TEMP}' >/dev/null 2>&1" EXIT |
| 66 | |
| 67 | # The lvs command is usually in /usr/sbin. But if it's run as a non-root |
| 68 | # user, it will print out "WARNING: Running as a non-root user. Functionality |
| 69 | # may be unavailable." and exit with success anyway. So we have to detect |
| 70 | # this and make the plugin exit UNKNOWN in that case. If there is a $1 it's |
| 71 | # the output of lvs. |
| 72 | PATH="$PATH:/usr/sbin:/sbin" |
| 73 | if [ -z "$1" ]; then |
| 74 | lvs > "${TEMP}" 2>&1 |
| 75 | else |
| 76 | cat "$1" > "${TEMP}" 2>/dev/null # For testing only |
| 77 | fi |
| 78 | |
| 79 | if grep 'command not found' "${TEMP}" > /dev/null 2>&1; then |
| 80 | NOTE="OK $(cat "${TEMP}")" |
| 81 | elif grep 'WARNING: Running as a non-root user' "${TEMP}" >/dev/null 2>&1; then |
| 82 | NOTE="UNK You must execute lvs with root privileges" |
| 83 | else |
| 84 | local VOLS=$(check_lvm_snapshot_fullness "${TEMP}" "${OPT_CRIT}") |
| 85 | if [ "${VOLS}" ]; then |
| 86 | NOTE="CRIT LVM snapshot volumes over ${OPT_CRIT}% full: ${VOLS}" |
| 87 | else |
| 88 | VOLS=$(check_lvm_snapshot_fullness "${TEMP}" "${OPT_WARN}") |
| 89 | if [ "${VOLS}" ]; then |
| 90 | NOTE="WARN LVM snapshot volumes over ${OPT_WARN}% full: ${VOLS}" |
| 91 | fi |
| 92 | fi |
| 93 | fi |
| 94 | |
| 95 | echo $NOTE |
| 96 | } |
| 97 | |
| 98 | # ######################################################################## |
| 99 | # Determine whether this program is being executed directly, or sourced/included |
| 100 | # from another file. |
| 101 | # ######################################################################## |
| 102 | is_not_sourced() { |
| 103 | [ "${0##*/}" = "pmp-check-lvm-snapshots" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ] |
| 104 | } |
| 105 | |
| 106 | # ######################################################################## |
| 107 | # Execute the program if it was not included from another file. |
| 108 | # This makes it possible to include without executing, and thus test. |
| 109 | # ######################################################################## |
| 110 | if is_not_sourced; then |
| 111 | OUTPUT=$(main "$@") |
| 112 | EXITSTATUS=$STATE_UNKNOWN |
| 113 | case "${OUTPUT}" in |
| 114 | UNK*) EXITSTATUS=$STATE_UNKNOWN; ;; |
| 115 | OK*) EXITSTATUS=$STATE_OK; ;; |
| 116 | WARN*) EXITSTATUS=$STATE_WARNING; ;; |
| 117 | CRIT*) EXITSTATUS=$STATE_CRITICAL; ;; |
| 118 | esac |
| 119 | echo "${OUTPUT}" |
| 120 | exit $EXITSTATUS |
| 121 | fi |
| 122 | |
| 123 | # ############################################################################ |
| 124 | # Documentation |
| 125 | # ############################################################################ |
| 126 | : <<'DOCUMENTATION' |
| 127 | =pod |
| 128 | |
| 129 | =head1 NAME |
| 130 | |
| 131 | pmp-check-lvm-snapshots - Alert when LVM snapshots are running out of copy-on-write space. |
| 132 | |
| 133 | =head1 SYNOPSIS |
| 134 | |
| 135 | Usage: pmp-check-lvm-snapshots [OPTIONS] |
| 136 | Options: |
| 137 | -c CRIT Critical threshold; default 95%. |
| 138 | -w WARN Warning threshold; default 90%. |
| 139 | --help Print help and exit. |
| 140 | --version Print version and exit. |
| 141 | Options must be given as --option value, not --option=value or -Ovalue. |
| 142 | Use perldoc to read embedded documentation with more details. |
| 143 | |
| 144 | =head1 DESCRIPTION |
| 145 | |
| 146 | This Nagios plugin looks at the output of the 'lvs' command to find LVM snapshot volumes |
| 147 | that are beginning to run out of copy-on-write space. If a snapshot fills up its |
| 148 | copy-on-write space, it will fail. This is also a useful way to detect whether |
| 149 | some process, such as a backup, failed to release a snapshot volume after |
| 150 | finishing with it. |
| 151 | |
| 152 | =head1 PRIVILEGES |
| 153 | |
| 154 | This plugin does not access MySQL. |
| 155 | |
| 156 | This plugin executes the following UNIX commands that may need special privileges: |
| 157 | |
| 158 | =over |
| 159 | |
| 160 | =item * |
| 161 | |
| 162 | lvs |
| 163 | |
| 164 | =back |
| 165 | |
| 166 | =head1 COPYRIGHT, LICENSE, AND WARRANTY |
| 167 | |
| 168 | This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ Percona Inc. |
| 169 | Feedback and improvements are welcome. |
| 170 | |
| 171 | THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 172 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 173 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 174 | |
| 175 | This program is free software; you can redistribute it and/or modify it under |
| 176 | the terms of the GNU General Public License as published by the Free Software |
| 177 | Foundation, version 2. You should have received a copy of the GNU General |
| 178 | Public License along with this program; if not, write to the Free Software |
| 179 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 180 | |
| 181 | =head1 VERSION |
| 182 | |
| 183 | $PROJECT_NAME$ pmp-check-lvm-snapshots $VERSION$ |
| 184 | |
| 185 | =cut |
| 186 | |
| 187 | DOCUMENTATION |