0

I'm having issues with a script i'm writing. I'm trying to monitor free disk space, and to have an email generated for each filesystem over a certain threshold. When using this script with 'echo' instead of 'mail', the output looks correct to my terminal. When i incorporate mail, only the critical warning is sent, and the body of the email includes the other filesystems. I'm trying to send individual emails for each filesystem. I'm not looking for an answer, but maybe a certain place to focus on to try and determine what my issue is.

 #!/bin/bash
    #The following script will check filesystems (local/nfs only)
    #and notify via email current user if over certain threshold.

    #Thresholds for script to call
    CRITICAL=90
    ALERT=70

    #Gets local/nfs disk info, greps out header/tmp filesystems and awks column 1 and 5
    #while read loop is enabled to parse through each line
    df -l | grep -vE '(^Filesystem)' | awk '{ print ($5  " " $1)}' | while read output;
    do
        #updates variables to reads current step
        usage=$( echo $output | awk '{print $1}' | cut -d'%' -f1 )
        part=$( echo $output | awk '{print $2}' )

        #if percentage matches alert or critical, email current user
        if [ $usage -ge $CRITICAL ]; then
            mail -s "Critical Warning: Filesystem $part is at $usage% of capacity." $USER
        elif [ $usage -ge $ALERT ]; then
            mail -s "Warning: Filesystem $part is at $usage% of capacity." $USER
        fi
    done
  • 2
    You've got your answer WRT the mail problem. A few more hints; you need no grep, the grep function could be put into the awk call awk '!/^Filesystem/{ print $5, $1}', and you could avoid all this clumsy processing by reading more than one field directly read usage part. And to remove the % character use usage=${usage%\%}. – Janis Mar 9 '15 at 18:17
2

Your mail command expects its message to be on STDIN, and so reads the remainder of the output generated by your df ... awk pipeline.

If you really don't want any message body in your mail message, simply pipe STDIN from /dev/null:

mail -s "Critical Warning: Filesystem $part is at $usage% of capacity." $USER </dev/null
1

As mentioned above, mail is needing some sort of stdin data or /dev/null to keep it from grabbing the stdio.

But perhaps rather than null data, you might wish to include more information inside the mail body that may help diagnose issues. This example adds an extra data stamp. (always useful to avoid miss-configured mail forwarders from messing with time stamps in mail headers) a full df report on all disks, not just the problem one, and a snapshot of the current processes which may be useful if disk space is being filled by a runaway process.

Example:
mail -s "Critical Warning: Filesystem $part is at $usage% of capacity." $USER << EOM

Critical Report Generated `date`
Disk status:
`df -h`
Process Status: 
`top -b -n 1'
EOM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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