More Related Content Similar to Awsで構築したのだよ 06 ec2インスタンス起動時にCloudWatchのアラームを追加する (20) Awsで構築したのだよ 06 ec2インスタンス起動時にCloudWatchのアラームを追加する3. お品書き
ユーザのコンソール操作をログに残したい
ec2で自分のインスタンスIDが知りたい
ec2で自分についているタグを読み取りたい
ec2起動時にホスト名を変更したい
プロセス監視、メモリ使用率、ディスク使用率をCloudwatchのカスタムメ
トリクスに追加したい
ec2起動時にアラーム設定をクラウドウォッチに追加したい
Amazon Linuxのsshの認証方式を、ID/PWに変更したい。
VPNのプライベートネットワークでNTPを使いたい
プライベートネットワークのRDSにssh経由で接続したい
夜間にec2を落としたい
サーバーのシャットダウン時にログを退避したい
10. ec2起動時にアラーム設定を
クラウドウォッチに追加したい
(4/6)
アラートの追加と削除用のスクリプトを用意する。
監視内容閾値備考
StatusCheckFailed >= 0 サーバダウン時
0(成功) か1(失敗)
DiskUsage_root >=80 使用率が80%を超えたら
CPUUtilization >=90 CPU使用率
MemoryUsage >=90 メモリ使用率
process_http <1 プロセスが無くなったら
process_tomcat6 <1 プロセスが無くなったら
11. アラート登録用スクリプトを用意します。
# vi entryAlert.rb
#!/usr/bin/env ruby
# encoding: utf-8
# 1.メトリクスの監視をアラートに登録する。
#
require 'rubygems'
require 'aws-sdk'
require './ec2tag'
AWS.config(YAML.load(File.read("./aws.yml")))
@instanceID = `wget --no-proxy -q -O - http://169.254.169.254/latest/meta-data/instance-id`
@ec2 = AWS::EC2.new().client
# ec2のタグよりNAMEを取得します。
@name = get_name_tag(@ec2, @instanceID)
@system = 'SYSTEM01'
@process1 = 'httpd'
@process2 = 'tomcat6'
12. cw_config = [
{
"namespace"=> "AWS/EC2",
"alert_level"=> "RED",
"description"=> "SERVER DOWN",
"metric_name"=> "StatusCheckFailed",
"threshold"=> 0,
"comparison_operator"=> "GreaterThanThreshold",
"statistic"=> "Average",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Alert"],
},
{
"namespace"=> "Custom/EC2",
"alert_level"=> "YEL",
"description"=> "DISK USAGE",
"metric_name"=> "DiskUsage_root",
"threshold"=> 80,
"comparison_operator"=> "GreaterThanThreshold",
"statistic"=> "Average",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Warning"],
},
13. {
"namespace"=> "AWS/EC2",
"alert_level"=> "YEL",
"description"=> "CPU Utilization",
"metric_name"=> "CPUUtilization",
"threshold"=> 90,
"comparison_operator"=> "GreaterThanThreshold",
"statistic"=> "Average",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Caution"],
},
{
"namespace"=> "Custom/EC2",
"alert_level"=> "YEL",
"description"=> "MEMORY USAGE",
"metric_name"=> "MemoryUsage",
"threshold"=> 90,
"comparison_operator"=> "GreaterThanThreshold",
"statistic"=> "Average",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Caution"],
},
14. {
"namespace"=> "Custom/EC2",
"alert_level"=> "RED",
"description"=> "PROCESS DOWN[#{@process1}]",
"metric_name"=> "process_#{@process1}",
"threshold"=> 1,
"comparison_operator"=> "LessThanThreshold",
"statistic"=> "Maximum",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Alert"],
},
{
"namespace"=> "Custom/EC2",
"alert_level"=> "RED",
"description"=> "PROCESS DOWN[#{@process2}]",
"metric_name"=> "process_#{@process2}",
"threshold"=> 1,
"comparison_operator"=> "LessThanThreshold",
"statistic"=> "Maximum",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Alert"],
},
]
15. cw = AWS::CloudWatch.new
cw_config.each do |config|
alert_name = ["[#{@system}_#{config["alert_level"]}]", config["description"],
"(#{@name}[#{@instanceID}])"].join("")
cw.alarms.create( alert_name,{
"namespace"=> config["namespace"],
"metric_name"=> config["metric_name"],
"threshold"=> config["threshold"],
"comparison_operator"=> config["comparison_operator"],
"statistic"=> config["statistic"],
"period"=> 300,
"dimensions"=> [ { "name"=> "InstanceId", "value"=> @instanceID} ],
"evaluation_periods"=> 1,
"alarm_actions"=> config["arn"]}
)
end
16. アラート削除用スクリプトを用意します。
# vi deleteAlert.rb
#!/usr/bin/env ruby
# encoding: utf-8
# 1.アラーを削除する。
#
require 'rubygems'
require 'aws-sdk'
require './ec2tag'
AWS.config(YAML.load(File.read("./aws.yml")))
@instanceID = `wget --no-proxy -q -O - http://169.254.169.254/latest/meta-data/instance-id`
@ec2 = AWS::EC2.new().client
@name = get_name_tag(@ec2, @instanceID)
@system = 'SYSTEM01'
@process1 = 'httpd'
@process2 = 'tomcat6'
17. cw_config = [
{
"namespace"=> "AWS/EC2",
"alert_level"=> "RED",
"description"=> "SERVER DOWN",
"metric_name"=> "StatusCheckFailed",
"threshold"=> 0,
"comparison_operator"=> "GreaterThanThreshold",
"statistic"=> "Average",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Alert"],
},
{
"namespace"=> "Custom/EC2",
"alert_level"=> "YEL",
"description"=> "DISK USAGE",
"metric_name"=> "DiskUsage_root",
"threshold"=> 80,
"comparison_operator"=> "GreaterThanThreshold",
"statistic"=> "Average",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Warning"],
},
18. {
"namespace"=> "AWS/EC2",
"alert_level"=> "YEL",
"description"=> "CPU Utilization",
"metric_name"=> "CPUUtilization",
"threshold"=> 90,
"comparison_operator"=> "GreaterThanThreshold",
"statistic"=> "Average",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Caution"],
},
{
"namespace"=> "Custom/EC2",
"alert_level"=> "YEL",
"description"=> "MEMORY USAGE",
"metric_name"=> "MemoryUsage",
"threshold"=> 90,
"comparison_operator"=> "GreaterThanThreshold",
"statistic"=> "Average",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Caution"],
},
19. {
"namespace"=> "Custom/EC2",
"alert_level"=> "RED",
"description"=> "PROCESS DOWN[#{@process1}]",
"metric_name"=> "process_#{@process1}",
"threshold"=> 1,
"comparison_operator"=> "LessThanThreshold",
"statistic"=> "Maximum",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Alert"],
},
{
"namespace"=> "Custom/EC2",
"alert_level"=> "RED",
"description"=> "PROCESS DOWN[#{@process2}]",
"metric_name"=> "process_#{@process2}",
"threshold"=> 1,
"comparison_operator"=> "LessThanThreshold",
"statistic"=> "Maximum",
"arn"=> ["arn:aws:sns:ap-northeast-1:969331845486:Alert"],
},
]
20. cw = AWS::CloudWatch.new
cw_config.each do |config|
alert_name = ["[#{@system}_#{config["alert_level"]}]", config["description"],
"(#{@name}[#{@instanceID}])"].join("")
cw.alarms.delete( alert_name )
end
21. タグ取得用スクリプトを用意します。
# vi ec2tag.rb
def get_tag_set(ec2, instance_id)
tag_set = ec2.describe_instances(:instance_ids =>
[instance_id])[:instance_index][instance_id][:tag_set]
return tag_set
end
def get_name_tag(ec2, instance_id)
tag_set = get_tag_set(@ec2, @instanceID)
tag_set.each do |tag|
if /name/i =~ tag[:key]
@name = tag[:value]
return tag[:value]
end
end
reruen ""
end
24. # vi /etc/init.d/entry-to-cloudwatch
#!/bin/sh
# chkconfig: 345 80 20
#
# This script will be executed *before* all the other holt scripts.
# You can put your own terminate stuff in here.
case "$1" in
start)
# Alert entry to Cloud Watch
touch /var/lock/subsys/entry-to-cloudwatch
/usr/bin/ruby /opt/aws/entry_alert.rb
;;
stop)
# Alert delete form Cloud Watch
rm -f /var/lock/subsys/entry-to-cloudwatch
/usr/bin/ruby /opt/aws/delete_alert.rb
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
25. パーミッションを変更
# chmod 755 /etc/init.d/entry-to-cloudwatch
自動起動の設定
# chkconfig entry-to-cloudwatch on