Module: PgHero::Methods::System
- Included in:
- Database
- Defined in:
- lib/pghero/methods/system.rb
Instance Method Summary collapse
- #access_key_id ⇒ Object
- #connection_stats(options = {}) ⇒ Object
- #cpu_usage(options = {}) ⇒ Object
- #db_instance_identifier ⇒ Object
- #rds_stats(metric_name, options = {}) ⇒ Object
- #read_iops_stats(options = {}) ⇒ Object
- #region ⇒ Object
- #replication_lag_stats(options = {}) ⇒ Object
- #secret_access_key ⇒ Object
- #system_stats_enabled? ⇒ Boolean
- #write_iops_stats(options = {}) ⇒ Object
Instance Method Details
#access_key_id ⇒ Object
64 65 66 |
# File 'lib/pghero/methods/system.rb', line 64 def access_key_id ENV["PGHERO_ACCESS_KEY_ID"] || ENV["AWS_ACCESS_KEY_ID"] end |
#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 |
#db_instance_identifier ⇒ Object
76 77 78 |
# File 'lib/pghero/methods/system.rb', line 76 def db_instance_identifier databases[current_database].db_instance_identifier end |
#rds_stats(metric_name, options = {}) ⇒ Object
24 25 26 27 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 |
# File 'lib/pghero/methods/system.rb', line 24 def rds_stats(metric_name, = {}) 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 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 {} 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 |
#region ⇒ Object
72 73 74 |
# File 'lib/pghero/methods/system.rb', line 72 def region ENV["PGHERO_REGION"] || ENV["AWS_REGION"] || (defined?(Aws) && Aws.config[:region]) || "us-east-1" 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 |
#secret_access_key ⇒ Object
68 69 70 |
# File 'lib/pghero/methods/system.rb', line 68 def secret_access_key ENV["PGHERO_SECRET_ACCESS_KEY"] || ENV["AWS_SECRET_ACCESS_KEY"] end |
#system_stats_enabled? ⇒ Boolean
60 61 62 |
# File 'lib/pghero/methods/system.rb', line 60 def system_stats_enabled? !!((defined?(Aws) || defined?(AWS)) && access_key_id && secret_access_key && 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 |