Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

Here's a cron that runs Monday to Friday at 8am that will alert if usage is above 90%

0 8 * * 1-5 df | tail --lines=+2 | sed s/%//g | awk '{ if($5 > 90) print $0;}'|mail -s "space alert" address@domain.com
  • 0 8 * * 1-5 tells crontab to run this at 8am Monday-Friday (1-5)
  • df | tail --lines=+2 takes all the output after skipping the first 2 lines
  • sed s/%//g' strips percent signs
  • awk ... if the 5th field in the output is a number greater than 90 it prints that line
  • |mail -s "space alert" address@domain.com sends that line to the body of an email to address@domain.com titled "space alert"
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .