From 78f4e42d21958963ad893742110a16a292b97ecb Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 22 Oct 2016 01:48:36 +0200 Subject: [PATCH 01/10] [cpubyuser] use 'clean_fieldname' instead of incomplete substitution (Closes: #757) --- plugins/system/cpubyuser | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/system/cpubyuser b/plugins/system/cpubyuser index c2522233..d5a42a11 100755 --- a/plugins/system/cpubyuser +++ b/plugins/system/cpubyuser @@ -37,6 +37,8 @@ #%# family=auto #%# capabilities=autoconf +. "$MUNIN_LIBDIR/plugins/plugin.sh" + if [ "$1" = "autoconf" ]; then if [ -n "$USERS" ]; then echo "yes" @@ -54,11 +56,11 @@ if [ "$1" = "config" ]; then echo "graph_vlabel %" echo "graph_scale no" echo "graph_period second" - _USERS=${USERS//[-.]/_} + _USERS="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)" echo "graph_order $_USERS others" FIRSTUSER=1; for USER in $USERS "others"; do - _USER=${USER//[-.]/_} + _USER="$(clean_fieldname "$USER")" echo "${_USER}.label $USER" echo "${_USER}.info CPU used by user $USER" echo "${_USER}.type GAUGE" From a46588050103e8816e415ebc24b3ac7d616cc07d Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 22 Oct 2016 01:54:24 +0200 Subject: [PATCH 02/10] [cpubyuser] simplify AREASTACK usage --- plugins/system/cpubyuser | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/plugins/system/cpubyuser b/plugins/system/cpubyuser index d5a42a11..5c1020a7 100755 --- a/plugins/system/cpubyuser +++ b/plugins/system/cpubyuser @@ -58,18 +58,12 @@ if [ "$1" = "config" ]; then echo "graph_period second" _USERS="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)" echo "graph_order $_USERS others" - FIRSTUSER=1; for USER in $USERS "others"; do _USER="$(clean_fieldname "$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 + echo "${_USER}.draw AREASTACK" done exit fi From 002e8bbf996ce0c8038d8efd97c268c0cb575c56 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 22 Oct 2016 01:54:57 +0200 Subject: [PATCH 03/10] [cpubyuser] clear variable names --- plugins/system/cpubyuser | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/plugins/system/cpubyuser b/plugins/system/cpubyuser index 5c1020a7..f2c548f1 100755 --- a/plugins/system/cpubyuser +++ b/plugins/system/cpubyuser @@ -56,14 +56,14 @@ if [ "$1" = "config" ]; then echo "graph_vlabel %" echo "graph_scale no" echo "graph_period second" - _USERS="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)" - echo "graph_order $_USERS others" - for USER in $USERS "others"; do - _USER="$(clean_fieldname "$USER")" - echo "${_USER}.label $USER" - echo "${_USER}.info CPU used by user $USER" - echo "${_USER}.type GAUGE" - echo "${_USER}.draw AREASTACK" + user_fields="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)" + echo "graph_order $user_fields others" + for user in $USERS "others"; do + 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" done exit fi From 6f7473b2a7b26912565233530b13f2da0de7dc45 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 22 Oct 2016 02:09:43 +0200 Subject: [PATCH 04/10] [cpubyuser] proper 'no' output for autoconf --- plugins/system/cpubyuser | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/system/cpubyuser b/plugins/system/cpubyuser index f2c548f1..04dc2c2f 100755 --- a/plugins/system/cpubyuser +++ b/plugins/system/cpubyuser @@ -43,7 +43,7 @@ if [ "$1" = "autoconf" ]; then if [ -n "$USERS" ]; then echo "yes" else - echo "\$USERS not defined." + echo "no (USERS setting is missing)" fi exit fi From 4b21d5819537416c7f88965036abc7fc7425a422 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 22 Oct 2016 03:50:32 +0200 Subject: [PATCH 05/10] [cpubyuser] expand magic 'ALL' user before config --- plugins/system/cpubyuser | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/system/cpubyuser b/plugins/system/cpubyuser index 04dc2c2f..39469981 100755 --- a/plugins/system/cpubyuser +++ b/plugins/system/cpubyuser @@ -39,6 +39,9 @@ . "$MUNIN_LIBDIR/plugins/plugin.sh" +[ "$USERS" = "ALL" ] && USERS=$(w --no-header | awk '{ print $1 }' | sort | uniq) + + if [ "$1" = "autoconf" ]; then if [ -n "$USERS" ]; then echo "yes" @@ -68,10 +71,6 @@ if [ "$1" = "config" ]; then exit fi -if [ "$USERS" = "ALL" ]; then - USERS=$( w | awk '{ print $1 }' ) -fi - top -b -n 1 | tail -n +8 | \ awk -v USERS="$USERS" ' { if ($2 != "USER") CPU_USER[$2]+=$9 } From 391a4bfa416540a1e2aff48b6e296b595805d9d3 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 22 Oct 2016 03:52:05 +0200 Subject: [PATCH 06/10] [cpubyuser] user constant for magic 'others' user --- plugins/system/cpubyuser | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/system/cpubyuser b/plugins/system/cpubyuser index 39469981..096a32c0 100755 --- a/plugins/system/cpubyuser +++ b/plugins/system/cpubyuser @@ -39,6 +39,7 @@ . "$MUNIN_LIBDIR/plugins/plugin.sh" +OTHER_FIELD="others" [ "$USERS" = "ALL" ] && USERS=$(w --no-header | awk '{ print $1 }' | sort | uniq) @@ -60,8 +61,8 @@ if [ "$1" = "config" ]; then 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 others" - for user in $USERS "others"; do + echo "graph_order $user_fields $OTHER_FIELD" + for user in $USERS "$OTHER_FIELD"; do user_field="$(clean_fieldname "$user")" echo "${user_field}.label $user" echo "${user_field}.info CPU used by user $user" From 5bec8ec6b59099f678f24d2a16a160c1912cc13c Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 22 Oct 2016 03:55:45 +0200 Subject: [PATCH 07/10] [cpubyuser] improved top output without header --- plugins/system/cpubyuser | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/system/cpubyuser b/plugins/system/cpubyuser index 096a32c0..26fed6e4 100755 --- a/plugins/system/cpubyuser +++ b/plugins/system/cpubyuser @@ -72,9 +72,9 @@ if [ "$1" = "config" ]; then exit fi -top -b -n 1 | tail -n +8 | \ +top -b -n 1 | sed '1,/^ *PID /d' | \ awk -v USERS="$USERS" ' - { if ($2 != "USER") CPU_USER[$2]+=$9 } + { CPU_USER[$2]+=$9 } END { others_sum = 0 for (user in CPU_USER) { From 8280dbb0f5050b077453947daa60dc5d3ca39b60 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 22 Oct 2016 03:59:22 +0200 Subject: [PATCH 08/10] [cpubyuser] improve parsing for long usernames (Closes: #757) "ps" abbreviates long username - thus we prepare a separate mapping --- plugins/system/cpubyuser | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/plugins/system/cpubyuser b/plugins/system/cpubyuser index 26fed6e4..2931962c 100755 --- a/plugins/system/cpubyuser +++ b/plugins/system/cpubyuser @@ -74,17 +74,32 @@ fi top -b -n 1 | sed '1,/^ *PID /d' | \ awk -v USERS="$USERS" ' - { 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 { - 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] + 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 } - 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 From 85d91b50f4c3a6d2808a0b4f302c792cba0c119b Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 22 Oct 2016 04:02:49 +0200 Subject: [PATCH 09/10] [cpubyuser] fix whitespace --- plugins/system/cpubyuser | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/system/cpubyuser b/plugins/system/cpubyuser index 2931962c..0f7a2cc3 100755 --- a/plugins/system/cpubyuser +++ b/plugins/system/cpubyuser @@ -2,12 +2,12 @@ # # 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: +# 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 # -# 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. # You need to also make sure that awk is installed @@ -30,8 +30,8 @@ # # Parameters understood: # -# config (required) -# autoconf (optional - used by munin-config) +# config (required) +# autoconf (optional - used by munin-config) # #%# family=auto @@ -63,7 +63,7 @@ if [ "$1" = "config" ]; then 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 - user_field="$(clean_fieldname "$user")" + user_field="$(clean_fieldname "$user")" echo "${user_field}.label $user" echo "${user_field}.info CPU used by user $user" echo "${user_field}.type GAUGE" From 2db4f2ca5654cfc4e2e19691e7c63bc7004dbfa4 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 22 Oct 2016 04:03:35 +0200 Subject: [PATCH 10/10] [cpubyuser] switch from bash to sh --- plugins/system/cpubyuser | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/system/cpubyuser b/plugins/system/cpubyuser index 0f7a2cc3..77d599f5 100755 --- a/plugins/system/cpubyuser +++ b/plugins/system/cpubyuser @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # # Plugin to monitor CPU usage, for a selected set of users #