Class: Fluent::Plugin::ZabbixSimpleOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_zabbix_simple.rb

Defined Under Namespace

Classes: KeyMap

Constant Summary collapse

DEFAULT_BUFFER_TYPE =
"memory"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



30
31
32
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 30

def host
  @host
end

#key_sizeObject (readonly)

Returns the value of attribute key_size.



30
31
32
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 30

def key_size
  @key_size
end

#map_keysObject (readonly)

Returns the value of attribute map_keys.



30
31
32
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 30

def map_keys
  @map_keys
end

#portObject (readonly)

Returns the value of attribute port.



30
31
32
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 30

def port
  @port
end

#zabbix_serverObject (readonly)

Returns the value of attribute zabbix_server.



30
31
32
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 30

def zabbix_server
  @zabbix_server
end

Instance Method Details

#configure(conf) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 43

def configure(conf)
  compat_parameters_convert(conf, :buffer)
  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



71
72
73
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 71

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

#format(tag, time, record) ⇒ Object



92
93
94
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 92

def format(tag, time, record)
  [time, record].to_msgpack
end

#formatted_to_msgpack_binary?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 96

def formatted_to_msgpack_binary?
  true
end

#map_key(key, pattern, replace) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 125

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

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 75

def send_zabbix(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



67
68
69
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 67

def shutdown
  super
end

#startObject



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

def start
  super
end

#write(chunk) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fluent/plugin/out_zabbix_simple.rb', line 100

def write(chunk)
  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
    chunk.msgpack_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_zabbix(zbx_sender, zbx_key, value, time)
        end
      end
    end
    zbx_sender.disconnect
  end
end