Last active
August 29, 2015 14:06
-
-
Save masaomoc/4eb66a4042e946ffecc8 to your computer and use it in GitHub Desktop.
Collect All EC2 Scheduled events
This file contains 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
#!/usr/bin/env ruby | |
# EC2のイベント通知を検出するスクリプト | |
require 'aws-sdk-v1' | |
require 'credential_traverser' | |
require 'thread' | |
#AWS.config(logger: Logger.new($stdout), log_level: :debug) | |
CredentialTraverser.traverse do |profile| | |
puts profile | |
@mutex = Mutex.new | |
threads = [] | |
AWS.regions.count.times do |i| | |
threads << Thread::start do | |
@mutex.synchronize do | |
AWS.config(region: AWS.regions.to_a[i].name) | |
end | |
ec2 = AWS::EC2.new | |
ec2cli = AWS::EC2::Client.new | |
AWS.memoize do | |
resp = ec2cli.describe_instance_status | |
arr = resp.instance_status_set.reject { |i| i[:events_set].empty? } | |
.sort_by { |i| ec2.instances[i[:instance_id]].tags.Name } | |
.map { |i| | |
[ i[:instance_id], ec2.instances[i[:instance_id]].tags.Name, | |
i[:events_set][0][:not_before], i[:events_set][0][:not_after] | |
].join(',') | |
} | |
next if arr.empty? | |
puts 'InstanceId,Name,NotBefore,NotAfter' | |
puts arr.join("\n") | |
end | |
end | |
end | |
threads.count.times do |i| | |
threads[i].join | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
スレッドを初めて使ってみた。使い方があっているかわからない。