Terminal Heroes 8: Memory usage scripts

Posted by Ktoso on 24/02/2010 – 11:54

I have a low-memory VPS running online and thus always have to worry about what uses how much memory… Of course top and my favorite htop are very good tools to check this, but sometimes I just want to get a super simple report if I’m overusing memory or not yet. The bellow scripts (most of them was found online or hacked up from multiple scripts to fit my needs) print simple yet very readable memory usage information.

memused

#!/bin/bash
  1.  
  2. #if [ $1 = '']; then
  3. #    ps auxf | awk '{sum=sum+$6}; END {print sum/1024}';
  4. #else
  5. #    ps auxf | grep $1 | awk '{sum=sum+$6}; END {print sum/1024}';
  6. #fi
  7. bean=`cat /proc/user_beancounters` guar=`echo "$bean" | grep vmguar | awk '{ print $4;}'` burst=`echo "$bean" | grep privvm | awk '{ print $5;}'` priv=`echo "$bean" | grep privvm | awk '{ print $2;}'`
  8. let total=guar/256 let used=priv/256 let burst=burst/256
  9. echo "VPS memory usage:"
  10. echo "Used: $used MB"
  11. echo "Total: $total MB"
  12. echo "Burstable to: $burst MB"

Sample output:
VPS memory usage:
Used: 296 MB
Total: 256 MB
Burstable to: 512 MB

memfree

#!/bin/bash
  1. #
  2. # Revised 02-Feb-2007: include kernel memory (kmemsize) in 'used' calculation
  3. # and show percentages in output.
  4. #
  5.  
  6. BEAN=`cat /proc/user_beancounters`
  7. GUAR=`echo "$BEAN" | grep vmguar | awk '{ print $4;}'`
  8. PRIV=`echo "$BEAN" | grep privvm | awk '{ print $2;}'`
  9. KMEM=`echo "$BEAN" | grep kmem | awk '{ print $3;}'`
  10.  
  11. let TOTL=$GUAR/256
  12.  
  13. let KMMB=$KMEM/1048576
  14. let PVMB=$PRIV/256
  15. let USED=$KMMB+$PVMB
  16. let FREE=$TOTL-$USED
  17.  
  18. if [ "$FREE" -gt "0" ]; then
  19.     let UPER=$USED*100/$TOTL
  20.     let FPER=100-$UPER
  21. else
  22.     let UPER="100"
  23.     let FPER="0"
  24. fi
  25.  
  26. echo "VPS Memory:"
  27. echo " Total: $TOTL mb Used: $USED mb (${UPER}%) Free: $FREE mb (${FPER}%)"

Sample output:
VPS Memory:
Total: 256 mb Used: 302 mb (100%) Free: -46 mb (0%)

memheld

#!/bin/bash
  1. grep oomguarpages /proc/user_beancounters | awk '{s=$2;t=$3;u=$4; {print "VPS Memory Usage\nCurrent Held: " s/256 "MB\nMax Held: " t/256 "MB\nBarrier: "u/256"MB" }}'

Sample output:
VPS Memory Usage
Current Held: 160.832MB
Max Held: 294.605MB
Barrier: 256MB

memps

#!/bin/bash
  1.  
  2. echo "ps axo pmem,pcpu,comm | sort"
  3. ps axo pmem,pcpu,comm | sort

Sample output:
ps axo pmem,pcpu,comm | sort
0.0 0.0 script
0.0 0.0 script
0.1 0.0 init
#...
2.2 0.0 httpd
4.0 0.0 mysqld
7.3 0.0 httpd
8.0 0.0 httpd
%MEM %CPU COMMAND

memprivmpages

#!/bin/bash
  1.  
  2. beans=`cat /proc/user_beancounters | grep priv`
  3. max=`echo $beans | awk '{ print $4;}'`
  4. use=`echo $beans | awk '{ print $2;}'`
  5. let "per=$use*100/$max"
  6. let "mb=$use/256"
  7. let "mmb=$max/256"
  8.  
  9. echo "privvmpages usage: $mb MB ($per% of $mmb)"

Sample output:
privvmpages usage: 296 MB (57% of 512)
For those wondering, yes I do have memory usage above “100%” in the output of some of these scripts, and it’s partially true. A VPS does usually have some guaranteed memory, and some “burstable” – so you can use more than just the guaranteed memory for some period of time. On the mashine I ran this I have 256 guaranteed and a “total” of 512MB – including the burstable memory.

If you like any of these, feel free to use them. I use them by creating a scripts directory and adding it to the PATH, so I can simply invoke them from anywhere.

PS: For more detailed memory usage information (as graphs etc), I’m using Munin and really recommend it. The graphs it draws are really nice, and it doesn’t inflict heavy load on the server, and it can monitor remote hosts if needed!

Tags: , , , , , , ,

This post is under “coding, terminal heroes” and has no respond so far.

Post a reply