Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atushi/6306265 to your computer and use it in GitHub Desktop.
Save atushi/6306265 to your computer and use it in GitHub Desktop.
How to use the Cloud Watch API about getting the instance status with proxy.
#!/bin/bash
# setting for proxy
export EC2_JVM_ARGS="-Dhttp.proxySet=true -DproxyHost=YOURPROXYHOST -DproxyPort=YOURPROXYPORT"
export SERVICE_JVM_ARGS=${EC2_JVM_ARGS}
# setting for cloud watch
export AWS_CREDENTIAL_FILE=$AWS_CLOUDWATCH_HOME/credentials
export AWS_CLOUDWATCH_URL=https://monitoring.amazonaws.com
export PATH=$AWS_CLOUDWATCH_HOME/bin:$PATH
# get ec2 instance id
instanceid=`wget --no-proxy -q -O - http://169.254.169.254/latest/meta-data/instance-id`
# memory
mem_total=`free -m | grep 'Mem' | tr -s ' ' | cut -d ' ' -f 2`
mem_free=`free -m | grep 'buffers/cache' | tr -s ' ' | cut -d ' ' -f 4`
let "mem_used=mem_total-mem_free"
let "mem_used_percent=mem_used*100/mem_total"
# diskUsage
diskusage=$(df | grep 'xvde' | tr -s ' ' | cut -d ' ' -f 5 | cut -d '%' -f 1)
# loadaverage
loadaverage=$(uptime | sed -e "s@^.*load average: @@" | tr -s ' ' | cut -d ' ' -f 1 | cut -d ',' -f 1)
# put the JVM info to the CloudWatch
mon-put-data --metric-name "FreeMemoryMBytes" --namespace "Status" --dimensions "InstanceId=$instanceid" --value "$mem_free" --unit "Megabytes" --timestamp "$TimeStamp" --region ap-southeast-1
mon-put-data --metric-name "UsedMemoryPercent" --namespace "Status" --dimensions "InstanceId=$instanceid" --value "$mem_used_percent.00" --unit "Percent" --timestamp "$TimeStamp" --region ap-southeast-1
mon-put-data --metric-name "DiskUsage" --namespace "Status" --dimensions "InstanceId=$instanceid" --value "$diskusage.00" --unit "Percent" --timestamp "$TimeStamp" --region ap-southeast-1
mon-put-data --metric-name "LoadAverage" --namespace "Status" --dimensions "InstanceId=$instanceid" --value "$loadaverage" --unit "Count" --timestamp "$TimeStamp" --region ap-southeast-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment