Module: PgHero::Methods::System

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

Instance Method Summary collapse

Instance Method Details

#azure_stats(metric_name, duration: nil, period: nil, offset: nil, series: false) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pghero/methods/system.rb', line 80

def azure_stats(metric_name, duration: nil, period: nil, offset: nil, series: false)
  # TODO DRY with RDS stats
  duration = (duration || 1.hour).to_i
  period = (period || 1.minute).to_i
  offset = (offset || 0).to_i
  end_time = Time.at(((Time.now - offset).to_f / period).ceil * period)
  start_time = end_time - duration

  interval =
    case period
    when 60
      "PT1M"
    when 300
      "PT5M"
    when 900
      "PT15M"
    when 1800
      "PT30M"
    when 3600
      "PT1H"
    else
      raise Error, "Unsupported period"
    end

  client = Azure::Monitor::Profiles::Latest::Mgmt::Client.new
  # call utc to convert +00:00 to Z
  timespan = "#{start_time.utc.iso8601}/#{end_time.utc.iso8601}"
  results = client.metrics.list(
    azure_resource_id,
    metricnames: metric_name,
    aggregation: "Average",
    timespan: timespan,
    interval: interval
  )

  data = {}
  result = results.value.first
  if result
    result.timeseries.first.data.each do |point|
      data[point.time_stamp.to_time] = point.average
    end
  end

  add_missing_data(data, start_time, end_time, period) if series

  data
end

#connection_stats(**options) ⇒ Object



22
23
24
# File 'lib/pghero/methods/system.rb', line 22

def connection_stats(**options)
  system_stats(:connections, **options)
end

#cpu_usage(**options) ⇒ Object



18
19
20
# File 'lib/pghero/methods/system.rb', line 18

def cpu_usage(**options)
  system_stats(:cpu, **options)
end

#free_space_stats(**options) ⇒ Object



38
39
40
# File 'lib/pghero/methods/system.rb', line 38

def free_space_stats(**options)
  system_stats(:free_space, **options)
end

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



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
69
70
71
72
73
74
75
76
77
78
# File 'lib/pghero/methods/system.rb', line 42

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

    client = Aws::CloudWatch::Client.new(aws_options)

    duration = (duration || 1.hour).to_i
    period = (period || 1.minute).to_i
    offset = (offset || 0).to_i
    end_time = Time.at(((Time.now - offset).to_f / period).ceil * period)
    start_time = end_time - duration

    resp = client.get_metric_statistics(
      namespace: "AWS/RDS",
      metric_name: metric_name,
      dimensions: [{name: "DBInstanceIdentifier", value: aws_db_instance_identifier}],
      start_time: start_time.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

    add_missing_data(data, start_time, end_time, period) if series

    data
  else
    raise NotEnabled, "System stats not enabled"
  end
end

#read_iops_stats(**options) ⇒ Object



30
31
32
# File 'lib/pghero/methods/system.rb', line 30

def read_iops_stats(**options)
  system_stats(:read_iops, **options)
end

#replication_lag_stats(**options) ⇒ Object



26
27
28
# File 'lib/pghero/methods/system.rb', line 26

def replication_lag_stats(**options)
  system_stats(:replication_lag, **options)
end

#system_stats_enabled?Boolean

Returns:

  • (Boolean)


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

def system_stats_enabled?
  !system_stats_provider.nil?
end

#system_stats_providerObject



8
9
10
11
12
13
14
15
16
# File 'lib/pghero/methods/system.rb', line 8

def system_stats_provider
  if aws_db_instance_identifier
    :aws
  elsif gcp_database_id
    :gcp
  elsif azure_resource_id
    :azure
  end
end

#write_iops_stats(**options) ⇒ Object



34
35
36
# File 'lib/pghero/methods/system.rb', line 34

def write_iops_stats(**options)
  system_stats(:write_iops, **options)
end