LoginSignup
14
15

More than 5 years have passed since last update.

EC2,CloudWatchでメモリ使用量とかを監視する方法

Last updated at Posted at 2012-12-11

以下のシェルを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/

14
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
15