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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a simple script for filesystems. 3

Status
Not open for further replies.

lblauen

Technical User
Aug 13, 2002
69
I need someones help. I need a small script that I would run in cron to check filesystems using the df command and if a filesystem is above say 80% put that information in a file that I can email out. But I also need to tell the script to ignore some of the filesystems cause I don't care how full they get.

/dev/hd4 131072 106916 19% 2966 5% /
/dev/hd2 2654208 519648 81% 42513 7% /usr
/dev/hd9var 131072 44148 67% 954 3% /var
/dev/hd3 131072 98904 25% 300 1% /tmp
/dev/hd1 0 0 -1% - - /home
/dev/hd10opt 131072 20488 85% 2646 9% /opt
Like here I would what /usr in the file saying its 81% used but I can tell it to ignore /home and /opt
I would have the output file say something like this:

Following filesystems are getting full Please check.
/dev/hd2 2654208 519648 81% 42513 7% /usr

Can this be done if so how please. Thanks Lloyd
 
You could just list the filesystems you are interested on the command line:

[tt]#!/bin/ksh
df -lk / /var /usr | awk '$5 > 80' > /tmp/df.$$
if [[ $(wc -l < /tmp/df.$$) -gt1) ]]
then
( echo &quot;Following filesystems are getting full, please check:&quot;
cat /tmp/df.$$ ) | mailx -s &quot;Full filesystems&quot; you@yourdomain.com
fi
rm -f /tmp/df.$$[/tt]

(not tested)

Annihilannic.
 
This look's good for a start. I still need it to ignore certain filesystems. I was going to take this script and add checking for page space above say 80% and report it to. How can I add that to it.
 
I need to modify it to something like this but it fails. I'm using AIX 5.1

#!/bin/ksh
D1=&quot;`hostname`&quot; # Want hostname in mail
df -Ik / /var /usr /opt | awk '$5 > 80' >> /tmp/check
#if [[ $(wc -l < /tmp/df.$$) -gt1) ]] this fails
I think we just need to check if the file is there. If it is
then we continue if not we stop.
then
echo &quot;Following filesystems are getting full on $D1, please check: >> /tmp/check
`mail -v -s &quot;Central Systems Notification&quot; you.domain.com </tmp/check
fi
rm -f /tmp/check # then remove file.
 
#!/bin/ksh
D1=&quot;`hostname`&quot;
df -Ik / /var /usr /opt | awk '$5 > 80' >> /tmp/check
if [[ -e /tmp/check ]];
then
echo &quot;Following filesystems are getting full on $D1, please check:&quot; >>/tmp/check
`mail -v -s &quot;Central Systems Notification&quot; your@domain.com </tmp/check`
fi
rm -f /tmp/check

now I just need to add pagspace above 80%
 
Sorry, typo. There's supposed to be a space between -gt and 1.

Also you said you still need it to ignore certain filesystems; but it will only list the filesystems on the df command line, so that should achieve what you want?

Alternatively if you want to use the script on a number of systems without changing it you could exclude them using the following:

[tt]#!/bin/ksh
df -lk | awk '$5 > 80 && $6 !~ &quot;^(/home|/opt)$&quot;' > /tmp/df.$$
if [[ &quot;$(wc -l < /tmp/df.$$)&quot; -gt 1 ]]
then
( echo &quot;Following filesystems are getting full, please check:&quot;
cat /tmp/df.$$ ) | mailx -s &quot;Full filesystems&quot; you@yourdomain.com
fi
rm -f /tmp/df.$$[/tt]

Annihilannic.
 
Here is my script for AIX 5.1, modify to suit. I actually have 2 alerts and a reset level, this script will only alert once per level:

##########################################################
# Check file system for disk full and sends messages #
# #
# Parameters : $HOST = Message Header (Node id) #
# $LEVEL1 = level 1 alert (in %) #
# $LEVEL2 = level 2 alert (in %) #
# $RLEVEL = reset level (in %) #
# /tmp used for record keeping #
###########################################################
typeset -u HOST=`hostname`
LIST=&quot;tmp home var usr archive opt&quot;
SYSTIME=`date +&quot;%OH:%OM&quot;`
LEVEL1=80
LEVEL2=90
RLEVEL=79

for i in $LIST
do
df | grep $i > $i.z
sed '/tmp/q' $i.z > $i.x
sed -e 's/%//' $i.x > $i.y
XXX=`awk '{print $4}' $i.y`
LAST=`awk '{print $1}' /tmp/$i.last`

#
# send message level 1
#

if [ &quot;$XXX&quot; -ge &quot;$LEVEL1&quot; ] && [ &quot;$LAST&quot; -lt &quot;$LEVEL1&quot; ]
then
echo &quot;$SYSTIME : $HOST /$i $LEVEL1% Full!&quot;|mail -s $HOST help alert
echo $LEVEL1 > /tmp/$i.last
fi

#
# send message level 2
#

if [ &quot;$XXX&quot; -ge &quot;$LEVEL2&quot; ] && [ &quot;$LAST&quot; -eq &quot;$LEVEL1&quot; ]
then
echo &quot;$SYSTIME : $HOST /$i $LEVEL2% Full!!&quot;|mail -s $HOST help alert
echo $LEVEL2 > /tmp/$i.last
fi

#
# reset last
#

if [ &quot;$XXX&quot; -lt &quot;$RLEVEL&quot; ]
then
echo $XXX > /tmp/$i.last
fi

done


IBM Certified Confused - MQSeries
IBM Certified Flabbergasted - AIX 5 pSeries System Administration
MS Certified Windblows Rebooter
 
Thank you for all the help. I now have reports coming in and letting me know when filespace is getting full. This is the best place to get help. Keep up the great work.
 
Help. The script works great unless the file systems goes to 100% How can I fix it? Here is the command I used and its results. If set to 90% it won't report the filesystem full if I set it to 100% I get them all.

346: df -Ik / /var /usr /opt /tmp | awk '$5 > 90 '
[testdb2::root]:
347: df -Ik / /var /usr /opt /tmp | awk '$5 > 100 '
/dev/hd4 131072 39572 91500 31% /
/dev/hd9var 393216 78380 314836 20% /var
/dev/hd2 4063232 2338912 1724320 58% /usr
/dev/hd10opt 262144 64760 197384 25% /opt
/dev/hd3 196608 196596 12 100% /tmp
 
Try....

df -Ik / /var /usr /opt /tmp |awk '{p=$5;sub(&quot;%$&quot;,&quot;&quot;,p)} int(p)>90'
 
Thanks that fixed it right up. This is the right place for help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top