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!

Sum Filespace script (KORN)

Status
Not open for further replies.

heprox

IS-IT--Management
Dec 16, 2002
178
US
I'm looking to modify this existing script:

#!/bin/ksh
FILESYSTEMS="/ /usr /var /prod /arch_nb"
for FILESYSTEM in $FILESYSTEMS
do
FREESPACE=$(df -Pk ${FILESYSTEM} | awk '$4 ~ /[0-9]+/{print $4}')
echo "${FILESYSTEM} free space: ${FREESPACE}" \
done

...to tell me the amount of free space (in Mb) for each filesystem, and them total it for the whole LPAR. Anyone?
 
Hi heprox,

I am puzzled by the df -Pk because -P produces output in 512-byte blocks (but only on some versions of UNIX) and -k produces output in kilobytes (1024-byte blocks).

So, if I understand you correctly, how about something like:
Code:
#!/bin/ksh
TOTALFREE=0
FILESYSTEMS="/ /usr /var /prod /arch_nb"
for FILESYSTEM in $FILESYSTEMS
do
   FREESPACE=$(df -k ${FILESYSTEM} | awk '$4 ~ /[0-9]+/ {print $4}')
   ((FREESPACE=FREESPACE/1024))
   ((TOTALFREE=TOTALFREE + FREESPACE))
   (echo "${FILESYSTEM} ${FREESPACE}"
   ) | awk '{printf "%-12s free space: %10d \n",$1,$2}'
done
(echo "${TOTALFREE}"
) | awk '{printf "Total amount free space: %10d Mbytes\n",$1)'

Please note that I haven't tested the above code and I am prone to the occasional typo. Also there are bound to be some awk wizards who can do the above far more efficiently.

Anyway, I hope it helps you.

Mike
 
for info - from a report program we use
Code:
df -Ik | grep -v Filesystem | awk 'BEGIN {printf ("%-20s\t%s\t%s\t%s\n", "File System", "Total Mb", "Free Mb", "Mount Point")}{ printf ("%-20s\t%.2f\t\t%.2f\t% s\n", $1, $2/1024, $4/1024, $6 )}'



Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
On HP-UX df -P specifies a output format of specific columns.

Rather than loop thru each file-system, you could just use df once, then use awk to re-format the output to what you want, e.g....
Code:
#!/bin/ksh
FILESYSTEMS="/ /usr /var /prod /arch_nb"
df -Pk ${FILESYSTEMS} | awk '
  BEGIN {fmt = "%-10s free space: %8i MB\n"}
  $4 ~ /^[0-9]+$/ {
       printf fmt, $6, $4 / 1024;
       sum += $4
  }
  END {printf fmt, "Total", sum / 1024}'
The re-formatted output would look like...
[tt]
/usr free space: 214 MB
/var free space: 327 MB
/ free space: 78 MB
Total free space: 639 MB[/tt]
 
Ygor, I like you statement however in AIX 5.2 something is worng with the syntax I get:

/ free space: %8ii MB
/usr free space: %8ii MB
/var free space: %8ii MB
/prod free space: %8ii MB
/arch_nb free space: %8ii MB
Total free space: %8ii MB

Mike, your statement returns:

2%: 0403-053 Expression is not complete; more tokens exp
ected.
 
heprox,

Change the fmt string like so:

Code:
fmt = "%-10s free space: %8d MB\n"

Yes, there's just a single character change. The default awk in AIX doesn't support the "i"(integer) format string specification.



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Just to know, will the following awk script works on AIX ?
BEGIN {
cmd="sync;LANG=C df -tk";fmt="%-28s %-23s%9s %9s %7s\n"
printf fmt,"MountDir","FileSystem","Total(Mb)","Free","Used"
trt=sprintf(fmt,"----------","----------","---------","---------","------")
printf trt
fmt="%-28s %-23s%#9.2f %#9.2f %6.2f%%\n"
while((cmd|getline)==1){
if(substr($1,1,1)=="/"){
mountDir=$1
mountDev=substr($2,1,1)=="(" ? substr($2,2) : $2
sub(/[) \t]*:$/,"",mountDev)
nbFree=(substr($3,1,1)==")" ? $4 : $3)/1000
} else if($0~/otal/){
nbTot=($1~/otal/ ? $2 : $1)/1000
if(nbTot){
printf fmt,mountDir,mountDev,nbTot,nbFree,100*(nbTot-nbFree)/nbTot
tTot+=nbTot;tFree+=nbFree;tNb++
}}
}; close(cmd)
exit
}
END {
if(tTot && tNb>1){
printf trt""fmt,"Summary","All",tTot,tFree,100*(tTot-tFree)/tTot
}}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top