LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-14-2011, 10:02 AM   #1
linuxandtsm
Member
 
Registered: May 2011
Posts: 194

Rep: Reputation: Disabled
script to send an email


Hi all,

I have a bash script to send alert email if disk usage exceeds 90%. This works fine but this sends one email for each of filesystem that exceeds 90%. I want to send only one email listing all filesystems with over 90% usage. How to change this script. Thanks in advance!

Code:
#!/bin/sh
df -Ph |awk '{ print $5 " " $1 " " $6 }' | while read outstring;
do
  percentuse=$(echo $outstring | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $outstring | awk '{ print $2 }' )
  mountedon=$(echo $outstring | awk '{ print $3 }' )

  if [ $percentuse -ge 90 ]; then
    echo "Filesystem near fill-up |
     mail -s "disk space is low \"$mountedon\" " me@email.com
  fi
done
 
Old 09-14-2011, 10:19 AM   #2
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Rep: Reputation: 16
Maybe this one:
Code:
#!/bin/sh
df -Ph |awk '{ print $5 " " $1 " " $6 }' | while read outstring;
do
  percentuse=$(echo $outstring | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $outstring | awk '{ print $2 }' )
  mountedon1=$(echo $outstring | awk '{ print $3 }' )

  if [ $percentuse -ge 90 ]; then
    mountedon=$mountedon\ $mountedon1
  fi
done

echo "Filesystem near fill-up |
     mail -s "disk space is low \"$mountedon\" " me@email.com
 
Old 09-14-2011, 10:46 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

@doru: I don't think that $mountedon is still set outside the while read loop.

Something like this should work if the while read loop is used:
Code:
#!/bin/sh

LOG="/tmp/script.log"

df -Ph | awk '{ print $5 " " $1 " " $6 }' | while read outstring;
do
  percentuse=$(echo $outstring | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $outstring | awk '{ print $2 }' )
  mountedon=$(echo $outstring | awk '{ print $3 }' )

  if [[ $percentuse -ge 90 ]]; then
    echo "Filesystem near fill-up : $mountedon" >> $LOG
  fi
done

[[ -a $LOG ]] && mail -s "disk space is low" me@email.com < $LOG && rm $LOG
Hope this helps.
 
1 members found this post helpful.
Old 09-14-2011, 10:57 AM   #4
linuxandtsm
Member
 
Registered: May 2011
Posts: 194

Original Poster
Rep: Reputation: Disabled
Hi druuna,

Thanks for the quick reply.
I got this error.

