2
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00
contrib-munin/plugins/system/cpubyuser

106 lines
3 KiB
Text
Raw Normal View History

2016-10-22 04:03:35 +02:00
#!/bin/sh
2007-11-06 11:02:48 +01:00
#
# Plugin to monitor CPU usage, for a selected set of users
#
2016-10-22 04:02:49 +02:00
# 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:
2007-11-06 11:02:48 +01:00
# [cpubyuser]
# env.USERS root yann
#
2016-10-22 04:02:49 +02:00
# If env.USERS is set to ALL, count all logged in users.
#
2007-11-06 11:02:48 +01:00
# 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:
#
2016-10-22 04:02:49 +02:00
# config (required)
# autoconf (optional - used by munin-config)
2007-11-06 11:02:48 +01:00
#
#%# family=auto
#%# capabilities=autoconf
. "$MUNIN_LIBDIR/plugins/plugin.sh"
OTHER_FIELD="others"
[ "$USERS" = "ALL" ] && USERS=$(w --no-header | awk '{ print $1 }' | sort | uniq)
2007-11-06 11:02:48 +01:00
if [ "$1" = "autoconf" ]; then
if [ -n "$USERS" ]; then
echo "yes"
else
echo "no (USERS setting is missing)"
2007-11-06 11:02:48 +01:00
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"
2016-10-22 01:54:57 +02:00
user_fields="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)"
echo "graph_order $user_fields $OTHER_FIELD"
for user in $USERS "$OTHER_FIELD"; do
2016-10-22 04:02:49 +02:00
user_field="$(clean_fieldname "$user")"
2016-10-22 01:54:57 +02:00
echo "${user_field}.label $user"
echo "${user_field}.info CPU used by user $user"
echo "${user_field}.type GAUGE"
echo "${user_field}.draw AREASTACK"
2007-11-06 11:02:48 +01:00
done
exit
fi
top -b -n 1 | sed '1,/^ *PID /d' | \
2007-11-06 11:02:48 +01:00
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_PER_PID[$1]=$9 }
2007-11-06 11:02:48 +01:00
END {
split(USERS, user_array)
for (user_index in user_array) {
user = user_array[user_index]
# retrieve all process IDs belonging to the user
"ps -u "user" -o pid --no-headers 2>/dev/null | tr \"\n\" \" \"" | getline pids
user_cpu = 0
split(pids, pid_array)
# summarize the cpu usage of this usage
for (pid_index in pid_array) {
pid = pid_array[pid_index]
user_cpu += CPU_PER_PID[pid]
delete CPU_PER_PID[pid]
}
print user, user_cpu
2007-11-06 11:02:48 +01:00
}
# add all remaining cpu usages into "others"
others_sum = 0
for (other_usage in CPU_PER_PID) others_sum+=CPU_PER_PID[other_usage]
print "'"$OTHER_FIELD"'", others_sum;
}' | while read -r user count; do
# apply fieldname cleanup
echo "$(clean_fieldname "$user").value $count"
done