Skip to content

Instantly share code, notes, and snippets.

@shahkamran
Forked from stalluri/diskStatus.sh
Created February 13, 2021 14:44
Show Gist options
  • Save shahkamran/9905bd6f079587d27e6dcf5603a0d5b4 to your computer and use it in GitHub Desktop.
Save shahkamran/9905bd6f079587d27e6dcf5603a0d5b4 to your computer and use it in GitHub Desktop.
Publish disk utilization percentage metric to AWS Cloudwatch
#! /bin/sh
###############################################
# Purpose : To push an application metric (Disk 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_disk_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
# Disk utilization monitoring script to populate metric
# Change the revelant mount path in the grep parameter below
METRIC_VALUE=`df -hl | grep xvda1 | awk '{print $5}' | sed 's/%//g'`
###############################################
# 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