-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze-cloudtrail.rb
67 lines (52 loc) · 1.37 KB
/
analyze-cloudtrail.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/ruby
require 'json'
require 'optparse'
require 'date'
options = Hash.new
parser = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename __FILE__}"
opts.on('--key KEY', 'aws key') do |key|
options[:key] = key
end
options[:path] = "#{File.dirname(__FILE__)}"
opts.on('--path PATH', 'path to cloudtrail logs. defaults to current directory') do |path|
options[:path] = path
end
end
parser.parse!
if options[:key] == nil
puts parser.help
exit 1
end
key = options[:key]
path = options[:path]
stats = Hash.new do |h,k|
h[k] = {
count: 0,
last_timestamp: DateTime.new(0)
}
end
last_time = DateTime.new(0)
Dir.foreach(path) do |file|
unless file.match(/.+\.json/)
next
end
f = File.open("#{path}/#{file}", 'r')
jsonString = f.read
f.close
json = JSON.parse(jsonString)
json["Records"].each do |record|
unless record['userIdentity']['accessKeyId'] == key
next
end
event_name = record['eventName']
stats[event_name][:count] += 1
if stats[event_name][:last_timestamp] < DateTime.parse(record['eventTime'])
stats[event_name][:last_timestamp] = DateTime.parse(record['eventTime'])
end
stats[event_name][:ip_address] = record['sourceIPAddress']
end
end
stats.each do |event,values|
puts "#{event}, #{values[:count]}, #{values[:last_timestamp]}, #{values[:ip_address]}"
end