以下のシェルをCRONで定期実行
custom_metrics.sh
#!/bin/zsh
export AWS_CREDENTIAL_FILE=`dirname $0`/credential
export AWS_CLOUDWATCH_HOME=/opt/aws/apitools/mon
export PATH=/opt/aws/bin:$PATH
export JAVA_HOME=/usr/lib/jvm/jre
# get ec2 instance id
instanceid=`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`
memtotal=`free | grep 'Mem' | tr -s ' ' | cut -d ' ' -f 2`
memfree=`free | grep 'buffers/cache' | tr -s ' ' | cut -d ' ' -f 4`
let "memused=100-(memfree*100/memtotal)"
swapmemused=`free |grep Swap:|awk '{print $3}'`
loadave1=`uptime | tr -s ' ' | cut -d ' ' -f 9 | cut -d ',' -f 1`
steal=`vmstat | tail -1 | tr -s ' ' | cut -d ' ' -f 18`
mon-put-data --metric-name "SwapMemory" --namespace "DP/App" --dimensions "InstanceId=$instanceid" --value "$swapmemused" --unit "Bytes" --region ap-northeast-1
mon-put-data --metric-name "FreeMemory" --namespace "DP/App" --dimensions "InstanceId=$instanceid" --value "$memfree" --unit "Bytes" --region ap-northeast-1
mon-put-data --metric-name "UsedMemoryPercent" --namespace "DP/App" --dimensions "InstanceId=$instanceid" --value "$memused" --unit "Percent" --region ap-northeast-1
mon-put-data --metric-name "LoadAverage" --namespace "DP/App" --dimensions "InstanceId=$instanceid" --value "$loadave1" --unit "Count" --region ap-northeast-1
mon-put-data --metric-name "Steal" --namespace "DP/App" --dimensions "InstanceId=$instanceid" --value "$steal" --unit "Percent" --region ap-northeast-1
*Pathは各自の環境に合わせて
参考にした記事
http://dev.classmethod.jp/cloud/cloudwatch-custom-metrics-vmstat-free-uptime/