2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00
contrib-munin/plugins/other/network-usage

145 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
# Now updated to support 32-bit operating systems
# Requires bash
: << =cut
bandwidth_ - Wildcard-plugin to monitor total network traffic and
predict 30 day bandwidth usage
=head1 CONFIGURATION
This is a wildcard plugin. To monitor an interface, link
bandwidth_<interface> to this file. E.g.
ln -s /usr/share/munin/node/plugins-auto/bandwidth_ \
/etc/munin/node.d/bandwidth_eth0
...will monitor eth0
Most likely usage is to monitor an interface connected to your ISP.
=head1 USAGE
Any device found in /proc/net/dev can be monitored. Examples include ipsec*,
eth*, irda* and lo.
Please not that aliases cannot be monitored with this plugin.
=head1 VERSION
$Id: bandwidth_,v 1.2 2011/09/16 17:54:50 root Exp $
=head1 AUTHOR
Sean Whitney
=head1 LICENSE
GPLv2
=head1 MAGIC MARKERS
#%# family=contrib
#%# capabilities=autoconf suggest
=head1 BUGS
I know that bandwidth is bits and base10 as opposed to bytes and base2.
However the purpose of this plugin it to monitor your monthly bandwidth
consumption to make sure you don't go over your ISP's peak. ISP's seem
to be interested in expressing peaks in bytes....
=cut
# Change to show your outside interface
INTERFACE=${0##*bandwidth_}
HISTORY="/var/lib/munin/plugin-state/bandwidth_$INTERFACE.state"
TOTALUPTIME=0
TOTALINPUT=0
TOTALOUTPUT=0
OLDUPTIME=0
OLDINPUT=0
OLDOUTPUT=0
test -f $HISTORY || touch $HISTORY
BC=$(which bc)
test ${#BC} || (echo "bc not found, please install bc" && exit 1)
case $1 in
# I know that "bandwidth is bits and base10 as opposed to bytes and base2.
# However the purpose of this plugin it to monitor your monthly bandwidth
# consumption to make sure you don't go over your ISP's peak. ISP's seem
# to be interested in expressing peaks in bytes....
config)
cat <<'EOM'
graph_title Monthly Bandwidth average
graph_vlabel Bytes
average.label current average
monthly.label monthly projection
average.info Your average bandwidth usage based on uptime
monthly.info Your projected monthly bandwidth usage based on uptime
graph_category network
graph_args --base 1024 -l 0
graph_info This graph show your current average bandwidth usage and projected 30 day average based on your current consumption
average.warning 8944269393.92
monthly.warning 268435456000
EOM
exit 0;;
esac
source $HISTORY
INPUT=$(ifconfig $INTERFACE|grep bytes|awk '{print $2}'|sed s/bytes://g)
OUTPUT=$(ifconfig $INTERFACE|grep bytes|awk '{print $6}'|sed s/bytes://g)
UPTIME=$(cat /proc/uptime | cut -d'.' -f1-1)
# Dealing with a reboot. I'm not worrying about a uptime rollover.
# Evidently uptime is 64-bit even on 32-bit machines using "jiffies".
if [[ $OLDUPTIME -gt $UPTIME ]]; then
TOTALUPTIME=$(($TOTALUPTIME+$UPTIME))
TOTALINPUT=$(($TOTALINPUT+$INPUT))
TOTALOUTPUT=$(($TOTALOUTPUT+$OUTPUT))
# Dealing with a 32-bit counter rollover. This is detected by comparing
# The last value with the new value and the uptime. Else update normally
else
TOTALUPTIME=$(($TOTALUPTIME+$UPTIME-$OLDUPTIME))
if [[ $OLDINPUT -gt $INPUT ]]; then
TOTALINPUT=$(($TOTALINPUT+$INPUT))
else
TOTALINPUT=$((TOTALINPUT+$INPUT-$OLDINPUT))
fi
if [[ $OLDOUTPUT -gt $OUTPUT ]]; then
TOTALOUTPUT=$((TOTALOUTPUT+$OUTPUT))
else
TOTALOUTPUT=$((TOTALOUTPUT+$OUTPUT-$OLDOUTPUT))
fi
fi
TOTAL=$(($TOTALINPUT+$TOTALOUTPUT))
UPV=$(echo "scale=3;$TOTALUPTIME/60/60/24"|bc)
DAILY=$(echo "scale=3;$TOTAL/$UPV"|bc)
MONTHLY=$(echo "scale=3;$DAILY*30"|bc)
echo "TOTALUPTIME=$TOTALUPTIME" > $HISTORY
echo "OLDUPTIME=$UPTIME" >> $HISTORY
echo "TOTALINPUT=$TOTALINPUT" >> $HISTORY
echo "OLDINPUT=$INPUT" >> $HISTORY
echo "TOTALOUTPUT=$TOTALOUTPUT" >> $HISTORY
echo "OLDOUTPUT=$OUTPUT" >> $HISTORY
echo "average.value $DAILY"
echo "monthly.value $MONTHLY"