Class: Notifu::Actor

Inherits:
Object show all
Includes:
Util, Sidekiq::Worker
Defined in:
lib/notifu/workers/actor.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

action_log, log, option

Class Attribute Details

.descObject

Returns the value of attribute desc


32
33
34
# File 'lib/notifu/workers/actor.rb', line 32

def desc
  @desc
end

.nameObject

Returns the value of attribute name


31
32
33
# File 'lib/notifu/workers/actor.rb', line 31

def name
  @name
end

.retryObject

Returns the value of attribute retry


33
34
35
# File 'lib/notifu/workers/actor.rb', line 33

def retry
  @retry
end

Instance Attribute Details

#contactsObject

Returns the value of attribute contacts


28
29
30
# File 'lib/notifu/workers/actor.rb', line 28

def contacts
  @contacts
end

#issueObject

Returns the value of attribute issue


27
28
29
# File 'lib/notifu/workers/actor.rb', line 27

def issue
  @issue
end

Instance Method Details

#actObject

`act` function must be defined in child classes (provides notification actors modularity)


39
40
41
# File 'lib/notifu/workers/actor.rb', line 39

def act
  exit 1
end

#apply_template(t) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/notifu/workers/actor.rb', line 66

def apply_template t
  @data ||= OpenStruct.new({
    notifu_id: self.issue.notifu_id,
    datacenter: self.issue.datacenter,
    host: self.issue.host,
    service: self.issue.service,
    status: self.issue.code.to_state,
    code: self.issue.code.to_s,
    message: self.issue.message,
    first_event: Time.at(self.issue.time_created.to_i),
    duration: (Time.now.to_i - self.issue.time_created.to_i).duration,
    interval: self.issue.interval.duration,
    refresh: self.issue.refresh.duration,
    occurrences_count: self.issue.occurrences_count,
    occurrences_trigger: self.issue.occurrences_trigger,
    uchiwa_url: "#{Notifu::CONFIG[:uchiwa_url]}/#/client/#{self.issue.datacenter}/#{self.issue.host}?check=#{self.issue.service}",
    playbook: self.issue.playbook
  })
  begin
    ERB.new(t).result(@data.instance_eval { binding })
  rescue Exception => e
    log "error", "Failed to render ERB template - #{e.class}: #{e.message}"
    ERB.new(default_template).result(@data.instance_eval {binding})
  end
end

#default_templateObject


92
93
94
# File 'lib/notifu/workers/actor.rb', line 92

def default_template
  "<%= status %> [<%= dc %>/<%= host %>/<%= service %>]: (<%= message %>) <%= duration %> [<%= notifu_id %>]"
end

#load_data(args) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/notifu/workers/actor.rb', line 52

def load_data args
  self.issue = Notifu::Model::Issue.with(:notifu_id, args[0])
  if self.issue == nil
    log "error", "Issue with NID #{args[0]} doesn't exist!"
    return false
  end
  self.contacts = Array.new
  args[1].each do |contact|
    c = Notifu::Model::Contact.with(:name, contact)
    self.contacts << c if c
  end
  return true
end

#perform(*args) ⇒ Object


46
47
48
49
50
# File 'lib/notifu/workers/actor.rb', line 46

def perform *args
  sleep 1
  load_data args || return
  act
end