Last active
May 24, 2023 21:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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