Last active
August 29, 2015 14:03
-
-
Save stalluri/8d0f04f29d12594536f7 to your computer and use it in GitHub Desktop.
Publish process status metric to AWS Cloudwatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
############################################### | |
# Purpose : To push an application metric (process status 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" | |
PROCESS_NAME="my_app_name.py" | |
METRIC_NAME="my_app_name.py_status" | |
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 | |
# Process monitoring script to populate metric | |
if pgrep $PROCESS_NAME > /dev/null 2>&1 | |
then | |
METRIC_VALUE=1 | |
else | |
METRIC_VALUE=0 | |
fi | |
############################################### | |
# 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