#!/bin/bash
#
# Swift monitoring script for Nagios
#
# Copyright © 2012 eNovance <licensing@enovance.com>
#
# Author: Julien Danjou <julien@danjou.info>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

set -e

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

usage ()
{
    echo "Usage: $0 [OPTIONS]"
    echo " -h               Get help"
    echo " -A <url>         URL for obtaining an auth token"
    echo " -U <username>    Username to use to get an auth token"
    echo " -K <key>         Password to use ro get an auth token"
    echo " -V <authversion> Version for authentication"
    echo " -c <container>   Container to upload to"
    echo " -s <maxsize>     Determine maximum file size in KB"
    echo "                  (default: 1024)"
}

while getopts 'hH:A:U:K:V:c:s:' OPTION
do
    case $OPTION in
        h)
            usage
            exit 0
            ;;
        A)
            export OS_AUTH_URL=$OPTARG
            ;;
        U)
            export OS_USERNAME=$OPTARG
            ;;
        K)
            export OS_PASSWORD=$OPTARG
            ;;
        V)
            export ST_AUTH_VERSION=$OPTARG
            ;;
        c)
            container=$OPTARG
            ;;
        s)
            multi=$OPTARG
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done

multi=${multi:-1024}
container=${container:-check_swift}

if ! which swift >/dev/null 2>&1
then
    echo "Swift command not found"
    exit $STATE_UNKNOWN
fi

delete_files ()
{
    test -n "$KEY" && swift delete "$container" "$KEY" >/dev/null 2>&1 || true
    rm -f "$TMPFILE" "$TMPFILE_TARGET"
}

trap delete_files EXIT

TMPFILE=`mktemp`
BLOCK_NUMBER=$(($RANDOM * $multi / 32767))
BLOCK_SIZE=1024
dd if=/dev/urandom of=$TMPFILE count=$BLOCK_NUMBER bs=$BLOCK_SIZE >/dev/null 2>&1

TMPFILE_TARGET=`mktemp`

if ! KEY=$(swift upload "$container" "$TMPFILE" 2>/dev/null)
then
    echo "Unable to upload file"
    exit $STATE_CRITICAL
fi

if ! swift download "$container" "$KEY" -o "$TMPFILE_TARGET" >/dev/null 2>&1
then
    echo "File upload OK, but unable to download file"
    exit $STATE_CRITICAL
fi

if ! swift delete "$container" "$KEY" >/dev/null 2>&1
then
    echo "File upload+download OK, but unable to delete uploaded file"
    exit $STATE_CRITICAL
fi

echo "Upload+download+delete of $(($BLOCK_NUMBER * $BLOCK_SIZE / 1024)) KiB file in container $container"
