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: http://www.ajohnstone.com/achives/monitoring-processes-with-ps/

Plugin code:

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

emacs /usr/share/munin/plugins/proc_cpu

with the following contents:

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

. $MUNIN_LIBDIR/plugins/plugin.sh

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

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
                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 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"
        done
        exit 0
fi

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"
  done
  rm -f $TMPFILE
}

Now:

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:

[proc_cpu]
user munin
group munin

Restart the service:

service munin-node restart

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

Tips:
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:http://munin-monitoring.org/browser/munin-contrib/plugins/system/cpu-usage-by-process

Also try this:

cd /usr/share/munin/plugins
wget https://redmine.koumbit.net/projects/munin-contrib/repository/revisions/256709738d6a15b80715d91de4b7af55f1e3905e/raw/plugins/processes/multicpu
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:
[proc_mem]
env.processes php mysqld apache2

Now add the script:

#!/bin/bash
#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) ))
then 
	#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
else 
	echo "CPU load is ok"
fi

Add the cron job:

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