#! /bin/sh
#
# Plugin to monitor memory usage by type.
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - only used by munin-config)
#
# $Log: memory_types.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
    	if /sbin/sysctl hw.pagesize > /dev/null 2>&1; 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 by category'
	echo 'graph_category system'
	echo 'graph_info This graph shows how the machine uses its memory.'

	echo 'graph_order exec anon file kernel free'

	echo 'exec.label exec'
	echo 'exec.info memory for cached executables'
	echo 'exec.draw AREA'

	echo 'anon.label anon'
	echo 'anon.info anonymous memory'
	echo 'anon.draw STACK'

	echo 'file.label file'
	echo 'file.info memory for cached non-executable files'
	echo 'file.draw STACK'

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

	echo 'free.label free'
	echo 'free.info free memory'
	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; }
/anonymous pages$/{ anon = $1;     print "anon.value "     $1 * bpp; }
/cached file pages$/{ file = $1;   print "file.value "     $1 * bpp; }
/cached executable pages$/{ exec = $1; print "exec.value " $1 * bpp; }
END { kernel = managed - anon - file - exec - free;
    print "kernel.value " kernel * bpp; }'
