#!/bin/sh
#
# Plugin to monitor memory usage.
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - only used by munin-config)
#
# $Log: memory.in,v $
# Revision 1.1.1.1  2006/06/04 20:53:57  he
# Import the client version of the Munin system monitoring/graphing
# tool -- project homepage is at http://munin.sourceforge.net/
#
# This package has added support for NetBSD, via a number of new plugin
# scripts where specific steps needs to be taken to collect information.
#
# I also modified the ntp_ plugin script to make it possible to not
# plot the NTP poll delay, leaving just jitter and offset, which IMO
# produces a more telling graph.
#
#
#
#
# Magic markers (optional - only used by munin-config and some
# installation scripts):
#
#%# family=auto
#%# capabilities=autoconf

if [ "$1" = "autoconf" ]; then
    if [ -x /sbin/sysctl ]; then
        /sbin/sysctl hw.pagesize > /dev/null
    	if [ $? = "0" ]; then
	    	echo yes
    		exit 0
    	else
	        echo no
		exit 1
	fi
    else
        echo no
        exit 1
    fi
fi

PAGESIZE=`/sbin/sysctl -n hw.pagesize`
MEMSIZE=`vmstat -s | awk '/pages managed/ { print $1 }'`
MEMMAX=`echo 2k $PAGESIZE $MEMSIZE '*p' | dc`

if [ "$1" = "config" ]; then
        echo 'graph_args --base 1024 -l 0 --vertical-label Bytes --upper-limit '$MEMMAX
	echo 'graph_title Memory usage'
	echo 'graph_category system'
	echo 'graph_info This graph shows what the machine uses its memory for.'

	echo 'graph_order active inactive wired kernel free'

	echo 'active.label active'
	echo 'active.info pages recently statistically used'
	echo 'active.draw AREA'

	echo 'inactive.label inactive'
	echo 'inactive.info pages recently statistically unused'
	echo 'inactive.draw STACK'

	echo 'wired.label wired'
	echo 'wired.info pages that are fixed into memory'
	echo 'wired.draw STACK'

	echo 'kernel.label kernel'
	echo 'kernel.info pages used by the kernel'
	echo 'kernel.draw STACK'

	echo 'free.label free'
	echo 'free.info pages without data content'
	echo 'free.draw STACK'

	exit 0
fi

vmstat -s | awk -v bpp=$PAGESIZE '
/pages managed$/  { managed = $1; }
/pages free$/     { free = $1;     print "free.value "     $1 * bpp; }
/pages active$/   { active = $1;   print "active.value "   $1 * bpp; }
/pages inactive$/ { inactive = $1; print "inactive.value " $1 * bpp; }
/pages wired$/    { wired = $1;    print "wired.value "    $1 * bpp; }
END { kernel = managed - wired - inactive - active - free;
    print "kernel.value " kernel * bpp; }'
