#!/bin/bash
#
# Check the disk usage of the Swift ring 
#
# Copyright 2014 eNovance <licensing@enovance.com>
#
# Author: Gaetan Trellu <gaetan.trellu@enovance.com>
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
#
# Requirement : swift-recon
#
set -e

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

RECON_BINARY=/usr/bin/swift-recon

function usage() {
    echo "This plugin checks the ring disk usage by using the swift-recon command."
    echo 
    echo "Usage: `basename $0` [OPTIONS]"
    echo "  -h   Display this help"
    echo "  -c   Set the critical limit in % for the ring usage. Ex: 95"
    echo "  -w   Set the warning limit in % for the ring usage. Ex: 90"
    echo 
}

while getopts 'w:c:h' OPTION
do
    case $OPTION in
        h)
            usage   
            exit 0
            ;;
        c)
            export CRITICAL=$OPTARG
            ;;
        w)
            export WARNING=$OPTARG
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done

#Default values
CRITICAL=${CRITICAL:-"90"}
WARNING=${WARNING:-"80"}

if [ ! -x $RECON_BINARY ]
then
    echo "CRITICAL: Unable to use the swift-recon binary"
    exit $STATE_CRITICAL
else

    RING_USAGE=$(swift-recon -d | grep "avg" | awk '{ print $NF }' | cut -d"." -f1)

    if [ "$RING_USAGE" -ge "$WARNING" -a "$RING_USAGE" -lt "$CRITICAL" ]
    then
        echo "WARNING: $RING_USAGE% space used"
        exit $STATUS_WARNING
    fi

    if [ "$RING_USAGE" -ge "$CRITICAL" ]
    then
        echo "CRITICAL: $RING_USAGE% space used"
        exit $STATUS_CRITICAL
    else
        echo "OK: $RING_USAGE% space used"
        exit $STATUS_OK
    fi

fi

