Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 175 lines (155 sloc) 6.1 KB
#!/bin/bash
#
# -------------------------------------------------------------------------- #
# Copyright 2016-2018, StorPool (storpool.com) #
# #
# 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. #
#--------------------------------------------------------------------------- #
function splog() { logger -t "tm_sp_${0##*/}[$$]" "$*"; }
DISK="$1"
DS_ID="$2"
VM_ID="$3"
DRIVER="$4"
DRIVER_PATH="${0%/*}"
SP_JSON_PATH="/tmp"
SP_SPACE_USED_LVMWAY=
SP_VOLUME_SPACE_JSON="storpool_volume_usedSpace.json"
SP_CMD_VOLUME_SPACE="cat _SP_JSON_PATH_/_SP_VOLUME_SPACE_JSON_"
SP_SNAPSHOT_SPACE_JSON="storpool_snapshot_space.json"
SP_CMD_SNAPSHOT_SPACE="cat _SP_JSON_PATH_/_SP_SNAPSHOT_SPACE_JSON_"
if [ -f "${DRIVER_PATH}/../../addon-storpoolrc" ]; then
source "${DRIVER_PATH}/../../addon-storpoolrc"
fi
if [ -f "/etc/storpool/addon-storpool.conf" ]; then
source "/etc/storpool/addon-storpool.conf"
fi
DS_NAME="${ONE_PX:-one}-ds-$DS_ID"
function boolTrue()
{
case "${1^^}" in
1|Y|YES|TRUE|ON)
return 0
;;
*)
return 1
esac
}
if boolTrue "$DEBUG_TM_MONITOR_DISK_VERBOSE"; then
splog "$PWD $DISK $DS_ID $VM_ID $DRIVER"
[ -d "$SP_DS_TMP" ] && splog "found SP_DS_TMP:$SP_DS_TMP"
fi
DISK_LINK="$(readlink "$DISK")"
DISK_DEV="$(readlink -f "$DISK")"
#splog "$DISK $DISK_LINK $DISK_DEV"
if [ -z "$DISK_LINK" ] || [ -n "${DISK_LINK%/dev/storpool*}" ]; then
if boolTrue "$DEBUG_TM_MONITOR_DISK_VERBOSE"; then
splog "$DISK NOT on storpool! $DISK_LINK"
fi
"${0}-misc" "$DISK" "$DS_ID" "$VM_ID"
exit $?
else
if boolTrue "$DEBUG_TM_MONITOR_DISK_VERBOSE"; then
splog "$DISK StorPool volume $DISK_LINK"
fi
fi
DISK_ID="${DISK##*\.}"
spVolume=${DISK_LINK#/dev/storpool/}
if boolTrue "$SP_SPACE_USED_LVMWAY"; then
used="storedSize"
columns="data other"
else
used="spaceUsed"
columns="other data"
fi
#"name",storedSize,spaceUsed
if [ -f "$SP_DS_TMP/sizes" ]; then
#if boolTrue "$DEBUG_TM_MONITOR_DISK_VERBOSE"; then
# splog "walking $SP_DS_TMP/sizes for volume $spVolume"
#fi
fgrep "$spVolume" "$SP_DS_TMP/sizes" | while IFS=, read name $columns; do
name=${name//\"/}
if [ -n "$disk_size" ]; then
snap_id=${name#*snap}
if [ -n "$snap_id" ]; then
if [ "$snap_id" = "$name" ]; then
if boolTrue "$DEBUG_TM_MONITOR_DISK_VERBOSE"; then
splog "skipping $name looking for *snap"
fi
continue
fi
snap_size=$((data/1024/1024))
if boolTrue "$DEBUG_TM_MONITOR_DISK"; then
splog "$DISK ID:$DISK_ID SNAP:$snap_id SIZE:$snap_size ($((other/1024/1024))) snapshot:$name"
fi
echo -n "SNAPSHOT_SIZE=[ID=${snap_id},DISK_ID=${DISK_ID},SIZE=${snap_size}] "
else
if boolTrue "$DEBUG_TM_MONITOR_DISK_VERBOSE"; then
splog "skipping $name looking for *snap but snap_id=$snap_id"
fi
fi
else
if [ "$spVolume" = "$name" ]; then
disk_size="${data:-0}"
disk_size=$((disk_size/1024/1024))
if [ "$disk_size" = "0" ] && [ -n "$data" ] && [ "$data" -gt "0" ]; then
disk_size=1
fi
if boolTrue "$DEBUG_TM_MONITOR_DISK"; then
splog "$DISK ID:$DISK_ID SIZE:$disk_size ($((other/1024/1024))) volume:$name"
fi
echo -n "DISK_SIZE=[ID=${DISK_ID},SIZE=${disk_size}] "
else
splog "skip '$name' looking for '$spVolume'"
fi
fi
done
exit
else
if boolTrue "$DEBUG_TM_MONITOR_DISK"; then
splog "${DRIVER}:${DISK} not found $SP_DS_TMP/sizes"
fi
fi
# expand SP_CMD_VOLUME_SPACE macro variables
SP_CMD_VOLUME_SPACE="${SP_CMD_VOLUME_SPACE//_DATASTORE_ID_/$DS_ID}"
SP_CMD_VOLUME_SPACE="${SP_CMD_VOLUME_SPACE//_CLUSTER_ID_/$CLUSTER_ID}"
SP_CMD_VOLUME_SPACE="${SP_CMD_VOLUME_SPACE//_SP_API_HTTP_HOST_/$SP_API_HTTP_HOST}"
SP_CMD_VOLUME_SPACE="${SP_CMD_VOLUME_SPACE//_SP_VOLUME_SPACE_JSON_/$SP_VOLUME_SPACE_JSON}"
SP_CMD_VOLUME_SPACE="${SP_CMD_VOLUME_SPACE//_SP_JSON_PATH_/$SP_JSON_PATH}"
# expand SP_CMD_SNAPSHOT_SPACE macro variables
SP_CMD_SNAPSHOT_SPACE="${SP_CMD_SNAPSHOT_SPACE//_DATASTORE_ID_/$DS_ID}"
SP_CMD_SNAPSHOT_SPACE="${SP_CMD_SNAPSHOT_SPACE//_CLUSTER_ID_/$CLUSTER_ID}"
SP_CMD_SNAPSHOT_SPACE="${SP_CMD_SNAPSHOT_SPACE//_SP_API_HTTP_HOST_/$SP_API_HTTP_HOST}"
SP_CMD_SNAPSHOT_SPACE="${SP_CMD_SNAPSHOT_SPACE//_SP_SNAPSHOT_SPACE_JSON_/$SP_SNAPSHOT_SPACE_JSON}"
SP_CMD_SNAPSHOT_SPACE="${SP_CMD_SNAPSHOT_SPACE//_SP_JSON_PATH_/$SP_JSON_PATH}"
if boolTrue "$DEBUG_TM_MONITOR_DISK_VERBOSE";then
splog "SP_CMD_VOLUME_SPACE:$SP_CMD_VOLUME_SPACE"
fi
spSize=$(eval $SP_CMD_VOLUME_SPACE 2>/dev/null | jq -r ".data | map( select( .name==\"${spVolume}\" ) ) | .[] | [.${used}] | @csv" 2>/dev/null) #"
disk_size="${spSize:-0}"
disk_size=$((disk_size/1024/1024))
if [ "$disk_size" = "0" ] && [ -n "$spSize" ] && [ "$spSize" -gt "0" ]; then
disk_size=1
fi
if boolTrue "$DEBUG_TM_MONITOR_DISK"; then
splog "$DISK ID:$DISK_ID SIZE:$disk_size ($used)"
fi
echo -n "DISK_SIZE=[ID=${DISK_ID},SIZE=${disk_size}] "
while IFS=',' read name snap_size; do
name=${name//\"/}
snap_id=${name#*snap}
snap_size=$((snap_size/1024/1024))
if boolTrue "$DEBUG_TM_MONITOR_DISK"; then
splog "$DISK ID:$DISK_ID SNAP:$snap_id SIZE:$snap_size ($used)"
fi
echo -n "SNAPSHOT_SIZE=[ID=${snap_id},DISK_ID=${DISK_ID},SIZE=${snap_size}] "
done < <(eval $SP_CMD_SNAPSHOT_SPACE 2>/dev/null | jq -r ".data | map( select( .name | contains(\"${spVolume}-snap\") ) ) | .[] | [.name, .${used}] | @csv" | sort)