root / plugins / system / cpubyuser @ 391a4bfa
Historique | Voir | Annoter | Télécharger (2,37 ko)
| 1 |
#!/bin/bash |
|---|---|
| 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 | tail -n +8 | \ |
| 76 |
awk -v USERS="$USERS" ' |
| 77 |
{ if ($2 != "USER") CPU_USER[$2]+=$9 }
|
| 78 |
END {
|
| 79 |
others_sum = 0 |
| 80 |
for (user in CPU_USER) {
|
| 81 |
m = match(USERS,user) |
| 82 |
if (m != 0) {
|
| 83 |
_user=user |
| 84 |
gsub(/[-.]/,"_", _user); |
| 85 |
print _user".value", CPU_USER[user] |
| 86 |
} else |
| 87 |
others_sum += CPU_USER[user] |
| 88 |
} |
| 89 |
print "others.value", others_sum; |
| 90 |
}' |
