Class: Zabbirc::Zabbix::Event

Inherits:
Resource::Base show all
Defined in:
lib/zabbirc/zabbix/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource::Base

#[], api, #api, #id, #initialize, #method_missing, model_name, set_model_name

Methods included from Resource::Associations

#has_many, #has_one

Methods included from Resource::Finders

#find, #get

Constructor Details

This class inherits a constructor from Zabbirc::Zabbix::Resource::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Zabbirc::Zabbix::Resource::Base

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



21
22
23
# File 'lib/zabbirc/zabbix/event.rb', line 21

def attrs
  @attrs
end

Class Method Details

.recent(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/zabbirc/zabbix/event.rb', line 6

def self.recent options={}
  params = {
      acknowledged: false,
      time_from: Zabbirc.config.notify_about_events_from_last.ago.utc.to_i,
      priority_from: 0,
      selectRelatedObject: :extend,
      selectHosts: :extend
  }.merge(options)

  priority_from = Priority.new(params.delete(:priority_from))
  events = get params
  events = events.find_all{|e| e.priority >= priority_from }
  events.sort{|x,y| x.priority <=> y.priority }
end

Instance Method Details

#acknowledge(message) ⇒ Object



31
32
33
34
# File 'lib/zabbirc/zabbix/event.rb', line 31

def acknowledge message
  res = api.event.acknowledge(eventids: id, message: message)
  res["eventids"].collect(&:to_i).include? id.to_i
end

#acknowledged?Boolean



36
37
38
# File 'lib/zabbirc/zabbix/event.rb', line 36

def acknowledged?
  acknowledged.to_i == 1
end

#created_atObject



40
41
42
# File 'lib/zabbirc/zabbix/event.rb', line 40

def created_at
  Time.at(clock.to_i)
end

#format_label(fmt) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/zabbirc/zabbix/event.rb', line 73

def format_label fmt
  fmt.gsub("%priority-code", "#{priority.code}").
      gsub("%priority-num", "#{priority.number}").
      gsub("%time", "#{created_at.to_formatted_s(:short)}").
      gsub("%msg", "#{message}").
      gsub("%id", "#{id}").
      gsub("%state", "#{state}")
end

#labelObject



69
70
71
# File 'lib/zabbirc/zabbix/event.rb', line 69

def label
  format_label "|%id| %time [%priority-code] %msg - %state"
end

#messageObject



60
61
62
63
64
65
66
67
# File 'lib/zabbirc/zabbix/event.rb', line 60

def message
  desc = related_object.description
  if desc.include?("{HOST.NAME}")
    desc.sub("{HOST.NAME}", hosts.collect(&:host).join(', '))
  else
    "#{desc} on #{hosts.collect(&:host).join(', ')}"
  end
end

Raises:

  • (AttributeError)


25
26
27
28
29
# File 'lib/zabbirc/zabbix/event.rb', line 25

def related_object
  raise AttributeError, "`source` attribute required" if @attrs[:source].blank?
  raise AttributeError, "`object` attribute required" if @attrs[:object].blank?
  @related_object ||= determine_related_object
end

#valueObject Also known as: state



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zabbirc/zabbix/event.rb', line 44

def value
  case @attrs[:source].to_i
  when 0
    case @attrs[:value].to_i
    when 0
      :ok
    when 1
      :problem
    end
  else
    @attrs[:value]
  end
end