Last active
November 5, 2017 22:21
-
-
Save lancehudson/b1057713ba6ae58c630a1836989b44f0 to your computer and use it in GitHub Desktop.
AWS Custom Metrics
This file contains 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
*/5 * * * * /usr/local/bin/putMetrics.sh |
This file contains 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
#!/bin/bash | |
die() { status=$1; shift; echo "FATAL: $*"; exit $status; } | |
EC2_INSTANCE_ID="`wget -q -O - http://instance-data/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://instance-data/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:'`" | |
DATE="`date +%FT%T.000Z`" | |
MEMFREE="`free -m | grep Mem | awk '{print $4}'`" | |
MEMUSED="`free -m | grep Mem | awk '{print $3}'`" | |
MEMUTIL="`free -m | grep Mem | awk '{print $3/$2 * 100.0}'`" | |
aws --region $EC2_REGION cloudwatch put-metric-data --metric-name MemoryFree --namespace AWS/EC2 --unit Megabytes --value $MEMFREE --timestamp $DATE --dimensions InstanceId=$EC2_INSTANCE_ID | |
aws --region $EC2_REGION cloudwatch put-metric-data --metric-name MemoryUsed --namespace AWS/EC2 --unit Megabytes --value $MEMUSED --timestamp $DATE --dimensions InstanceId=$EC2_INSTANCE_ID | |
aws --region $EC2_REGION cloudwatch put-metric-data --metric-name MemoryUtilization --namespace AWS/EC2 --unit Percent --value $MEMUTIL --timestamp $DATE --dimensions InstanceId=$EC2_INSTANCE_ID | |
df -l -x tmpfs -x devtmpfs -x overlay | tail -n +2 | while read line ; do | |
MOUNT="`echo \"$line\" | awk '{print $6}'`" | |
DISKUSED="`echo \"$line\" | awk '{print $4 / 1024}'`" | |
DISKFREE="`echo \"$line\" | awk '{print $3 / 1024}'`" | |
DISKUTIL="`echo \"$line\" | awk '{print $3/$2 * 100.0}'`" | |
aws --region $EC2_REGION cloudwatch put-metric-data --metric-name DiskFree --namespace AWS/EC2 --unit Megabytes --value $DISKFREE --timestamp $DATE --dimensions InstanceId=$EC2_INSTANCE_ID,MountPoint=$MOUNT | |
aws --region $EC2_REGION cloudwatch put-metric-data --metric-name DiskUsed --namespace AWS/EC2 --unit Megabytes --value $DISKUSED --timestamp $DATE --dimensions InstanceId=$EC2_INSTANCE_ID,MountPoint=$MOUNT | |
aws --region $EC2_REGION cloudwatch put-metric-data --metric-name DiskUtilization --namespace AWS/EC2 --unit Percent --value $DISKUTIL --timestamp $DATE --dimensions InstanceId=$EC2_INSTANCE_ID,MountPoint=$MOUNT | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment