munin-contrib/plugins/user/cpubyuser

118 lines
3.5 KiB
Bash
Executable File

#!/bin/sh
#
# Plugin to monitor CPU usage, for a selected set of users
#
# Usage: Place in /etc/munin/plugins/ (or link it there using ln -s)
# Add this to your /etc/munin/plugin-conf.d/munin-node:
# [cpubyuser]
# user root # required if /proc can't be read from by any user!
# env.USERS root yann
# env.OTHER_FIELD others # enable 'others'-list, set the label/field name
#
# root and yann being a list of the users to monitor.
#
# If env.USERS is set to ALL, count all logged in users, and if set to
# ALLPROC, *all* users with a running process will be listed, except for root.
#
# You need to also make sure that awk is installed
#
# 2019-08-30 v 1.4 pcy <pcy@ulyssis.org>:
# - add USERS=ALLPROC, not relying on a tty or pty being present for user detection
# - OTHERS_FIELD now defaults to disabled, explicitly give it a
# value to re-enable it (eg. 'others')
# - use ps(1) instead of top(1) for easier and more robust
# parsing/summary calculation
#
# 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
. "$MUNIN_LIBDIR/plugins/plugin.sh"
[ -z "$USERS" ] && USERS=ALL
if [ "$USERS" = "ALLPROC" ]; then
USERS="$(ps ax --format uname | tail +2 | sort -u | grep -v -e '^root$')"
elif [ "$USERS" = "ALL" ]; then
USERS="$(w --no-header | cut -d' ' -f 1 | sort -u)"
fi
if [ "$1" = "autoconf" ]; then
if [ -n "$USERS" ]; then
echo "yes"
else
echo "no (USERS setting is missing)"
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 cpu"
echo "graph_info This graph shows CPU usage, for monitored users."
echo "graph_vlabel %"
echo "graph_scale no"
echo "graph_period second"
user_fields="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)"
echo "graph_order $user_fields $(clean_fieldname "$OTHER_FIELD")"
for user in $USERS "$OTHER_FIELD"; do
if [ -n "$user" ]; then
user_field="$(clean_fieldname "$user")"
echo "${user_field}.label $user"
echo "${user_field}.info CPU used by user $user"
echo "${user_field}.type GAUGE"
echo "${user_field}.draw AREASTACK"
fi
done
exit
fi
OTHER_PRINT=""
[ -z "$OTHER_FIELD" ] || OTHER_PRINT="print \"$(clean_fieldname "$OTHER_FIELD")\", others_sum;"
ps ax --format "%cpu user" | tail +2 | \
awk -v USERS="$USERS" '
# Store the CPU usage of each process - the mapping to the
# user happens later. We cannot use the second column
# (username) directly, since it may be abbreviated (ending
# with "+").
{ CPU_USER[$2]=$1 }
END {
others_sum = 0
split(USERS, user_array)
for (user in CPU_USER) {
m = match(USERS,user)
if (m != 0) {
_user=user
gsub(/[-.]/,"_",_user);
print _user, (CPU_USER[user])
} else
others_sum += CPU_USER[user]
}
'"$OTHER_PRINT"'
}' | while read -r user count; do
# apply fieldname cleanup
echo "$(clean_fieldname "$user").value $count"
done