awsで構築したのだよ 06...
TRANSCRIPT
AWSで構築したのだよ
「ec2起動時にアラーム設定を
クラウドウォッチに追加したい」
注意
順番に記載されているため、前のテーマで記載されている内容が前提となる場合があります。
AWS構築時のノウハウを残し共有します
ユーザのコンソール操作をログに残したい
ec2で自分のインスタンスIDが知りたい
ec2で自分についているタグを読み取りたい
ec2起動時にホスト名を変更したい
プロセス監視、メモリ使用率、ディスク使用率をCloudwatchのカスタムメ
トリクスに追加したい
ec2起動時にアラーム設定をクラウドウォッチに追加したい
AmazonLinuxのsshの認証方式を、ID/PWに変更したい。
VPNのプライベートネットワークでNTPを使いたい
プライベートネットワークのRDSにssh経由で接続したい
夜間にec2を落としたい
サーバーのシャットダウン時にログを退避したい
お品書き
ec2を新しく起動したときに、自動でCloudwatch
にアラーム設定を行い、監視対象とする。
ec2を停止するときに、Cloudwatchのアラーム設定を解除し監視対象から外す。
ec2起動時にアラーム設定をクラウドウォッチに追加したい
(1/6)
ユーザに、カスタムメトリクスに登録権限を付与します。
設定方法は、カスタムメトリクス追加の回をご覧ください。
ec2起動時にアラーム設定をクラウドウォッチに追加したい
(2/6)
通知用のトピックを作成する
ec2起動時にアラーム設定をクラウドウォッチに追加したい
(3/6)
Subscribeしたメール宛てに、確認メールが届きますのでリンクをクリックして確認を行います。
Alertと同様に、CautionとWarningを作成します。
作成したAlert、Caution、WarningのTopic ARNをメ
モします。これから作成するスクリプトに埋め込むのに使います。
アラートの追加と削除用のスクリプトを用意する。
ec2起動時にアラーム設定をクラウドウォッチに追加したい
(4/6)
監視内容 閾値 備考
StatusCheckFailed >= 0 サーバダウン時0(成功) か 1(失敗)
DiskUsage_root >=80 使用率が80%を超えたら
CPUUtilization >=90 CPU使用率
MemoryUsage >=90 メモリ使用率
process_http <1 プロセスが無くなったら
process_tomcat6 <1 プロセスが無くなったら
#!/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'
# vi entryAlert.rb
アラート登録用スクリプトを用意します。
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"],
},
{
"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"],
},
{
"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"],
},
]
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
#!/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'
# vi deleteAlert.rb
アラート削除用スクリプトを用意します。
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"],
},
{
"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"],
},
{
"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"],
},
]
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
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
# vi ec2tag.rb
タグ取得用スクリプトを用意します。
手動でスクリプトを実行し、動作を確認します。
# ruby entryAlert.rb
# ruby deleteAlert.rb
ec2起動時にアラーム設定をクラウドウォッチに追加したい
(5/6)
起動停止時にスクリプトを実行するように設定する。
ec2起動時にアラーム設定をクラウドウォッチに追加したい
(6/6)
# 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
パーミッションを変更# chmod 755 /etc/init.d/entry-to-cloudwatch
自動起動の設定# chkconfig entry-to-cloudwatch on