Class: Fluent::ZabbixOutput

Inherits:
Output
  • Object
show all
Includes:
Mixin::ConfigPlaceholders
Defined in:
lib/fluent/plugin/out_zabbix.rb

Constant Summary collapse

ZBXD =
"ZBXD\x01"
ZBX_PROTO_VALUE_SENDER_DATA =
'sender data'

Instance Method Summary collapse

Constructor Details

#initializeZabbixOutput

Returns a new instance of ZabbixOutput.



9
10
11
12
13
# File 'lib/fluent/plugin/out_zabbix.rb', line 9

def initialize
  super
  require 'socket'
  require 'yajl'
end

Instance Method Details

#bulk_send(time, bulk) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fluent/plugin/out_zabbix.rb', line 60

def bulk_send(time, bulk)
  bulk.each do |d|
  end
  begin
    sock = TCPSocket.open(@zabbix_server, @port)
    log.debug("zabbix: #{sock} #{bulk}, ts: #{time}")
    status = send_to_zabbix(sock, time, bulk)
  rescue => e
    log.warn "plugin-zabbix: raises exception: #{e}"
    status = false
  ensure
    sock.close if sock
  end

  unless status
    log.warn "plugin-zabbix: failed to send to zabbix_server: #{@zabbix_server}:#{@port} #{bulk}"
  end
end

#configure(conf) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fluent/plugin/out_zabbix.rb', line 31

def configure(conf)
  super

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

  if @name_keys.nil? and @name_key_pattern.nil?
    raise Fluent::ConfigError, "missing both of name_keys and name_key_pattern"
  end
  if not @name_keys.nil? and not @name_key_pattern.nil?
    raise Fluent::ConfigError, "cannot specify both of name_keys and name_key_pattern"
  end
  if @name_keys
    @name_keys = @name_keys.split(/ *, */)
  end
  if @name_key_pattern
    @name_key_pattern = Regexp.new(@name_key_pattern)
  end
end

#emit(tag, es, chain) ⇒ Object



79
80
81
82
83
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
111
112
# File 'lib/fluent/plugin/out_zabbix.rb', line 79

def emit(tag, es, chain)
  if @name_keys
    es.each {|time,record|
      host = gen_host(record)
      bulk = []
      @name_keys.each {|key|
        if record[key]
          bulk.push({ :key => format_key(tag, key, record),
                      :value => format_value(record[key]),
                      :host => host,
                      :time => time.to_i,
                    })
        end
      }
      bulk_send(time, bulk) if bulk.size > 0
    }
  else # for name_key_pattern
    es.each {|time,record|
      host = gen_host(record)
      bulk = []
      record.keys.each {|key|
        if @name_key_pattern.match(key) && record[key]
          bulk.push({ :key => format_key(tag, key, record),
                      :value => format_value(record[key]),
                      :host => host,
                      :time => time.to_i,
                    })
        end
      }
      bulk_send(time, bulk) if bulk.size > 0
    }
  end
  chain.next
end

#format_key(tag, key, record) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fluent/plugin/out_zabbix.rb', line 128

def format_key(tag, key, record)
  if @prefix_key
    if record[@prefix_key]
      key = "#{record[@prefix_key]}.#{key}"
    end
  end
  if @add_key_prefix
    if @add_key_prefix == '${tag}'
      "#{tag}.#{key}"
    else
      "#{@add_key_prefix}.#{key}"
    end
  else
    key
  end
end

#format_value(value) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/fluent/plugin/out_zabbix.rb', line 145

def format_value(value)
  if value.kind_of? Float
      # https://www.zabbix.com/documentation/2.4/manual/config/items/item
      # > Allowed range (for MySQL): -999999999999.9999 to 999999999999.9999 (double(16,4)).
      # > Starting with Zabbix 2.2, receiving values in scientific notation is also supported. E.g. 1e+70, 1e-70.
    value.round(4).to_s
  else
    value.to_s
  end
end

#gen_host(record) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fluent/plugin/out_zabbix.rb', line 114

def gen_host(record)
  if @host_key
    if record[@host_key]
      host = record[@host_key]
    else
      log.warn "plugin-zabbix: host_key is configured '#{@host_key}', but this record has no such key. use host '#{@host}'"
      host = @host
    end
  else
    host = @host
  end
  return host
end

#send_to_zabbix(sock, time, bulk) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/fluent/plugin/out_zabbix.rb', line 156

def send_to_zabbix(sock, time, bulk)
  req = Yajl::Encoder.encode({
    :request => ZBX_PROTO_VALUE_SENDER_DATA,
    :clock => time.to_i,
    :data => bulk,
  })
  sock.write(ZBXD + [ req.size ].pack('q') + req)
  sock.flush

  header = sock.read(5)
  if header != ZBXD
    return false
  end
  len = sock.read(8).unpack('q')[0]
  res = Yajl::Parser.parse(sock.read(len))
  return res['response'] == "success"
end

#shutdownObject



56
57
58
# File 'lib/fluent/plugin/out_zabbix.rb', line 56

def shutdown
  super
end

#startObject



52
53
54
# File 'lib/fluent/plugin/out_zabbix.rb', line 52

def start
  super
end