Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BashScript for getting vm summary of OpenStack.
#!/bin/bash
#
# VMの利用状況を集計するスクリプトです。
#
# - jq コマンド必須です
# http://stedolan.github.io/jq/
# # How to install
# wget -O /usr/bin/jq http://stedolan.github.io/jq/download/linux64/jq
# chmod +x /usr/bin/jq
#
# - 指定したプロジェクト(TENANT_NAME)の総合的な利用状況を表示
# * 各機能のユニークユーザ数
# * VM数 / vCPU数 / メモリ容量
# * イメージとスナップショットの数と容量
# * ボリュームとスナップショットの数と容量
#
# - イメージの利用状況ランキング
# * インスタンスの元となったイメージから集計
# * そのため通常イメージとスナップショットが混合
# * イメージ名を OSName-Version-UpdateDate-Number として運用しており
# 例)Debian-7.0.0-20130801-01
# 最初の OSName と Versionのメジャーバージョン部分のみ を切り取って
# ランキングの名前としています
# * スナップショットの場合は規則に従っていないため
# 最初の単語とハイフン、そして次の単語までを切り取る形で使われます
#
# - ユーザID順で消費リソース情報を表示
# * VM数, vCPU, メモリ
# * イメージスナップショット数, 容量
# * ボリューム数, 容量
# * ボリュームスナップショット数, 容量
#
# - VMイメージの利用状況ランキングを表示
# * VM数
# * vCPU数
# * RAM容量
# * ボリューム容量
#
# - 表示の注意事項
# * 分母の - はクォータによる限界設定がない項目です
# * 分子の - は情報不足で集計できない項目です
#
# - GlanceイメージのAPI取得は、削除済みイメージとスナップショットを
# 取得できないためMySQLから全レコードを直接取得しています
# ※APIの status=DELETED はまさに削除している時が対象なので
# 削除完了してしまうとリストには出てこない
#
# - Volume,VolumeSnapshotのAPIから、作成したuser_idが取得できないので
# MySQLから別途取得しています
#
PATH=/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#######################################
#
# 設定
#
TENANT_NAME=test
GLANCE_API_CONF=/etc/glance/glance-api.conf
CINDER_CONF=/etc/cinder/cinder.conf
#######################################
#
# 開始の出力
#
echo "======================================="
echo " VM Summary Report"
echo "======================================="
echo
#######################################
#
# 設定取得
#
MYSQL_PATTERN='^.*mysql:\/\/\([^:]\+\):\([^@]\+\)@\([^:]\+\):\([^\/]\+\)\/\(.\+\)$'
MYSQL_REPLACE='-h \3 -u \1 -P \4 -p\2 \5'
MYSQL_ADD_OPTIONS=" --batch --skip-column-names"
IMAGE_MYSQL_OPTIONS=`grep "^sql_connection.*mysql://" $GLANCE_API_CONF \
| sed -e "s/$MYSQL_PATTERN/$MYSQL_REPLACE/g"`
IMAGE_MYSQL_OPTIONS+="$MYSQL_ADD_OPTIONS"
VOLUME_MYSQL_OPTIONS=`grep "^sql_connection.*mysql://" $CINDER_CONF \
| sed -e "s/$MYSQL_PATTERN/$MYSQL_REPLACE/g"`
VOLUME_MYSQL_OPTIONS+="$MYSQL_ADD_OPTIONS"
#######################################
#
# コマンド確認
#
#
# jq is json processor
#
type jq > /dev/null 2>&1
[ $? -ne 0 ] && echo "not found jq." && exit 1
#######################################
#
# Keystone認証
#
#
# 環境変数読み込み
#
. ~/.bashrc
export OS_TENANT_NAME=$TENANT_NAME
#
# 認証データ取得
#
TOKENS=`eval "
curl -s -d '{
\"auth\":{
\"passwordCredentials\":{
\"username\": \"$OS_USERNAME\", \"password\":\"$OS_PASSWORD\"
},
\"tenantName\":\"$OS_TENANT_NAME\"
}
}' -H 'Content-type: application/json' ${OS_AUTH_URL}tokens
"`
#
# トークンID, テナントID, API URL 取得
#
TOKEN_ID=` echo "$TOKENS" | jq '.access.token.id' -r`
PROJECT_ID=` echo "$TOKENS" | jq '.access.token.tenant.id' -r`
PROJECT_NAME=`echo "$TOKENS" | jq '.access.token.tenant.name' -r`
COMPUTE_URL=` echo "$TOKENS" \
| jq '.access.serviceCatalog[] | select(.type == "compute").endpoints[].adminURL' -r`
IMAGE_URL=` echo "$TOKENS" \
| jq '.access.serviceCatalog[] | select(.type == "image").endpoints[].adminURL' -r`
VOLUME_URL=` echo "$TOKENS" \
| jq '.access.serviceCatalog[] | select(.type == "volume").endpoints[].adminURL' -r`
[ "$TOKEN_ID" = "null" ] && echo "can not get token id. check TENANT_NAME." && exit 1
#######################################
#
# データ収集
#
# デフォルトリミットが小さい場合があるので全件取得対応
LIMIT=10000
#
# VMのリスト
#
SERVER_DETAIL=`curl -s $COMPUTE_URL/servers/detail?limit=$LIMIT -H "X-Auth-Token: $TOKEN_ID"`
echo "$SERVER_DETAIL" | head -1 | grep "^{" > /dev/null 2>&1
[ $? -ne 0 ] && echo "can not get server list." && exit 1
#
# Quotaのリスト
#
QUOTA_COMPUTE=`curl -s "$COMPUTE_URL/os-quota-sets/$PROJECT_ID" -H "X-Auth-Token: $TOKEN_ID"`
echo "$QUOTA_COMPUTE" | head -1 | grep "^{" > /dev/null 2>&1
[ $? -ne 0 ] && echo "could not get compute quota list." && exit 1
QUOTA_VOLUME=`curl -s "$VOLUME_URL/os-quota-sets/$PROJECT_ID" -H "X-Auth-Token: $TOKEN_ID"`
echo "$QUOTA_VOLUME" | head -1 | grep "^{" > /dev/null 2>&1
[ $? -ne 0 ] && echo "could not get nova quota list." && exit 1
#
# flavorのリスト
#
FLAVOR_DETAIL=`curl -s "$COMPUTE_URL/flavors/detail?limit=$LIMIT" -H "X-Auth-Token: $TOKEN_ID"`
echo "$FLAVOR_DETAIL" | head -1 | grep "^{" > /dev/null 2>&1
[ $? -ne 0 ] && echo "could not get flavor list." && exit 1
#
# イメージ&スナップショットのリスト
#
IMAGE_DETAIL=`curl -s "$IMAGE_URL/v2/images?limit=$LIMIT" -H "X-Auth-Token: $TOKEN_ID"`
echo "$IMAGE_DETAIL" | head -1 | grep "^{" > /dev/null 2>&1
[ $? -ne 0 ] && echo "could not get image list." && exit 1
#
# 過去に存在したものを含むイメージのリスト
# - 既存インスタンスかつ削除済みイメージの名前取得のため
# - MySQLから取得
# - 結果リストはあえてAPIのJSONと同じ形式にしておきます
ALL_IMAGE_DATA=`mysql $IMAGE_MYSQL_OPTIONS -e "SELECT id, name FROM images"`
ALL_IMAGE_JSON_LIST=`echo "$ALL_IMAGE_DATA" | awk -F"\t" '{ print "{\"id\":\"" $1 "\",\"name\":\"" $2 "\"}" }'`
ALL_IMAGE_JSON="[]"
for JSON in ${ALL_IMAGE_JSON_LIST[@]}
do
ALL_IMAGE_JSON=`echo "$ALL_IMAGE_JSON" | jq ". + [ $JSON ]"`
done
ALL_IMAGE_DETAIL="{ \"images\" : $ALL_IMAGE_JSON }"
#
# ボリュームのリスト
#
# - 現在はuser_idが取得できないので、MySQLから別に取得する
VOLUME_DETAIL=`curl -s "$VOLUME_URL/volumes/detail" -H "X-Auth-Token: $TOKEN_ID"`
echo "$VOLUME_DETAIL" | head -1 | grep "^{" > /dev/null 2>&1
[ $? -ne 0 ] && echo "could not get volume list." && exit 1
QUERY="SELECT id, user_id FROM volumes where status != 'deleted' and project_id = '$PROJECT_ID'"
VOLUME_DATA=`mysql $VOLUME_MYSQL_OPTIONS -e "$QUERY"`
VOLUME_JSON_LIST=`echo "$VOLUME_DATA" | \
awk -F"\t" '{ print "{\"id\":\"" $1 "\",\"user_id\":\"" $2 "\"}" }'`
VOLUME_USERS_JSON="[]"
for JSON in ${VOLUME_JSON_LIST[@]}
do
VOLUME_USERS_JSON=`echo "$VOLUME_USERS_JSON" | jq ". + [ $JSON ]"`
done
VOLUME_USERS="{ \"volumes\" : $VOLUME_USERS_JSON }"
#
# ボリュームスナップショットのリスト
#
# - 現在はuser_idが取得できないので、MySQLから別に取得する
VOLUME_SNAPSHOT_DETAIL=`curl -s "$VOLUME_URL/snapshots/detail" -H "X-Auth-Token: $TOKEN_ID"`
echo "$VOLUME_SNAPSHOT_DETAIL" | head -1 | grep "^{" > /dev/null 2>&1
[ $? -ne 0 ] && echo "could not get volume snapshot list." && exit 1
QUERY="SELECT id, user_id FROM snapshots where status != 'deleted' and project_id = '$PROJECT_ID'"
VOLUME_SNAPSHOT_DATA=`mysql $VOLUME_MYSQL_OPTIONS -e "$QUERY"`
VOLUME_SNAPSHOT_JSON_LIST=`echo "$VOLUME_SNAPSHOT_DATA" | \
awk -F"\t" '{ print "{\"id\":\"" $1 "\",\"user_id\":\"" $2 "\"}" }'`
VOLUME_SNAPSHOT_USERS_JSON="[]"
for JSON in ${VOLUME_SNAPSHOT_JSON_LIST[@]}
do
VOLUME_SNAPSHOT_USERS_JSON=`echo "$VOLUME_SNAPSHOT_USERS_JSON" | jq ". + [ $JSON ]"`
done
VOLUME_SNAPSHOT_USERS="{ \"snapshots\" : $VOLUME_SNAPSHOT_USERS_JSON }"
#######################################
#
# データ編集
#
#########
#
# JSON初期化
#
# - (スナップショットではない)通常イメージはユーザIDが記録されていない
# ので集計できない ... images, image_gb は現在は未使用だが書いておく
SUMMARY_TOTAL='{
"vm_users" : 0,
"image_users" : 0, "image_snapshot_users" : 0,
"volume_users" : 0, "volume_snapshot_users" : 0,
"vm" : 0, "vcpus" : 0, "ram" : 0,
"images" : 0, "image_bytes" : 0,
"image_snapshots" : 0, "image_snapshot_bytes" : 0,
"volumes" : 0, "volume_gb" : 0,
"volume_snapshots" : 0, "volume_snapshot_gb" : 0
}'
SUMMARY_OS='{ "dummy" : { "name" : "", "count" : 0 } }'
SUMMARY_USERS='{ "dummy" :
{
"user_id" : "",
"vm" : 0, "vcpus" : 0, "ram" : 0,
"images" : 0, "image_bytes" : 0,
"image_snapshots" : 0, "image_snapshot_bytes" : 0,
"volumes" : 0, "volume_gb" : 0,
"volume_snapshots" : 0, "volume_snapshot_gb" : 0
}
}'
SUMMARY_USER_KEYS=`echo "$SUMMARY_USERS" | jq ".dummy | keys | .[]" -r`
#########
#
# VMリストの配列
# - 必要なカラムのみ抽出
SERVERS=(`echo "$SERVER_DETAIL" | \
jq '.servers[] | { user_id, image, flavor }' \
--compact-output`)
#
# VM情報を1つずつループ
#
for SERVER in "${SERVERS[@]}"
do
#
# データ整理
#
USER_ID=` echo "$SERVER" | jq .user_id -r`
FLAVOR_ID=`echo "$SERVER" | jq .flavor.id`
IMAGE_ID=` echo "$SERVER" | jq .image.id`
FLAVOR=`echo "$FLAVOR_DETAIL" | jq ".flavors[] | select(.id == $FLAVOR_ID)"`
vCPUs=` echo "$FLAVOR" | jq .vcpus`
RAM=` echo "$FLAVOR" | jq .ram`
IMAGE=`echo "$ALL_IMAGE_DETAIL" | jq ".images[] | select(.id == $IMAGE_ID)"`
IMAGE_NAME=`echo "$IMAGE" | jq .name -r`
OS_NAME=`echo "$IMAGE_NAME" | sed -e 's/^\([^-]\+\)-\([^-\.]\+\).*$/\1-\2/'`
OS_KEY_NAME=`echo "$OS_NAME" | sed -e 's/-//g'`
#
# トータル情報集計
#
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".vm += 1"`
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".vcpus += $vCPUs"`
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".ram += $RAM"`
HAS_USER=`echo "$SUMMARY_USERS" | jq "has(\"$USER_ID\")"`
[ "$HAS_USER" = "false" ] && SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".vm_users += 1"`
SUMMARY_OS=`echo "$SUMMARY_OS" | jq ".$OS_KEY_NAME.name = \"$OS_NAME\""`
SUMMARY_OS=`echo "$SUMMARY_OS" | jq ".$OS_KEY_NAME.count += 1"`
#
# ユーザ別情報集計
#
for KEY in ${SUMMARY_USER_KEYS[@]}
do
[ "$KEY" = "user_id" ] && continue
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.$KEY += 0"`
done
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.user_id = \"$USER_ID\""`
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.vm += 1"`
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.vcpus += $vCPUs"`
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.ram += $RAM"`
done
#########
#
# イメージ&スナップショットリストの配列
# - 必要なカラムのみ抽出
IMAGES=(`echo "$IMAGE_DETAIL" | \
jq '.images[] | { id, size, user_id, image_type }' \
--compact-output`)
#
# イメージ&スナップショット情報を1つずつループ
#
for IMAGE in "${IMAGES[@]}"
do
#
# データ整理
#
IMAGE_ID=` echo "$IMAGE" | jq .id`
SIZE=` echo "$IMAGE" | jq .size`
USER_ID=` echo "$IMAGE" | jq .user_id -r`
IMAGE_TYPE=`echo "$IMAGE" | jq .image_type -r`
#
# スナップショット集計
#
if [ "$IMAGE_TYPE" = "snapshot" ];
then
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".image_snapshots += 1"`
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".image_snapshot_bytes += $SIZE"`
USER_IMAGE_SNAPSHOTS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.image_snapshots"`
if [ "$USER_IMAGE_SNAPSHOTS" = 0 -o "$USER_IMAGE_SNAPSHOTS" = "null" ]; then
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".image_snapshot_users += 1"`
fi
#
# ユーザ別情報集計
#
for KEY in ${SUMMARY_USER_KEYS[@]}
do
[ "$KEY" = "user_id" ] && continue
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.$KEY += 0"`
done
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.user_id = \"$USER_ID\""`
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.image_snapshots += 1"`
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.image_snapshot_bytes += $SIZE"`
#
# 通常イメージ集計
#
else
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".images += 1"`
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".image_bytes += $SIZE"`
fi
done
#########
#
# ボリュームリストの配列
# - 必要なカラムのみ抽出
VOLUMES=(`echo "$VOLUME_DETAIL" | \
jq '.volumes[] | { id, size }' \
--compact-output`)
#
# ボリューム情報を1つずつループ
#
for VOLUME in "${VOLUMES[@]}"
do
#
# データ整理
#
VOLUME_ID=`echo "$VOLUME" | jq .id`
SIZE=` echo "$VOLUME" | jq .size`
USER_ID=` echo "$VOLUME_USERS" | \
jq ".volumes[] | select(.id == $VOLUME_ID) | .user_id" -r`
#
# トータル情報集計
#
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".volumes += 1"`
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".volume_gb += $SIZE"`
USER_VOLUMES=`echo "$SUMMARY_USERS" | jq ".$USER_ID.volumes"`
if [ "$USER_VOLUMES" = 0 -o "$USER_VOLUMES" = "null" ]; then
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".volume_users += 1"`
fi
#
# ユーザ別情報集計
#
for KEY in ${SUMMARY_USER_KEYS[@]}
do
[ "$KEY" = "user_id" ] && continue
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.$KEY += 0"`
done
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.user_id = \"$USER_ID\""`
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.volumes += 1"`
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.volume_gb += $SIZE"`
done
#########
#
# ボリュームスナップショットリストの配列
# - 必要なカラムのみ抽出
VOLUME_SNAPSHOTS=(`echo "$VOLUME_SNAPSHOT_DETAIL" | \
jq '.snapshots[] | { id, size }' \
--compact-output`)
#
# ボリュームスナップショット情報を1つずつループ
#
for VOLUME_SNAPSHOT in "${VOLUME_SNAPSHOTS[@]}"
do
#
# データ整理
#
VOLUME_SNAPSHOT_ID=`echo "$VOLUME_SNAPSHOT" | jq .id`
SIZE=` echo "$VOLUME_SNAPSHOT" | jq .size`
USER_ID=` echo "$VOLUME_SNAPSHOT_USERS" | \
jq ".snapshots[] | select(.id == $VOLUME_SNAPSHOT_ID) | .user_id" -r`
#
# トータル情報集計
#
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".volume_snapshots += 1"`
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".volume_snapshot_gb += $SIZE"`
USER_VOLUME_SNAPSHOTS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.volume_snapshots"`
if [ "$USER_VOLUME_SNAPSHOTS" = 0 -o "$USER_VOLUME_SNAPSHOTS" = "null" ]; then
SUMMARY_TOTAL=`echo "$SUMMARY_TOTAL" | jq ".volume_snapshot_users += 1"`
fi
#
# ユーザ別情報集計
#
for KEY in ${SUMMARY_USER_KEYS[@]}
do
[ "$KEY" = "user_id" ] && continue
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.$KEY += 0"`
done
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.user_id = \"$USER_ID\""`
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.volume_snapshots += 1"`
SUMMARY_USERS=`echo "$SUMMARY_USERS" | jq ".$USER_ID.volume_snapshot_gb += $SIZE"`
done
#######################################
#
# データ整理
#
#
# 色んなソートを用意
# - それぞれのソートで値が 0 のVMは表示から除外する
SORT_OS=( `echo "$SUMMARY_OS" | jq "[.[]] | sort_by(.count) | .[]" --compact-output`)
SORT_USER_ID_USERS=( `echo "$SUMMARY_USERS" | jq "[.[]] | sort_by(.user_id) | .[]" --compact-output`)
SORT_VM_USERS=( `echo "$SUMMARY_USERS" | jq "[.[]] | sort_by(.vm) | .[] | select(.vm > 0)" --compact-output`)
SORT_vCPUs_USERS=( `echo "$SUMMARY_USERS" | jq "[.[]] | sort_by(.vcpus) | .[] | select(.vcpus > 0)" --compact-output`)
SORT_RAM_USERS=( `echo "$SUMMARY_USERS" | jq "[.[]] | sort_by(.ram) | .[] | select(.ram > 0)" --compact-output`)
SORT_VOLUME_GB_USERS=(`echo "$SUMMARY_USERS" | jq "[.[]] | sort_by(.volume_gb) | .[] | select(.volumes > 0)" --compact-output`)
#
# トータル値
#
TOTAL_VM=` echo "$SUMMARY_TOTAL" | jq ".vm"`
TOTAL_vCPUs=` echo "$SUMMARY_TOTAL" | jq ".vcpus"`
TOTAL_RAM=` echo "$SUMMARY_TOTAL" | jq ".ram"`
TOTAL_IMAGES=` echo "$SUMMARY_TOTAL" | jq ".images"`
TOTAL_IMAGE_BYTES=` echo "$SUMMARY_TOTAL" | jq ".image_bytes"`
TOTAL_IMAGE_GB=$(($TOTAL_IMAGE_BYTES / 1024 / 1024 / 1024))
TOTAL_IMAGE_SNAPSHOTS=` echo "$SUMMARY_TOTAL" | jq ".image_snapshots"`
TOTAL_IMAGE_SNAPSHOT_BYTES=` echo "$SUMMARY_TOTAL" | jq ".image_snapshot_bytes"`
TOTAL_IMAGE_SNAPSHOT_GB=$(($TOTAL_IMAGE_SNAPSHOT_BYTES / 1024 / 1024 / 1024))
TOTAL_VOLUMES=` echo "$SUMMARY_TOTAL" | jq ".volumes"`
TOTAL_VOLUME_GB=` echo "$SUMMARY_TOTAL" | jq ".volume_gb"`
TOTAL_VOLUME_SNAPSHOTS=` echo "$SUMMARY_TOTAL" | jq ".volume_snapshots"`
TOTAL_VOLUME_SNAPSHOT_GB=` echo "$SUMMARY_TOTAL" | jq ".volume_snapshot_gb"`
# ユニークユーザ数
TOTAL_VM_USERS=` echo "$SUMMARY_TOTAL" | jq ".vm_users"`
TOTAL_IMAGE_SNAPSHOT_USERS=` echo "$SUMMARY_TOTAL" | jq ".image_snapshot_users"`
TOTAL_VOLUME_USERS=` echo "$SUMMARY_TOTAL" | jq ".volume_users"`
TOTAL_VOLUME_SNAPSHOT_USERS=`echo "$SUMMARY_TOTAL" | jq ".volume_snapshot_users"`
# クォータ
QUOTA_INSTANCES=`echo "$QUOTA_COMPUTE" | jq ".quota_set.instances"`
QUOTA_CORES=` echo "$QUOTA_COMPUTE" | jq ".quota_set.cores"`
QUOTA_RAM=` echo "$QUOTA_COMPUTE" | jq ".quota_set.ram"`
QUOTA_VOLUMES=` echo "$QUOTA_VOLUME" | jq ".quota_set.volumes"`
QUOTA_VOLUME_GB=`echo "$QUOTA_VOLUME" | jq ".quota_set.gigabytes"`
QUOTA_SNAPSHOTS=`echo "$QUOTA_VOLUME" | jq ".quota_set.snapshots"`
#######################################
#
# データ出力
#
#
# プロジェクト情報
#
cat <<EOT
====================
Project Info
--------------------
Project Name : $PROJECT_NAME
Project ID : $PROJECT_ID
EOT
#
# トータル情報
#
cat <<EOT
====================
TOTAL
--------------------
VM Users : $TOTAL_VM_USERS
Image Users : -
Image SS Users : $TOTAL_IMAGE_SNAPSHOT_USERS
Volume Users : $TOTAL_VOLUME_USERS
Volume SS Users : $TOTAL_VOLUME_SNAPSHOT_USERS
VM : $TOTAL_VM / $QUOTA_INSTANCES
vCPUs : $TOTAL_vCPUs / $QUOTA_CORES
RAM : $TOTAL_RAM / $QUOTA_RAM (MB)
Images (All) : $TOTAL_IMAGES / -
ImageGB (All) : $TOTAL_IMAGE_GB / - (GB)
Snapshots : $TOTAL_IMAGE_SNAPSHOTS / -
SnapshotGB : $TOTAL_IMAGE_SNAPSHOT_GB / - (GB)
Volumes : $TOTAL_VOLUMES / $QUOTA_VOLUMES
VolumeGB : $TOTAL_VOLUME_GB / $QUOTA_VOLUME_GB (GB)
Snapshots : $TOTAL_VOLUME_SNAPSHOTS / $QUOTA_SNAPSHOTS
SnapshotGB : $TOTAL_VOLUME_SNAPSHOT_GB / - (GB)
EOT
#
# OS数
#
cat <<EOT
====================
OS Image
--------------------
EOT
for JSON in "${SORT_OS[@]}"
do
OS_NAME=` echo "$JSON" | jq ".name" -r`
OS_COUNT=`echo "$JSON" | jq ".count"`
[ -z "$OS_NAME" ] && continue
SPACE_NUM=$((24 - ${#OS_NAME}))
SPACES=`eval "printf ' %.0s' {1..$SPACE_NUM}"`
echo "$OS_NAME$SPACES: $OS_COUNT"
done
echo
#
# ユーザIDソート
# - 全データ表示
cat <<EOT
====================
User's All Data
--------------------
EOT
for JSON in "${SORT_USER_ID_USERS[@]}"
do
USER_ID=` echo "$JSON" | jq ".user_id" -r`
VM_COUNT=` echo "$JSON" | jq ".vm"`
vCPUs=` echo "$JSON" | jq ".vcpus"`
RAM=` echo "$JSON" | jq ".ram"`
IMAGE_SNAPSHOTS=` echo "$JSON" | jq ".image_snapshots"`
IMAGE_SNAPSHOT_BYTES=`echo "$JSON" | jq ".image_snapshot_bytes"`
IMAGE_SNAPSHOT_GB=$(($IMAGE_SNAPSHOT_BYTES / 1024 / 1024 / 1024))
VOLUMES=` echo "$JSON" | jq ".volumes"`
VOLUME_GB=` echo "$JSON" | jq ".volume_gb"`
VOLUME_SNAPSHOTS=` echo "$JSON" | jq ".volume_snapshots"`
VOLUME_SNAPSHOT_GB=` echo "$JSON" | jq ".volume_snapshot_gb"`
[ -z "$USER_ID" ] && continue
SPACE_NUM=$((24 - ${#USER_ID}))
SPACES=`eval "printf ' %.0s' {1..$SPACE_NUM}"`
echo -n "$USER_ID$SPACES: VM=$VM_COUNT, vCPUs=$vCPUs, RAM=$RAM, "
echo -n "ImageSnapshots=$IMAGE_SNAPSHOTS, ImageSnapshotGB=$IMAGE_SNAPSHOT_GB, "
echo -n "Volumes=$VOLUMES, VolumeGB=$VOLUME_GB, "
echo "VolumeSnapshots=$VOLUME_SNAPSHOTS, VolumeSnapshotGB=$VOLUME_SNAPSHOT_GB"
done
echo
#
# ユーザ別VM数ソート
#
cat <<EOT
====================
User's VM Rank
--------------------
EOT
for JSON in "${SORT_VM_USERS[@]}"
do
USER_ID=` echo "$JSON" | jq ".user_id" -r`
VM_COUNT=` echo "$JSON" | jq ".vm"`
vCPUs=` echo "$JSON" | jq ".vcpus"`
RAM=` echo "$JSON" | jq ".ram"`
[ -z "$USER_ID" ] && continue
SPACE_NUM=$((24 - ${#USER_ID}))
SPACES=`eval "printf ' %.0s' {1..$SPACE_NUM}"`
echo "$USER_ID$SPACES: VM=$VM_COUNT, vCPUs=$vCPUs, RAM=$RAM"
done
echo
#
# ユーザ別vCPU数ソート
#
cat <<EOT
====================
User's vCPUs Rank
--------------------
EOT
for JSON in "${SORT_vCPUs_USERS[@]}"
do
USER_ID=` echo "$JSON" | jq ".user_id" -r`
VM_COUNT=` echo "$JSON" | jq ".vm"`
vCPUs=` echo "$JSON" | jq ".vcpus"`
[ -z "$USER_ID" ] && continue
SPACE_NUM=$((24 - ${#USER_ID}))
SPACES=`eval "printf ' %.0s' {1..$SPACE_NUM}"`
echo "$USER_ID$SPACES: vCPUs=$vCPUs, VM=$VM_COUNT"
done
echo
#
# ユーザ別メモリ数ソート
#
cat <<EOT
====================
User's RAM Rank
--------------------
EOT
for JSON in "${SORT_RAM_USERS[@]}"
do
USER_ID=` echo "$JSON" | jq ".user_id" -r`
VM_COUNT=` echo "$JSON" | jq ".vm"`
vCPUs=` echo "$JSON" | jq ".vcpus"`
RAM=` echo "$JSON" | jq ".ram"`
[ -z "$USER_ID" ] && continue
SPACE_NUM=$((24 - ${#USER_ID}))
SPACES=`eval "printf ' %.0s' {1..$SPACE_NUM}"`
echo "$USER_ID$SPACES: RAM=$RAM, VM=$VM_COUNT, vCPUs=$vCPUs"
done
echo
#
# ユーザ別ボリューム容量ソート
#
cat <<EOT
====================
User's Volume Rank
--------------------
EOT
for JSON in "${SORT_VOLUME_GB_USERS[@]}"
do
USER_ID=` echo "$JSON" | jq ".user_id" -r`
VM_COUNT=` echo "$JSON" | jq ".vm"`
VOLUMES=` echo "$JSON" | jq ".volumes"`
VOLUME_GB=`echo "$JSON" | jq ".volume_gb"`
[ -z "$USER_ID" ] && continue
SPACE_NUM=$((24 - ${#USER_ID}))
SPACES=`eval "printf ' %.0s' {1..$SPACE_NUM}"`
echo "$USER_ID$SPACES: VolumeGB=$VOLUME_GB, Volumes=$VOLUMES, VM=$VM_COUNT"
done
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.