Class: Flapjack::Data::Alert

Inherits:
Object
  • Object
show all
Includes:
Utility, Zermelo::Records::RedisSet
Defined in:
lib/flapjack/data/alert.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utility

#hashify, #load_template, #local_timezone, #relative_time_ago, #remove_utc_offset, #stringify, #symbolize, #time_period_in_words, #truncate

Class Method Details

.notification_type(act, cond) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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

Instance Method Details

#last_stateObject



126
127
128
# File 'lib/flapjack/data/alert.rb', line 126

def last_state
  @last_state ||= (self.last_action || self.last_condition)
end

#last_state_title_caseObject



134
135
136
# 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_typeObject



78
79
80
# File 'lib/flapjack/data/alert.rb', line 78

def notification_type
  self.class.notification_type(action, condition)
end

#rollup_statesObject

TODO handle JSON exception



65
66
67
68
69
70
71
# 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) ⇒ Object



73
74
75
76
# 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 = {}) ⇒ Object

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'


148
149
150
151
152
153
154
155
156
157
# 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_summaryObject



138
139
140
141
142
143
# 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

#stateObject



122
123
124
# File 'lib/flapjack/data/alert.rb', line 122

def state
  @state ||= (self.action || self.condition)
end

#state_title_caseObject



130
131
132
# File 'lib/flapjack/data/alert.rb', line 130

def state_title_case
  ['ok'].include?(state) ? state.upcase : state.titleize
end

#to_sObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# 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

#typeObject



100
101
102
103
104
105
106
107
108
109
# 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_caseObject



111
112
113
114
115
116
117
118
119
120
# 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