#!/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 CPU usage'
	echo 'graph_order system user waitio idle'
	echo 'graph_category system'
	ncpu=`kstat -p -c misc -n system_misc -s '/^ncpus$/' | cut -f2 -d'	'`
	cpumax=`expr "$ncpu" '*' 100`
	if [ "$scaleto100" = "yes" ]; then
		graphlimit=100
	else
		graphlimit=$cpumax
	fi
	echo "graph_args --base 1000 --lower-limit 0 --rigid --upper-limit $graphlimit"
	echo 'graph_vlabel %'
	echo 'graph_scale no'
	echo 'graph_period ${graph_period}'
	echo 'system.label system'
	echo 'system.draw AREA'
	echo 'system.type DERIVE'
	echo 'system.min 0'
	echo "system.max $cpumax"
	echo 'user.label user'
	echo 'user.draw STACK'
	echo 'user.type DERIVE'
	echo 'user.min 0'
	echo "user.max $cpumax"
	echo "waitio.max $cpumax"
	echo 'waitio.label waitio'
	echo 'waitio.draw STACK'
	echo 'waitio.type DERIVE'
	echo 'waitio.min 0'
	echo 'idle.label idle'
	echo 'idle.draw STACK'
	echo 'idle.type DERIVE'
	echo 'idle.min 0'
	echo "idle.max $cpumax"
	if [ "$scaleto100" = "yes" ]; then
		echo "system.cdef system,$ncpu,/"
		echo "user.cdef user,$ncpu,/"
		echo "waitio.cdef waitio,$ncpu,/"
		echo "idle.cdef idle,$ncpu,/"
	fi
	exit 0
fi

kstat -p -c misc -m cpu_stat -s '/^(user|kernel|wait|idle)$/' | sed -e 's/.*://' | awk '
BEGIN {
	map["user"] = "user"
	map["kernel"] = "system"
	map["wait"] = "waitio"
	map["idle"] = "idle"
}
length(map[$1]) > 0 {
	sum[map[$1]] += $2
	total += $2
}
END {
	for (item in sum) {
		print item ".value", sum[item]
	}
}'
