Skip to content

Instantly share code, notes, and snippets.

@acrookston
Last active May 24, 2023 21:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save acrookston/54f406e8c8ca21053508 to your computer and use it in GitHub Desktop.
Save acrookston/54f406e8c8ca21053508 to your computer and use it in GitHub Desktop.
Munin plugin to track CPU usage from specific processes based on process id's
#!/bin/sh
#
# Extended 2015 by Andrew Crookston <andrew@caoos.com> to use pidfiles instead of process names
# Original (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 pidfiles env. Format: name:pidfile name:pidfile. e.g.:
#
# [proc_cpu_pid]
# env.pidfiles munin-node:/var/run/munin/munin-node.pid
#
. $MUNIN_LIBDIR/plugins/plugin.sh
if [ "$1" = "autoconf" ]; then
echo yes
exit 0
fi
pidfiles=${pidfiles:="munin-node:/var/run/munin/munin-node.pid"}
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 ids'
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 $pidfiles; do
name=${proc%:*}
echo "name.label $name"
done
exit 0
fi
for proc in $pidfiles; do
name=${proc%:*}
pid=`cat ${proc#*:}`
value=$(top -b -n1 -p $pid | grep "\b$pid\b" | awk 'BEGIN { SUM = 0 } { SUM += $9} END { print SUM }')
echo "$name.value $value"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment