#!/bin/sh
#
# Plugin to monitor CPU usage.
#
# Usage: Place in /etc/munin/plugins/ (or link it there  using ln -s)
#
# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# $Log$
# Revision 1.8.2.3  2005/02/03 13:28:50  lupe
# correct field.info
#
# Revision 1.8.2.2  2005/01/28 14:51:22  lupe
# Add graph_info and some filed.info
#
# Revision 1.10  2005/01/28 14:47:31  lupe
# Add graph_info and some filed.info
#
# Revision 1.9  2005/01/25 08:48:28  lupe
# Correct multi-CPU bugs
#
# Revision 1.8.2.1  2005/01/25 09:22:52  lupe
# Correct multi-CPU bugs
#
# Revision 1.8  2004/12/09 22:12:55  jimmyo
# Added "graph_period" option, to make "graph_sums" usable.
#
# Revision 1.7  2004/11/28 09:43:54  lupe
# 6-CURRENT support
#
# Revision 1.6  2004/11/21 00:16:56  jimmyo
# Changed a lot of plugins so they use DERIVE instead of COUNTER.
#
# Revision 1.5  2004/05/20 19:02:36  jimmyo
# Set categories on a bunch of plugins
#
# Revision 1.4  2004/05/09 19:18:35  jimmyo
# Added support for scaleto100-parameter (patch from Lupe Christoph).
#
# Revision 1.3  2004/02/18 16:39:36  jimmyo
# Turned off scaling of values for cpu-graphs (no more nano-percentages).
#
# Revision 1.2  2004/02/01 18:59:54  lupe
# FreeBSD 5 compatibility.
#
# Revision 1.1  2004/01/02 18:50:00  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.3  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
#
# Magic markers - optional - used by installation scripts and
# munin-config:
#
#%# family=auto
#%# capabilities=autoconf



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

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

	STATUNITS=`/sbin/sysctl -n kern.clockrate | cut -f15 -d' '`
	PERCENT=`/sbin/sysctl -n hw.ncpu | awk '{print ($1)*100}'`
	NCPU=`/sbin/sysctl -n hw.ncpu`
	if [ "$scaleto100" = yes ]; then
		CDEF="$STATUNITS,/,100,*,$NCPU,/"
		PERCENT=100
	else
		CDEF="$STATUNITS,/,100,*"
		PERCENT=$(($NCPU*100))
	fi
#	SYSWARNING=$PERCENT*30/100
#	SYSCRITICAL=$PERCENT*50/100
#	INTWARNING=$PERCENT*80/100
#	USRWARNING=$PERCENT*80/100
	echo 'graph_title CPU usage'
	echo 'graph_order system interrupt user nice idle'
	echo "graph_args --base 1000 -r --lower-limit 0 --upper-limit $PERCENT "
	echo 'graph_vlabel %'
	echo 'graph_scale no'
	echo 'graph_info This graph shows how CPU time is spent.'
	echo 'graph_category system'
	echo 'graph_period second'
	echo 'system.label system'
	echo 'system.draw AREA'
	echo 'system.max 5000'
	echo 'system.type DERIVE'
	echo 'system.min 0'
#	echo "system.warning $SYSWARNING" 
#	echo "system.critical $SYSCRITICAL" 
	echo 'system.info CPU time spent by the kernel in system activities'
	echo "system.cdef system,$CDEF"
	echo 'interrupt.label interrupt'
	echo 'interrupt.draw STACK'
	echo 'interrupt.max 5000'
#	echo "interrupt.warning $INTWARNING"
	echo 'interrupt.type DERIVE'
	echo 'interrupt.min 0'
	echo 'interrupt.info CPU time spent by the kernel processing interrupts'
	echo "interrupt.cdef interrupt,$CDEF"
	echo 'user.label user'
	echo 'user.draw STACK'
	echo 'user.max 5000'
#	echo "user.warning $USRWARNING"
	echo 'user.type DERIVE'
	echo 'user.info CPU time spent by normal programs and daemons'
	echo 'user.min 0'
	echo "user.cdef user,$CDEF"
	echo 'nice.label nice'
	echo 'nice.draw STACK'
	echo 'nice.max 5000'
	echo 'nice.type DERIVE'
	echo 'nice.info CPU time spent by nice(1)d programs'
	echo 'nice.min 0'
	echo "nice.cdef nice,$CDEF"
	echo 'idle.label idle'
	echo 'idle.draw STACK'
	echo 'idle.max 5000'
	echo 'idle.type DERIVE'
	echo 'idle.info Idle CPU time'
	echo 'idle.min 0'
	echo "idle.cdef idle,$CDEF"
	exit 0
fi

/sbin/sysctl -n kern.cp_time | awk -F , '{ print "user.value " $1 "\nnice.value " $2 "\nsystem.value " $3 "\ninterrupt.value " $4 "\nidle.value " $5 }'

