#!/bin/sh
#
# Wildcard-plugin to monitor network interfaces. To monitor an
# interface, link if_err_<interface> to this file. E.g.
#
#    ln -s /usr/share/munin/node/plugins-auto/if_err_ /etc/munin/node.d/if_err_eth0
#
# ...will monitor eth0.
#
# Any devince 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.
#
# Magic markers (optional - used by munin-config and some installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf suggest

INTERFACE=`basename $0 | sed 's/^if_err_//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 rcvd trans" 
	echo "graph_title $INTERFACE errors"
	echo 'graph_args --base 1000'
	echo 'graph_vlabel packets in (-) / out (+) per ${graph_period}'
	echo 'graph_category network'
	echo "graph_info This graph shows the amount of errors on the $INTERFACE network interface."
	echo 'rcvd.label packets'
        echo 'rcvd.type COUNTER'
        echo 'rcvd.graph no'
        echo 'rcvd.warning 1'
        echo 'trans.label packets'
	echo 'trans.type COUNTER'
	echo 'trans.negative rcvd'
        echo 'trans.warning 1'
	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 "rcvd.value " $3 "\ntrans.value " $11 \
    }' \
    /proc/net/dev
