Public
Snippet $10 authored by Andy Sayler

alert_freespace

alert_freespace.sh
#!/usr/bin/env bash

# Andy Sayler
# andy.sayler@gmail.com
# 03/2014
# Check free space and alert if less than threshold

USAGE="
Usage:
    ${0} <Mount> [<Threshold>]
"

EXIT_FAILURE=1
EXIT_SUCCESS=0

NOTIFY="root"

# Check Required Args
if [[ ${#} -lt 1 ]]
then
    echo "${USAGE}"
    exit ${EXIT_FAILURE}
fi
MOUNT="${1}"

# Read in threshold (if present)
THRESHOLD=90
if [[ -n ${2} ]]
then
    THRESHOLD=${2}
fi

# Setup Output
SUFFIX=$(echo "${MOUNT}" | tr '/' '_')
OUTPUT="/tmp/alert_freespace_out_${SUFFIX}"
rm -f "${OUTPUT}"

# Grab Input
status=$(df -T -h | grep "\s${MOUNT}\$")
used=$(echo "${status}" | tr -s ' ' | cut -d ' ' -f 6 | tr -d '%')
type=$(echo "${status}" | tr -s ' ' | cut -d ' ' -f 2)

# Check Threshold
if [[ ${used} -gt ${THRESHOLD} ]]
then
    echo "WARNING: ${MOUNT} is ${used}% full" >> "${OUTPUT}"
    df -T -h "${MOUNT}" >> "${OUTPUT}"
    if [[ "${type}" == 'btrfs' ]]
    then
	btrfs filesystem df "${MOUNT}" >> "${OUTPUT}"
    fi
    cat "${OUTPUT}" | mail "${NOTIFY}" -s "WARNING: ${MOUNT} is ${used}% full"
fi

exit ${EXIT_SUCCESS}