./diskusage.sh: line 18: [[-a: command not found
 
Old 09-14-2011, 11:07 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by linuxandtsm View Post
Thanks for the quick reply.
You're welcome

Quote:
I got this error.

./diskusage.sh: line 18: [[-a: command not found
You seem to be missing a space between [[ and -a.

Hope this helps.
 
Old 09-14-2011, 01:48 PM   #6
linuxandtsm
Member
 
Registered: May 2011
Posts: 194

Original Poster
Rep: Reputation: Disabled
Hi druuna,

works perfect and is there any way to make text in the email to be bold.
For example if i want to highlight the name of the filesystem in the text.

And could you please explain what
Code:
[[ -a $LOG ]]
is doing
and also where can i read more about these.
Thank you!

Last edited by linuxandtsm; 09-14-2011 at 02:03 PM.
 
Old 09-14-2011, 03:24 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by linuxandtsm View Post
And is there any way to make text in the email to be bold.
For example if i want to highlight the name of the filesystem in the text.
The message that is send is plain text and to my knowledge you cannot apply layout to plain text.

However, it should be possible to create and send a mail in HTML layout.

I don't have any bash experience doing that. Maybe another LQ member can help you with that.

Quote:
[[ -a $LOG ]]
The -a option checks if a file is present. This:
Code:
[[ -a $LOG ]] && mail -s.....
Checks to see if the log file is present and if so (the &&) execute the mail command.

Last edited by druuna; 09-14-2011 at 03:27 PM.
 
Old 09-14-2011, 03:32 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Some bash tutorials:

- Bash Guide for Beginners
- Advanced Bash-Scripting Guide
- BASH Frequently Asked Questions

Sed/Awk

- gnu sed
- gnu awk

Hope this helps.
 
Old 09-15-2011, 08:44 AM   #9
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,326

Rep: Reputation: 253Reputation: 253Reputation: 253
As a small variation of druunaís script, you can also pipe all to the mail command in case you use a subshell and avoid a temporary file:
Code:
#!/bin/sh

(

unset FILLING

df -Ph | awk '{ print $5 " " $1 " " $6 }' | while read outstring;
do
  percentuse=$(echo $outstring | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $outstring | awk '{ print $2 }' )
  mountedon=$(echo $outstring | awk '{ print $3 }' )

  if [[ $percentuse -ge 90 ]]; then
    FILLING=1
    echo "Filesystem near fill-up : $mountedon"
  fi
done

if [ -z "$FILLING" ]; then
    echo "All filesystem are in safe condition"
fi

) | mail -s "Daily disk space report" me@email.com
On the hand hand you can say that you want only mails in case a filesystem is filling up, but then you donít know whether the script didnít run last night for any reason or that there is no filesystem in question at all.
 
Old 09-15-2011, 09:03 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi Reuti,

As stated in post #3: Variables set inside the while read loop are not known outside that loop (FILLING in this case).

This part will always be printed in your script:
Code:
if [ -z "$FILLING" ]; then
    echo "All filesystem are in safe condition"
With a confusing output as a result:
Code:
Filesystem near fill-up : /data/Movie
Filesystem near fill-up : /data/Sounds
All filesystem are in safe condition
 
Old 09-15-2011, 09:16 AM   #11
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,326

Rep: Reputation: 253Reputation: 253Reputation: 253
Ouch, you are right of course. We would need to request the #!/bin/bash as shell in the script and put the selection at the end:
Code:
#!/bin/bash

(

unset FILLING

while read outstring;
do
  percentuse=$(echo $outstring | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $outstring | awk '{ print $2 }' )
  mountedon=$(echo $outstring | awk '{ print $3 }' )

  if [[ $percentuse -ge 90 ]]; then
    FILLING=1
    echo "Filesystem near fill-up : $mountedon"
  fi
done < <(df -Ph | awk '{ print $5 " " $1 " " $6 }')

if [ -z "$FILLING" ]; then
    echo "All filesystem are in safe condition"
fi

) | mail -s "Daily disk space report" me@email.com
 
Old 09-15-2011, 09:19 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@Reuti: That works!
 
Old 09-15-2011, 09:31 AM   #13
linuxandtsm
Member
 
Registered: May 2011
Posts: 194

Original Poster
Rep: Reputation: Disabled
Hi druuna and Reuti,

Thank you both for helping me.

If i want to run this script daily as cron job, is it sufficient to put an entry in crontab via crontab -e or
should this script also be placed in /etc/cron.daily in RHEL5 ?
 
Old 09-15-2011, 09:35 AM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

You can use crontab -e (no root privileges are needed to run this script, so use a normal user).

An entry like this should work (run script daily at 01:05):
Code:
05 01 * * * /full/path/to/diskusage.sh
Hope this helps.
 
Old 09-15-2011, 11:53 AM   #15
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Rep: Reputation: 16
Quote:
Originally Posted by druuna View Post
Hi,

@doru: I don't think that $mountedon is still set outside the while read loop.
Yes it does. You need to use local within a function to get any sort of encapsulation in bash. And in sh, too, druuna.

My script needs two improvements:
1. It sends an email no matter what. mountedon should be tested before sending mail.
2. It sends all valuable information in subject. It should be placed in the message body.

Last edited by doru; 09-16-2011 at 09:28 AM. Reason: And in sh, too, druuna.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to send email... bfloeagle Linux - General 5 10-13-2009 11:20 AM
send an email of script output s_linux SUSE / openSUSE 19 07-25-2009 04:47 AM
send email from a script r.stiltskin Ubuntu 7 07-26-2008 01:36 AM
How to send an email within a script??? johnsanty Programming 19 09-02-2006 01:43 PM
script to send email message meluser Programming 2 03-26-2003 01:35 AM


All times are GMT -5. The time now is 11:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration