Skip to content

Instantly share code, notes, and snippets.

@masaomoc
Last active August 29, 2015 14:06
Show Gist options
  • Save masaomoc/4eb66a4042e946ffecc8 to your computer and use it in GitHub Desktop.
Save masaomoc/4eb66a4042e946ffecc8 to your computer and use it in GitHub Desktop.
Collect All EC2 Scheduled events
#!/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
@masaomoc
Copy link
Author

スレッドを初めて使ってみた。使い方があっているかわからない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment