mirror of
https://github.com/munin-monitoring/contrib.git
synced 2025-07-22 02:51:03 +00:00
[plugins/user/cpubyuser] Bugfixes and enhancements
- Fix USERS=ALL only enumerating processes with a tty or pty attached (and thus not eg. user-owned FastCGI processes, owned by eg. Apache) - ps(1) is used to make process list parsing easier and more robust - OTHERS_FIELD now defaults to disabled
This commit is contained in:
parent
a1c4bcb25a
commit
0031acd637
1 changed files with 45 additions and 33 deletions
|
@ -2,15 +2,26 @@
|
||||||
#
|
#
|
||||||
# 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/plugins/ (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]
|
||||||
|
# user root # required if /proc can't be read from by any user!
|
||||||
# env.USERS root yann
|
# env.USERS root yann
|
||||||
|
# env.OTHER_FIELD others # enable 'others'-list, set the label/field name
|
||||||
#
|
#
|
||||||
# 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.
|
# If env.USERS is set to ALL, count all logged in users, and if set to
|
||||||
# You need to also make sure that awk is installed
|
# 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, explicitely give it a
|
||||||
|
# value to reenable 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:
|
# 2008-12-08 v 1.3.1 Hanisch Elián:
|
||||||
# - support for dots in user names.
|
# - support for dots in user names.
|
||||||
|
@ -39,9 +50,12 @@
|
||||||
|
|
||||||
. "$MUNIN_LIBDIR/plugins/plugin.sh"
|
. "$MUNIN_LIBDIR/plugins/plugin.sh"
|
||||||
|
|
||||||
OTHER_FIELD="others"
|
[ -z "$USERS" ] && USERS=ALL
|
||||||
[ "$USERS" = "ALL" ] && USERS=$(w --no-header | awk '{ print $1 }' | sort | uniq)
|
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 [ "$1" = "autoconf" ]; then
|
||||||
if [ -n "$USERS" ]; then
|
if [ -n "$USERS" ]; then
|
||||||
|
@ -61,44 +75,42 @@ if [ "$1" = "config" ]; then
|
||||||
echo "graph_scale no"
|
echo "graph_scale no"
|
||||||
echo "graph_period second"
|
echo "graph_period second"
|
||||||
user_fields="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)"
|
user_fields="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)"
|
||||||
echo "graph_order $user_fields $OTHER_FIELD"
|
echo "graph_order $user_fields $(clean_fieldname "$OTHER_FIELD")"
|
||||||
for user in $USERS "$OTHER_FIELD"; do
|
for user in $USERS "$OTHER_FIELD"; do
|
||||||
user_field="$(clean_fieldname "$user")"
|
if [ -n "$user" ]; then
|
||||||
echo "${user_field}.label $user"
|
user_field="$(clean_fieldname "$user")"
|
||||||
echo "${user_field}.info CPU used by user $user"
|
echo "${user_field}.label $user"
|
||||||
echo "${user_field}.type GAUGE"
|
echo "${user_field}.info CPU used by user $user"
|
||||||
echo "${user_field}.draw AREASTACK"
|
echo "${user_field}.type GAUGE"
|
||||||
|
echo "${user_field}.draw AREASTACK"
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
top -b -n 1 | sed '1,/^ *PID /d' | \
|
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" '
|
awk -v USERS="$USERS" '
|
||||||
# Store the CPU usage of each process - the mapping to the
|
# Store the CPU usage of each process - the mapping to the
|
||||||
# user happens later. We cannot use the second column
|
# user happens later. We cannot use the second column
|
||||||
# (username) directly, since it may be abbreviated (ending
|
# (username) directly, since it may be abbreviated (ending
|
||||||
# with "+").
|
# with "+").
|
||||||
{ CPU_PER_PID[$1]=$9 }
|
{ CPU_USER[$2]=$1 }
|
||||||
END {
|
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
|
|
||||||
}
|
|
||||||
# add all remaining cpu usages into "others"
|
|
||||||
others_sum = 0
|
others_sum = 0
|
||||||
for (other_usage in CPU_PER_PID) others_sum+=CPU_PER_PID[other_usage]
|
split(USERS, user_array)
|
||||||
print "'"$OTHER_FIELD"'", others_sum;
|
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
|
}' | while read -r user count; do
|
||||||
# apply fieldname cleanup
|
# apply fieldname cleanup
|
||||||
echo "$(clean_fieldname "$user").value $count"
|
echo "$(clean_fieldname "$user").value $count"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue