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!

Script to check disk space and react on size comparison.

Status
Not open for further replies.

rcbatlinux

Technical User
Mar 30, 2001
80
US
I'm working on a script to do a df -k | grep "SDI" for specific file systems and from there awk out the 3rd column and compare the value to a specified size that I feel I need to react on. I am using ksh. In essence:

df -k | grep "SDI"

if $3rd column < 300000 then do this
else
do that
fi



I've been dabling with awk, nawk and a for loop but I want to do this by performing the df command once. For example I have:


for diskspace in `df -k | grep &quot;SDI&quot; | awk '{print $3'`
do
if [ $diskspace < 300000 ]
then
echo &quot;$diskspace Problem!&quot;
else
echo &quot;Ok&quot;
fi
done

Works fine BUT! I want to list with the echo &quot;Problem!&quot; the directory that is involved as well. But I've tried feeding in the entire line and awk out the columns I need in the if statement but can't seem to get it to work. Any ideas??

rcbatlinux


 
this may be the tips to the alternative solutions to what you want to do.

to see which file or directory that takes space the most, we can use 'sort' command to sort the files according to # of bytes that they take.

%du -s * | sort +0 -1 -n -r > out

you then can use 'less' or 'more' to see the content in file 'out'

+0 -1 -> means sort the first column
-n -> means sort according to string numerical value
-r -> means sort from largest to least

to get the files that take space larger than 1000 bytes. this below command search files recusively from the current directory.
find . -size +1000c -print
 
thanks for the reply. Not what I'm after though. I understand what you are saying. What I am referring to is this. A df command lists the following:

device size free directory

I want to check each directory's free space using the df -k command compared to a value. If any one of these values is < my value then I want to execute a statement. If not then I'll execute another statement.

rcbatlinux
 
How about this:

df -k | awk '/SDI/ && $3 < 300000 {printf(&quot;Problem! %sKB used in %s&quot;,$3,$6)}'

It may need a bit of refinement but I think it does what you want.

Greg.
 
Greg,

That worked excellent. Thank you. My object on this is this. A program is ran each day at the end of the day and I want it to display for the user the df -k then do the comparison and report any problems. IF a problem exists then to a read and prompt the user with a choice to quit or continue. If no problems then continue without a read.

I can do this easy enough with my for loop above in previous message, but how could I encorporate this awk statement into an if statement to test this condition and react to the the condition?

rcbatlinux
 
If I understand you correctly, you want to loop through each line where there is a problem. If there is a problem, the user is given the option of quitting the processing of the df -k output, or moving on to the next problem line?

If there are no problems, nothing happens.

Why does the user need to be prompted to go to the next line? Is there something else you need to do if the user chooses to continue?

Greg.
 
Actually the goal would be to display the status of the df -k and state if it is &quot;ok&quot; or a &quot;problem&quot; and after it has gone through the df -k and displayed this information, should a &quot;problem&quot; have displayed, then determine whether to stop the user to determine whether to continue or not. If no &quot;problem&quot; was detected then continue on without stopping.

rcbatlinux
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top