Module: PgHero::Methods::System

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

Instance Method Summary collapse

Instance Method Details

#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

#free_space_stats(**options) ⇒ Object



24
25
26
# File 'lib/pghero/methods/system.rb', line 24

def free_space_stats(**options)
  rds_stats("FreeStorageSpace", options)
end

#rds_stats(metric_name, duration: nil, period: nil, offset: nil) ⇒ Object



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

def rds_stats(metric_name, duration: nil, period: nil, offset: nil)
  if system_stats_enabled?
    aws_options = {region: region}
    if access_key_id
      aws_options[:access_key_id] = access_key_id
      aws_options[:secret_access_key] = secret_access_key
    end

    client =
      if defined?(Aws)
        Aws::CloudWatch::Client.new(aws_options)
      else
        AWS::CloudWatch.new(aws_options).client
      end

    duration = (duration || 1.hour).to_i
    period = (period || 1.minute).to_i
    offset = (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
    raise NotEnabled, "System stats not enabled"
  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

#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

#system_stats_enabled?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/pghero/methods/system.rb', line 70

def system_stats_enabled?
  !!((defined?(Aws) || defined?(AWS)) && 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