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, id_attr_name, #initialize, #method_missing, model_name, set_id_attr_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.



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

def attrs
  @attrs
end

#host_groupsObject



46
47
48
49
50
51
52
# File 'lib/zabbirc/zabbix/event.rb', line 46

def host_groups
  @host_groups ||= begin
    host_ids = hosts.collect(&:id).uniq
    hosts = Host.get(hostids: host_ids, selectGroups: :extend)
    hosts.flat_map(&:groups)
  end
end

Class Method Details

.preload_host_groups(events) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/zabbirc/zabbix/event.rb', line 26

def self.preload_host_groups events
  host_ids = events.flat_map(&:hosts).collect(&:id).uniq
  hosts = Host.get(hostids: host_ids, selectGroups: :extend)
  events.each do |event|
    event_host_ids = event.hosts.collect(&:id)
    event.host_groups = hosts.select{|h| event_host_ids.include? h.id }.flat_map(&:groups)
  end
end

.recent(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

  preload_host_groups events

  events = events.find_all{|e| e.priority >= priority_from }
  events.sort{|x,y| x.priority <=> y.priority }
end

Instance Method Details

#acknowledge(message) ⇒ Object



54
55
56
57
# File 'lib/zabbirc/zabbix/event.rb', line 54

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

#acknowledged?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/zabbirc/zabbix/event.rb', line 59

def acknowledged?
  acknowledged.to_i == 1
end

#any_host_matches?(regexp) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/zabbirc/zabbix/event.rb', line 110

def any_host_matches? regexp
  hosts.any?{|h| h.name =~ regexp }
end

#created_atObject



63
64
65
# File 'lib/zabbirc/zabbix/event.rb', line 63

def created_at
  Time.at(clock.to_i)
end

#format_label(fmt) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/zabbirc/zabbix/event.rb', line 100

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("%sid", "#{shorten_id}").
      gsub("%state", "#{state}")
end

#labelObject



96
97
98
# File 'lib/zabbirc/zabbix/event.rb', line 96

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

#messageObject



87
88
89
90
91
92
93
94
# File 'lib/zabbirc/zabbix/event.rb', line 87

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)


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

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

#shorten_idObject



81
82
83
# File 'lib/zabbirc/zabbix/event.rb', line 81

def shorten_id
  @shorten_id ||= Zabbirc.events_id_shortener.get_shorten_id id
end

#valueObject Also known as: state



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/zabbirc/zabbix/event.rb', line 67

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