root / plugins / system / cpubyuser @ 4b400a73
Historique | Voir | Annoter | Télécharger (3,03 ko)
| 1 | 2db4f2ca | Lars Kruse | #!/bin/sh |
|---|---|---|---|
| 2 | 3bd553cd | fireba11 | # |
| 3 | # Plugin to monitor CPU usage, for a selected set of users |
||
| 4 | # |
||
| 5 | 85d91b50 | Lars Kruse | # 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 | 3bd553cd | fireba11 | # [cpubyuser] |
| 8 | # env.USERS root yann |
||
| 9 | # |
||
| 10 | 85d91b50 | Lars Kruse | # If env.USERS is set to ALL, count all logged in users. |
| 11 | 19ca132a | rkarlsba | # |
| 12 | 3bd553cd | fireba11 | # 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 | 85d91b50 | Lars Kruse | # config (required) |
| 34 | # autoconf (optional - used by munin-config) |
||
| 35 | 3bd553cd | fireba11 | # |
| 36 | |||
| 37 | #%# family=auto |
||
| 38 | #%# capabilities=autoconf |
||
| 39 | |||
| 40 | 78f4e42d | Lars Kruse | . "$MUNIN_LIBDIR/plugins/plugin.sh" |
| 41 | |||
| 42 | 391a4bfa | Lars Kruse | OTHER_FIELD="others" |
| 43 | 4b21d581 | Lars Kruse | [ "$USERS" = "ALL" ] && USERS=$(w --no-header | awk '{ print $1 }' | sort | uniq)
|
| 44 | |||
| 45 | |||
| 46 | 3bd553cd | fireba11 | if [ "$1" = "autoconf" ]; then |
| 47 | if [ -n "$USERS" ]; then |
||
| 48 | echo "yes" |
||
| 49 | else |
||
| 50 | 6f7473b2 | Lars Kruse | echo "no (USERS setting is missing)" |
| 51 | 3bd553cd | fireba11 | 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 | 002e8bbf | Lars Kruse | user_fields="$(for user in $USERS; do clean_fieldname "$user" | tr '\n' ' '; done)" |
| 64 | 391a4bfa | Lars Kruse | echo "graph_order $user_fields $OTHER_FIELD" |
| 65 | for user in $USERS "$OTHER_FIELD"; do |
||
| 66 | 85d91b50 | Lars Kruse | user_field="$(clean_fieldname "$user")" |
| 67 | 002e8bbf | Lars Kruse | 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 | 3bd553cd | fireba11 | done |
| 72 | exit |
||
| 73 | fi |
||
| 74 | |||
| 75 | 5bec8ec6 | Lars Kruse | top -b -n 1 | sed '1,/^ *PID /d' | \ |
| 76 | 3bd553cd | fireba11 | awk -v USERS="$USERS" ' |
| 77 | 8280dbb0 | Lars Kruse | # 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 | 3bd553cd | fireba11 | END {
|
| 83 | 8280dbb0 | Lars Kruse | 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 | 3bd553cd | fireba11 | } |
| 98 | 8280dbb0 | Lars Kruse | # 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 |
