2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00
contrib-munin/plugins/system/cpubyuser
pascal d824c7696f p/cpubyuser: get stats from top instead of ps
The top cpu statistics fit better to the other cpu statistics in munin.

The ps based statistics are calculated as average over the runtime of a
program, which does not neccessarily reflect the current situation.
2014-03-22 13:31:27 +01:00

89 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
#
# Plugin to monitor CPU usage, for a selected set of users
#
# Usage: Place in /etc/munin/node.d/ (or link it there using ln -s)
# Add this to your /etc/munin/plugin-conf.d/munin-node:
# [cpubyuser]
# env.USERS root yann
#
# root and yann being a list of the users to monitor.
# You need to also make sure that awk is installed
#
# 2008-12-08 v 1.3.1 Hanisch Elián:
# - support for dots in user names.
# - fix labels
#
# 2008-12-01 v 1.3 Hanisch Elián:
# - fixes, refactoring and code cleanup
# - Users that use cpu but aren't in the USERS env var
# are plotted as "others", set others.graph to 'no' if
# you don't want this.
#
# 2008-03-20 v 1.2 fireball: fixed minor screwup, works now ^^
#
# 2008-01-09 v 1.1 fireball: fixed "-" in usernames, those get replaced by "_" now.
# set usernames in config accordingly (that is with _)
#
#
# Parameters understood:
#
# config (required)
# autoconf (optional - used by munin-config)
#
#%# family=auto
#%# capabilities=autoconf
if [ "$1" = "autoconf" ]; then
if [ -n "$USERS" ]; then
echo "yes"
else
echo "\$USERS not defined."
fi
exit
fi
if [ "$1" = "config" ]; then
echo "graph_args --base 1000 -r --lower-limit 0"
echo "graph_title CPU usage, by user"
echo "graph_category system"
echo "graph_info This graph shows CPU usage, for monitored users."
echo "graph_vlabel %"
echo "graph_scale no"
echo "graph_period second"
_USERS=${USERS//[-.]/_}
echo "graph_order $_USERS others"
FIRSTUSER=1;
for USER in $USERS "others"; do
_USER=${USER//[-.]/_}
echo "${_USER}.label $USER"
echo "${_USER}.info CPU used by user $USER"
echo "${_USER}.type GAUGE"
if [ $FIRSTUSER -eq 1 ]; then
echo "${_USER}.draw AREA"
FIRSTUSER=0
else
echo "${_USER}.draw STACK"
fi
done
exit
fi
top -b -n 1 | tail -n +8 | \
awk -v USERS="$USERS" '
{ if ($2 != "USER") CPU_USER[$2]+=$9 }
END {
others_sum = 0
for (user in CPU_USER) {
m = match(USERS,user)
if (m != 0) {
_user=user
gsub(/[-.]/,"_", _user);
print _user".value", CPU_USER[user]
} else
others_sum += CPU_USER[user]
}
print "others.value", others_sum;
}'