How to monitor CPU usage stats per process with munin

This post will walk you through the process of installing a plugin to monitor your munin nodes/clients for CPU usage per process
Note that the edits are to be done on the node(s).

Original script page:

Plugin code:

Create /usr/share/munin/plugins/proc_cpu:

emacs /usr/share/munin/plugins/proc_cpu

with the following contents:

# (c) 2010, Andrew Johnstone andrew
# Based on the 'proc_mem' plugin, written by Rodrigo Sieiro rsieiro
# Configure it by using the processes env var, i.e.:
# [proc_mem]
# env.processes        php mysqld apache2

. $MUNIN_LIBDIR/plugins/

if [ "$1" = "autoconf" ]; then
        echo yes
        exit 0

processes=${processes:="php mysqld apache2"}

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

        NCPU=$(egrep '^cpu[0-9]+ ' /proc/stat | wc -l)
        PERCENT=$(($NCPU * 100))
        if [ "$scaleto100" = "yes" ]; then
        SYSWARNING=`expr $PERCENT '*' 30 / 100`
        SYSCRITICAL=`expr $PERCENT '*' 50 / 100`
        USRWARNING=`expr $PERCENT '*' 80 / 100`

        echo 'graph_title CPU usage by process'
        echo "graph_args --base 1000 -r --lower-limit 0 --upper-limit $graphlimit"
        echo 'graph_vlabel %'
        echo 'graph_category processes'
        echo 'graph_info This graph shows the cpu usage of several processes'

        for proc in $processes; do
                  echo "$proc.label $proc"
        exit 0

TMPFILE=`mktemp -t top.XXXXXXXXXX` && {

  top -b -n1 > $TMPFILE

  for proc in $processes; do
    value=$(cat $TMPFILE | grep $proc | awk 'BEGIN { SUM = 0 } { SUM += $9} END { print SUM }')
    echo "$proc.value $value"
  rm -f $TMPFILE


chmod 755 /usr/share/munin/plugins/proc_cpu
ln -s /usr/share/munin/plugins/proc_cpu /etc/munin/plugins/proc_cpu

Edit: /etc/munin/plugin-conf.d/munin-node

Add the following to the end of that file:

user munin
group munin

Restart the service:

service munin-node restart

The data will appear under:
Overview :: MuninMonitor :: MuninMonitor :: proc cpu

The plugins are put in /usr/share/munin/plugins/pluginname. They need to be chmodded to 755. There should be a link to the plugin from /etc/munin/plugins/pluginname. Once installed, restart munin-node with:

service munin-node restart

Please also check this:

Also try this:

cd /usr/share/munin/plugins
chmod 755 multicpu
ln -s multicpu /etc/munin/plugins/multicpu
service munin-node restart

New script:
Edit /etc/munin/plugin-conf.d/munin-node
Add the following lines to the end:
env.processes php mysqld apache2

Now add the script:

#Get last 5 min cpu load
load=$(uptime | awk '{print $11}' | sed 's/[^a-zA-Z0-9.]//g')
echo "CPU load:"$load
if (( $(echo "$load > 0.8" | bc -l) ))
	#CPU load is high (>1)
	#Gets list of top 10 processes using cpu load and adds them to log list
	proclist=$(ps -Ao fname,pcpu | sort -nrk 2,2 | awk '!x[$1]++' | awk '{print $1}' | head -n 10 | sed ':a;N;$!ba;s/\n/ /g')
	echo "Top processes: "$proclist
	sed -i "/env.processes/c\env.processes        $proclist/g" /etc/munin/plugin-conf.d/munin-node
	#Now restart
	service munin-node restart
	echo "CPU load is ok"

Add the cron job:

*/5 * * * * /bin/bash /root/bash-advanced-scripts/topprocesses

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.