#!/bin/sh
#
# Plugin to monitor CPU usage.
#
# Usage: Place in /etc/munin/node.d/ (or link it there  using ln -s)
#
# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# Magic markers - optional - used by installation scripts and
# munin-config:
#
#%# family=auto
#%# capabilities=autoconf

if [ "$1" = "autoconf" ]; then
	if [ -x /usr/bin/kstat ]; then
		echo yes
		exit 0
	else
		echo no
		exit 1
	fi
fi

if [ "$1" = "config" ]; then
	echo 'graph_title Paging Out'
	echo 'graph_order pgout pgpgout pgfree scan'
	echo "graph_args --base 1000"
	echo 'graph_category system'
	echo 'pgout.label pgout'
	echo 'pgout.draw LINE2'
	echo 'pgout.type DERIVE'
	echo 'pgout.min 0'
	echo 'pgout.max 1000000000'
	echo 'pgpgout.label pgpgout'
	echo 'pgpgout.draw LINE2'
	echo 'pgpgout.type DERIVE'
	echo 'pgpgout.min 0'
	echo 'pgpgout.max 1000000000'
	echo 'pgfree.label pgfree'
	echo 'pgfree.draw LINE2'
	echo 'pgfree.type DERIVE'
	echo 'pgfree.min 0'
	echo 'pgfree.max 1000000000'
	echo 'scan.label scan'
	echo 'scan.draw LINE2'
	echo 'scan.type DERIVE'
	echo 'scan.min 0'
	echo 'scan.max 1000000000'
	exit 0
fi

kstat -p -c misc -m cpu_stat -s '/^(pgout|pgpgout|dfree|scan)$/' | sed -e 's/.*://' | awk '
BEGIN {
	map["pgout"] = "pgout"
	map["pgpgout"] = "pgpgout"
	map["dfree"] = "pgfree"
	map["scan"] = "scan"
}
length(map[$1]) > 0 {
	sum[map[$1]] += $2
}
END {
	for (item in sum) {
		print item ".value", sum[item]
	}
}'
