class Ruboty::ElbMonitor::Actions::State

Public Instance Methods

call() click to toggle source
# File lib/ruboty/elb_monitor/actions/state.rb, line 7
def call
  message.reply(state)
rescue => e
  message.reply(e.message)
end

Private Instance Methods

get_state(name, start_time, end_time, period) click to toggle source
# File lib/ruboty/elb_monitor/actions/state.rb, line 34
def get_state(name, start_time, end_time, period)
  cloudwatch = Ruboty::ElbMonitor.cloudwatch(name, start_time, end_time)
  Ruboty::ElbMonitor.metrics.map do |metric|
    cloudwatch.get_statistics(metric, period: period)
  end
end
get_time(trend) click to toggle source
# File lib/ruboty/elb_monitor/actions/state.rb, line 41
def get_time(trend)
  from   = message[:from]      rescue nil
  to     = message[:to]        rescue nil
  time   = message[:time].to_i rescue nil
  format = message[:format]    rescue nil
  if from && to
    last = Time.parse(from)
    now  = Time.parse(to)
    [ last, now, now - last ]
  else
    now = Time.now
    last = case format.to_s
           when "w"
             now - (time * 7 * 24 * 60 * 60)
           when "d"
             now - (time * 24 * 60 * 60)
           when "h"
             now - (time * 60 * 60)
           when "m"
             now - (time * 60)
           else
             now
           end
    period = trend ? (now - last) / time : (now - last)
    [ last, now, period.to_i ]
  end
end
get_value(st) click to toggle source
# File lib/ruboty/elb_monitor/actions/state.rb, line 69
def get_value(st)
  values = []
  st.datapoints.each do |dp|
    values << (dp.unit.to_s.downcase == "seconds" ? "%0.2f"%dp.value : "%d"%dp.value)
  end
  values.empty? ? 0 : values.join(Ruboty::ElbMonitor.trend_separator)
end
state(trend=false) click to toggle source
# File lib/ruboty/elb_monitor/actions/state.rb, line 14
def state(trend=false)
  return "ELB not found" if elbs.empty?

  start_time, end_time, period = get_time(trend)

  msg = []
  msg << "#{start_time.iso8601} - #{end_time.iso8601}"
  elbs.each do |name, real|
    msg << "*** #{name}(#{real}) ***"

    resp = get_state(real, start_time, end_time, period)
    label_max = resp.map(&:label).map(&:size).max
    resp.each do |st|
      value = get_value(st)
      msg << "  - #{st.label.ljust(label_max)}\t#{value}"
    end
  end
  msg.join("\n")
end