Display vps memory usage

March 11th, 2009 by keith Leave a reply »

Free is a little bash script for use on a Virtuozzo or OpenVz VPS to display the current memory usage at the command line.

#!/bin/bash

#

# Revised 02-Feb-2007: include kernel memory (kmemsize) in 'used' calculation

# and show percentages in output.

# 

BEAN=`cat /proc/user_beancounters`

GUAR=`echo "$BEAN" | grep vmguar | awk '{ print $4;}'`

PRIV=`echo "$BEAN" | grep privvm | awk '{ print $2;}'`

KMEM=`echo "$BEAN" | grep kmem | awk '{ print $3;}'`

let TOTL=$GUAR/256

let KMMB=$KMEM/1048576

let PVMB=$PRIV/256

let USED=$KMMB+$PVMB

let FREE=$TOTL-$USED

if [ "$FREE" -gt "0" ]; then

  let UPER=$USED*100/$TOTL

  let FPER=100-$UPER

else

  let UPER="100"

  let FPER="0"

fi

echo "VPS Memory:"

echo "  Total: $TOTL mb  Used: $USED mb (${UPER}%)  Free: $FREE mb (${FPER}%)"

Instructions:
  1. Copy the above code and as root paste and save it as /bin/Free
    (that’s “Free” with a capital “F”).
  2. Make it executable: chmod 700 /bin/Free

Try it out:

[root@vps]# Free
VPS Memory:
Total: 256 mb Used: 92 mb (35%) Free: 164 mb (65%)

Advertisement

Leave a Reply