March 31, 2010

Pretty printing openvz memory allocation

awk: when you can’t be bothered to write a proper while loop.

/usr/sbin/vzlist -o ctid,hostname,privvmpages.b | \
awk 'BEGIN { total=0; } $1 ~ /CTID/ { printf "%5s %20s %5s\n", $1, $2, "mem"; next; } \
$1 ~ /[0-9]+/ { total += $3; \
split($2, arr, /\./); printf "%5s %20s %5.2f\n", $1, arr[1], $3 * 4 / 1024 / 1024; } \
END { printf "total %20s %5.2f\n", "", total * 4 / 1024 / 1024; }'