Module: Pgpass

Defined in:
lib/sensu-plugins-postgres/pgpass.rb

Instance Method Summary collapse

Instance Method Details

#pgpassObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sensu-plugins-postgres/pgpass.rb', line 12

def pgpass
  if File.file?(config[:pgpass])
    pgpass = Hash[%i[hostname port database user password].zip(read_pgpass(config[:pgpass]))]
    pgpass[:database] = nil if pgpass[:database] == '*'
    pgpass.each do |k, v|
      config[k] ||= v
    end
  else
    config[:hostname] ||= ENV['PGHOST']     || 'localhost'
    config[:port]     ||= ENV['PGPORT']     || 5432
    config[:database] ||= ENV['PGDATABASE'] || 'postgres'
    config[:user]     ||= ENV['PGUSER']     || 'postgres'
    config[:password] ||= ENV['PGPASSWORD']
  end
end

#read_pgpass(pg_pass_file) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/sensu-plugins-postgres/pgpass.rb', line 4

def read_pgpass(pg_pass_file)
  File.readlines(pg_pass_file).each do |line|
    return line.strip.split(':') unless line.start_with?('#')

    next
  end
end