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