Shell script partitions space monitor

Hello friends,

I'm newbie in shell scripting...
Until now i have this script:

 
#!/bin/sh
emailok = email1@domain.com
emailissue = email2@domain.com
limit="50"
df -h | grep -vE '^Filesystem' | awk '{print $1 " " $4 " " $5}'|while read val
do
partition=$(echo $val | awk '{print $1}')
echo "partition:$partition"
usedspace=$(echo $val | awk '{print $3}'| cut -d'%' -f1)
echo "usedspace:$usedspace%"
availspace=$(echo $val | awk '{print $2}')
echo "space available:$availspace"
echo "limit:$limit"
if [ $usedspace -gt $limit ]; then
echo "less than 50% space in $partition = $availspace"|mail -s "space info" $emailissue
else
echo "All ok." |mail -s "space info" $emailok
fi
done

the problem is that i have 2 partitions, and i receive 2 mails. If i have more than 2, then i will receive more mails.

what i want is to receive 1 mail, with 2 or more messages.

any ideas?

nobody?

Try this:

#!/bin/sh
emailok = email1@domain.com
emailissue = email2@domain.com
limit="50"
msgtxt=/tmp/email_msg.txt
msgok =/tmp/msg_ok.txt
touch $msgtxt
touch $msgok 

df -h | grep -vE '^Filesystem' | awk '{print $1 " " $4 " " $5}'|while read val
do
partition=$(echo $val | awk '{print $1}')
echo "partition:$partition"
usedspace=$(echo $val | awk '{print $3}'| cut -d'%' -f1)
echo "usedspace:$usedspace%"
availspace=$(echo $val | awk '{print $2}')
echo "space available:$availspace"
echo "limit:$limit"
if [ $usedspace -gt $limit ]; then
echo "less than 50% space in $partition = $availspace" >> $msgtxt
else
echo "All ok." > $msgok 
fi
done

if [ -s $msgtxt ]; then
cat $msgok | mail -s "space info" $emailok 
else
cat $msgtxt | mail -s "space info" $emailissue
fi

cheers,
Devaraj Takhellambam

Thanks a lot...
I've did some changes...

 
!/bin/bash
emailok=email@domain.com
emailissue=email2@domain.com
limit="50"
msgissue=/tmp/msgissue.txt
msgok=/tmp/msgok.txt
touch $msgissue
touch $msgok
df -h | grep -vE '^Filesystem' | awk '{print $1 " " $4 " " $5}'|while read val
do
partition=$(echo $val | awk '{print $1}')
echo "partition:$partition"
usedspace=$(echo $val | awk '{print $3}'| cut -d'%' -f1)
echo "usedspace:$usedspace%"
availspace=$(echo $val | awk '{print $2}')
echo "space available:$availspace"
echo "limit:$limit"
if [ $usedspace -gt $limit ]; then
echo "less then 50% space in $partition .">$msgissue
else
echo "All ok.">$msgok
fi
done
if [ -s $msgissue ]; then
cat $msgok | mail -s "space info" $emailok
else
cat $msgissue | mail -s "space info" $emailissue
fi

now I get an empty mail, instead of a mail with issue message.... :frowning: why?

1.Can you check what are the contents of these files?

msgissue=/tmp/msgissue.txt
msgok=/tmp/msgok.txt
  1. This should be a double redirect ( append mode) as this is in a loop.
echo "less then 50% space in $partition .">>$msgissue

cheers,
Devaraj Takhellambam

1.I've already checked...

/tmp/msgok.txt has "All ok" message

/tmp/msgissue.txt is empty

2.Now it's doubled...

Still problems....

Null message body; hope that's ok

Can you change the mail command to

mailx -s "space info"  $emailok < $msgok

and

mailx -s "space info" $emailissue < $msgissue 

cheers,
Devaraj Takhellambam

It's ok... it's working... thanks a lot for help...

actually, it doesn't work... i receive more messages than i need.... first line always repeats at last.... so, if i have 3 partition with more than 50% used space, i will receive a mail with 4 messages...

less than 50% in /hda1
less than 50% in /hda2
less than 50% in /hda3
less than 50% in /hda1

You must remove the file and recreate it every time you run the script.