#!/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/plugins-auto/if_errcoll_ /etc/munin/node.d/if_errcoll_eth0
#
# ...will monitor eth0.
#
# Any device found in /usr/bin/kstat can be monitored.
#
# Magic markers (optional - used by munin-config and some installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf suggest


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

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

if [ "$1" = "suggest" ]; then
	if [ -x /usr/bin/kstat ]; then
		kstat -p -s '/^ierrors$/' | awk -F: '{ print $3 }'
		exit 0
	else
		exit 1
	fi
fi

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

	echo "graph_order ierrors oerrors collisions"
	echo "graph_title $INTERFACE Errors & Collisions"
	echo 'graph_args --base 1000'
	echo 'graph_vlabel events / ${graph_period}'
	echo 'graph_category network'
	echo 'ierrors.label Input Errors'
        echo 'ierrors.type COUNTER'
        echo 'ierrors.max 2000000000'
        echo 'oerrors.label Output Errors'
	echo 'oerrors.type COUNTER'
        echo 'oerrors.max 2000000000'
        echo 'collisions.label Collisions'
	echo 'collisions.type COUNTER'
        echo 'collisions.max 2000000000'
	exit 0
fi;

kstat -p -n $INTERFACE -s '/^([io]errors|collisions)$/' | awk -F: '
{
	split($4, four, "\t")
	print four[1] ".value", four[2]
}'
