You can use a simple shell script with cron on EC2 node to collect and report memory utilization to CloudWatch as custom metrics.
First, you will need to get and configure CloudWatch command line tools.
Download Amazon CloudWatch command line tools from here: http://aws.amazon.com/developertools/2534
http://ec2-downloads.s3.amazonaws.com/CloudWatch-2010-08-01.zip
Set up CloudWatch command line tools: http://docs.amazonwebservices.com/AmazonCloudWatch/latest/GettingStartedGuide/SetupCLI.html
Copy, paste, and if needed adjust the following bash script.
#################################################################
File: /home/ec2-user/memreport.sh
#!/bin/bash
export AWS_CLOUDWATCH_HOME=/home/ec2-user/CloudWatch-1.0.12.1
export AWS_CREDENTIAL_FILE=$AWS_CLOUDWATCH_HOME/credentials
export AWS_CLOUDWATCH_URL=https://monitoring.amazonaws.com
export PATH=$AWS_CLOUDWATCH_HOME/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 -m | grep 'Mem' | tr -s ' ' | cut -d ' ' -f 2`
memfree=`free -m | grep 'buffers/cache' | tr -s ' ' | cut -d ' ' -f 4`
let "memused=100-memfree*100/memtotal"
mon-put-data --metric-name "UsedMemoryPercent" --namespace "System/Linux" --dimensions "InstanceId=$instanceid" --value "$memused" --unit "Percent"
#################################################################
Now add invocation of this script to the crontab, e.g. call it every 5 minutes.
First, you will need to get and configure CloudWatch command line tools.
http://ec2-downloads.s3.amazonaws.com/CloudWatch-2010-08-01.zip
Set up CloudWatch command line tools: http://docs.amazonwebservices.com/AmazonCloudWatch/latest/GettingStartedGuide/SetupCLI.html
Copy, paste, and if needed adjust the following bash script.
#################################################################
File: /home/ec2-user/memreport.sh
#!/bin/bash
export AWS_CLOUDWATCH_HOME=/home/ec2-user/CloudWatch-1.0.12.1
export AWS_CREDENTIAL_FILE=$AWS_CLOUDWATCH_HOME/credentials
export AWS_CLOUDWATCH_URL=https://monitoring.amazonaws.com
export PATH=$AWS_CLOUDWATCH_HOME/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 -m | grep 'Mem' | tr -s ' ' | cut -d ' ' -f 2`
memfree=`free -m | grep 'buffers/cache' | tr -s ' ' | cut -d ' ' -f 4`
let "memused=100-memfree*100/memtotal"
freeStorageSpace='df -m|grep "/dev/xvda"|tr -s ' '|cut -d ' ' -f 4'
mon-put-data --metric-name "FreeStorageSpace" --namespace "System/Linux" --dimensions "InstanceId=$instanceid" --value "$freeStorageSpace" --unit "Megabytes"
mon-put-data --metric-name "UsedMemoryPercent" --namespace "System/Linux" --dimensions "InstanceId=$instanceid" --value "$memused" --unit "Percent"
#################################################################
Now add invocation of this script to the crontab, e.g. call it every 5 minutes.
*/5 * * * * /home/ec2-user/memreport.sh