#!/bin/bash

#        Program to check used disk-space from (hetzner) ftp-server
#
#        Created: 12/04/2012
#        Version: 0.9
#        Author:  Florian Schaal (info@schaal-24.de)

# Copyright (c) 2012 Florian Schaal (info@schaal-24.de.) All rights reserved.
#
# This plugin is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License.
# See http://www.fsf.org/licensing/licenses/gpl.html
# 
# 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.
#
#
# TODO
# check if warn < crit
# change values for warn and crit to parameters

source /usr/local/nagios/libexec/utils.sh
# percent used avail
WARN=92
CRIT=97

USER=$1 

echo "df -h" > $0.$USER.tmp
t=`sftp -b $0.$USER.tmp $USER@${USER}.your-backup.de|grep GB`
rm -f $0.$USER.tmp
size=`echo $t|cut -d" " -f1|sed -e 's/GB//g'`
tsize=`echo $t|cut -d" " -f1`
used=`echo $t|cut -d" " -f2|sed -e 's/GB//g'`
tused=`echo $t|cut -d" " -f2`
avail=`echo $t|cut -d" " -f3`
capacity=`echo $t|cut -d" " -f5|sed -e 's/\%//g'`

exitstatus=$STATE_OK
RESULT="OK:"
if [ $capacity -ge $WARN ]; then exitstatus=$STATE_WARNING; RESULT="WARN:"; fi
if [ $capacity -ge $CRIT ]; then exitstatus=$STATE_CRITICAL; RESULT="CRIT:"; fi
WARN=`echo $size*0.$WARN|bc`
CRIT=`echo $size*0.$CRIT|bc`
RESULT=$RESULT" $tused/$tsize used ($capacity%)|USED=$used;$WARN;$CRIT"
echo $RESULT
exit $exitstatus
