読者です 読者をやめる 読者になる 読者になる

IT分からんわ~_~;

IT色々お勉強中のブログ

CloudWatchでEC2を監視

標準だけだと足り無いのでカスタム用メトリックスを作成

[hoge@ip-2342-234 cloudwatch]$ ls
credential  custom_metrics.sh  http_status_check.sh
#!/bin/bash

export JAVA_HOME=/usr/lib/jvm/jre
export AWS_CLOUDWATCH_HOME=/opt/aws/apitools/mon
export EC2_REGION=ap-northeast-1
export AWS_CREDENTIAL_FILE=/home/deployer/cloudwatch/credential
instanceid=i-6786876768 # EC-2メニューの「Instances」から該当サーバのInstanceIDを取得して記述

# http status check
status=`/home/deployer/cloudwatch/http_status_check.sh https://hogehoge.work` # 監視したいurlを記述
if [ $status -eq 200 ]; then
Fail=0
else
Fail=1
fi
/opt/aws/bin/mon-put-data --metric-name "Http Status fail" --namespace "Custom Metrix" --dimensions "InstanceId=$instanceid" --value "$Fail" --unit "Count"

# memory check
memtotal=`free -m | grep 'Mem' | tr -s ' ' | cut -d ' ' -f 2`
memfree=`free -m | grep 'buffers/cache' | tr -s ' ' | cut -d ' ' -f 4`
let "memused=100-memfree*100/memtotal"
/opt/aws/bin/mon-put-data --metric-name "FreeMemoryMBytes" --namespace "Custom Metrix" --dimensions "InstanceId=$instanceid" --value "$memfree" --unit "Megabytes"
/opt/aws/bin/mon-put-data --metric-name "UsedMemoryPercent" --namespace "Custom Metrix" --dimensions "InstanceId=$instanceid" --value "$memused" --unit "Percent"

# sidekiq check
sidekiq=`ps aux | grep sidekiq | grep -v grep | wc -l`

if [ "$sidekiq" -gt "0" ]; then
 SidekiqProcesses=1
else
 SidekiqProcesses=0
fi
/opt/aws/bin/mon-put-data --metric-name "Sidekiq Processes" --namespace "Custom Metrix" --dimensions "InstanceId=$instanceid" --value "$SidekiqProcesses" --unit "Count"

# redis check
redis=`ps cax | grep redis | wc -l`
if [ "$redis" -gt "0" ]; then
 RedisProcesses=1
else
 RedisProcesses=0
fi
/opt/aws/bin/mon-put-data --metric-name "Redis Processes" --namespace "Custom Metrix" --dimensions "InstanceId=$instanceid" --value "$RedisProcesses" --unit "Count"

# nginx check
nginx=`ps cax | grep nginx | wc -l`

if [ "$nginx" -gt "0" ]; then
 NginxProcesses=1
else
 NginxProcesses=0
fi
/opt/aws/bin/mon-put-data --metric-name "Nginx Processes" --namespace "Custom Metrix" --dimensions "InstanceId=$instanceid" --value "$NginxProcesses" --unit "Count"

# unicorn check
unicorn=`ps -ef | grep unicorn | grep -v grep | wc -l`

if [ "$unicorn" -gt "0" ]; then
 UnicornProcesses=1
else
 UnicornProcesses=0
fi
/opt/aws/bin/mon-put-data --metric-name "Unicorn Processes" --namespace "Custom Metrix" --dimensions "InstanceId=$instanceid" --value "$UnicornProcesses" --unit "Count"

# cron check
cron=`ps cax | grep cron | wc -l`

if [ "$cron" -gt "0" ]; then
 CronProcesses=1
else
 CronProcesses=0
fi
/opt/aws/bin/mon-put-data --metric-name "Cron Processes" --namespace "Custom Metrix" --dimensions "InstanceId=$instanceid" --value "$CronProcesses" --unit "Count"

# diskspace check
path='/'
freespace=`df --local --block-size=1M $path | grep $path | tr -s ' ' | cut -d ' ' -f 4`
usedpercent=`df --local $path | grep $path | tr -s ' ' | cut -d ' ' -f 5 | grep -o "[0-9]*"`

mon-put-data --metric-name "FreeSpaceMBytes" --namespace "Custom Metrix" --dimensions "InstanceId=$instanceid,Path=$path" --value "$freespace" --unit "Megabytes"
mon-put-data --metric-name "UsedSpacePercent" --namespace "Custom Metrix" --dimensions "InstanceId=$instanceid,Path=$path" --value "$usedpercent" --unit "Percent"

crontab -eでクーロンの設定

MAILTO=dev@creww.me
*/5 * * * * /home/deployer/cloudwatch/custom_metrics.sh