#!/bin/sh
# -*- sh -*-

: << =cut

=head1 NAME

if_err_ - Wildcard-plugin to monitor network interfaces

=head1 CONFIGURATION

This is a wildcard plugin.  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.

This plugin does not use environment variables.

=head1 USAGE

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.

=head1 AUTHOR

Unknown author

=head1 LICENSE

Unknown license

=head1 MAGIC MARKERS

 #%# family=auto
 #%# capabilities=autoconf suggest

=head1 VERSION

 $Id: if_err_.in 2634 2009-10-21 01:05:28Z janl $

=cut

. $MUNIN_LIBDIR/plugins/plugin.sh

INTERFACE=${0##*/if_err_}

if [ "$1" = "autoconf" ]; then
	if [ -r /proc/net/dev ]; then
		echo yes
		exit 0
	else
		echo "no (/proc/net/dev not found)"
		exit 0
	fi
fi

if [ "$1" = "suggest" ]; then
    if [ -r /proc/net/dev ]; then
	awk '
            /^ *(eth|tap|bond|wlan|ath|ra|sw|vlan|venet|veth|msh)[0-9]+(\.[0-9]+)?:/ {
                 split($0, a, /: */);
                 gsub(/^ +/,"",a[1]);
                 if (($2 > 0) || ($10 > 0)) print a[1]; }' /proc/net/dev
    fi
    exit 0
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'
	print_warning rcvd
	print_critical rcvd
	print_warning trans
	print_critical trans
	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
