#!/bin/bash
#
# Copyright 2009 © Antoine Durand-Gasselin <adg@crans.org>
# License: BSD2
#
# # Parameters:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
# 	suggest  (optional - used by munin-config)
#

#. $MUNIN_LIBDIR/plugins/plugin.sh

myname="connexions"

name="${name-\<$myname\>}"
REGEX="${regex-\<$name\>}"
services=(http ssh smtp imap nfs ldap)


if [ "$1" = "autoconf" ]; then
	echo yes
	exit 0
fi

if [ "$1" = "suggest" ]; then
	exit 0
fi

if [ "$1" = "config" ]; then
    cat <<EOF
graph_title Number of $myname
graph_args --base 1000 --vertical-label connexions -l 0
graph_category network
in.label Connexions entrantes
in.draw AREA
in.colour c0c0ff
out.label Connexions sortantes
out.draw STACK
out.colour d0ffd0
`for dmn in ${services[@]}; do echo $dmn.label Connexions $dmn; done`
other.label Connexions diverses
EOF
exit 0
fi

ip=$(ifconfig | perl -ne 'print ${^MATCH} if /10\.231\.136\.\d+/p')
ips=${IP/10/}
TMP=`mktemp -t sockets.XXXXXX`
trap "rm $TMP* 2>/dev/null" 0

/sbin/ss > $TMP

for dmn in ${services[@]}; do
printf "$dmn.value "
grep "$ips:$dmn" $TMP | wc -l
done

printf "other.value "
perl -ne "print if ! /$ips:`echo ${services[@]} | sed 's/ /|/g'`/" $TMP | wc -l

printf "in.value "
perl -ne "print if ! /$ips:\d{5}/" $TMP | wc -l

printf "out.value "
perl -ne "print if /$ips:\d{5}/" $TMP | wc -l
