Debuginfo

思考とアウトプット

AWS cloudwatchで監視

前職ではmonitoringって言ってたけど、日本語では監視なんだと最近気づきました。

  1. CPU Utilization - EC2デフォルトでAlarmを作る
  2. Memory Usage - Custom metricsを作る
  3. Load Avarage - Custom metricsを作る
  4. サービスの監視 - LBでalarmを作る
  5. RDS FreeableMemory - RDSデフォルトで作る
  6. cloudwatch logs - エージェントいれる

1,2,3はここを参考にしました。 違う点はインタンスを動的に取得してるところと、サービス監視はLBのUnavailableを見てるのでコメントアウト

#!/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/ec2-user/cloudwatch/credential
instanceid=`/opt/aws/bin/ec2-metadata -i | awk '{print $2 }'`

# http status check
#status=`/home/ec2-user/cloudwatch/http_status_check.sh http://www.global-step.jp` # 監視したい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"

# loadaverage check
loadave1=`uptime | tr -s ' ' | cut -d ' ' -f 11 | cut -d ',' -f 1`
/opt/aws/bin/mon-put-data --metric-name "LoadAverage" --namespace "Custom Metrix" --dimensions "InstanceId=$instanceid" --value "$loadave1" --unit "Count"

Cloudwatch Logs

特に詰まることなくドキュメント通り。

https://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/QuickStartEC2Instance.html

#/etc/awslogs/awslogs.conf
[/var/log/messages]
datetime_format = %b %d %H:%M:%S
file = /var/log/messages
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = start_of_file
log_group_name = /var/log/messages

[/srv/www/gsaweb/shared/log/production.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /srv/www/gsaweb/shared/log/production.log
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = start_of_file
log_group_name = /srv/www/gsaweb/shared/log/production.log

[/var/log/maillog]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/maillog
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = start_of_file
log_group_name = /var/log/maillog