class Ruboty::ElbMonitor::Aws::CloudWatch

Constants

CLIENT_OPTIONS
STATS_AVERAGE_GROUP
STATS_MAXIMUM_GROUP
STATS_SUM_GROUP

Attributes

elb_name[R]
end_time[R]
start_time[R]

Public Class Methods

new(elb_name, options) click to toggle source
# File lib/ruboty/elb_monitor/aws/cloudwatch.rb, line 7
def initialize(elb_name, options)
  @elb_name   = elb_name
  @start_time = options[:start_time]
  @end_time   = options[:end_time]

  opts = options.select {|k, v| CLIENT_OPTIONS.include? k.to_sym }
  @client = ::Aws::CloudWatch::Client.new(opts)
end

Public Instance Methods

get_statistics(metric_name, stats: nil, start_time: nil, end_time: nil, period: nil) click to toggle source
# File lib/ruboty/elb_monitor/aws/cloudwatch.rb, line 16
def get_statistics(metric_name,
                   stats:      nil,
                   start_time: nil,
                   end_time:   nil,
                   period:     nil)
  stats      ||= default_stats(metric_name)
  end_time   ||= @end_time   || Time.now
  start_time ||= @start_time || (end_time - 3600)
  period     ||= end_time - start_time

  opts = {
    namespace:   "AWS/ELB",
    metric_name: metric_name,
    dimensions:  [ {name: "LoadBalancerName", value: @elb_name} ],
    start_time:  start_time,
    end_time:    end_time,
    period:      period.to_i,
    statistics:  [stats]
  }
  resp = @client.get_metric_statistics(opts)

  datapoints = default_datapoints(start_time, end_time, period)
  resp.datapoints.each do |datapoint|
    ts = datapoint.timestamp
    dp = datapoints[ts.to_i]
    dp.value = datapoint_to_value(datapoint)
    dp.unit  = datapoint.unit
    dp.freeze
  end

  OpenStruct.new(
    elb_name:   @elb_name,
    start_time: start_time,
    end_time:   end_time,
    metric:     resp.label,
    stats:      stats,
    label:      "#{resp.label}(#{stats})",
    datapoints: datapoints.sort_by{|k,v| k}.map(&:last)
  ).freeze
end

Private Instance Methods

datapoint_to_value(dp) click to toggle source
# File lib/ruboty/elb_monitor/aws/cloudwatch.rb, line 94
def datapoint_to_value(dp)
  dp.sample_count || dp.average || dp.sum || dp.minimum || dp.maximum
end
default_datapoints(start_time, end_time, period) click to toggle source
# File lib/ruboty/elb_monitor/aws/cloudwatch.rb, line 102
def default_datapoints(start_time, end_time, period)
  stime = sec_truncate(start_time)
  datapoints = {}
  ((end_time - start_time) / period).to_i.times do |i|
    ts = stime + i * period
    datapoints[ts.to_i] = OpenStruct.new(timestamp: ts, value: 0)
  end
  datapoints
end
default_stats(metric) click to toggle source
# File lib/ruboty/elb_monitor/aws/cloudwatch.rb, line 81
def default_stats(metric)
  case metric
  when *STATS_MAXIMUM_GROUP
    "Maximum"
  when *STATS_AVERAGE_GROUP
    "Average"
  when *STATS_SUM_GROUP
    "Sum"
  else
    raise ArgumentError, "#{metric} is unknown metric name"
  end
end
sec_truncate(t) click to toggle source
# File lib/ruboty/elb_monitor/aws/cloudwatch.rb, line 98
def sec_truncate(t)
  Time.new(t.year, t.mon, t.day, t.hour, t.min, nil, t.utc_offset)
end