[mrtg] Mrtg conf

Steve Shipway s.shipway at auckland.ac.nz
Thu Sep 4 00:26:28 CEST 2008

Your configuration has:

Target[cpu]: `/usr/bin/awk '/cpu /{print $2+$3; print $2+$3+$4; print "quite some time"; print "domain.com"}'</proc/stat`
MaxBytes[cpu]: 100
Options[cpu]: nopercent,growright,noinfo,bits

You are getting your CPU info from /proc/stat, and the cpu line.  However, this line does not contain a count of seconds used.

See this page: http://www.linuxhowtos.org/System/procstat.htm

Since you are using counter mode (ie, no 'gauge' option) then you will be graphing the rate of change of these counters.  So far so good.  But you want this in percent, so basically, you need:

(CPU seconds used)/(seconds available)*100%

Now, the CPU count is NOT in seconds.  It is in 'jiffies', which are typically (but not always) 0.01 seconds.  You can check the actual value of USER_HZ in the include/asm/param.h file under your kernel source directory.  Therefore, you need:

((change in CPU count)/100)/(seconds available)*100%

Excellent - the 100s cancel out, and we should be OK!  I assume you've already worked things out this far...
But Wait!  We have 4 CPUs on the machine, so the 'seconds available' is actually 4 times the 'seconds passed'.  This means we need:

(change in CPU count)/((seconds passed)*(number of CPUs))

The '/(seconds passed)' is done automatically by MRTG when it does its polling.  So, our Target definition needs to return:

(change in CPU count)/(number of CPUs)

The columns on the CPU line corresponds to:

 *   label
 *   user: normal processes executing in user mode
 *   nice: niced processes executing in user mode
 *   system: processes executing in kernel mode
 *   idle: twiddling thumbs
 *   iowait: waiting for I/O to complete
 *   irq: servicing interrupts
 *   softirq: servicing softirqs

Now, you want to graph (user time) and (total time used), which are ($2) and ($2+$4+$6)

Target[cpu]: `/usr/bin/awk '/cpu /{print ($2/4); print (($2+$4+$6)/4); print "quite some time"; print "domain.com"}'</proc/stat`

So, lets try running that to test:

$ /usr/bin/awk '/cpu /{print ($2/4); print (($2+$4+$6)/4); print "quite some time"; print "domain.com"}'</proc/stat
quite some time

Oh no, what's gone wrong here?  Awk is trying to be helpful and is printing in exponential notation, which MRTG doesn't understand.  This would probably result in some very odd percentage calculations.  So lets fix that:

$ /usr/bin/awk '/cpu /{printf "%f\n",($2/4); printf "%f\n", (($2+$4+$6)/4); print "quite some time"; print "domain.com"}'</proc/stat
quite some time

This gives us the definition:

Target[cpu]: `/usr/bin/awk '/cpu /{printf "%f\n",($2/4); printf "%f\n", (($2+$4+$6)/4); print "quite some time"; print "domain.com"}'</proc/stat`

This is probably what we need.  Set MaxBytes to 100 (as you have), and it should work.  If you are using the Routers2 frontend, then use the extended options 'fixunit', 'nopercent' and 'nototal' so that the percentages are handled correctly.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.oetiker.ch/pipermail/mrtg/attachments/20080904/25d961ed/attachment-0001.html 

More information about the mrtg mailing list