#!/bin/bash

NO_ARGS=0 
E_OPTERROR=65

IGNORE_EXT='bz2|gz|1|2|3|4'
IGNORE_DIR='domlogs'

if [ $# -eq "$NO_ARGS" ]  # Script invoked with no command-line args?
then
  echo "Usage: `basename $0` -w WARNING_treshhold -c CRITICAL_treshhold -d DIRECTORY"
  echo "Example: `basename $0` -w 100000k -c 500000k -d /var/log"
  echo ""
  echo "size must be in Kilobytes!"
  exit $E_OPTERROR        # Exit and explain usage, if no argument(s) given.
fi  

while getopts ":w:c:d:" Option
# Initial declaration.
# a, b, c, d, e, f, and g are the options (flags) expected.
# The : after option 'e' shows it will have an argument passed with it.
do
  case $Option in
    w ) WARN=$OPTARG ;;
    c ) CRIT=$OPTARG ;;
    d ) DIR=$OPTARG  ;;
  esac
done
shift $(($OPTIND - 1))

# find files greater then CRIT
CRIT_FILES=`find $DIR -size +$CRIT|xargs -r ls -lh|awk '{print $9,$5}'|egrep -v "\\.($IGNORE_EXT) "| grep -v $IGNORE_DIR`
if [ "$CRIT_FILES" ] ; then
	echo "CRITICAL in $DIR: $CRIT_FILES"
	exit 2
fi

# find files greater then WARN but less then CRIT
WARN_FILES=`find $DIR -size +$WARN -size -$CRIT|xargs -r ls -lh|awk '{print $9,$5}'|egrep -v "\\.($IGNORE_EXT) "| grep -v $IGNORE_DIR`
if [ "$WARN_FILES" ] ; then
        echo "WARNING in $DIR: $WARN_FILES"
        exit 1
fi

echo "$DIR is OK"
exit 0
