Last active
August 29, 2015 14:06
-
-
Save Twinuma/b63f701009445790f553 to your computer and use it in GitHub Desktop.
全リージョンのEC2メンテナンスイベントを一覧表示するRubyスクリプトhttp://dev.classmethod.jp/cloud/aws/ruby-ec2-describe-events/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 以下のように$HOME/./configに記載されたプロファイル名を指定して実行してください。 | |
./cm-ec2-describe-events.rb --profile hoge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem install -profile_parser |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem install -sdk |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require '-sdk' | |
require 'optparse' | |
begin | |
require '/profile_parser' | |
rescue LoadError; end | |
config = {} | |
ARGV.options do |opt| | |
begin | |
_opts = {} | |
opt.on('-h', '--help') { puts opt.help; exit 0 } | |
opt.on('-d', '--debug') { _opts[:log_level] = :debug } | |
opt.on('-k', '--access-key ACCESS_KEY') { |v| _opts[:access_key_id] = v } | |
opt.on('-s', '--secret-key SECRET_KEY') { |v| _opts[:secret_access_key] = v } | |
opt.on('-r', '--region REGION') { |v| _opts[:region] = v } | |
opt.on('--profile PROFILE') { |v| parser = ::ProfileParser.new; _opts = parser.get(v) } | |
opt.parse! | |
if _opts.empty? | |
puts opt.help | |
exit 1 | |
end | |
.config(_opts) | |
rescue => e | |
$stderr.puts e | |
exit 1 | |
end | |
end | |
ec2 = ::EC2.new | |
.memoize do | |
#全リージョンを対象とする | |
ec2.regions.each do |region| | |
puts region.name | |
reg = ec2.regions[region.name] | |
reg.instances.each do |i| | |
# イベント取得 | |
status = reg.client.describe_instance_status( :instance_ids => [ i.id ]) | |
events = status.data[:instance_status_set][0][:events_set] rescue nil | |
next if events.nil? or events.empty? | |
events.each do |event| | |
next if event[:code] != "system-reboot" # システムリブート以外のイベントはスキップ | |
next if event[:description] =~ /^\[Completed\]/ # 完了したイベントはスキップ | |
printf "%10s %-20s %-20s %-20s %-16s - %-16s\n", | |
i.id, | |
i.tags["Name"], | |
i.availability_zone, | |
event[:description], | |
event[:not_before].localtime.strftime("%Y-%m-%d %H:%M"), | |
event[:not_after].localtime.strftime("%Y-%m-%d %H:%M") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment