Last active
July 28, 2022 15:09
-
-
Save yousan/2fbb75a5850a6dbbc85cbf757960aa69 to your computer and use it in GitHub Desktop.
Check CloudWatch usage by AWS CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## The original file is here. https://dev.classmethod.jp/cloud/aws/check-cloudwatch-logs-incomigbytes-by-aws-cli/ | |
## Show CloudWatch log size as CSV (TSV) format. | |
## CloudWatchのロググループをCSV(TSV)形式で出力します | |
# Sample | |
# ./get_cloudwatch_logs_incomingbytes_sum.sh yousan-aws ap-northeast-1 3 | |
# | |
# Byte Date LogGroup MegaByte GigaByte | |
# 19820.0 2019-09-19T00:00:00Z /aws/lambda/yousantest 0.02 0.00 | |
# 35428.0 2019-09-18T00:00:00Z /aws/lambda/yousantest 0.03 0.00 | |
# 24301.0 2019-09-17T00:00:00Z /aws/lambda/yousantest 0.02 0.00 | |
# Simple usage from online: curl -s -L https://gist.githubusercontent.com/yousan/2fbb75a5850a6dbbc85cbf757960aa69/raw/974df254543c5c2f87968c63753f41d14b0f799d/get_cloudwatch_logs_incomingbytes_sum.sh | bash | |
# | |
# ./get_cloudwatch_logs_incomingbytes_sum.sh [AWS profilename] [AWS Regionname] [Days to show] | |
# | |
# Usage with AWS profile name; | |
# curl -s -L http://bit.ly/2m3KrMk | bash -s my-aws-profile ap-northeast-1 5 | |
# | |
PROFILE=${1:-default} | |
REGION=${2:-ap-northeast-1} | |
DAYSAGO=${3:-3} ### 何日分遡って確認するかを指定 | |
### MacかLinuxかの判定 | |
if [ "$(uname)" == 'Darwin' ]; then | |
OS='Mac' | |
elif [ "$(expr substr $(uname -s) 1 5)" == 'Linux' ]; then | |
OS='Linux' | |
else | |
echo "Your platform ($(uname -a)) is not supported." | |
exit 1 | |
fi | |
### LogGroupの一覧を取得 | |
LOG_GROUPS=$(aws --profile=${PROFILE} --region=${REGION} cloudwatch list-metrics \ | |
--namespace "AWS/Logs" \ | |
--metric-name "IncomingLogEvents" \ | |
--query "Metrics[].Dimensions[?Name==\`LogGroupName\`].Value" \ | |
--output text) | |
# CSV(TSV)用のヘッダー行 | |
echo -e "Byte\tDate\tLogGroup\tMegaByte\tGigaByte" | |
### LogGroup単位で前日分からDAYSAGOに指定した日数だけ処理する | |
for LOG_GROUP in ${LOG_GROUPS} | |
do | |
ENDDAY=0 | |
STARTDAY=$((ENDDAY+${DAYSAGO})) | |
### MacとLinuxでdateコマンドのオプションを指定 | |
if [ "${OS}" == "Mac" ]; then | |
STARTTIME="$(date -u -v-${STARTDAY}d +%Y-%m-%dT00:00:00Z)" | |
ENDTIME="$(date -u -v-${ENDDAY}d +%Y-%m-%dT00:00:00Z)" | |
else | |
STARTTIME="$(date -u --date "${STARTDAY} days ago" +%Y-%m-%dT00:00:00Z)" | |
ENDTIME="$(date -u --date "${ENDDAY} days ago" +%Y-%m-%dT00:00:00Z)" | |
fi | |
### LogGroupごとに1日分のIncomingBytesの合計を出力 | |
# echo "### LogGroup: ${LOG_GROUP} ###" | |
OUTPUT=$(aws --profile=${PROFILE} --region=${REGION} \ | |
cloudwatch get-metric-statistics \ | |
--namespace "AWS/Logs" \ | |
--dimensions Name=LogGroupName,Value="${LOG_GROUP}" \ | |
--metric-name "IncomingBytes" \ | |
--statistics "Sum" \ | |
--start-time "${STARTTIME}" \ | |
--end-time "${ENDTIME}" \ | |
--period 86400 \ | |
--query "reverse(sort_by(Datapoints,&Timestamp)[?Sum>\`0\`].{Sum:Sum,Timestamp:Timestamp})" \ | |
--output text) | |
IFS=$'\n' | |
for line in $OUTPUT | |
# Create each rows with MB and GB | |
do | |
# @see https://unix.stackexchange.com/questions/423453/convert-value-from-scientific-notation-to-decimal-in-shell | |
MB=$(echo $line | cut -f 1 | awk '{printf("%.2f", $1/1024/1024)}') | |
GB=$(echo $line | cut -f 1 | awk '{printf("%.2f", $1/1024/1024/1024)}') | |
echo -e "${line}\t${LOG_GROUP}\t${MB}\t${GB}" | |
done | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment