#!/bin/bash


#####################################
# Antoine Meme 
# version 1.0 - 06/10/2009
# www.ameme.fr
# email : antoine.meme@gmail.com
# Sanofi Aventis Ambares
####################################


APPNAME=$(basename $0)

###############################
# Check binaries before start #
###############################

binaries=$(cat<<all_required_binaries
  wget
all_required_binaries)

for required_binary in $binaries; do
  which $required_binary > /dev/null
  if [ "$?" != '0' ];then
    echo -e "UNKNOWN: $APPNAME: No usable '$required_binary' binary in '$PATH'\n"
    exit 3
  fi
done



######################
# print Help Message #
######################

usage () {

cat<<EOU
Usage of $APPNAME
---------------------------------------------------------------------

Datastore ESX - ESXi 

Options:
  -H
     IP du serveur
     (Requis)
  -u 
     User
  -p 
     Password
  -d
     Nom du Datastore
  -h 
     Afficher ce message d'aide
  -w 
     Pourcentage WARNING
  -c 
     Pourcentage CRITICAL
  
Exemple :
  $APPNAME -H 172.23.6.94 -u root -p passwdroot -d datastore_DISK_146 -w 85 -c 95

---------------------------------------------------------------------
EOU
exit 3
}




################################
# Get Options from commandline #
################################

while getopts "w:c:d:p:u:H:h" option
do
  case $option in
    u ) USER=$OPTARG ;;
    H ) HOST=$OPTARG ;;
    p ) PASSWD=$OPTARG ;;
    d ) DATASTORE=$OPTARG ;;
    w ) WARNING=$OPTARG ;;
    c ) CRITICAL=$OPTARG;;
    h ) usage ;;
  esac
done



###########################################
# Exit Unknown if not all arguments given #
###########################################

if [ -z "$USER" ] || [ -z "$HOST" ] || [ -z "$PASSWD" ] || [ -z "$DATASTORE" ] || [ -z "$WARNING" ]  || [ -z "$CRITICAL" ];then
 echo -e "Erreur : definir -H ipserveresxi -u user -p passwd -d datastorename -w warning% -c critical%"
 exit 3
fi


###########################
# Main part of the script #
###########################


wget https://$HOST/folder?dcPath=ha-datacenter --no-check-certificate --http-user=$USER --http-password=$PASSWD -O /tmp/$1_filesystem.$DATASTORE -q

NAME=`cat /tmp/$1_filesystem.$DATASTORE | grep -i "$DATASTORE" | awk -F= '{print $4}' | awk -F\" '{print $1}'`
CAPACITY=`cat /tmp/$1_filesystem.$DATASTORE | grep -i "$DATASTORE" | awk -F\> '{print $7}' | awk -F\< '{print $1}'`
FREE=`cat /tmp/$1_filesystem.$DATASTORE | grep -i "$DATASTORE" | awk -F\> '{print $9}' | awk -F\< '{print $1}'`

rm -f /tmp/$1_filesystem.$DATASTORE

if [ -z "$NAME" ] || [ -z "$CAPACITY" ] || [ -z "$FREE" ];then
{
	echo "Error";
	exit 3
}
fi

let "capacityMo = (($CAPACITY / 1048576))"
let "freeMo = (($FREE / 1048576))"
let "usedMo = (($capacityMo - $freeMo))"
let "result = (($usedMo * 100))"
let "usedPourcentMo = (($result / $capacityMo))"

if [ $usedPourcentMo -ge $CRITICAL ];then
{
        echo "CRITICAL - $NAME Capacity : $capacityMo Mo, Free : $freeMo Mo, $usedPourcentMo% used";
        exit 2
}
elif [ $usedPourcentMo -ge $WARNING ];then
{
	echo "WARNING - $NAME Capacity : $capacityMo Mo, Free : $freeMo Mo, $usedPourcentMo% used";
	exit 1
}
else
{
	echo "OK - $NAME Capacity : $capacityMo Mo, Free : $freeMo Mo, $usedPourcentMo% used";
	exit 0
}
fi

