2

I am at work right now, and my superiors asked me to write/find a shell script for the Red hat Server edition that checks a folder size, and if it's above a certain limit, it will send an email.Can anyone here help me find or help me create a script like that??

I thank you in advance,

Jayakrishnan T

Jayakrishnan T
  • 298
  • 2
  • 8
  • 22
  • 1
    This should get you started: du -sc /path/to/dir/ | grep total | awk '{print $1}' Stick that in a for loop with an if statement and a "mail" command. Be better if you make it send one email with a list of all the dirs that are oversized though, rather than one each. – Sirex May 18 '11 at 06:51

4 Answers4

2

Are you already running Nagios?

Check out check_dirsize or check_filesize_dir:

http://exchange.nagios.org/directory/Plugins/Uncategorized/Operating-Systems/Linux/CheckDirSize/details

http://exchange.nagios.org/directory/Plugins/Uncategorized/Operating-Systems/Linux/check_filesize_dir/details

Both could be easily adapted to run out of cron if you like.

dmourati
  • 25,540
  • 2
  • 42
  • 72
1
#!/bin/bash
DIR=/path/to/dir
SIZE=10000
MAILADDR="mail@domain.com"
if [ $(du -s $DIR | awk '{print $1}') -gt $SIZE ]; then
    echo "$DIR" | mail -s "Directory limit exceeded in $DIR" $MAILADDR
fi

SIZE has to be given in Bytes!

noqqe
  • 173
  • 8
0

I would think that inotifywait(1) from inotify-tools would be helpful here.

glenn jackman
  • 4,630
  • 1
  • 17
  • 20
0

This answer is based on my specific directory needed but you could easily modify it to suite any volume name or even a multiple volumes. I'm not the best with scripting and adapted some functions I found, it will be good to see responses with improvement. Thanks

Script will check the size of /home and if it is too large then email the user who has the largest directory. Note: any non-human user that has a directory, like a service account, would need to be excluded using an exclude list. Service accounts do not check their email very often. This script assumes that once the top offender clears out all they can and if then the disk is over the limit then the next run will find the next largest disk hog and email them. Suggest running every 15 minutes so you may catch the user while they are still logged in.

#!/bin/bash

DISK="/home" # disk to monitor
CURRENT=$(df -h | grep ${DISK} | awk {'print $4'}) # get disk usage from monitored disk
MAX="85%" # max 85% disk usage
DOMAIN="your.com"

# functions #
function max_exceeded() {

    # Max Exceeded now find the largest offender
    cd $DISK
    for i in `ls` ; do du -s $i ; done > /tmp/mail.1
    sort -gk 1 /tmp/mail.1 | tail -1 | awk -F " " '{print $2}' > /tmp/mail.offender
    OFFENDER=`cat /tmp/mail.offender`
    echo $OFFENDER
    du -sh $DISK/$OFFENDER > /tmp/mail.over85
    mail -s "$HOSTNAME $DISK Alert!" "$OFFENDER@$DOMAIN, admin@$DOMAIN"  < /tmp/mail.over85
}

function main() {
    # check if current disk usage is greater than or equal to max usage.
    if [ ${CURRENT} ]; then
            if [ ${CURRENT%?} -ge ${MAX%?} ]; then
            # if it is greater than or equal to max usage we call our max_exceeded function and send mail
            echo "Max usage (${MAX}) exceeded. The /home disk usage is it at ${CURRENT}. Sending email."
            max_exceeded
        fi
    fi
}


# init #
main

#CLEANUP ON AISLE ONE
rm /tmp/mail.1
rm /tmp/mail.offender
rm /tmp/mail.over85

Ken
  • 1
  • 1