Révision 5ff3522e
if1sec_: implement autostart, be more like if_
- look for already running acquire process, or start one
- use CDEF to return bits/second (like if_)
- increase lifetime to 450 days (like if_)
- changed category to plain "network" (like if_)
- report max interface speed (like if_)
- small performance improvement: don't fork two cat(1) and one date(1) every second
(this roughly halves the CPU time used on my system)
| plugins/network/if1sec_ | ||
|---|---|---|
| 1 | 1 |
#! /bin/sh |
| 2 |
# Currently the plugin does *not* autostart |
|
| 3 |
# |
|
| 4 |
# It has to be launched via rc.d : |
|
| 5 |
# munin-run if1sec_eth0 acquire |
|
| 6 | 2 |
|
| 7 |
pluginfull="$0" # full name of plugin
|
|
| 8 |
plugin="${0##*/}" # name of plugin
|
|
| 3 |
pluginfull="$0" # full name of plugin
|
|
| 4 |
plugin="${0##*/}" # name of plugin
|
|
| 9 | 5 |
pidfile="$MUNIN_PLUGSTATE/munin.$plugin.pid" |
| 10 | 6 |
cache="$MUNIN_PLUGSTATE/munin.$plugin.value" |
| 11 | 7 |
|
| 12 |
IFACE="${0##*/if1sec_}" # interface
|
|
| 8 |
IFACE="${0##*/if1sec_}" # interface
|
|
| 13 | 9 |
|
| 14 | 10 |
if [ ! -r "/sys/class/net/$IFACE/statistics/tx_bytes" ] |
| 15 | 11 |
then |
| ... | ... | |
| 19 | 15 |
|
| 20 | 16 |
if [ "$1" = "acquire" ] |
| 21 | 17 |
then |
| 22 |
(
|
|
| 23 |
while sleep 1
|
|
| 24 |
do
|
|
| 25 |
echo $(
|
|
| 26 |
date +%s
|
|
| 27 |
cat /sys/class/net/$IFACE/statistics/tx_bytes
|
|
| 28 |
cat /sys/class/net/$IFACE/statistics/rx_bytes
|
|
| 29 |
)
|
|
| 30 |
done | awk "{
|
|
| 31 |
print \"${IFACE}_tx.value \" \$1 \":\" \$2;
|
|
| 32 |
print \"${IFACE}_rx.value \" \$1 \":\" \$3;
|
|
| 33 |
system(\"\");
|
|
| 34 |
}" >> $cache
|
|
| 35 |
) &
|
|
| 36 |
echo $! > $pidfile
|
|
| 37 |
exit 0
|
|
| 18 |
(
|
|
| 19 |
exec <&- >&- 2>&-
|
|
| 20 |
while sleep 1
|
|
| 21 |
do
|
|
| 22 |
read tx < /sys/class/net/$IFACE/statistics/tx_bytes
|
|
| 23 |
read rx < /sys/class/net/$IFACE/statistics/rx_bytes
|
|
| 24 |
echo "$tx $rx"
|
|
| 25 |
done | awk "{
|
|
| 26 |
now = systime()
|
|
| 27 |
print \"${IFACE}_tx.value \" now \":\" \$1
|
|
| 28 |
print \"${IFACE}_rx.value \" now \":\" \$2
|
|
| 29 |
system(\"\") |
|
| 30 |
}" >> $cache
|
|
| 31 |
) &
|
|
| 32 |
echo $! > $pidfile
|
|
| 33 |
exit 0
|
|
| 38 | 34 |
fi |
| 39 | 35 |
|
| 40 | 36 |
|
| 41 | 37 |
if [ "$1" = "config" ] |
| 42 | 38 |
then |
| 43 |
cat <<EOF
|
|
| 39 |
cat <<EOF
|
|
| 44 | 40 |
graph_title Interface 1sec stats for ${IFACE}
|
| 45 |
graph_category 1sec::network
|
|
| 46 |
graph_data_size custom 1d, 10s for 1w, 1m for 1t, 5m for 1y
|
|
| 47 |
graph_vlabel Bytes
|
|
| 41 |
graph_category network |
|
| 42 |
graph_data_size custom 1d, 10s for 1w, 1m for 1t, 5m for 450d
|
|
| 43 |
graph_vlabel bits/sec
|
|
| 48 | 44 |
update_rate 1 |
| 49 |
${IFACE}_tx.label ${IFACE} TX bytes
|
|
| 45 |
${IFACE}_tx.label ${IFACE} TX bits
|
|
| 46 |
${IFACE}_tx.cdef ${IFACE}_tx,8,*
|
|
| 50 | 47 |
${IFACE}_tx.type DERIVE
|
| 51 | 48 |
${IFACE}_tx.min 0
|
| 52 |
${IFACE}_rx.label ${IFACE} RX bytes
|
|
| 49 |
${IFACE}_rx.label ${IFACE} RX bits
|
|
| 50 |
${IFACE}_rx.cdef ${IFACE}_rx,8,*
|
|
| 53 | 51 |
${IFACE}_rx.type DERIVE
|
| 54 | 52 |
${IFACE}_rx.min 0
|
| 55 | 53 |
EOF |
| 56 |
exit 0 |
|
| 54 |
# If max speed is easily available, report it. More complex |
|
| 55 |
# code could be copied from "if_". |
|
| 56 |
if [ -r /sys/class/net/$IFACE/speed ] |
|
| 57 |
then |
|
| 58 |
SPEED=$(cat /sys/class/net/$IFACE/speed) |
|
| 59 |
echo "${IFACE}_rx.max" $((SPEED / 8 * 1000000))
|
|
| 60 |
echo "${IFACE}_tx.max" $((SPEED / 8 * 1000000))
|
|
| 61 |
echo "${IFACE}_rx.info Received traffic on the $IFACE interface. Maximum speed is ${SPEED} Mbps."
|
|
| 62 |
echo "${IFACE}_tx.info Transmitted traffic on the $IFACE interface. Maximum speed ${SPEED} Mbps."
|
|
| 63 |
fi |
|
| 64 |
exit 0 |
|
| 57 | 65 |
fi |
| 58 | 66 |
|
| 59 |
# values |
|
| 67 |
# Autorun logic |
|
| 68 |
running=true |
|
| 69 |
if [ ! -e ${cache} ]
|
|
| 70 |
then |
|
| 71 |
# no cache file |
|
| 72 |
running=false |
|
| 73 |
elif [ ! -s ${cache} ]
|
|
| 74 |
then |
|
| 75 |
# empty cache file |
|
| 76 |
if [ -s ${pidfile} ]
|
|
| 77 |
then |
|
| 78 |
# kill -0 will return success if this user is allowed to send |
|
| 79 |
# a signal to the process. This means the PID exists and |
|
| 80 |
# belongs to the plugin user, which should have few false |
|
| 81 |
# positives. |
|
| 82 |
if ! kill -0 $(cat ${pidfile})
|
|
| 83 |
then |
|
| 84 |
running=false |
|
| 85 |
fi |
|
| 86 |
else |
|
| 87 |
running=false |
|
| 88 |
fi |
|
| 89 |
fi |
|
| 90 |
if ! $running |
|
| 91 |
then |
|
| 92 |
$0 acquire & |
|
| 93 |
# wait a little so we have some data to report |
|
| 94 |
sleep 3 |
|
| 95 |
fi |
|
| 96 |
|
|
| 97 |
# Finally: Print collected values, truncate to mark spot. |
|
| 60 | 98 |
cat ${cache}
|
| 61 | 99 |
> ${cache}
|
| 62 | 100 |
|
Formats disponibles : Unified diff