#!/bin/sh
#
# $OpenBSD: if_pps_,v 1.2 2010/02/25 17:07:17 sthen Exp $
#
# (c) 2009-2010 Michael Knudsen <mk@openbsd.org>
#
# Wildcard-plugin to monitor packet rates. To monitor an
# interface, link if_pps_<interface> to this file. E.g.
#
#  ln -s /usr/local/libexec/munin/plugins/if_pps_ \
#     /etc/munin/plugins/if_pps_bge0
#
# ...will monitor bge0.
#
# Any device found using netstat -i 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_pps_//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 -n | sed -n -e '/^faith/d' -e '/^lo[0-9]/d' -e '/^pflog/d' -e '/<Link>/s/\** .*//p'
		exit 0
	else
		exit 1
	fi
fi

if [ "$1" = "config" ]; then
	echo "graph_order rpkt opkt"
	echo "graph_title $INTERFACE packets"
	echo 'graph_args --base 1000'
	echo 'graph_vlabel packets/${graph_period} in (-) out (+)'
	echo 'graph_category network'
	echo "graph_info This graph shows the traffic of the $INTERFACE network interface."
	echo 'rpkt.label received'
	echo 'rpkt.type DERIVE'
	echo 'rpkt.min 0'
	echo 'rpkt.graph no'
	echo 'opkt.label pps'
	echo 'opkt.type DERIVE'
	echo 'opkt.min 0'
	echo 'opkt.negative rpkt'
	echo "opkt.info Packets sent (+) and received (-)."
	exit 0
fi;

/usr/bin/netstat -i -n -I $INTERFACE | awk '
/<Link>/ {
	if (NF == 8) {
		print "rpkt.value", $4;
		print "opkt.value", $6;
	} else {
		print "rpkt.value", $5;
		print "opkt.value", $7;
	}
}'
