#!/bin/bash
#
# This script check disk usage by using df

#
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
CRITICAL=101
WARNING=101

PROGNAME=`/bin/basename $0`

# Make sure the correct number of command line
# arguments have been supplied
usage(){
	echo "Usage: $PROGNAME <mountpoint> [-w % -c %]"
}

# Check if parameters are ok
if [ $# -lt 1 ]
then
    usage
    exit $STATE_UNKNOWN
fi

# Save mountpoint
MOUNTPOINT=$1
shift

while test -n "$1"; do
    case "$1" in
        -w)
            WARNING=$2
            shift
            ;;
        -c)
            CRITICAL=$2
            shift
            ;;
        *)
            echo "Unknown argument: $1"
            usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done

PERC=`df $MOUNTPOINT | awk '{ a = $5 } END { print a }'`

PERCENT=`echo $PERC | sed -e 's!%!!g'`
CRITICAL=`echo $CRITICAL | sed -e 's!%!!g'`
WARNING=`echo $WARNING | sed -e 's!%!!g'`

if [ $PERCENT -gt $CRITICAL ]
then
	echo "CRITICAL - $MOUNTPOINT = $PERCENT % | usage_$MOUNTPOINT=$PERC;$WARNING%;$CRITICAL%"
	exit $STATE_CRITICAL
fi

if [ $PERCENT -ge $WARNING ]
then
	echo "WARNING - $MOUNTPOINT = $PERCENT % | usage_$MOUNTPOINT=$PERC;$WARNING%;$CRITICAL%"
	exit $STATE_WARNING
fi

echo "OK - $MOUNTPOINT = $PERCENT % | usage_$MOUNTPOINT=$PERC;$WARNING%;$CRITICAL%"
exit $STATE_OK
