class Flapjack::Data::Alert

Public Class Methods

notification_type(act, cond) click to toggle source
# File lib/flapjack/data/alert.rb, line 82
def self.notification_type(act, cond)
  case act
  when 'acknowledgement'
    'acknowledgement'
  when /\Atest_notifications(?:\s+#{Flapjack::Data::Condition.unhealthy.keys.join('|')})?\z/
    'test'
  when nil
    case cond
    when 'ok'
      'recovery'
    when 'warning', 'critical', 'unknown'
      'problem'
    else
      'unknown'
    end
  end
end

Public Instance Methods

last_state() click to toggle source
# File lib/flapjack/data/alert.rb, line 126
def last_state
  @last_state ||= (self.last_action || self.last_condition)
end
last_state_title_case() click to toggle source
# File lib/flapjack/data/alert.rb, line 134
def last_state_title_case
  ['ok'].include?(last_state) ? last_state.upcase : last_state.titleize
end
notification_type() click to toggle source
# File lib/flapjack/data/alert.rb, line 78
def notification_type
  self.class.notification_type(action, condition)
end
rollup_states() click to toggle source

TODO handle JSON exception

# File lib/flapjack/data/alert.rb, line 65
def rollup_states
  if self.rollup_states_json.nil?
    @rollup_states = nil
    return
  end
  @rollup_states = Flapjack.load_json(self.rollup_states_json)
end
rollup_states=(rollup_states) click to toggle source
# File lib/flapjack/data/alert.rb, line 73
def rollup_states=(rollup_states)
  @rollup_states = rollup_states
  self.rollup_states_json = rollup_states.nil? ? nil : Flapjack.dump_json(rollup_states)
end
rollup_states_detail_text(opts = {}) click to toggle source

produces a textual list of checks that are failing broken down by state, eg: Critical: ‘PING’ on ‘foo-app-01.example.com’, ‘SSH’ on ‘foo-app-01.example.com’;

Warning: 'Disk / Utilisation' on 'foo-app-02.example.com'
# File lib/flapjack/data/alert.rb, line 148
def rollup_states_detail_text(opts = {})
  return '' if rollup_states.nil?
  max_checks = opts[:max_checks_per_state]
  rollup_states.each_with_object([]) do |(alert_state, alerts), memo|
    alerts = alerts[0..(max_checks - 1)] unless max_checks.nil? || (max_checks <= 0)
    next if alerts.empty?
    alerts << '...' if alerts.size < rollup_states[alert_state].size
    memo << "#{alert_state.titleize}: #{alerts.join(', ')}"
  end.join('; ')
end
rollup_states_summary() click to toggle source
# File lib/flapjack/data/alert.rb, line 138
def rollup_states_summary
  return '' if rollup_states.nil?
  rollup_states.each_with_object([]) do |(alert_state, alerts), memo|
    memo << "#{alert_state.titleize}: #{alerts.size}"
  end.join(', ')
end
state() click to toggle source
# File lib/flapjack/data/alert.rb, line 122
def state
  @state ||= (self.action || self.condition)
end
state_title_case() click to toggle source
# File lib/flapjack/data/alert.rb, line 130
def state_title_case
  ['ok'].include?(state) ? state.upcase : state.titleize
end
to_s() click to toggle source
# File lib/flapjack/data/alert.rb, line 159
def to_s
  contact = medium.contact
  msg = "Alert via #{medium.transport}:#{medium.address} to contact #{contact.id} (#{contact.name}): "
  msg += type_sentence_case
  if rollup
    msg += " - #{rollup_states_summary} (#{rollup_states_detail_text(:max_checks_per_state => 3)})"
  else
    msg += " - '#{self.check.name}'"
    unless ['acknowledgement', 'test'].include?(type)
      msg += " is #{state_title_case}"
    end
    if ['acknowledgement'].include?(type)
      msg += " has been acknowledged, unscheduled maintenance created for "
      msg += time_period_in_words(acknowledgement_duration)
    end
    if summary && summary.length > 0
      msg += " - #{summary}"
    end
  end
end
type() click to toggle source
# File lib/flapjack/data/alert.rb, line 100
def type
  case self.rollup
  when "problem"
    "rollup_problem"
  when "recovery"
    "rollup_recovery"
  else
    notification_type
  end
end
type_sentence_case() click to toggle source
# File lib/flapjack/data/alert.rb, line 111
def type_sentence_case
  case type
  when "rollup_problem"
    "Problem summary"
  when "rollup_recovery"
    "Problem summaries finishing"
  else
    type.titleize
  end
end