Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am new to shell scripting and want to implement a script on my server which will automatically send email alerts if the disk usage goes over 90% and second email alert when it goes over 95%. my filesystem is abc:/xyz/abc and mount is /pqr.

Please help me with the scripts.

share|improve this question
3  
This is something you should search for or attempt to write, not to ask someone to write for you. If you are not into that, then you should hire someone to do so for you. –  johnshen64 Jul 24 '13 at 16:15
    
@johnshen64- ADMIN="def@stu.com" ALERT=70 df -H | grep -vE 'abc:/xyz/abc | tmpfs |cdrom' | awk '{ print $5 " " $1 }' | while read output; do usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 ) partition=$(echo $output | awk '{ print $2 }' ) if [ $usep -ge $ALERT ]; then mail -s "Alert: Almost out of disk space $usep" $ADMIN fi done now can you let me know whats wrong with this script? i wanted to see the different approaches. –  Tanay Mathur Jul 24 '13 at 16:24
1  
@TanayMathur - Please update your question with the script and use the text formatting option instead of posting all that in a comment, which is pretty unreadable. –  nerdwaller Jul 24 '13 at 16:53
    
I concur, and that would be a very good start. The question as it is is liable to get closed. –  johnshen64 Jul 24 '13 at 17:01
    
nagiosplugins.org/man/check_disk –  Squeezy Jul 24 '13 at 17:52

1 Answer 1

Here's one of the many ways of doing it. This one is quick and dirty:

#!/bin/bash

errortext=""
EMAILS="address@domain.com,otheraddress@domain.com"

# get file system disk usage report for 
# all file systems (-a) 
# with POSIX output (P) 
# in human readable format (h)
# Look for only vgroup (I've LVM), but if you want only /home and /var 
# to be checked for, do something like this: egrep 'home|var'

for line in `df -aPh | egrep 'vgroup-' | sort | awk '{print$6"-"$5"-"$4}'`
do

    # get the percent and chop off the %
    percent=`echo "$line" | awk -F - '{print$2}' | cut -d % -f 1`
    partition=`echo "$line" | awk -F - '{print$1}' | cut -d % -f 1`

    # Let's set the limit to 90% when alert should be sent
    limit=90

    if [ $percent -ge $limit ]; then
        errortext="$errortext $line"
    fi
done

# send an email
if [ -n "$errortext" ]; then
    echo "$errortext" | mail -s "NOTIFICATION: some partitions on almost full"         $EMAILS
fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.