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



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
127
128
129
130
131
# File 'lib/pghero/methods/system.rb', line 86

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
  timespan = "#{start_time.iso8601}/#{end_time.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



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

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

#cpu_usage(**options) ⇒ Object



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

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

#free_space_stats(**options) ⇒ Object



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

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

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



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
79
80
81
82
83
84
# File 'lib/pghero/methods/system.rb', line 43

def rds_stats(metric_name, duration: nil, period: nil, offset: nil, series: false)
  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.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



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

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

#replication_lag_stats(**options) ⇒ Object



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

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

TODO remove defined checks in 3.0



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

def system_stats_provider
  if aws_db_instance_identifier && (defined?(Aws) || defined?(AWS))
    :aws
  elsif gcp_database_id
    :gcp
  elsif azure_resource_id
    :azure
  end
end

#write_iops_stats(**options) ⇒ Object



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

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