root / plugins / cpu / multicpu1sec @ 17f78427
Historique | Voir | Annoter | Télécharger (2 ko)
| 1 |
#!/bin/bash |
|---|---|
| 2 |
# (c) 2012 - Bushmills |
| 3 |
# License : GPLv2 |
| 4 |
|
| 5 |
#%# family=auto |
| 6 |
#%# capabilities=autoconf |
| 7 |
|
| 8 |
|
| 9 |
interval=1 # mpstat sampling interval |
| 10 |
timeout=1200 # 20 minutes daemon watchdog timeout |
| 11 |
watchdog=60 # test for timeout every $watchdog seconds |
| 12 |
pluginfull="$0" # full name of plugin |
| 13 |
plugin="${0##*/}" # name of plugin
|
| 14 |
pidfile="$MUNIN_PLUGSTATE/munin.$plugin.pid" |
| 15 |
cache="$MUNIN_PLUGSTATE/munin.$plugin.value" |
| 16 |
graph="$plugin" |
| 17 |
style="AREA" |
| 18 |
cpus=$(grep -c ^processor /proc/cpuinfo) |
| 19 |
|
| 20 |
run_watchdog() { # should also trap kill and term signals
|
| 21 |
kill -0 $(cat $pidfile) 2> /dev/null || rm -f $pidfile |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
run_acquire() {
|
| 26 |
echo "$$" > $pidfile |
| 27 |
LANG=C mpstat -P ALL $interval | |
| 28 |
awk -v cpus=$cpus '$2>=0&&$2<99 {print $2, systime(), (100-$11)/cpus; system("");}' >> $cache
|
| 29 |
rm -f $pidfile $cache |
| 30 |
} |
| 31 |
|
| 32 |
run_daemon() {
|
| 33 |
run_watchdog |
| 34 |
if [ -f $pidfile ]; then |
| 35 |
touch $pidfile |
| 36 |
else |
| 37 |
$pluginfull acquire & |
| 38 |
fi |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
# -------------------------------------------------------------------------- |
| 43 |
|
| 44 |
run_autoconf() {
|
| 45 |
run=(yes no) |
| 46 |
type -t mpstat > /dev/null |
| 47 |
echo "${run[$?]}"
|
| 48 |
} |
| 49 |
|
| 50 |
run_config() {
|
| 51 |
run_daemon |
| 52 |
cat << EOF |
| 53 |
graph_title $graph |
| 54 |
graph_category 1sec |
| 55 |
graph_vlabel average cpu use % |
| 56 |
graph_scale no |
| 57 |
graph_total All CPUs |
| 58 |
update_rate 1 |
| 59 |
graph_data_size custom 1d, 10s for 1w, 1m for 1t, 5m for 1y |
| 60 |
EOF |
| 61 |
cpun=0 |
| 62 |
for ((i=0; i<$cpus; i++)); do |
| 63 |
cat << EOF |
| 64 |
cpu${cpun}.label CPU $cpun
|
| 65 |
cpu${cpun}.draw $style
|
| 66 |
cpu${cpun}.min 0
|
| 67 |
EOF |
| 68 |
style=STACK |
| 69 |
((cpun++)) |
| 70 |
done |
| 71 |
} |
| 72 |
|
| 73 |
run_fetch() {
|
| 74 |
run_daemon |
| 75 |
awk 'NF==3 {print "cpu" $1 ".value " $2 ":" $3}' $cache
|
| 76 |
> $cache |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
run_${1:-fetch}
|
| 81 |
exit 0 |
| 82 |
|
| 83 |
# export -f functionname export functionname to subshell, avoiding the need |
| 84 |
# for locating plugin for subshell calling, when process needs a different |
| 85 |
# pid. Instead, $SHELL -c functionname can be used. useful for calling |
| 86 |
# acquire which needs a different pid than watchdog, otherwise watchdog |
| 87 |
# could/will kill itself when expiring before the watched process is killed. |
| 88 |
# not a POSIX feature. |
