Skip to content

Instantly share code, notes, and snippets.

@Twinuma
Last active August 29, 2015 14:06
Show Gist options
  • Save Twinuma/b63f701009445790f553 to your computer and use it in GitHub Desktop.
Save Twinuma/b63f701009445790f553 to your computer and use it in GitHub Desktop.
全リージョンのEC2メンテナンスイベントを一覧表示するRubyスクリプトhttp://dev.classmethod.jp/cloud/aws/ruby-ec2-describe-events/
# 以下のように$HOME/./configに記載されたプロファイル名を指定して実行してください。
./cm-ec2-describe-events.rb --profile hoge
gem install -profile_parser
gem install -sdk
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