This repository has been archived by the owner on Dec 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
rds-pgbadger.rb
executable file
·50 lines (40 loc) · 1.83 KB
/
rds-pgbadger.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
#!/usr/bin/env ruby
require 'optparse'
require 'yaml'
require 'ox'
require 'aws-sdk-core'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: rds-pgbadger.rb [options]"
opts.on('-e', '--env NAME', 'Environement name') { |v| options[:env] = v }
opts.on('-i', '--instance-id NAME', 'RDS instance identifier') { |v| options[:instance_id] = v }
opts.on('-d', '--date DATE', 'Filter logs to given date in format YYYY-MM-DD.') { |v| options[:date] = v }
end.parse!
raise OptionParser::MissingArgument.new(:env) if options[:env].nil?
raise OptionParser::MissingArgument.new(:instance_id) if options[:instance_id].nil?
creds = YAML.load(File.read(File.expand_path('~/.fog')))
puts "Instantiating RDS client for #{options[:env]} environment."
rds = Aws::RDS::Client.new(
region: 'us-east-1',
access_key_id: creds[options[:env]]['aws_access_key_id'],
secret_access_key: creds[options[:env]]['aws_secret_access_key']
)
log_files = rds.describe_db_log_files(db_instance_identifier: options[:instance_id], filename_contains: "postgresql.log.#{options[:date]}")[:describe_db_log_files].map(&:log_file_name)
dir_name = "#{options[:instance_id]}-#{Time.now.to_i}"
Dir.mkdir("out/#{dir_name}")
Dir.mkdir("out/#{dir_name}/error")
log_files.each do |log_file|
puts "Downloading log file: #{log_file}"
open("out/#{dir_name}/#{log_file}", 'w') do |f|
rds.download_db_log_file_portion(db_instance_identifier: options[:instance_id], log_file_name: log_file).each do |r|
print "."
f.puts r[:log_file_data]
end
puts "."
end
puts "Saved log to out/#{dir_name}/#{log_file}."
end
puts "Generating PG Badger report."
`pgbadger --prefix "%t:%r:%u@%d:[%p]:" --outfile out/#{dir_name}/#{dir_name}.html out/#{dir_name}/error/*.log.*`
puts "Opening report out/#{dir_name}/#{dir_name}.html."
`open out/#{dir_name}/#{dir_name}.html`