Skip to content

Instantly share code, notes, and snippets.

@cm-watanabeseigo
Last active March 3, 2017 05:01
Show Gist options
  • Save cm-watanabeseigo/a6cf9dab30f0ef29234f53fb0a3197ff to your computer and use it in GitHub Desktop.
Save cm-watanabeseigo/a6cf9dab30f0ef29234f53fb0a3197ff to your computer and use it in GitHub Desktop.
#!/bin/bash
# print paramaters and format
PARAMS="
DBInstanceIdentifier
AvailabilityZone
SecondaryAvailabilityZone
DBInstanceClass
DBInstanceStatus
"
FORMAT="[%s] %s %s (%s) %s %s\n"
DEFAULT_INTERVAL=10
# parse args
RDS_NAME=${1:-help}
INTERVAL=${2:-${DEFAULT_INTERVAL}}
# show usage (help)
case "$RDS_NAME" in
*help|-h)
printf "usage: %s <rds name> [<interval>]\n" $(basename $0)
printf " default interval: %s (sec)\n" $DEFAULT_INTERVAL
exit
;;
esac
# validate INTERVAL
expr $INTERVAL + 1 >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "please specify interval as number."
exec $0 help
fi
# build AWSCLI command line
cmd="aws rds describe-db-instances --db-instance-identifier ${RDS_NAME}"
cmd="$cmd --query DBInstances[].[$(echo $PARAMS | xargs | sed -e 's/ /,/g')]"
cmd="$cmd --output text"
# check command line (runnable or die)
$cmd >/dev/null || exit
# loop
printf "$FORMAT" "Time" $PARAMS
while :
do
printf "$FORMAT" $(date +%T) $($cmd 2>/dev/null)
sleep $INTERVAL || exit
done

使い方

  • PATHの通ったところにおいて実行権限つけてください
  • AWSCLIが動作するよう環境設定は別途行って下さい
  • 第1引数にRDS名を指定
  • 第2引数にインターバル(秒数)
    • 無指定の場合は10秒がデフォルト

usage)

$ aws-rdsstat --help
usage: aws-rdsstat <rds name> [<interval>]
  default interval: 10 (sec)

ex)

$ export AWS_DEFAULT_PROFILE=default
$ aws-rdsstat my-rds01

項目を増やす場合

  1. 5~9行目のどこかに項目名を追加(describe-db-instancesの結果とにらめっこして決めて下さい)
  2. 11行目のFORMATの内容を編集(適当な位置に%sを追加)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment