#!/bin/bash
#
#
# pure-ftpd-bw plugin
# show the bandwidth used by pure-ftpd, counts the bytes sent and received
# made by Dju
# v1.2
#
# commands needed: logtail - grep
#
#
# Configuration:
# Maybe need to add following lines to plugins config file
# (e.g. /etc/munin/plugin-conf.d/pure-ftpd) to run pure-ftpwho
# as user with apropirate privilegs then restart munin-node.
#
# [pure-ftpd-bw]
# user root
#
#
# Parameters
#
#       config   (required)
#       autoconf (optional - used by munin-config)
#
#
# Magic markers (optional - used by munin-config and installation scripts):
#
#%# family=auto
#%# capabilities=autoconf


LOGFILE=/var/log/pure-ftpd/transfer.log
LOGTAIL=$(which logtail)
OFFSET_FILE=$MUNIN_PLUGSTATE/pure-ftpd-bw.offset


if [ "$1" = "autoconf" ]; then
        if [ -f $LOGFILE ]; then
                if [ ! -z "$LOGTAIL" -a -f $LOGTAIL -a -x $LOGTAIL ]; then
                        echo yes
                        exit 0
                else
                        echo "no (logtail not found)"
                        exit 0
                fi
        else
                echo "no (logfile $LOGFILE does not exist)"
                exit 0
        fi
fi

if [ "$1" = "config" ]; then
        echo 'graph_title Pure Ftpd Bandwidth'
        echo 'graph_args --base 1000 -l 0'
        echo 'graph_vlabel Datas sent / received'
        echo 'graph_category network'
        echo 'dl.label Bytes downloaded'
        echo 'ul.label Bytes uploaded'
        exit 0
fi

TMP1=`mktemp`
if [ -f $TMP1 ]; then
        $LOGTAIL -o $OFFSET_FILE -f $LOGFILE | grep 'GET \|PUT ' > $TMP1
        dls=$(awk '/GET / {print $9}' $TMP1)
        dl=0
        for d in $dls; do dl=$(expr $dl + $d); done
        echo "dl.value ${dl}"
        uls=$(awk '/PUT / {print $9}' $TMP1)
        ul=0
        for u in $uls; do ul=$(expr $ul + $u); done
        echo "ul.value ${ul}"
        rm $TMP1
else
        echo "cant write temp file"
        exit 1
fi


