Class: ZabbixPusher::Pusher

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix_pusher.rb

Instance Method Summary collapse

Constructor Details

#initialize(templates, options = {}) ⇒ Pusher

Returns a new instance of Pusher.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zabbix_pusher.rb', line 8

def initialize(templates, options = {} )
  options[:zabbix_server_name] = 'localhost' unless options[:zabbix_server_name]
  options[:zabbix_server_port] = '10051' unless options[:zabbix_server_port]

  @options = options

  @templates = []
  @templates = Dir.glob(File.join(templates),'*.xml') if File.directory?(templates)
  @templates = templates.map{ |template| template if File.exist?(template) }.compact if templates.is_a?(Array)
  @templates =  [templates] if File.exist?(templates)

  @items = Hash.new

  @templates.each do |template|
    template_items = Nokogiri::XML(File.open(template))
    items = template_items.xpath('//item').map {|item| item.attributes['key'].text}.compact
    items.each do |item|
        parts = item.match(/([^\[]+)\[([^\]]+)/)
        key = parts[1]
        attributes = parts[2]
        (@items[key.to_sym]) ? @items[key.to_sym] << attributes : @items[key.to_sym] = [attributes]
    end
  end
end

Instance Method Details

#send(items = :all) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zabbix_pusher.rb', line 33

def send(items = :all)

  return if @items.nil?

  pushers = ( items == :all) ? (@items.keys.map{ |key| "ZabbixPusher::#{key.to_s.camelize}".constantize}) : ["ZabbixPusher::#{items.camelize}".constantize]

  processed_items = Hash.new

  pushers.map do |pusher|
    pusher_key = pusher.to_s.demodulize.underscore.to_sym
    processed_items.update pusher.new(@items[pusher_key],@options[pusher_key]).send(:processed_items)
  end
  processed_items
end