cancel
Showing results for 
Search instead for 
Did you mean: 

File System Alert Script

 
SOLVED
Go to solution
Highlighted
Regular Advisor

File System Alert Script

Here's what i'm looking to do:

Write a small script to send out a email notification to myself when a filesystem reaches a certain percent (say 96 %). I'm pretty sure i'd have to use the df command to go with the "awk" command to pull the % field. Can somebody give me a little help? The awk command is kicking my a$$.... Thanks alot to all!

Kyle Harris
16 REPLIES 16
Highlighted
Honored Contributor

Re: File System Alert Script

++++++++++++cut here++++++++++++++
#!/usr/bin/awk -f

BEGIN {

#Skip the initial header output of bdf
getline

#set flag to do one time echoing stuff
flag=0

#The argument passed to this script is a path whose last few characters
#contain the hostname. Find the exact hostname
a = index(FILENAME,"tmp\/")
b=substr(FILENAME,a+4)
}
{

#Ignore some stuff like /net etc...
if (NF > 5) {
print substr($5,1,length($5)-1)

#Remove the % symbol and check if usage is more than 90%
if ( int(substr($5,1,length($5)-1)) >= 90 ) {

#Check for flag to print header in mail
if ( flag == 0 ) {
print "The following file system(s) are more than 90% full for " b
flag=1
}
#Print mountpoint, usage,....
print $1 "\t\t" $5 "\t\t" $6
}
}
}

END {
#Formatting stuff
if ( flag == 1) {
print "================="
}
}
++++++++++++end cut++++++++++++++

Pipe the output of bdf to this script and watch the output.

cheerio
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Highlighted
Regular Advisor

Re: File System Alert Script

Belaji,
Linux doesn't have a BDF command. It's "df" in linux. Anyway, that looked pretty complex, but if it works it works. I imagined it would only be about 5 or 6 lines for the whole script.
Highlighted
Honored Contributor
Solution

Re: File System Alert Script

hey
sorry. this was a script which i used to use in the HP unix environment.

do this.

run df -h and save it a file.

save this snippet as some script.awk.

and run script.awk

if the filesystem size is greater than 90% it fill fill them.


i have a script which actually does an remsh into a list of hosts and calls this script for every host.

and sends a consolidated mail from it.

am leaving home in the next ten minutes. so if u need any more info, reply quickly.

cheerio
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Highlighted
Regular Advisor

Re: File System Alert Script

I should be ok man. I'll be leaving work soon so i'll check your stuff out tomorrow. Thanks again. Pts were rewarded!..

Kyle
Highlighted
Honored Contributor

Re: File System Alert Script

and here is a one liner for your work. modify accordingly.

df -k | awk '{if (int(substr($5,1,length($5)-1)) >= 90) { print $1 " is " $5 " full"}}'

cheers
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Highlighted
Honored Contributor

Re: File System Alert Script

oh! no issues and thanks for the pts.
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Highlighted
Esteemed Contributor

Re: File System Alert Script

Here is a script I put together that should suit your needs... you'll need to change a few minot things such as the send to address and figure out how often you want the script to run. But other than that I've tested this on my red hat box



mark=1
while [ $mark = 1 ]
do
df |
while read a b c d e f
do
MTP=$(echo $f | grep -v "Mounted on")
USE=$(echo $e | grep -v Use | sed 's/.$//g')
if [ "$USE" >= "96" ]
then
echo "$MTP $USE " | mailx -s "FS alert" sendto@work.com
sleep 100 #or whatever time
else
sleep 100 #or whatever time
fi
done
done
All paths lead to destiny
Highlighted
Regular Advisor

Re: File System Alert Script

One more question... When you guys say greater then 90, i know your talking about the percent of a particular file system. Since the "df" command shows it 90% will it recognize 90 or not since the percent sign is immediately following it? Thanks.

Kyle
Highlighted
Honored Contributor

Re: File System Alert Script

if u see mine, here is the part which strips the % and uses that as a no.

int(substr($5,1,length($5)-1))

-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.