#!/usr/bin/sh
#
# netstat_rate_tcp_ - Munin wildcard plug-in to parse netstat TCP counters
#
# Contributed by <ralph dot grothe at itdz minus berlin dot de>
#
#%# family=contrib
#%# capabilities=autoconf suggest

proto=$(basename $0|cut -d_ -f3)
type=${0##*_}

if [[ $1 = autoconf ]]; then
    if uname -s|grep -qEi hp-?ux; then
	echo yes
	exit 0
    else
	echo "no (This plugin only parses output of HP-UX netstat command)"
	exit 0
    fi
fi
[[ $1 = suggest ]] && printf "socket\nthroughput\n" && exit 0

set -A SOCKET_LABELS requests accepts established closed

if [[ $1 = config ]]; then
    echo "graph_title netstat $proto $type rate"
    echo "graph_category network"
    echo "graph_args --base 1000"
    case $type in
	socket)
	    echo "graph_vlabel connections per \${graph_period}"
	    for tag in ${SOCKET_LABELS[*]}; do
		printf "%s.label %s\n%s.type COUNTER\n" $tag $tag $tag
	    done
	    ;;
	throughput)
	    echo "graph_vlabel packets per \${graph_period}"
	    echo "ingress.label packets received"
	    echo "ingress.type COUNTER"
	    echo "ingress.graph no"
	    echo "egress.label packets sent"
	    echo "egress.type COUNTER"
	    echo "egress.negative ingress"
	    ;;
    esac
    exit 0
fi

case $type in
    socket)
	typeset -i i=-1
	netstat -s -p tcp \
	    |awk '/connections? (requests|accepts|established|closed)/{print$1}' \
    		|while read count; do
		    printf "%s.value " ${SOCKET_LABELS[((i+=1))]}
		    echo $count
    		done
	;;
    throughput)
	set -- $(netstat -s -p tcp|awk '/packets (sent|received)$/{print$1}')
	echo "ingress.value $2"
	echo "egress.value $1"
	;;
esac
