Skip to content

Instantly share code, notes, and snippets.

@bageljp
Created April 16, 2013 04:47
Show Gist options
  • Save bageljp/5393418 to your computer and use it in GitHub Desktop.
Save bageljp/5393418 to your computer and use it in GitHub Desktop.
CloudWatch custom metric put.
#!/bin/bash
#--------------------------------------
#
# Author(s):
#
# K.Kadoyama
#
# Usage:
#
# mon_put.bash
#
# Revision:
#
# 1.0 by K.Kadoyama
# ---------------
# Create new.
#
#--------------------------------------
#------------------
# define
#------------------
#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
export SH_ORIG="${0##*/}"
export CMD_MON_PUT="mon-put-data"
export LOG_FAC="local0"
# global conf
#[ -e "/etc/profile.d/aws_define.sh" ] && source /etc/profile.d/aws_define.sh
source /etc/profile.d/aws_define.sh
# get ec2 instance id
instanceid="`curl -s http://169.254.169.254/latest/meta-data/instance-id`"
#------------------
# function
#------------------
func_log() {
if [ $# -lt 2 ]; then
logger -p ${LOG_FAC}.warning "### WARN[${SH_ORIG}]: argument error."
return 1
fi
case "$1" in
info)
logger -p ${LOG_FAC}.info "### INFO[${SH_ORIG}]: $2"
;;
warn | warning)
logger -p ${LOG_FAC}.warning "### WARN[${SH_ORIG}]: $2"
;;
err | error)
logger -p ${LOG_FAC}.error "### ERROR[${SH_ORIG}]: $2"
;;
*)
logger -p ${LOG_FAC}.error "### WAN[${SH_ORIG}]: Log facility is not defined. Original message is [$2]"
esac
return 0
}
func_chk_cmd() {
which "$1" > /dev/null 2>&1
ret=$?
if [ ${ret} -ne 0 ]; then
func_log "error" "$1 not found."
return 1
fi
return 0
}
func_mem_put() {
memtotal="`free -m | awk '/Mem:/ {print $2}'`"
memfree="`free -m | awk '/buffers\/cache/ {print $4}'`"
memused=`echo "scale=3; 100-${memfree}*100/${memtotal}" | bc`
[ "x" == "x${memused%%.*}" ] && memused="0${memused}
${CMD_MON_PUT} -m "MEMUtilization" -n "System/Linux" -d "InstanceId=${instanceid} -v ${memused} -u "Percent"
ret=$?
[ ${ret} -ne 0 ] && func_log "warn" "memory put error."
}
#------------------
# check
#------------------
func_chk_cmd "${CMD_MON_PUT}"
[ $? -ne 0 ] && exit $?
func_chk_cmd "bc"
[ $? -ne 0 ] && exit $?
#------------------
# main
#------------------
func_mem_put
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment