Module: PgHero::Methods::System
- Included in:
- Database
- Defined in:
- lib/pghero/methods/system.rb
Instance Method Summary collapse
- #connection_stats(**options) ⇒ Object
- #cpu_usage(**options) ⇒ Object
- #free_space_stats(**options) ⇒ Object
- #rds_stats(metric_name, duration: nil, period: nil, offset: nil) ⇒ Object
- #read_iops_stats(**options) ⇒ Object
- #replication_lag_stats(**options) ⇒ Object
- #system_stats_enabled? ⇒ Boolean
- #write_iops_stats(**options) ⇒ Object
Instance Method Details
#connection_stats(**options) ⇒ Object
8 9 10 |
# File 'lib/pghero/methods/system.rb', line 8 def connection_stats(**) rds_stats("DatabaseConnections", ) end |
#cpu_usage(**options) ⇒ Object
4 5 6 |
# File 'lib/pghero/methods/system.rb', line 4 def cpu_usage(**) rds_stats("CPUUtilization", ) end |
#free_space_stats(**options) ⇒ Object
24 25 26 |
# File 'lib/pghero/methods/system.rb', line 24 def free_space_stats(**) rds_stats("FreeStorageSpace", ) 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? = {region: region} if access_key_id [:access_key_id] = access_key_id [:secret_access_key] = secret_access_key end client = if defined?(Aws) Aws::CloudWatch::Client.new() else AWS::CloudWatch.new().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(**) rds_stats("ReadIOPS", ) end |
#replication_lag_stats(**options) ⇒ Object
12 13 14 |
# File 'lib/pghero/methods/system.rb', line 12 def replication_lag_stats(**) rds_stats("ReplicaLag", ) end |
#system_stats_enabled? ⇒ 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(**) rds_stats("WriteIOPS", ) end |