Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

File System Alert Script

SOLVED
Go to solution
Kyle D. Harris
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
Balaji N
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.
Kyle D. Harris
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.
Balaji N
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.
Kyle D. Harris
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
Balaji N
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.
Balaji N
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.
John Meissner
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
Kyle D. Harris
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
Balaji N
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.
Kyle D. Harris
Regular Advisor

Re: File System Alert Script

Ok, thanks alot guys. That should do it for my question. Thanks for all the help from both of you.
Huc_1
Honored Contributor

Re: File System Alert Script

Hello

I am using this attached perl script to do the job

Some one in this forums did post a script very much like this one but for HP-UX os I did modify it to run on my linux.
I did quite a bite of modification, but I am afraid I do not remember who the original writer
is ...

I am just posting this modified version because it did teach me a fews tricks in perl, and I thing it could be usefull to you

modify reportfull and email_users in the script to your needs,

Jean-Pierre

Smile I will feel the difference
John Meissner
Esteemed Contributor

Re: File System Alert Script

Kyle... in my script above i use SED to cut off the % symbol... so it's looking at the number.
All paths lead to destiny
Kyle D. Harris
Regular Advisor

Re: File System Alert Script

John, i copy pasted your script. I changed the percent to 85 and of course my email address in the sendx .... I have one file system at 88 percent, thus i made the percent 85 just to see if it works. I also included: "#!/usr/bin/perl" at the top of the script. I'm still having trouble getting it to work correctly. Any other ideas i may not be doing correctly? Thanks.

Kyle
Balaji N
Honored Contributor

Re: File System Alert Script

hi
john's script is a simple shell script.

so u need to add the line #!/usr/bin/sh on top.

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

Re: File System Alert Script

i actually prefer using the top line of

#! /usr/bin/ksh

I just like the korn shell...
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: File System Alert Script

OK - sorry... i had a problem in my script..... i fixed it... here it is...:


#!/usr/bin/ksh
mark=1
while [ $mark = 1 ]
do
df | grep -v Filesystem |
while read line
do
MTP=$(echo "$line" | awk '{print $6}')
USE=$(echo "$line" | awk '{print $5}' | sed 's/.$//g')
if [ "$USE" > "60" ]
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