Class: Sensu::Extension::InfluxRelay

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu/extensions/influxdb2/influx_relay.rb

Instance Method Summary collapse

Instance Method Details

#buffer_sizeObject



60
61
62
# File 'lib/sensu/extensions/influxdb2/influx_relay.rb', line 60

def buffer_size
  @buffer.map { |_db, tp| tp.map { |_p, points| points.length }.inject(:+) }.inject(:+) || 0
end

#flush_bufferObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sensu/extensions/influxdb2/influx_relay.rb', line 18

def flush_buffer
  logger.info('Flushing Buffer')
  @buffer.each do |db, tp|
    tp.each do |p, points|
      influxdb = EventMachine::HttpRequest.new("#{@influx_conf['base_url']}/write")
      post_data = {}
      post_data[:query] = { 'db' => db, 'precision' => p, 'u' => @influx_conf['username'], 'p' => @influx_conf['password'] }
      post_data[:body] = points.join(" \n")
      if @influx_conf['use_basic_auth']
        post_data[:head] = { 'authorization' => [@influx_conf['basic_user'], @influx_conf['basic_pass']] }
      end
      result = influxdb.post(post_data)
      next if @influx_conf.key?(db) && @influx_conf['debug_relay'] == false # this is to avoid the performance impact of checking the response everytime
      result.callback do
        if result.response =~ /.*error.*/
          logger.error("InfluxDB response: #{result.response}")
          if result.response =~ /.*database not found.*/
            post_data = {}
            post_data[:body] = ''
            post_data[:query] = {
              'db' => db,
              'precision' => p,
              'u' => @influx_conf['username'],
              'p' => @influx_conf['password'],
              'q' => "create database #{db}"
            }
            if @influx_conf['use_basic_auth']
              post_data[:head] = { 'authorization' => [@influx_conf['basic_user'], @influx_conf['basic_pass']] }
            end
            EventMachine::HttpRequest.new("#{@influx_conf['base_url']}/query").post(post_data)
            @influx_conf[db] = true
          end
        else
          logger.debug("Written: #{post_data[:body]}")
          @influx_conf[db] = true
        end
      end
    end
    @buffer[db] = {}
  end
end

#init(config) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/sensu/extensions/influxdb2/influx_relay.rb', line 8

def init(config)
  @influx_conf = config
  @buffer = {}
  @flush_timer = EventMachine::PeriodicTimer.new(@influx_conf['buffer_max_age'].to_i) do
    unless buffer_size.zero?
      flush_buffer
    end
  end
end

#loggerObject



72
73
74
# File 'lib/sensu/extensions/influxdb2/influx_relay.rb', line 72

def logger
  Sensu::Logger.get
end

#push(database, time_precision, data) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/sensu/extensions/influxdb2/influx_relay.rb', line 64

def push(database, time_precision, data)
  @buffer[database] ||= {}
  @buffer[database][time_precision] ||= []

  @buffer[database][time_precision].push(data)
  flush_buffer if buffer_size >= @influx_conf['buffer_max_size']
end