Method: Notifu::Processor#process!

Defined in:
lib/notifu/workers/processor.rb

#process!Object

MAIN PROCESSING LOGIC #####################################



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/notifu/workers/processor.rb', line 71

def process!
  self.event.group_sla.each do |gs|

    # group related objects
    begin
      group = Notifu::Model::Group.with(:name, gs[:group])
      sla = Notifu::Model::Sla.with(:name, gs[:sla])
    rescue
      log "info", "#{self.event.notifu_id} [#{self.event.host}/#{self.event.service}/#{self.event.code.to_state}]: Object init failed. Is Notifu API running?"
      next
    end

    notified = {
      sla: String.new,
      group: String.new,
      actors: Array.new,
      contacts: Array.new,
      escalation_level: "none"
    }

    result = []

    # logic
    if enough_occurrences? && self.event.action.to_s == "create"
      result << "enough occurrences have passed"
      if ! silenced?
        result << "issue is not silenced"
        if duty_time? sla.timerange_values(self.now)
          result << "duty is active"
          if status_changed?
            result << "issue state has changed"
            notified = notify!(sla, group)
            result << "ACTION"
          else
            result << "issue state hasn't changed"
            case self.event.code
            when 0
              result << "issue is in OK state" << "IDLE"
            when 1
              result << "issue is in WARNING state"
              if first_notification?(sla, group)
                result << "issue is new"
                notified = notify!(sla, group)
                result << "ACTION"
              else
                result << "already notified" << "IDLE"
              end
            when 2
              result << "issue is not a warning"
              if renotify?(sla, group)
                result << "it's time to renotify"
                notified = notify!(sla, group)
                result << "ACTION"
              else
                result << "not yet time to renotify or escalate" << "IDLE"
              end
            else
              result << "unknown state (#{self.event.code})" << "IDLE"
            end
          end
        else
          result << "duty is not active at this time" << "IDLE"
        end
      else
        result << "issue is silenced" << "IDLE"
      end
    elsif self.event.action == "resolve" && self.issue.occurrences_count.to_i >= self.event.occurrences_trigger.to_i
      if ! silenced?
        result << "recovery of an event"
        notified = notify!(sla, group)
        result << "ACTION"
      elsif self.event.unsilence
        result << "recovery of an event (with unsilence)"
        unsilence!
      end
    else
      result << "not enough occurrences of this event" << "IDLE"
    end

    self.event.update_process_result!(notified)

    action_log_message = {
      logic: result.join(' -> '),
      result: result[-1],
      reason: result[-2],
      group: group.name,
      sla: sla.name,
      host: self.event.host,
      service: self.event.service,
      message: self.event.message,
      state: self.event.code.to_state,
      contacts: notified[:contacts].to_json,
      actors: notified[:actors].to_json,
      occurrences_trigger: self.event.occurrences_trigger.to_i,
      occurrences_count: self.event.occurrences_count.to_i,
      check_duration: self.event.duration,
      escalation_level: notified[:escalation_level].to_s,
      sidekiq_jid: self.jid,
      notifu_id: self.event.notifu_id,
      :"@timestamp" => self.now.iso8601,
    }

    action_log "processor", action_log_message

  end

  if self.event.process_result.length > 0
    self.issue.message = self.event.message
    self.issue.action = self.event.action
    self.issue.process_result = self.event.process_result
    @issue.save
  end

  if status_changed?
    self.issue.code = self.event.code
    self.issue.time_created = self.event.time_created
  end

  self.issue.occurrences_trigger = self.event.occurrences_trigger
  self.issue.occurrences_count = self.event.occurrences_count
  self.issue.time_last_event = self.event.time_last_event
  self.issue.sgs = self.event.sgs
  self.issue.aspiring_code = self.event.code
  self.issue.api_endpoint = self.event.api_endpoint
  self.issue.duration = self.event.duration

  @issue.save

  # delayed cleanup job
  cleanup!
end