#!/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)
#
# $Log: cpu.in,v $
# Revision 1.4  2004/05/06 21:39:54  jimmyo
# Added plugin acpi, contributed by Alexandre Dupouy.
#
# 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/01/31 19:56:37  jimmyo
# Linux/cpu now graphs all values on a 2.6 kernel (Deb#227946).
#
# Revision 1.1  2004/01/02 18:50:01  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.2  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 [ -r /proc/stat ]; then
		echo yes
		exit 0
	else
		echo no
		exit 1
	fi
fi

extinfo=""

if (`egrep '^cpu +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+' /proc/stat 2>/dev/null >/dev/null`)
then
	extinfo="iowait irq softirq"
fi

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

	NCPU=`expr \`grep '^cpu. ' /proc/stat | wc -l\` - 1`
	PERCENT=`expr $NCPU '*' 100`
	if [ "$scaleto100" = "yes" ]; then
		graphlimit=100
	else
		graphlimit=$PERCENT
	fi
	SYSWARNING=`expr $PERCENT '*' 30 / 100`
	SYSCRITICAL=`expr $PERCENT '*' 50 / 100`
	USRWARNING=`expr $PERCENT '*' 80 / 100`
	echo 'graph_title CPU usage'
	echo "graph_order system user nice idle" $extinfo
	echo "graph_args --base 1000 -r --lower-limit 0 --upper-limit $graphlimit"
	echo 'graph_vlabel %'
	echo 'graph_scale no'
	echo 'system.label system'
	echo 'system.draw AREA'
	echo 'system.max 5000'
	echo 'system.type COUNTER'
	echo "system.warning $SYSWARNING" 
	echo "system.critical $SYSCRITICAL" 
	echo 'user.label user'
	echo 'user.draw STACK'
	echo 'user.max 5000'
	echo "user.warning $USRWARNING"
	echo 'user.type COUNTER'
	echo 'nice.label nice'
	echo 'nice.draw STACK'
	echo 'nice.max 5000'
	echo 'nice.type COUNTER'
	echo 'idle.label idle'
	echo 'idle.draw STACK'
	echo 'idle.max 5000'
	echo 'idle.type COUNTER'
	if [ "$scaleto100" = "yes" ]; then
		echo "system.cdef system,$NCPU,/"
		echo "user.cdef user,$NCPU,/"
		echo "nice.cdef nice,$NCPU,/"
		echo "idle.cdef idle,$NCPU,/"
	fi
	if [ ! -z "$extinfo" ]
	then
		echo 'iowait.label iowait'
		echo 'iowait.draw STACK'
		echo 'iowait.max 5000'
		echo 'iowait.type COUNTER'
		echo 'irq.label irq'
		echo 'irq.draw STACK'
		echo 'irq.max 5000'
		echo 'irq.type COUNTER'
		echo 'softirq.label softirq'
		echo 'softirq.draw STACK'
		echo 'softirq.max 5000'
		echo 'softirq.type COUNTER'
		if [ "$scaleto100" = "yes" ]; then
			echo "iowait.cdef iowait,$NCPU,/"
			echo "irq.cdef irq,$NCPU,/"
			echo "softirq.cdef softirq,$NCPU,/"
		fi
	fi
	exit 0
fi


if [ ! -z "$extinfo" ]
then
	awk '/^cpu / { print "user.value " $2 "\nnice.value " $3 "\nsystem.value " $4 "\nidle.value " $5 "\niowait.value " $6 "\nirq.value " $7 "\nsoftirq.value " $8 }' < /proc/stat
else
	awk '/^cpu / { print "user.value " $2 "\nnice.value " $3 "\nsystem.value " $4 "\nidle.value " $5 }' < /proc/stat
fi
