The following script will monitor file system usage and, if it goes above certain thresholds, send an email alert. Note that it will only send an alert once per threshold unless usage drops back below the threshold. This means that you won’t get bombarded with emails.
The script sends an email to the alias diskusage. You can easily change that to an actual email address if you like.
Limitations of the script (minus indents which seem to have been lost by the editor):
- It only checks usage for ext3 file systems, but that’s easy to change
- At the moment the script uses the same thresholds for all file systems – maybe I’ll change that in a future version
Anyway, here’s the script:
?Download monitor-disk-usage.sh
#!/bin/sh
#
# Check disk usage and if over specified thresholds send an email
#
# Author: Me
# Date: 20071105
#
while true
do
df -t ext3
sleep 60
done | awk '
function SendEmail(HostName, FileSystem, PercentUsed)
{
Message="File system " FileSystem " on " HostName
Message=Message " is " PercentUsed "% full"
Subject="Low Disk Space Alert for " HostName
SysCommand="echo " Message "| mail -s \"" Subject
SysCommand=SysCommand "\" diskusage"
# Send the email and return the status of the system call
return system(SysCommand)
}
function CheckUsage(FileSystem,PercentUsed,Threshold)
{
OverThreshold="no"
if ( PercentUsed > Threshold ) {
OverThreshold="yes"
if ( FirstTime[FileSystem,Threshold] == "no" )
next
else {
if ( ! SendEmail(HostName, FileSystem, PercentUsed) ) {
# If we had no errors sending the email then dont send
# any more alerts
print "yes"
FirstTime[FileSystem,Threshold]="no"
}
}
} else {
# Make sure we toggle to send alerts in future
FirstTime[FileSystem,Threshold]="yes"
}
# If we are over the threshold return "yes"
return OverThreshold
}
/^\// {
FileSystem=$6
# Remove the % sign and convert to numeric
PercentUsed=substr($5,1,length($5)-1) * 1
# Check if over the threshold. Check the highest
# threshold first, there is no point in checking the
# lower threshold if the the higher one is exceeded
if (CheckUsage(FileSystem,PercentUsed,99)=="no")
if (CheckUsage(FileSystem,PercentUsed,95)=="no")
CheckUsage(FileSystem,PercentUsed,90)
} ' HostName=$HOSTNAME |
Post a Comment