#!/bin/sh
#
# Wildcard-plugin to monitor network interfaces. To monitor an
# interface, link if_<interface> to this file. E.g.
#
#    ln -s /usr/share/munin/node/plugins-auto/if_ /etc/munin/node.d/if_eth0
#
# ...will monitor eth0.
#
# Any device found in /usr/bin/netstat can be monitored.
#
# $Log$
# Revision 1.4.2.3  2005/02/17 10:57:54  lupe
# Added warning note in if_ output.
#
# Revision 1.4.2.2  2005/01/28 16:24:22  lupe
# Minor corrections
#
# Revision 1.4.2.1  2005/01/28 14:51:22  lupe
# Add graph_info and some filed.info
#
# Revision 1.4  2004/12/10 10:47:49  jimmyo
# Change name from ${scale} to ${graph_period}, to be more consistent.
#
# Revision 1.3  2004/12/09 22:12:55  jimmyo
# Added "graph_period" option, to make "graph_sums" usable.
#
# Revision 1.2  2004/05/20 19:02:36  jimmyo
# Set categories on a bunch of plugins
#
# Revision 1.1  2004/01/02 18:50:00  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.3  2003/11/07 22:12:50  jimmyo
# Changed deprecated plugin options
#
# Revision 1.2  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
#
# Magic markers (optional - used by munin-config and some installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf suggest


INTERFACE=`basename $0 | sed 's/^if_//g'`

if [ "$1" = "autoconf" ]; then
	if [ -x /usr/bin/netstat ]; then
		echo yes
		exit 0
	else
		echo "no (/usr/bin/netstat not found)"
		exit 1
	fi
fi

if [ "$1" = "suggest" ]; then
	if [ -x /usr/bin/netstat ]; then
		netstat -i -b | sed -n -e '/^faith/d' -e '/^lo[0-9]/d' -e '/^pflog/d' -e '/<Link#[0-9]*>/s/\** .*//p'
		exit 0
	else
		exit 1
	fi
fi

if [ "$1" = "config" ]; then

	echo "graph_order rbytes obytes" 
	echo "graph_title $INTERFACE traffic"
	echo 'graph_args --base 1000'
	echo 'graph_vlabel bits per ${graph_period} in (-) / out (+)'
	echo 'graph_category network'
	echo "graph_info This graph shows the traffic of the $INTERFACE network interface. Please note that the traffic is shown in bits per second, not bytes. IMPORTANT: Since the data source for this plugin use 32bit counters, this plugin is really unreliable and unsuitable for most 100Mb (or faster) interfaces, where bursts are expected to exceed 50Mbps. This means that this plugin is usuitable for most production environments."
	echo 'rbytes.label received'
        echo 'rbytes.type COUNTER'
        echo 'rbytes.graph no'
        echo 'rbytes.cdef rbytes,8,*'
        echo 'obytes.label bps'
	echo 'obytes.type COUNTER'
	echo 'obytes.negative rbytes'
	echo 'obytes.cdef obytes,8,*'
	echo "obytes.info Traffic sent (+) and received (-) on the $INTERFACE network interface."
	exit 0
fi;

/usr/bin/netstat -i -b -I $INTERFACE | awk '
/<Link#[0-9]*>/ {
	if (NF == 10) { 
		print "rbytes.value", $6;
		print "obytes.value", $9;
	} else {
		print "rbytes.value", $7;
		print "obytes.value", $10;
	}
}'
