#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2019, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

###############################################################################
# This script is used to monitor the free and used space of a datastore
###############################################################################

# -------- Set up the environment to source common tools & conf ------------

if [ -z "${ONE_LOCATION}" ]; then
    LIB_LOCATION=/usr/lib/one
else
    LIB_LOCATION=$ONE_LOCATION/lib
fi

. $LIB_LOCATION/sh/scripts_common.sh

DRIVER_PATH=$(dirname $0)

source ${DRIVER_PATH}/../../datastore/libfs.sh
source ${DRIVER_PATH}/../../etc/datastore/linstor_un/linstor_un.conf
source ${DRIVER_PATH}/../../datastore/linstor_un/linstor_utils.sh


# -------- Get datastore arguments from OpenNebula core ------------
if [ -n "$2" ]; then
    DRV_ACTION=$1
    DS_ID=$2
else
    DRV_ACTION=$(cat -)
    DS_ID=$1
fi

XPATH="${DRIVER_PATH}/../../datastore/xpath.rb -b $DRV_ACTION"

unset i j XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <($XPATH     /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/LS_CONTROLLERS \
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/LS_CERTFILE \
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/LS_KEYFILE \
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/LS_CAFILE \
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/RESOURCE_GROUP \
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/STORAGE_POOL)

LS_CONTROLLERS="${XPATH_ELEMENTS[j++]}"
LS_CERTFILE="${XPATH_ELEMENTS[j++]}"
LS_KEYFILE="${XPATH_ELEMENTS[j++]}"
LS_CAFILE="${XPATH_ELEMENTS[j++]}"
RESOURCE_GROUP="${XPATH_ELEMENTS[j++]}"
STORAGE_POOL="${XPATH_ELEMENTS[j++]}"

linstor_load_keys

if [ -z "$STORAGE_POOL" ]; then
    if [ -z "$RESOURCE_GROUP" ]; then
        error_message "Datastore template missing 'STORAGE_POOL' attribute."
        exit -1
    fi
    STORAGE_POOL=$($LINSTOR -m --output-version v0 resource-group list -r "$RESOURCE_GROUP" \
        | $JQ -r '.[][].select_filter.storage_pool_list // [] | join(" ")')
    if [ -z "$STORAGE_POOL" ]; then
        error_message "Resource group missing 'StoragePool' attribute."
        exit -1
    fi
fi

# ------------ Compute datastore usage -------------

MONITOR_SCRIPT="timeout 10 $LINSTOR -m --output-version v0 storage-pool list -s $STORAGE_POOL"
MONITOR_DATA="$(monitor_and_log "$MONITOR_SCRIPT" 2>&1)"
MONITOR_STATUS=$?

if [ "$MONITOR_STATUS" = "0" ]; then
    linstor_monitor_storpool "${MONITOR_DATA}"
else
    echo "$MONITOR_DATA"
    exit $MONITOR_STATUS
fi

# ------------ Compute VM disks usage -------------

MONITOR_SCRIPT="timeout 10 $LINSTOR -m --output-version v0 resource-definition list --prop Aux/one/DS_ID=${DS_ID}"
MONITOR_DATA="$(monitor_and_log "$MONITOR_SCRIPT" 2>&1)"
MONITOR_STATUS=$?

if [ "$MONITOR_STATUS" != "0" ]; then
    echo "$MONITOR_DATA"
    exit $MONITOR_STATUS
fi

MONITOR_SCRIPT="timeout 10 $LINSTOR -m --output-version v0 volume list"
MONITOR_DATA2="$(monitor_and_log "$MONITOR_SCRIPT" 2>&1)"
MONITOR_STATUS=$?

if [ "$MONITOR_STATUS" != "0" ]; then
    echo "$MONITOR_DATA2"
    exit $MONITOR_STATUS
fi

linstor_monitor_resources "${MONITOR_DATA}" "${MONITOR_DATA2}"
