#!/bin/sh
#
# Plugin to monitor network connections.
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - only used by munin-config)
#
#
# $Log$
# Revision 1.5  2004/12/09 18:00:00  jimmyo
# linux/fw_forwarded_local now initialises properly (Deb#284673).
#
# Revision 1.4  2004/10/27 17:50:01  jimmyo
# Fixed typo in linux/fw_forwarded_local (Deb#275535).
#
# Revision 1.3  2004/05/20 19:02:37  jimmyo
# Set categories on a bunch of plugins
#
# Revision 1.2  2004/05/15 21:33:29  jimmyo
# "Upped" som plugins from contrib/manual to manual or auto.
#
# Revision 1.1  2004/05/09 21:20:31  jimmyo
# Added fw_forwarded_local (by Xavier).
#
#
# Magic markers (optional - used by munin-config and some installation
# scripts):
#%# family=manual
#%# capabilities=autoconf



if [ "$1" = "autoconf" ]; then
	if ( cat /proc/net/ip_conntrack 2>/dev/null >/dev/null ); then
		echo yes
		exit 0
	else
		if [ $? -eq 127 ]
		then
			echo "no (ipconntrack not found)"
			exit 1
		else
			echo no
			exit 1
		fi
	fi
fi

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

	echo 'graph_title ipconntrack'
	echo 'graph_args -l 0 --base 1000'
	echo 'graph_vlabel established connections'
	echo 'graph_category network'
	echo 'forward.label forward'
	echo 'forward.type GAUGE'
	echo 'forward.max 500'''
	echo 'local.label local'
	echo 'local.type GAUGE'
	echo 'local.max 500'''
	exit 0
fi

cat /proc/net/ip_conntrack | grep ESTABLISHED | sed 's/.*src=\(.*\) .*dst=\(.*\) sport.*src=\(.*\) .*dst=\(.*\) spo.*/\1 \2 \3 \4/g' | awk 'BEGIN {{FORW=0} {LOCAL=0}} {if($1 != $4){ FORW += 1} else { LOCAL += 1}} END {{print "forward.value "FORW} {print "local.value "LOCAL}}'
