root / plugins / system / cpubyuser @ 4b400a73
Historique | Voir | Annoter | Télécharger (3,03 ko)
| 1 |
#!/bin/sh |
|---|---|
| 2 |
# |
| 3 |
# Plugin to monitor CPU usage, for a selected set of users |
| 4 |
# |
| 5 |
# Usage: Place in /etc/munin/node.d/ (or link it there using ln -s) |
| 6 |
# Add this to your /etc/munin/plugin-conf.d/munin-node: |
| 7 |
# [cpubyuser] |
| 8 |
# env.USERS root yann |
| 9 |
# |
| 10 |
# If env.USERS is set to ALL, count all logged in users. |
| 11 |
# |
| 12 |
# root and yann being a list of the users to monitor. |
| 13 |
# You need to also make sure that awk is installed |
| 14 |
# |
| 15 |
# 2008-12-08 v 1.3.1 Hanisch Elián: |
| 16 |
# - support for dots in user names. |
| 17 |
# - fix labels |
| 18 |
# |
| 19 |
# 2008-12-01 v 1.3 Hanisch Elián: |
| 20 |
# - fixes, refactoring and code cleanup |
| 21 |
# - Users that use cpu but aren't in the USERS env var |
| 22 |
# are plotted as "others", set others.graph to 'no' if |
| 23 |
# you don't want this. |
| 24 |
# |
| 25 |
# 2008-03-20 v 1.2 fireball: fixed minor screwup, works now ^^ |
| 26 |
# |
| 27 |
# 2008-01-09 v 1.1 fireball: fixed "-" in usernames, those get replaced by "_" now. |
| 28 |
# set usernames in config accordingly (that is with _) |
| 29 |
# |
| 30 |
# |
| 31 |
# Parameters understood: |
| 32 |
# |
| 33 |
# config (required) |
| 34 |
# autoconf (optional - used by munin-config) |
| 35 |
# |
| 36 |
|
| 37 |
#%# family=auto |
| 38 |
#%# capabilities=autoconf |
| 39 |
|
| 40 |
. "$MUNIN_LIBDIR/plugins/plugin.sh" |
| 41 |
|
| 42 |
OTHER_FIELD="others" |
| 43 |
[ "$USERS" = "ALL" ] && USERS=$(w --no-header | awk '{ print $1 }' | sort | uniq)
|
| 44 |
|
| 45 |
|
| 46 |
if [ "$1" = "autoconf" ]; then |
| 47 |
if [ -n "$USERS" ]; then |
| 48 |
echo "yes" |
| 49 |
else |
| 50 |
echo "no (USERS setting is missing)" |
| 51 |
fi |
| 52 |
exit |
| 53 |
fi |
| 54 |
|
| 55 |
if [ "$1" = "config" ]; then |
| 56 |
echo "graph_args --base 1000 -r --lower-limit 0" |
| 57 |
echo "graph_title CPU usage, by user" |
| 58 |
echo "graph_category system" |
| 59 |
echo "graph_info This graph shows CPU usage, for monitored users." |
| 60 |
echo "graph_vlabel %" |
| 61 |
echo "graph_scale no" |
| 62 |
echo "graph_period second" |
| 63 |
user_fields="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)" |
| 64 |
echo "graph_order $user_fields $OTHER_FIELD" |
| 65 |
for user in $USERS "$OTHER_FIELD"; do |
| 66 |
user_field="$(clean_fieldname "$user")" |
| 67 |
echo "${user_field}.label $user"
|
| 68 |
echo "${user_field}.info CPU used by user $user"
|
| 69 |
echo "${user_field}.type GAUGE"
|
| 70 |
echo "${user_field}.draw AREASTACK"
|
| 71 |
done |
| 72 |
exit |
| 73 |
fi |
| 74 |
|
| 75 |
top -b -n 1 | sed '1,/^ *PID /d' | \ |
| 76 |
awk -v USERS="$USERS" ' |
| 77 |
# Store the CPU usage of each process - the mapping to the |
| 78 |
# user happens later. We cannot use the second column |
| 79 |
# (username) directly, since it may be abbreviated (ending |
| 80 |
# with "+"). |
| 81 |
{ CPU_PER_PID[$1]=$9 }
|
| 82 |
END {
|
| 83 |
split(USERS, user_array) |
| 84 |
for (user_index in user_array) {
|
| 85 |
user = user_array[user_index] |
| 86 |
# retrieve all process IDs belonging to the user |
| 87 |
"ps -u "user" -o pid --no-headers 2>/dev/null | tr \"\n\" \" \"" | getline pids |
| 88 |
user_cpu = 0 |
| 89 |
split(pids, pid_array) |
| 90 |
# summarize the cpu usage of this usage |
| 91 |
for (pid_index in pid_array) {
|
| 92 |
pid = pid_array[pid_index] |
| 93 |
user_cpu += CPU_PER_PID[pid] |
| 94 |
delete CPU_PER_PID[pid] |
| 95 |
} |
| 96 |
print user, user_cpu |
| 97 |
} |
| 98 |
# add all remaining cpu usages into "others" |
| 99 |
others_sum = 0 |
| 100 |
for (other_usage in CPU_PER_PID) others_sum+=CPU_PER_PID[other_usage] |
| 101 |
print "'"$OTHER_FIELD"'", others_sum; |
| 102 |
}' | while read -r user count; do |
| 103 |
# apply fieldname cleanup |
| 104 |
echo "$(clean_fieldname "$user").value $count" |
| 105 |
done |
