#! /bin/sh
#
# Plugin to monitor NetBSD kernel memory pool usage.
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - only used by munin-config)
#
# $Log: memory_pools.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 /usr/bin/vmstat ]; then
    	echo yes
	exit 0
    else
        echo no
	exit 1
    fi
fi

PAGESIZE=`/sbin/sysctl -n hw.pagesize`

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


if [ "$1" = "config" ]; then

        echo 'graph_args --base 1024 -l 0 --vertical-label Bytes'
	echo 'graph_title Kernel Pool Memory usage'
	echo 'graph_category system'
	echo 'graph_info This graph shows the kernel memory pool usage'
	echo 'graph_order vnode ncache ffsino mbufs rest bufpl bufs'

	echo 'vnode.label vnodes'
	echo 'vnode.info vnode cache'
	echo 'vnode.draw AREA'

	echo 'ncache.label ncache'
	echo 'ncache.info namei cache'
	echo 'ncache.draw STACK'

	echo 'ffsino.label ffs-i'
	echo 'ffsino.info FFS inode cache'
	echo 'ffsino.draw STACK'

	echo 'mbufs.label mbufs'
	echo 'mbufs.info network buffers (mbufs + mbuf clusters)'
	echo 'mbufs.draw STACK'

	echo 'rest.label rest'
	echo 'rest.info sum of other memory pools'
	echo 'rest.draw STACK'

	echo 'bufpl.label bufpl'
	echo 'bufpl.info buffer pool'
	echo 'bufpl.draw STACK'

	echo 'bufs.label bufs'
	echo 'bufs.info buffer pools, for file metadata'
	echo 'bufs.draw STACK'

	exit 0
fi

vmstat -m -W > /dev/null 2>&1
if [ $? -eq 0 ]; then
	vmstat -m -W 2>/dev/null | awk '
/^Memory resource pool statistics/ { inpool=1; next; }
/^In use/			   { inpool=0; next; }
/^Name/ { next; }
/^vnodepl/ &&  inpool   { vnode  = $10 * $11; next; }
/^ncachepl/ && inpool   { ncache = $10 * $11; next; }
/^ffsinopl/ && inpool	{ ffsino = $10 * $11; next; }
/^dino1pl/ &&  inpool	{ dinopl = $10 * $11; next; }
/^buf.*k / &&  inpool   { bufk  += $10 * $11; next; }
/^bufpl/   &&  inpool	{ bufpl  = $10 * $11; next; }
/^mbpl/    &&  inpool   { mbufs += $10 * $11; next; }
/^mclpl/   &&  inpool   { mbufs += $10 * $11; next; }
$16 != "0x200" && $16 != "0x600" && inpool { rest  += $10 * $11; }
END {
	print "bufs.value " bufk;
	print "bufpl.value " bufpl;
	print "vnode.value " vnode;
	print "ncache.value " ncache;
	print "ffsino.value " ffsino;
	print "mbufs.value " mbufs;
	print "rest.value " rest;
}'
else
	# Need to guess size of pool pages, may not be correct
	# for small-memory machines (< 16MB)
	vmstat -m 2>/dev/null | awk -v bpp=$PAGESIZE '
/^Memory resource pool statistics/ { inpool=1; next; }
/^In use/			   { inpool=0; next; }
/^Name/				   { next; }
/^vnodepl/ &&  inpool   { vnode  = $8 * bpp; next; }
/^ncachepl/ && inpool   { ncache = $8 * bpp; next; }
/^ffsinopl/ && inpool	{ ffsino = $8 * bpp; next; }
/^dino1pl/ &&  inpool	{ dinopl = $8 * bpp; next; }
/^buf.*k / &&  inpool   { bufk  += $8 * 65536; next; }
/^bufpl/    && inpool	{ bufpl  = $8 * bpp; next; }
/^mbpl/    &&  inpool   { mbufs += $8 * bpp; next; }
/^mclpl/   &&  inpool   { mbufs += $8 * bpp; next; }
/^kvakernel/ 		{ next; }
/^kvakmem/              { next; }
inpool			{ rest  += $8 * bpp; }
END {
	print "bufs.value " bufk;
	print "bufpl.value " bufpl;
	print "vnode.value " vnode;
	print "ncache.value " ncache;
	print "ffsino.value " ffsino;
	print "mbufs.value " mbufs;
	print "rest.value " rest;
}'
fi
