Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filesystem Monitoring Script

Status
Not open for further replies.

khalidaaa

Technical User
Jan 19, 2006
2,323
BH
Guros,

I've just finished with this script for maintaining our filesystems and monitoring them not to exceed 95% in size. In addition i'm keeping the filesystems history to observe the increase to predict future expansion.

I thought of sharing it with you to see your way of dealing with the same issues and if there any enhancement to make to my script from your experience.

Code:
df -Pk | awk '$3 ~ /[0-9]/ { print $1,"\t",$3,"\t",substr($5,1,index($5,"%")-1)
}' > df.out

set -A new1 $( awk ' {print $1}' df.out )
set -A new2 $( awk ' {print $2}' df.out )
set -A new3 $( awk ' {print $3}' df.out )

if [[ ! -s out ]]
then
j=0;
cat /dev/null > out
while [[ $j -lt ${#new1[*]} ]]
do
print "${new1[$j]}\t${new2[$j]}" >> out
((j+=1))
done
fi

set -A old1 $( awk ' {print $1}' out )
set -A old2 $( awk ' {print $2}' out )

cat /dev/null > email.txt
i=0;
while [[ $i -lt ${#old1[*]} ]]
do
j=0;
while [[ $j -lt ${#new1[*]} ]]
do
if [[ ${old1[$i]} = ${new1[$j]} ]]
then
if [[ ${old2[$i]} -lt ${new2[$j]} ]]
then
(( temp = ${new2[$j]} - ${old2[$i]} ))
print "${old1[$i]} was increased in size by ${temp}KByte" >> email.txt
break
fi
fi
((j+=1))
done
((i+=1))
done


j=0;
cat /dev/null > out
while [[ $j -lt ${#new1[*]} ]]
do
# This if statement is for Handling exceptions
#(where not to check for higher than 95% filesystems)
if [[ ${new1[$j]} != "server22:/backup" ]]
then
if [[ ${new3[$j]} -ge 95 ]]
then
print "WARNING: ${new1[$j]} is ${new3[$j]}% utilized!!!" >> email.txt
fi
fi
print "${new1[$j]}\t${new2[$j]}" >> out
((j+=1))
done

#cat email.txt

if [[ -s email.txt ]]
then
mail -s "Daily Status Report" "khalid@mycompany.com" < email.txt
if

# History
echo "======================================================================" >> history.out
date >> history.out
echo "======================================================================" >> history.out
cat email.txt >> history.out
echo "======================================================================" >> history.out
df -Pg >> history.out

# Maintain the size of the history.out file to be 3000 lines only
tail -3000 history.out > temp.out
mv temp.out history.out

Regards,
Khalid
 
One style point - and please don't take this personally - but I much prefer indenting. For example the section
Code:
while [[ $j -lt ${#new1[*]} ]]
do
# This if statement is for Handling exceptions
#(where not to check for higher than 95% filesystems)
if [[ ${new1[$j]} != "server22:/backup" ]]
then
if [[ ${new3[$j]} -ge 95 ]]
then
print "WARNING: ${new1[$j]} is ${new3[$j]}% utilized!!!" >> email.txt
fi
fi
print "${new1[$j]}\t${new2[$j]}" >> out
((j+=1))
done
is more readable, and hence maintainable if it's written
Code:
while [[ $j -lt ${#new1[*]} ]]
do
# This if statement is for Handling exceptions
#(where not to check for higher than 95% filesystems)
  if [[ ${new1[$j]} != "server22:/backup" ]]
  then
    if [[ ${new3[$j]} -ge 95 ]]
    then
    print "WARNING: ${new1[$j]} is ${new3[$j]}% utilized!!!" >> email.txt
    fi
  fi
  print "${new1[$j]}\t${new2[$j]}" >> out
  ((j+=1))
done
That way matching the 'if's with the 'fi's is much easier.

Ceci n'est pas une signature
Columb Healy
 
And maby more descriptive names on the variables,
new1, new2, new3 etc. don't meen much to someone
new to the script, and maby not yourself in a years time.
;-)
 
Thanks Guys for your comments. I'll remember that always and i will try to correct this in the script that i have.

columb, I put this script to know your opinion :) so i know you say this for the sack of better way of scripting! By the way i used to do that indentation! not only this i was teaching classes and i always point to that good programming habit! But i stopped doing that in Unix coz its frustrating to maintain :p

geirendre, Descriptive names! point taken :)

Thanks again

Regards,
Khalid
 
Well, I don't remember which post it was, but I remember a similar post (about filesystem monitoring) and p5wizard said that in case of NFS filesystems it can stop. So he recomended using mount instead of df, so we use this in our servers:

Code:
mount | egrep -v 'nfs|procfs|cdrfs' | awk 'NR>2{print $1}'|\
xargs df ...
 
Thanks MoreFeo for the comment! i'm sure i will try this once i get back to work

Regards,
Khalid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top