VG calculation in GB

for i in `lsvg`
do

echo "VG Name:" $i
echo "Total VG Size:"
lsvg $i |grep "TOTAL PPs:" |awk '{print $7}' | cut -c2-
echo "Free VG Size:"
lsvg $i |grep "FREE PPs:" | awk '{print $7}' | cut -c2-

done

The PP Sizes are in MB. I like to have the sizes in GB.
Also, I like to have the output in horizontal, so that it can show as:

VG Name: AAA   Total VG Size: 2,201  Free VG Size: 102
VG Name: BBB    Total VG Size: 1,300  Free VG Size: 564
etc
etc

Please advise.

Divide by one thousand. Or 1024, depending on preference.

lsvg $i |grep "TOTAL PPs:" |awk '{print $7/1024}' | cut -c2-

I tried this, but gave me 0...
Please advise.

You need to delete the , first, before you can do math on that string.

awk '{ sub(/,/, "", $7); print $7/1024 }'

It is odd.. It still returns 0.
Please advise.

Without seeing the text you're feeding into these commands, I cannot. Please post it.

I'd say the problem is with the (

i.e.

VG PERMISSION:      read/write              TOTAL PPs:      968 (61952 megabytes)

Yes, ( is there.

Please let me know how to display the output horizontally.

I know it's there! I'm on a mobile now and in no position to write code!! Please post the complete output of an lsvg command, as Corona688 hinted at.

lsvg output looks i.e.

VOLUME GROUP:       rootvg                   VG IDENTIFIER:  00c701f000004c0000000117d056caf8
VG STATE:           active                   PP SIZE:        64 megabyte(s)
VG PERMISSION:      read/write               TOTAL PPs:      2186 (139904 megabytes)
MAX LVs:            256                      FREE PPs:       288 (18432 megabytes)
LVs:                16                       USED PPs:       1898 (121472 megabytes)
OPEN LVs:           12                       QUORUM:         1 (Disabled)
TOTAL PVs:          2                        VG DESCRIPTORS: 3
STALE PVs:          0                        STALE PPs:      0
ACTIVE PVs:         2                        AUTO ON:        yes
MAX PPs per VG:     32512                                     
MAX PPs per PV:     2032                     MAX PVs:        16
LTG size (Dynamic): 256 kilobyte(s)          AUTO SYNC:      no
HOT SPARE:          no                       BB POLICY:      relocatable 

It'd be much easier to use $6 -- it's a bare number, probably the one intended for machine processing. It appears to be in units of 64 megabytes.

Using printf with %.1f to print to one decimal place, adjust as needed. Use %d if you want no decimal points at all.

awk '{ printf("%.1f\n", ($6*64)/1024); }'

---------- Post updated at 12:41 PM ---------- Previous update was at 12:35 PM ----------

actually, you can even remove the grep:

lsvg $i | awk '/TOTAL PP/ { printf("%.1f", ($6*64)/1024); }'

In fact you can do the whole thing in awk:

lsvg $i | awk -v VG="$i" '
# Execute this code for lines matching TOTAL PP
/TOTAL PP/ { TPP=$6 }
# Execute this code for lines matching FREE PP
/FREE PP/ { FPP=$6 }
# Execute this code after all lines are read
END {
        TPP=(TPP*64)/1024;
        FPP=(FPP*64)/1024;
        printf("VG Name: %s Total VG Size: %.1f Free VG Size: %.1f\n", VG, TPP, FPP);
}'

And as the PP size can vary by volume group:

lsvg $(lsvg) | awk '/^VOLUME GROUP/ { printf "%s ", $3 } /PP SIZE/ { S=$6 }
/TOTAL PPs/ { printf "%d ", $6*S/1024 }
/FREE PPs/ { print $6*S/1024 }
'
rootvg 136 18
1 Like

Good point. Keeping that in mind:

lsvg $i | awk -v VG="$i" '
# Execute this code for lines matching TOTAL PP
/TOTAL PP/ { TPP=$6 }
# Execute this code for lines matching FREE PP
/FREE PP/ { FPP=$6 }
# You know the drill by now
/PP SIZE/ { S=$6 }
# Execute this code after all lines are read
END {
        TPP=(TPP*S)/1024;
        FPP=(FPP*S)/1024;
        printf("VG Name: %s Total VG Size: %.1f Free VG Size: %.1f\n", VG, TPP, FPP);
}'
1 Like

Using lsvg $(lsvg) eliminates the need to use (yuk) for i in `lsvg` , or a while loop. And there's a "cost" benefit in lsvg $(lsvg) than the iteration alternatives for those who think every CPU cycle is a prisoner(!), as lsvg is only executed twice regardless of how many volume groups there are.

So, the code I posted will give output for all volume groups.

lsvg | while read VG; do
  lsvg $VG ...
...
done

for example, will execute lsvg #of VG's + 1 times.

1 Like

Thank you so much! It works fine.