Skip to content

Instantly share code, notes, and snippets.

@lewayotte
Last active July 24, 2017 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lewayotte/06f33d682052638b8cf6bca463f12a10 to your computer and use it in GitHub Desktop.
Save lewayotte/06f33d682052638b8cf6bca463f12a10 to your computer and use it in GitHub Desktop.
AWS Bandwidth Usage Report by Instance
#!/bin/bash
EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`"
test -n "$EC2_INSTANCE_ID" || die 'cannot obtain instance-id'
EC2_AVAIL_ZONE="`wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone || die \"wget availability-zone has failed: $?\"`"
test -n "$EC2_AVAIL_ZONE" || die 'cannot obtain availability-zone'
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
STARTTIME=`date -d "-1 month -$(($(date +%-d)-1)) days" +%Y-%m-%dT00:00:00` #First day of Last Month
ENDTIME=`date -d "-$(($(date +%-d)-1)) days" +%Y-%m-%dT00:00:00` #Last day of Last Month
#echo $EC2_INSTANCE_ID . ' ' . $EC2_AVAIL_ZONE . ' ' . $EC2_REGION
SUM=0
for DATA in `/usr/local/bin/aws cloudwatch get-metric-statistics --metric-name NetworkOut --start-time $STARTTIME --end-time $ENDTIME --period 3600 --namespace AWS/EC2 --statistics Sum --dimensions Name=InstanceId,Value=$EC2_INSTANCE_ID --region $EC2_REGION --output text | awk -F" " '{print $2}'`; do
SUM=`expr $SUM + ${DATA%.*}` #%.* removes the float stuff
done
echo "$SUM Bytes"
GB=`expr $SUM / 1000 / 1000 / 1000`
echo "$GB Gigabytes"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment