#!/bin/bash
#
# Munin plugin for HFSC Traffic Shaping Statistics UP/DOWN
#
# It shows the download and upload statistic graph of a used net bandwidth per a user.
#
# This plugin was tailored to the HFSC solution
# presented at http://www.elessar.one.pl/article_kernel2.6.php
#
# You can find the plugin description and the installation notes here:
# http://www.elessar.one.pl/article_munin.php
#
###
# Written by Rafal Rajs
# Date: 2007/06/19
# Email: elessar1@poczta.wp.pl
# WWW: http://www.elessar.one.pl
###

# path to the file with global defs
. /etc/scripts/globals

# imported from HFSC script
# set class numbers

N_CLASS_D_1=70
N_CLASS_D_2=100
N_CLASS_U_1=130
N_CLASS_U_2=160
SH_TMP="/etc/scripts/tmp1.txt"

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

        echo "graph_title HFSC Traffic Shaping Stats - UP/DOWN"
        echo 'graph_vlabel bytes DOWN(-)/UP(+) per ${graph_period}'
        echo 'graph_width 450'
	echo 'graph_category network'

	j=1

	while [ $j -le $L_USERS ]
	do
	        echo "${USERNAMES[${j}]}_down.label ${USERNAMES[${j}]}"
	        echo "${USERNAMES[${j}]}_down.type COUNTER"
	        echo "${USERNAMES[${j}]}_down.graph no"

		if [ $j == 1 ]; then
		        echo "${USERNAMES[${j}]}_down.draw AREA"
		else
		        echo "${USERNAMES[${j}]}_down.draw STACK"
		fi;

	        echo "${USERNAMES[${j}]}_down.info Stats for ${USERNAMES[${j}]} - ${USER_IP[${j}]}"
	        echo "${USERNAMES[${j}]}_down.min 0"
	        echo "${USERNAMES[${j}]}_down.max 130000"

	        echo "${USERNAMES[${j}]}_up.label ${USERNAMES[${j}]}"
	        echo "${USERNAMES[${j}]}_up.type COUNTER"
	        echo "${USERNAMES[${j}]}_up.negative ${USERNAMES[${j}]}_down"

		if [ $j == 1 ]; then
		        echo "${USERNAMES[${j}]}_up.draw AREA"
		else
		        echo "${USERNAMES[${j}]}_up.draw STACK"
		fi;

	        echo "${USERNAMES[${j}]}_up.info Stats for ${USERNAMES[${j}]} - ${USER_IP[${j}]}"
	        echo "${USERNAMES[${j}]}_up.min 0"
	        echo "${USERNAMES[${j}]}_up.max 30000"


		j=$[$j+1]

	done;

#customized colours
	echo 'Serwer_down.colour 000000'
	echo 'Serwer_up.colour 000000'

        exit 0

fi;

#### DOWNLOAD

temp1=`/sbin/tc -s class show dev imq0 > $SH_TMP`

while read line
do
	test_hfsc=`echo $line | grep "hfsc"`

	j=1

        while [ $j -le $L_USERS ]
        do
		case $test_hfsc in

		*hfsc[\ ]1:$[$N_CLASS_D_1+$j]*)
		# check  N_CLASS_D_1 stats for every user
			read line
			temp1=`echo $line | awk '{ print $2; }'`
			STAT_USER[$j]=$[${STAT_USER[$j]}+$temp1]
#			echo "N_CLASS_D_1="$temp1
#			echo "N_CLASS_D_1_SUM "$j" ="${STAT_USER[$j]}
		;;
		*hfsc[\ ]1:$[$N_CLASS_D_2+$j]*)
		# check  N_CLASS_D_2 stats for every user
			read line
			temp1=`echo $line | awk '{ print $2; }'`
			STAT_USER[$j]=$[${STAT_USER[$j]}+$temp1]
#			echo "N_CLASS_D_2="$temp1
#			echo "N_CLASS_D_2_SUM "$j" ="${STAT_USER[$j]}
		;;
		esac

                j=$[$j+1]

        done;

done < $SH_TMP


####

j=1
while [ $j -le $L_USERS ]
do
	echo ${USERNAMES[${j}]}"_down.value "${STAT_USER[$j]}
	j=$[$j+1]
done;

#reset values

j=1
while [ $j -le $L_USERS ]
do
	STAT_USER[$j]=0
	j=$[$j+1]
done;

#### UPLOAD

temp1=`/sbin/tc -s class show dev imq1 > $SH_TMP`

while read line
do
	test_hfsc=`echo $line | grep "hfsc"`

	j=1

        while [ $j -le $L_USERS ]
        do
		case $test_hfsc in

		*hfsc[\ ]1:$[$N_CLASS_U_1+$j]*)
		# check  N_CLASS_U_1 stats for every user
			read line
			temp1=`echo $line | awk '{ print $2; }'`
			STAT_USER[$j]=$[${STAT_USER[$j]}+$temp1]
#			echo "N_CLASS_U_1="$temp1
#			echo "N_CLASS_U_1_SUM "$j" ="${STAT_USER[$j]}
		;;
		*hfsc[\ ]1:$[$N_CLASS_U_2+$j]*)
		# check  N_CLASS_U_2 stats for every user
			read line
			temp1=`echo $line | awk '{ print $2; }'`
			STAT_USER[$j]=$[${STAT_USER[$j]}+$temp1]
#			echo "N_CLASS_U_2="$temp1
#			echo "N_CLASS_U_2_SUM "$j" ="${STAT_USER[$j]}
		;;
		esac

                j=$[$j+1]

        done;

done < $SH_TMP

j=1
while [ $j -le $L_USERS ]
do
	echo ${USERNAMES[${j}]}"_up.value "${STAT_USER[$j]}
	j=$[$j+1]
done;


## clean temp file
temp1=`echo "" > $SH_TMP`

