Emailing script in FreeBSD

Hi,

I am currently checking the df on a server and if a disk is over a certain threshold, an email is sent out to alert others. However, this is run every minute; I do not want the email to be sent out continuously if a hard disk is running out of space. I would like my script to keep checking as normal, however if it sends an email it sets a flag to 1 (meaning that an email was sent) and if the flag remains 0 then no email was sent before, so then when it checks again if the HDD space is high yet has the same value as before - no email is sent. But if the value is different, then the email is sent.

Any idea on how I can do this please?

So far I have got my original script - checks df and emails if the threshold is reached. I have written another script which copies the df in a text file and then compares current with previous.

However then I am lost - nothing is working and I need help!

Thanks!
 
I've done something similar in the past but in a simpler way.
Make your script accepting a paramter that enables/disables the email feature. Then place in cron or periodic two entries for the same script, one with the false flag (no email) runned frequently, and one true running at the min time you want to receive a new email.

Otherwise, if you want to do as you described, your script can place a value on a hidden specific text file, e.g., .myCheck.txt. Place the 0 value if no e-mail was sent, 1 if sent. When the script starts, read the file and place the value into a variable. If it is one, do not send e-mail. Of course you have to manage how to reset the value, but this could be done manually.
 
At the part where you send out mail, touch a file in /tmp indicating that you have sent mail. Before sending mail, check if the file exists. If it does, do not send out mail.
 
Help needed in script

Well this is what I have done so far.
Script 1: Checks disk space and if a disk has reached the thresholded % an email is sent.
This script is ran every minute. So if a disk is high it will email every minute... on and on which is not ideal.

Script 2: I have managed to write another script which runs every 10mins and copies the df output to a text file (so this can be used for comparison later on)

Now I need to find a way so that if an email is sent - hence a disk is nearly full...once the email is sent it will not send the email again - as long as the % is the same.. (the comparison script is written out and it works).
However if when compared with the contents from script 2 differ.... then the email is sent - this may mean that the % has gone lower or higher.

Can someone help me out as i keep going around in loops...
ps i am new to unix so i have to find scripts online to write the code..
 
Don't make it too complex. If the threshold is reached, send out mail. Action from you is needed any way to clear up space on the disk.

For instance when you monitor /home with a treshold of 95%:

Code:
#!/bin/sh

used=`df /home | tail -1 | awk '{print $5*1}'`

if [ $used -gt 95 ]; then
        test ! -f /tmp/mail.sent && ( echo "disk almost full" | mail -s disk_alert you@somewhere.com ; touch /tmp/mail.sent )
fi

So the mail is sent out only once when the disk utilisation of /home is >95%. You will then have to fix it, and remove /tmp/mail.sent.
 
You can fix the latter by simply incorporating a 'token removal' of the file using something like

Code:
else
rm -f /tmp/mail.sent

That way you'll never have to worry about it, and the -f will not make it produce an error.
 
I agree with the logic proposed by frijsdijk and DutchDaemon.

Simple:
  • If threshold is breached: 1) send email if there is no flag file; 2) touch flag file.
  • If below threshold: delete flag file.

I also recommend using the -P (POSIX compliant) option for df(1). My experience has been that it will force all entries to remain on one line, even in the case of long filesystem names. Without that option, lines may wrap and confuse your awk(1) report.
 
I'm curious why your script needs to run so often?

The way I monitor disk space is to run a script once a day which tracks the trends of utilisation over time. If it predicts a disk will become full in less than 14 days, it sends an alert.
 
Back
Top