On Thu, Jan 26, 2006 at 05:13:37PM +0000, Mark Rogers wrote:
Brett Parker wrote:
Hmm, something like...
OK, I'm currently running with:
#!/bin/bash
warningpercentage=90 persontomail=me@someplace.com subjectofmail='Warning: You are running low on diskspace!'
percentageused=$(df -h | sed '1d; s#^.* ([0-9]*)%.*$#\1#;')
if [[ $percentageused -gt $warningpercentage ]]; then ( echo $nicemessage echo df -h ) | mail -s "$subjectofmail" $persontomail fi
The only significant issue I ran in to getting this far was missing quotes around $subjectofmail in mail command; I initially sent emails to you@, are@, running@, etc.
The bit missing is the fact I want to run it regularly from cron, but not get an email every 30 mins (or whatever) once the disk space runs low. I think it should ideally send a new one every time the %age drops from the last email sent (so if the threshold is 90% I'll get no more than 10 emails before the server fills). But obviously it needs to reset itself when the level drops back down.
So, my quick hack to get that is:
#!/bin/bash
warningpercentage=90 persontomail=me@someplace.com subjectofmail='Warning: You are running low on diskspace!'
oldusage=0 if [ -f ~/.diskspacewarning ]; then oldusage=`cat ~/.diskspacewarning`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ could be better... how about... oldusage=$(<~/.diskspacewarning)
fi
percentageused=$(df -h | sed '1d; s#^.* ([0-9]*)%.*$#\1#;')
if [ $percentageused -gt $oldusage ]; then if [[ $percentageused -gt $warningpercentage ]]; then ( echo $nicemessage echo df -h ) | mail -s "$subjectofmail" $persontomail fi fi
echo $percentageused > ~/.diskspacewarning
It works, but I'm sure it could be done better? [This is a learning exercise for me!]
There's (obviously) a few more tweaks that could be put in, but hey, it now fits it's requirements, don't break what doesn't need fixing :)
Cheers,