#! /bin/bash

#  Copyright (C) 2008 Joey Schulze <joey@infodrom.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; version 2 dated June, 1991.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program;  if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.

# Source: http://www.infodrom.org/Infodrom/tools/munin.html

# Supported configuration:
#
#   [vsftpd-rel]
#   user root
#   env.logfile /var/log/vsftpd.log
#   env.logtail /usr/bin/logtail

PROGNAME=vsftpd

LOGFILE=${logfile:-/var/log/vsftpd.log}
LOGTAIL=${logtail:-`which logtail`}

OFFSET=${MUNIN_PLUGSTATE}/${PROGNAME}.offset
STATE=${MUNIN_PLUGSTATE}/${PROGNAME}.state
PIVOT=${MUNIN_PLUGSTATE}/${PROGNAME}.pivot


install_ok()
{
    tempfile=$(which tempfile)
    if [ ! -r ${LOGFILE} -o \
	 -z "${LOGTAIL}" -o ! -x "${LOGTAIL}" -o \
	 -z "${tempfile}" -o ! -x "${tempfile}" ]
    then
	return 1
    fi

    return 0
}

if [ "$1" = "autoconf" ]
then
    tmpfile=`which tempfile`
    if install_ok
    then
	echo yes
	exit 0
    else
	echo no
	exit 1
    fi
fi

if [ "$1" = "config" ]
then
    echo 'system.type ABSOLUTE'
    echo 'graph_title Very Secure FTP Server'
    echo 'graph_vlabel Requests'
    echo 'graph_category network'
    echo 'ftp_conn.label connections'
    echo 'ftp_loginok.label successful logins'
    echo 'ftp_loginfail.label failed logins'
    echo 'ftp_uploadok.label successful uploads'
    echo 'ftp_uploadfail.label failed uploads'
    echo 'ftp_downloadok.label successful downloads'
    echo 'ftp_downloadfail.label failed downloads'
    echo 'ftp_deleteok.label successful deletes'
    echo 'ftp_deletefail.label failed deletes'
    exit 0
fi

test install_ok || exit 1

touch -d now-5minutes+30seconds $PIVOT
TEMP=`tempfile`
trap "rm -f ${PIVOT} ${TEMP}" INT EXIT

test -n "$TEMP" -a -w "$TEMP" || exit 1

if [ ! -s ${OFFSET} ]
then
    $LOGTAIL ${LOGFILE} ${OFFSET} > ${TEMP}
    exit 0
fi

if [ $STATE -ot $PIVOT ]
then
    $LOGTAIL ${LOGFILE} ${OFFSET} > ${TEMP}
    echo -n > ${STATE}

    echo "ftp_conn.value $(grep 'CONNECT' ${TEMP} | wc -l)" >> $STATE
    echo "ftp_loginok.value $(grep 'OK LOGIN' ${TEMP} | wc -l)" >> $STATE
    echo "ftp_loginfail.value $(grep 'FAIL LOGIN' ${TEMP} | wc -l)" >> $STATE
    echo "ftp_uploadok.value $(grep 'OK UPLOAD' ${TEMP} | wc -l)" >> $STATE
    echo "ftp_uploadfail.value $(grep 'FAIL UPLOAD' ${TEMP} | wc -l)" >> $STATE
    echo "ftp_downloadok.value $(grep 'OK DOWNLOAD' ${TEMP} | wc -l)" >> $STATE
    echo "ftp_downloadfail.value $(grep 'FAIL DOWNLOAD' ${TEMP} | wc -l)" >> $STATE
    echo "ftp_deleteok.value $(grep 'OK DELETE' ${TEMP} | wc -l)" >> $STATE
    echo "ftp_deletefail.value $(grep 'FAIL DELETE' ${TEMP} | wc -l)" >> $STATE
fi

cat $STATE
