lib/pghero/methods/system.rb in pghero-1.4.1 vs lib/pghero/methods/system.rb in pghero-1.4.2

- old
+ new

@@ -1,44 +1,51 @@ module PgHero module Methods module System - def cpu_usage - rds_stats("CPUUtilization") + def cpu_usage(options = {}) + rds_stats("CPUUtilization", options) end - def connection_stats - rds_stats("DatabaseConnections") + def connection_stats(options = {}) + rds_stats("DatabaseConnections", options) end - def replication_lag_stats - rds_stats("ReplicaLag") + def replication_lag_stats(options = {}) + rds_stats("ReplicaLag", options) end - def read_iops_stats - rds_stats("ReadIOPS") + def read_iops_stats(options = {}) + rds_stats("ReadIOPS", options) end - def write_iops_stats - rds_stats("WriteIOPS") + def write_iops_stats(options = {}) + rds_stats("WriteIOPS", options) end - def rds_stats(metric_name) + 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 - now = Time.now + 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: (now - 1 * 3600).iso8601, - end_time: now.iso8601, - period: 60, + 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]