Solaris Memory and Swap Monitor

This script is a Solaris 10 specific memory and swap monitor. It turns the output of swap -s into a more human readable form. Then it checks kernel kstat values for lotsfree and freemem and calculates the remaining memory. If the remainder falls below zero, the system will start paging.

The script takes a single parameter, a number dictating the number of seconds to sleep before running round the loop again. Sample output with a 10 second delay would look like:

root# memmon 10
Swap and Physical memory at Fri Jul 23 11:56:45 BST 2010
    Alloc: 9.45345 Gb Res: 7.52671 Gb Used: 16.9802 Gb Avail: 5.09682 Gb
    Freemem: 174944 Kb Lots: 128088 Kb Remainder: 46856 Kb
Swap and Physical memory at Fri Jul 23 11:56:55 BST 2010
    Alloc: 9.45356 Gb Res: 7.5266 Gb Used: 16.9802 Gb Avail: 5.09683 Gb
    Freemem: 177544 Kb Lots: 128088 Kb Remainder: 49456 Kb
Swap and Physical memory at Fri Jul 23 11:57:06 BST 2010
    Alloc: 9.45356 Gb Res: 7.5266 Gb Used: 16.9802 Gb Avail: 5.09683 Gb
    Freemem: 181928 Kb Lots: 128088 Kb Remainder: 53840 Kb
root#

Use ctrl-C to exit the script.

#!/usr/bin/ksh
while true
do
echo "Swap and Physical memory at `date`"

raw=`swap -s | sed -e 's/k//g'`
halloc=`echo $raw | awk '{print $2/1024/1024}'`
hres=`echo $raw | awk '{print $6/1024/1024}'`
hused=`echo $raw | awk '{print $9/1024/1024}'`
havail=`echo $raw | awk '{print $11/1024/1024}'`

SWAPH="    Alloc: $halloc Gb Res: $hres Gb Used: $hused Gb Avail: $havail Gb"
echo "$SWAPH"

lots=`kstat -p unix:0:system_pages:lotsfree | awk '{print $2 * 8}'`;
free=`kstat -p unix:0:system_pages:freemem | awk '{print $2 * 8}'`;
rem=`echo "($free - $lots)" | bc`
echo "    Freemem: $free Kb Lots: $lots Kb Remainder: $rem Kb"
sleep $1
done