#!/bin/sh
#
# Wildcard-plugin to monitor network interfaces. To monitor an
# interface, link if_<interface> to this file. E.g.
#
#    ln -s /usr/share/node/node/plugins-auto/if_ /etc/munin/node.d/if_eth0
#
# ...will monitor eth0.
#
# Any device found in /proc/net/dev can be monitored. Examples include
# ipsec*, eth*, irda* and lo. Please note that aliases cannot be
# monitored with this plugin.
#
# $Log$
# Revision 1.8.2.8  2005/03/07 19:23:07  jimmyo
# Made linux/if_ work with more versions of iwlist (SF#1150954).
#
# Revision 1.8.2.7  2005/02/17 11:35:09  jimmyo
# Typo fixes.
#
# Revision 1.8.2.6  2005/02/16 21:45:48  jimmyo
# Modified warning note in linux/if_ output.
#
# Revision 1.8.2.5  2005/02/16 21:27:33  jimmyo
# Added warning note in linux/if_ output.
#
# Revision 1.8.2.4  2005/02/16 20:59:59  jimmyo
# Make suggest a tad less trigger happy.
#
# Revision 1.8.2.3  2005/02/16 20:31:03  jimmyo
# Minor bugfix in linux/if_, with info fields.
#
# Revision 1.8.2.2  2005/02/16 17:08:10  jimmyo
# linux/if* now treats ra* interfaces as wireless.
#
# Revision 1.8.2.1  2005/01/29 21:17:12  jimmyo
# Added madwifi support to linux/if_* plugins.
#
# Revision 1.8  2004/12/10 10:47:49  jimmyo
# Change name from ${scale} to ${graph_period}, to be more consistent.
#
# Revision 1.7  2004/12/09 22:12:56  jimmyo
# Added "graph_period" option, to make "graph_sums" usable.
#
# Revision 1.6  2004/11/12 20:08:01  ilmari
# Fixed linux/if_(err_) braindamage affecting hosts with vlans or
# multi-digit interface numbers.
#
# Revision 1.5  2004/09/26 22:28:42  jimmyo
# Suggest wlan interfaces as well as eth interfaces.
#
# Revision 1.4  2004/09/25 22:29:16  jimmyo
# Added info fields to a bunch of plugins.
#
# Revision 1.3  2004/09/12 20:54:24  jimmyo
# Plugin now sets max value.
#
# Revision 1.2  2004/05/20 13:57:12  jimmyo
# Set categories to some of the plugins.
#
# Revision 1.1  2004/01/02 18:50:01  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 [ -r /proc/net/dev ]; then
		echo yes
		exit 0
	else
		echo "no (/proc/net/dev not found)"
		exit 1
	fi
fi

if [ "$1" = "suggest" ]; then
	if [ -r /proc/net/dev ]; then
		egrep '^ *(eth|wlan|ath|ra)[0-9]+:' /proc/net/dev | cut -f1 -d: | sed 's/ //g'
		exit 0
	else
		exit 1
	fi
fi

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

	echo "graph_order down up" 
	echo "graph_title $INTERFACE traffic"
	echo 'graph_args --base 1000'
	echo 'graph_vlabel bits in (-) / out (+) per ${graph_period}'
	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. To avoid this problem, use the ip_ plugin instead."
	echo 'down.label received'
        echo 'down.type COUNTER'
        echo 'down.graph no'
        echo 'down.cdef down,8,*'
        echo 'up.label bps'
	echo 'up.type COUNTER'
	echo 'up.negative down'
	echo 'up.cdef up,8,*'
	case "$INTERFACE" in
		ath*|wlan*|ra*)
			echo -n "up.info Traffic of the $INTERFACE interface. Maximum speed is "
			which iwlist >/dev/null 2>/dev/null || echo "undeterminable (please install iwlist)."
			iwlist $INTERFACE rate 2>/dev/null | awk '/Current Bit Rate/ { split ($0, arr, "[=:]"); split (arr[2], arr2, "M"); print (arr2[1]*1000000) " bits per second.\nup.max " (arr2[1]*1000000) "\ndown.max "(arr2[1]*1000000); }'
			;;
		*)
			echo -n "up.info Traffic of the $INTERFACE interface. Maximum speed is "
			which ethtool >/dev/null 2>/dev/null || echo "undeterminable (please install ethtool)."
			ethtool $INTERFACE 2>/dev/null | awk '/Speed/ { split ($2, arr2, "M"); print (arr2[1]*1000000) " bits per second.\nup.max " (arr2[1]*1000000) "\ndown.max "(arr2[1]*1000000); }'
			;;
	esac
	exit 0
fi;

# Escape dots in the interface name (eg. vlans) before using it as a regex
awk -v interface="$INTERFACE" \
    'BEGIN { gsub(/\./, "\\.", interface) } \
    $1 ~ "^" interface ":" {
        split($0, a, /: */); $0 = a[2]; \
        print "down.value " $1 "\nup.value " $9 \
    }' \
    /proc/net/dev

