nagios/check_sftp_disk
Moritz Rudert (helios) 68840f51ec initial commit
2012-02-12 10:23:57 +01:00

209 lines
5.5 KiB
Bash
Executable File

#! /bin/bash
PROGNAME="$(basename "$0")"
REVISION="0.1"
. "$(dirname "$0")/utils.sh"
print_usage() {
echo "Usage: $PROGNAME -w <limit> -c <limit> [<options>] <host>"
}
print_help() {
print_revision $PROGNAME $REVISION
echo ""
print_usage
echo ""
cat <<EOF
This plugin checks remote disk space/quota via SFTP.
Options:
-h, --help
Print detailed help screen
-w, --warning=INTEGER
Exit with WARNING status if less than INTEGER units of disk are free
-w, --warning=PERCENT%
Exit with WARNING status if less than PERCENT of disk space is free
-c, --critical=INTEGER
Exit with CRITICAL status if less than INTEGER units of disk are free
-c, --critical=PERCENT%
Exit with CRITCAL status if less than PERCENT of disk space is free
-W, --iwarning=PERCENT%
Exit with WARNING status if less than PERCENT of inode space is free
-K, --icritical=PERCENT%
Exit with CRITICAL status if less than PERCENT of inode space is free
-p, --path=PATH
Path or partition (may be repeated)
-u, --units=STRING
Choose bytes, kB, MB, GB, TB (default: MB)
-k, --kilobytes
Same as '--units kB'
-m, --megabytes
Same as '--units MB'
-l, --login=USER
Login as USER
-i, --identity=FILE
Use FILE as private keyfile for authentication
EOF
support
exit 0
}
ARGS="$(getopt -o hw:c:W:K:p:u:kmi:l: --long --help,--warning,--critical,--iwarning,--icritical,--path,--units,--kilobytes,--megabytes,--identity -n "$PROGNAME" -- "$@")"
eval set -- "$ARGS"
WARNING=""
CRITICAL=""
IWARNING=""
ICRITICAL=""
REMOTEPATH=""
IDENTITY=""
LOGIN=""
UNIT="MB"
while true; do
case "$1" in
-h|--help)
print_help
exit 0
;;
-w|--warning)
WARNING="$2"
shift
;;
-c|--critical)
CRITICAL="$2"
shift
;;
-W|--iwarning)
IWARNING="$2"
shift
;;
-K|--icritical)
ICRITICAL="$2"
shift
;;
-u|--units)
UNIT="$2"
shift
;;
-k|--kilobytes)
UNIT="kB"
shift
;;
-m|--megabytes)
UNIT="MB"
shift
;;
-p|--path)
REMOTEPATH="$2"
shift
;;
-i|--identity)
IDENTITY="-i $2"
shift
;;
-l|--login)
LOGIN="$2@"
shift
;;
--)
shift
break
;;
*)
echo "Something went wrong while parsing option: $1"
exit $STATE_UNKNOWN
;;
esac
shift
done
if [ $# -ne 1 -o -z "$WARNING" -o -z "$CRITICAL" ] ; then
print_usage
exit $STATE_UNKNOWN
fi
case "$UNIT" in
bytes|b) UNITDIV=1 ;;
kB) UNITDIV=1024 ;;
MB) UNITDIV=1048576 ;;
GB) UNITDIV=1073741824 ;;
TB) UNITDIV=1099511627776 ;;
*) UNITDIV=1048576 ;;
esac
# Sanity checks
if ! [ "${CRITICAL}" -ge 0 ] 2> /dev/null \
&& ! [ "${CRITICAL/\%/}" -ge 0 ] 2> /dev/null; then
echo "Critical threshold (${CRITICAL}) is neither number nor percentage!"
print_help
exit $STATE_UNKNOWN
fi
if ! [ "${WARNING}" -ge 0 ] 2> /dev/null \
&& ! [ "${WARNING/\%/}" -ge 0 ] 2> /dev/null; then
echo "Warning threshold (${WARNING}) is neither number nor percentage!"
print_help
exit $STATE_UNKNOWN
fi
if [ -n "${ICRITICAL}" ] \
&& ! [ "${ICRITICAL}" -ge 0 ] 2> /dev/null \
&& ! [ "${ICRITICAL/\%/}" -ge 0 ] 2> /dev/null; then
echo "ICritical threshold (${ICRITICAL}) is neither number nor percentage!"
print_help
exit $STATE_UNKNOWN
fi
if [ -n "${IWARNING}" ] \
&& ! [ "${IWARNING}" -ge 0 ] 2> /dev/null \
&& ! [ "${IWARNING/\%/}" -ge 0 ] 2> /dev/null; then
echo "IWarning threshold (${IWARNING}) is neither number nor percentage!"
print_help
exit $STATE_UNKNOWN
fi
HOST="$1"
if [ -n "${REMOTEPATH}" ] ; then
CMD="cd ${REMOTEPATH}\ndf\ndf -i"
else
CMD="df\ndf -i"
fi
if ! rawout="$(echo -e "${CMD}" | sftp $IDENTITY -b- "${LOGIN}${HOST}" 2>&1)"; then
echo "$rawout"
exit $STATE_UNKNOWN
fi
rawout="$(echo "$rawout" | grep -v '^sftp> df' | sed -e 's/[[:space:]]\+/ /g')" || exit $STATE_UNKNOWN
# TODO: if one failes, just set UNKNOWN as worst state, not yet exit
iout="$(echo "$rawout" | grep -A1 '^\s*Inodes' | tail -n 1)" #|| { [ -n "${IWARNING}" -o -n "${ICIRITCAL}" ] && exit $STATE_UNKNOWN }
out="$(echo "$rawout" | grep -A1 '^\s*Size' | tail -n 1)" || exit $STATE_UNKNOWN
percentage="$(echo "$out" | cut -d " " -f 6)"
percentage=$((100 - ${percentage/\%/}))
free="$(($(echo "$out" | cut -d " " -f 4) * 1024))"
ifree="$(echo "$iout" | cut -d " " -f 6)"
ifree=$((100 - ${ifree/\%/}))
details="free space: ${HOST} $((${free} / ${UNITDIV})) ${UNIT} (${percentage}% inode=${ifree}%)"
if ( [ "${CRITICAL/\%/}" != "${CRITICAL}" ] && [ "${percentage}" -lt "${CRITICAL/\%/}" ] ) || \
( [ "${CRITICAL/\%/}" = "${CRITICAL}" ] && [ "${free}" -lt "${CRITICAL}" ] ) || \
( [ -n "$ICRITICAL" ] && [ "${ifree}" -lt "${ICRITICAL/\%/}" ] ); then
echo "DISK CRITICAL - $details"
exit $STATE_CRITICAL
fi
if ( [ "${WARNING/\%/}" != "${WARNING}" ] && [ "${percentage}" -lt "${WARNING/\%/}" ] ) || \
( [ "${WARNING/\%/}" = "${WARNING}" ] && [ "${free}" -lt "${WARNING}" ] ) || \
( [ -n "$IWARNING" ] && [ "${ifree}" -lt "${IWARNING/\%/}" ] ); then
echo "DISK WARNING - $details"
exit $STATE_WARNING
fi
echo "DISK OK - $details"
exit $STATE_OK