Skip to content

Instantly share code, notes, and snippets.

@stalluri
Last active December 5, 2017 11:49
Show Gist options
  • Save stalluri/037ed9bef4c127ec66dc to your computer and use it in GitHub Desktop.
Save stalluri/037ed9bef4c127ec66dc to your computer and use it in GitHub Desktop.
Publish memory utilization in percentage to AWS Cloudwatch
#! /bin/sh
###############################################
# Purpose : To push an application metric (Memory utiization metric) for visibility and subsequently creating alarms on it
# Usage : Run this script on ec2 instance
# Crontab usage : */1 * * * * /home/ec2-user/publishProcessStatus.sh
#
# Dependencies : AWS CLI needs to be installed
# Author : Neptune.io, Inc.
###############################################
###############################################
# PLEASE CHANGE these variables appropriately
###############################################
export AWS_ACCESS_KEY_ID="AKISDFLKJAXNK2LK3SXKJX"
export AWS_SECRET_ACCESS_KEY="adlkjadf23lkWlskjdaldjfadfcindaxlk"
export AWS_REGION="us-east-1"
METRIC_NAME="my_app_server1_memory_utiliz_percent"
NAME_SPACE="ApplicationMetrics"
UNIT="None"
###############################################
# User defined script to collect an application metric
###############################################
# This value eventually will be published to AWS cloudwatch
METRIC_VALUE=0
# Memory utilization monitoring script to populate metric
METRIC_VALUE=`free | grep Mem | awk '{print $3/$2 * 100.0}'`
###############################################
# Publish metric to AWS cloudwatch
###############################################
# Fetch hostname
HOST_INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
# Add more dimensions in the format of Name=XYZ,Value=ABC and give the same in ALARM_METRIC_DIMENSIONS
METRIC_DIMENSIONS="InstanceId=$HOST_INSTANCE_ID"
/usr/local/bin/aws cloudwatch put-metric-data --namespace $NAME_SPACE --metric-name $METRIC_NAME --value $METRIC_VALUE --unit $UNIT --dimensions $METRIC_DIMENSIONS --region $AWS_REGION --timestamp "`date`"
###### END OF SCRIPT ##########################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment