Think of a way to add custom metrics to AWS CloudWatch

Introduction

We want to monitor to create a custom metrics to AWS CloudWatch. So I tried various examined Omoita',

will be to leave a Memorandum of Understanding to examine incidentally. CloudWatch is free frame in the 10 metrics, it can be set up to 10 alarm

By the way, setting even Ikeru have also monitoring system data on-premise server if, but may not be merit to bring ...

Install

1. because the tools used to add custom metrics of mon-put-data command of the familiar aws-apitools-mon, to install it.

※ By the way, it was noticed not read the instructions from the later, it is the same thing even aws-cli (futility feeling ...)

aws cloudwatch put-metric-data [option] [option] [option]
 

Such as felt, along with the option to use an option that can be used to send in a mon-put-data command.

Package-on

  • MacOSX

brew install aws-mon
 

  • Debian

sudo apt-get aws-mon 
 

  • Amazon Linux

sudo yum install aws-apitools-mon
 

  • CentOS 6.x

sudo yum install perl-DateTime perl-CPAN perl-Net-SSLeay perl-IO-Socket-SSL perl-Digest-SHA gcc -y

sudo yum install zip unzip
sudo yum install cloud-init
curl -L -O http://ec2-downloads.s3.amazonaws.com/CloudWatch-2010-08-01.zip
unzip CloudWatch-2010-08-01.zip
sudo cp -rfp CloudWatch-1.0.20.0/opt/aws/CloudWatch

※ Please put the 1.8_u171 later there if java-1.8.0-openjdk or oracle jdk entered so also are JDK

sudo yum install java-1.8.0-openjdk
 

  • CentOS 7.x

sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA -y 

sudo yum install zip unzip
curl -L -O http://ec2-downloads.s3.amazonaws.com/CloudWatch-2010-08-01.zip
unzip CloudWatch-2010-08-01.zip
sudo cp -rfp CloudWatch-1.0.20.0/opt/aws/CloudWatch

※ Please put the 1.8_u171 later there if java-1.8.0-openjdk or oracle jdk entered so also are JDK

sudo yum install java-1.8.0-openjdk
 

  • Other Linux, if a dependency can not be resolved, we recommend the installation of the CloudWatch-Agent

curl -L -O https://s3.amazonaws.com/amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip

unzip AmazonCloudWatchAgent.zip
sudo ./install.sh

  • Because Windows is CloudWatch-CLI is difficult, is good to put a quiet CloudWatch-Agent

https://s3.amazonaws.com/amazoncloudwatch-agent/windows/amd64/latest/AmazonCloudWatchAgent.zip
 

About Credential

For policy permissions of the IAM,


 CloudWatchActionsEC2Access

 ShiloudWatchAgentAdminPolicy

 ShieruoudWatchAgentServerPolicy

It should be fine as long as attached. However, once if you want to be other than the PUT from CLI side


 CloudWatchFullAccess

Put in, make sure the access state, gradually closed the also whether hand.

Credentials File Writing

To less than or equal to the specified path in AWS_CLOUDWATCH_HOME it is recommended to make the credentials file. Permissions and 400, the owner let's the user to run the shell or agent, format

credentials
AWSAccessKeyId=<Write your AWS access ID>

AWSSecretKey=<Write your AWS secret key>

Via Proxy

If you have a server for some reason on the back of the PROXY, you need to'll make a set of PROXY

or in the case of Linux, write the configuration to/etc/bashrc, defined in the script side to move as an agent

/etc/bashrc
USERNAME = "<USERNAME>" # authentication required proxy case is
 
PASSWORD = "<PASSWORD>" # authentication is required proxy if
HOST="<HOST>"
there is PORT1 = "<PORT>" # plurality of ports If
PORT2 = "<pORT>" # If you have more than one port

# Authentication if necessary proxy
PROXY1="$USERNAME:$PASSWORD@$HOST:$PORT1"
PROXY2="$USERNAME:$PASSWORD@$HOST:$PORT2"

# Authentication is the case of unnecessary proxy
PROXY1="$HOST:$PORT1"
PROXY2="$HOST:$PORT2"

# Of actual environment variable settings (http */ftp and socks exclusive)
export http_proxy="http://$PROXY1"
export https_proxy="https://$PROXY1"
export ftp_proxy="ftp://$PROXY1"
#export http_proxy="socks5://$PROXY2"
#export https_proxy="socks5://$PROXY2"
#export ftp_proxy="socks5://$PROXY2"

# Program that only uppercase version does not recognize (http */ftp and socks exclusive)
export HTTP_PROXY="http://$PROXY"
export HTTPS_PROXY="https://$PROXY"
export FTP_PROXY="ftp://$PROXY"
#export HTTP_PROXY="socks5://$PROXY2"
#export HTTPS_PROXY="socks5://$PROXY2"
#export FTP_PROXY="socks5://$PROXY2"

# Specification of addresses that do not use a proxy (but CIDR setting can not be the wild card specified)
export no_proxy="10.*.*.*,172.16.*.*,192.168.*.*,*.local,localhost,127.0.0.1"
export NO_PROXY="$no_proxy"

Feeding the data to CloudWatch

option often used to send in a mon-put-data command

other options Let's look at the help of the head family
https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/cli/cli-mon-put-data.html

namespace: Category (※ required fields)
 
within ANSI in 250 characters

metric-name: The name of the metrics (※ required fields)
within ANSI in 250 characters, freedom can be set, ANSI, so multi-byte is impossible

dimensions: up to 10 can be set tag (Key is less than 250 characters in the ANSI, use key is fixed) (any item)
key1=value1, key2=value2

unit: unit of metrics, number Toka percentage Toka
(Micro/Milli) Seconds: the case of the time
(Kilo/Mega/Giga/Tera ) Bytes: the case of the capacity (in bytes)
(Kilo/Mega/Giga/Tera) Bits: the capacity of the If (bit)
percent: percent
count: simple counting
(Kilo/Mega/Giga/Tera ) bytes/Second: for rate (bytes/s)
(Kilo/Mega/Giga/Tera) bits/Second: for rate (bit/s)
count/Second: If the rate (the number of times/s)

value: value

Writing of command

mon-put-data --metric-name "metrics of the name" --namespace "metrics namespace of" --dimensions "InstanceID = ID of the instance" --value "value" --unit "Count"
 

Sample script

The following is to inform the free% of storage that is mounted on EC2.

disk_use_percent.sh
#!/bin/sh

# Passed through the path in the command
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/aws/CloudWatch:/opt/aws/CloudWatch/bin

Environment variable to the #CloudWatch tool
export AWS_CLOUDWATCH_HOME=/opt/aws/CloudWatch
export AWS_CREDENTIAL_FILE=/opt/aws/CloudWatch/credentials

### at the time via PROXY to enable
# PROXY = "USERNAME: PASSWORD @ HOST: PORT" # authentication is required proxy
# PROXY = "HOST: PORT" # authentication is unnecessary proxy
#export http_proxy="http://$PROXY"
#export https_proxy="https://$PROXY"
#export ftp_proxy="ftp://$PROXY"
# # # do not use the Proxy host (CIDR not/wild cards accepted)
#export no_proxy="10.*.*.*,192.168.*.*,*.local,localhost,127.0.0.1"
#export NO_PROXY="$no_proxy"

# Instance name and host name dynamically acquire
INSTANCEID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
PUBLICNAME=`curl -s http://169.254.169.254/latest/meta-data/public-hostname`
#INSTANCEID=`curl -x "$PROXY" -s http://169.254.169.254/latest/meta-data/instance-id`
#PUBLICNAME=`curl -x "$PROXY" -s http://169.254.169.254/latest/meta-data/public-hostname`

# Commands for data sent
DISKUSE=`df |grep xvda1|awk '{print $5}'|tr -d '%'`

# Error: If data is greater than 100
if [ ${DISKUSE} -gt 100 ]; then
echo "Error : Illegal Data : ${DISKUSE}"
exit 1
fi

# Error: data - when the value
if [ ${DISKUSE} -lt 0 ]; then
echo "Error : Illegal Data : ${DISKUSE}"
exit 1
fi

# Error: If the data is Null
if [ "x${DISKUSE}" = "x" ]; then
echo "Error : Illegal Data : ${DISKUSE}"
exit 1
fi

# Data transmission
mon-put-data --metric-name "Disk Use" --namespace "${PUBLICNAME}" --dimensions "InstanceId=${INSTANCEID}" --value "${DISKUSE}" --unit "Percent"

There was a lot of twists and turns, once data can now be displayed is sent to the CloudWatch

Toka that the command of CloudWatch has not been updated from the 2010-08-01, Toka remains Honke manual is in English, I want plunged many

into the future, I feel that intention do not wonder you want to aggregate to CloudWatch Agent can be seen.

Or more, and I hope if someone of reference.