Class: Fluent::ZabbixSimpleBufferdOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_zabbix_simple_bufferd.rb

Overview

Fluent::ZabbixSimpleOutput

Copyright © 2013 NAKANO Hideo

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Defined Under Namespace

Classes: KeyMap

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZabbixSimpleBufferdOutput

Returns a new instance of ZabbixSimpleBufferdOutput.



21
22
23
24
25
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 21

def initialize
  super
  require 'zabbix'
  require 'socket'
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



27
28
29
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 27

def host
  @host
end

#key_sizeObject (readonly)

Returns the value of attribute key_size.



27
28
29
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 27

def key_size
  @key_size
end

#map_keysObject (readonly)

Returns the value of attribute map_keys.



27
28
29
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 27

def map_keys
  @map_keys
end

#portObject (readonly)

Returns the value of attribute port.



27
28
29
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 27

def port
  @port
end

#zabbix_serverObject (readonly)

Returns the value of attribute zabbix_server.



27
28
29
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 27

def zabbix_server
  @zabbix_server
end

Instance Method Details

#configure(conf) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 36

def configure(conf)
  super

  if @zabbix_server.nil?
    raise Fluent::ConfigError, "missing zabbix_server"
  end

  @map_keys = []
  (0..@key_size).each do |i|
    next unless conf["map_key#{i}"]
    pattern,replace = conf["map_key#{i}"].split(' ', 2)
    @map_keys.push(KeyMap.new(i, Regexp.new(pattern), replace))
  end

  if @map_keys.nil? or @map_keys.size == 0
    raise Fluent::ConfigError, "missing map_key[0..]"
  end
end

#create_zbx_senderObject



63
64
65
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 63

def create_zbx_sender
  Zabbix::Sender.new(:host => @zabbix_server, :port => @port)
end

#emit(tag, es, chain) ⇒ Object



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
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 84

def emit(tag, es, chain)
  zbx_sender = nil
  begin
    $log.trace { "connecting to zabbix server `#{@zabbix_server}(port:`#{@port}`)" }
    zbx_sender = create_zbx_sender
    zbx_sender.connect
    $log.trace "done connected to zabbix server"
  rescue
    $log.warn "could not connect to zabbix server `#{@zabbix_server}(port:`#{@port})`, exception: #{$!.class}, '#{$!.message}'"
  end

  if zbx_sender
    es.each do |time, record|
      record.each do |key,value|
        @map_keys.each do |map|
          zbx_key = map_key(key, map.pattern, map.replace)
          next unless zbx_key
          send(zbx_sender, zbx_key, value, time)
        end
      end
    end
    zbx_sender.disconnect
  end

  # call next chain
  chain.next
end

#map_key(key, pattern, replace) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 112

def map_key(key, pattern, replace)
  unless pattern =~ key
    nil
  else
    key.sub(pattern, replace)
  end
end

#send(zbx_sender, name, value, time) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 67

def send(zbx_sender, name, value, time)
  begin
    $log.debug { "name: #{name}, value: #{value}, time: #{time}" }

    opts = { :host => @host, :ts => time }
    status = zbx_sender.send_data(name, value.to_s, opts)

  rescue IOError, EOFError, SystemCallError
    # server didn't respond
    $log.warn "Zabbix::Sender.send_data raises exception: #{$!.class}, '#{$!.message}'"
    status = false
  end
  unless status
    $log.warn "failed to send to zabbix_server `#{@zabbix_server}(port:`#{@port}`), host:#{@host} '#{name}': #{value}"
  end
end

#shutdownObject



59
60
61
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 59

def shutdown
  super
end

#startObject



55
56
57
# File 'lib/fluent/plugin/out_zabbix_simple_bufferd.rb', line 55

def start
  super
end