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

Merge pull request #758 from sumpfralle/improve-cpubyuser

[cpubyuser] fix various issues
This commit is contained in:
sumpfralle 2016-10-22 12:49:27 +02:00 committed by GitHub
commit 37bb2b77f0

View File

@ -1,13 +1,13 @@
#!/bin/bash #!/bin/sh
# #
# Plugin to monitor CPU usage, for a selected set of users # 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) # 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: # Add this to your /etc/munin/plugin-conf.d/munin-node:
# [cpubyuser] # [cpubyuser]
# env.USERS root yann # env.USERS root yann
# #
# If env.USERS is set to ALL, count all logged in users. # If env.USERS is set to ALL, count all logged in users.
# #
# root and yann being a list of the users to monitor. # root and yann being a list of the users to monitor.
# You need to also make sure that awk is installed # You need to also make sure that awk is installed
@ -30,18 +30,24 @@
# #
# Parameters understood: # Parameters understood:
# #
# config (required) # config (required)
# autoconf (optional - used by munin-config) # autoconf (optional - used by munin-config)
# #
#%# family=auto #%# family=auto
#%# capabilities=autoconf #%# capabilities=autoconf
. "$MUNIN_LIBDIR/plugins/plugin.sh"
OTHER_FIELD="others"
[ "$USERS" = "ALL" ] && USERS=$(w --no-header | awk '{ print $1 }' | sort | uniq)
if [ "$1" = "autoconf" ]; then if [ "$1" = "autoconf" ]; then
if [ -n "$USERS" ]; then if [ -n "$USERS" ]; then
echo "yes" echo "yes"
else else
echo "\$USERS not defined." echo "no (USERS setting is missing)"
fi fi
exit exit
fi fi
@ -54,41 +60,46 @@ if [ "$1" = "config" ]; then
echo "graph_vlabel %" echo "graph_vlabel %"
echo "graph_scale no" echo "graph_scale no"
echo "graph_period second" echo "graph_period second"
_USERS=${USERS//[-.]/_} user_fields="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)"
echo "graph_order $_USERS others" echo "graph_order $user_fields $OTHER_FIELD"
FIRSTUSER=1; for user in $USERS "$OTHER_FIELD"; do
for USER in $USERS "others"; do user_field="$(clean_fieldname "$user")"
_USER=${USER//[-.]/_} echo "${user_field}.label $user"
echo "${_USER}.label $USER" echo "${user_field}.info CPU used by user $user"
echo "${_USER}.info CPU used by user $USER" echo "${user_field}.type GAUGE"
echo "${_USER}.type GAUGE" echo "${user_field}.draw AREASTACK"
if [ $FIRSTUSER -eq 1 ]; then
echo "${_USER}.draw AREA"
FIRSTUSER=0
else
echo "${_USER}.draw STACK"
fi
done done
exit exit
fi fi
if [ "$USERS" = "ALL" ]; then top -b -n 1 | sed '1,/^ *PID /d' | \
USERS=$( w | awk '{ print $1 }' )
fi
top -b -n 1 | tail -n +8 | \
awk -v USERS="$USERS" ' awk -v USERS="$USERS" '
{ if ($2 != "USER") CPU_USER[$2]+=$9 } # 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 }
END { END {
others_sum = 0 split(USERS, user_array)
for (user in CPU_USER) { for (user_index in user_array) {
m = match(USERS,user) user = user_array[user_index]
if (m != 0) { # retrieve all process IDs belonging to the user
_user=user "ps -u "user" -o pid --no-headers 2>/dev/null | tr \"\n\" \" \"" | getline pids
gsub(/[-.]/,"_", _user); user_cpu = 0
print _user".value", CPU_USER[user] split(pids, pid_array)
} else # summarize the cpu usage of this usage
others_sum += CPU_USER[user] 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
} }
print "others.value", others_sum; # 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