Module: PgHero::Methods::System

Included in:
Database
Defined in:
lib/pghero/methods/system.rb

Instance Method Summary collapse

Instance Method Details

#access_key_idObject



64
65
66
# File 'lib/pghero/methods/system.rb', line 64

def access_key_id
  ENV["PGHERO_ACCESS_KEY_ID"] || ENV["AWS_ACCESS_KEY_ID"]
end

#connection_stats(options = {}) ⇒ Object



8
9
10
# File 'lib/pghero/methods/system.rb', line 8

def connection_stats(options = {})
  rds_stats("DatabaseConnections", options)
end

#cpu_usage(options = {}) ⇒ Object



4
5
6
# File 'lib/pghero/methods/system.rb', line 4

def cpu_usage(options = {})
  rds_stats("CPUUtilization", options)
end

#db_instance_identifierObject



76
77
78
# File 'lib/pghero/methods/system.rb', line 76

def db_instance_identifier
  databases[current_database].db_instance_identifier
end

#rds_stats(metric_name, options = {}) ⇒ Object



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
# File 'lib/pghero/methods/system.rb', line 24

def rds_stats(metric_name, options = {})
  if system_stats_enabled?
    client =
      if defined?(Aws)
        Aws::CloudWatch::Client.new(access_key_id: access_key_id, secret_access_key: secret_access_key, region: region)
      else
        AWS::CloudWatch.new(access_key_id: access_key_id, secret_access_key: secret_access_key, region: region).client
      end

    duration = (options[:duration] || 1.hour).to_i
    period = (options[:period] || 1.minute).to_i
    offset = (options[:offset] || 0).to_i

    end_time = (Time.now - offset)
    # ceil period
    end_time = Time.at((end_time.to_f / period).ceil * period)

    resp = client.get_metric_statistics(
      namespace: "AWS/RDS",
      metric_name: metric_name,
      dimensions: [{name: "DBInstanceIdentifier", value: db_instance_identifier}],
      start_time: (end_time - duration).iso8601,
      end_time: end_time.iso8601,
      period: period,
      statistics: ["Average"]
    )
    data = {}
    resp[:datapoints].sort_by { |d| d[:timestamp] }.each do |d|
      data[d[:timestamp]] = d[:average]
    end
    data
  else
    {}
  end
end

#read_iops_stats(options = {}) ⇒ Object



16
17
18
# File 'lib/pghero/methods/system.rb', line 16

def read_iops_stats(options = {})
  rds_stats("ReadIOPS", options)
end

#regionObject



72
73
74
# File 'lib/pghero/methods/system.rb', line 72

def region
  ENV["PGHERO_REGION"] || ENV["AWS_REGION"] || (defined?(Aws) && Aws.config[:region]) || "us-east-1"
end

#replication_lag_stats(options = {}) ⇒ Object



12
13
14
# File 'lib/pghero/methods/system.rb', line 12

def replication_lag_stats(options = {})
  rds_stats("ReplicaLag", options)
end

#secret_access_keyObject



68
69
70
# File 'lib/pghero/methods/system.rb', line 68

def secret_access_key
  ENV["PGHERO_SECRET_ACCESS_KEY"] || ENV["AWS_SECRET_ACCESS_KEY"]
end

#system_stats_enabled?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/pghero/methods/system.rb', line 60

def system_stats_enabled?
  !!((defined?(Aws) || defined?(AWS)) && access_key_id && secret_access_key && db_instance_identifier)
end

#write_iops_stats(options = {}) ⇒ Object



20
21
22
# File 'lib/pghero/methods/system.rb', line 20

def write_iops_stats(options = {})
  rds_stats("WriteIOPS", options)
end