Class: GRI::InfluxdbWriter

Inherits:
Writer show all
Includes:
Utils
Defined in:
lib/gri/plugin/writer_influxdb.rb

Constant Summary

Constants inherited from Writer

Writer::TYPES

Instance Attribute Summary

Attributes inherited from Writer

#loop

Instance Method Summary collapse

Methods included from Utils

get_prop, key_encode, load_records, parse_host_key, parse_key, search_records, update_ltsv_file, url_encode

Methods inherited from Writer

create

Constructor Details

#initialize(options = {}) ⇒ InfluxdbWriter

Returns a new instance of InfluxdbWriter.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gri/plugin/writer_influxdb.rb', line 11

def initialize options={}
  @options = options
  database = 'gri'
  host = options[:influxdb_host] || 'localhost'
  username = options[:influxdb_username] || 'root'
  password = options[:influxdb_password] || 'root'

  @db = InfluxDB::Client.new database, :host=>host,
    :username=>username, :password=>password,
    :time_precision=>'s'
  dbs = @db.get_database_list
  if (dbs.select {|h| h['name'] == 'gri'}).empty?
    Log.info 'InfluxDB: create database'
    @db.create_database 'gri'
  end
  @buf = {}
end

Instance Method Details

#buffered_write(data_name, record) ⇒ Object



50
51
52
53
54
55
# File 'lib/gri/plugin/writer_influxdb.rb', line 50

def buffered_write data_name, record
  (ary = (@buf[data_name] ||= [])).push record
  if ary.size > 1000
    flush data_name, ary
  end
end

#finalizeObject



63
64
65
66
67
# File 'lib/gri/plugin/writer_influxdb.rb', line 63

def finalize
  for data_name, ary in @buf
    flush data_name, ary
  end
end

#flush(data_name, ary) ⇒ Object



57
58
59
60
61
# File 'lib/gri/plugin/writer_influxdb.rb', line 57

def flush data_name, ary
  while (sa = ary.take 10000; ary[0, 10000] = []; !sa.empty?)
    @db.write_point data_name, sa
  end
end

#write(records) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gri/plugin/writer_influxdb.rb', line 29

def write records
  time = Time.now.to_i
  for record in records
    record = record.dup
    time = record.delete '_time'
    #host = record.delete '_host'
    key = record.delete '_key'
    record.delete '_mtime'
    record.delete '_interval'
    data_name, index = parse_key key
    next if data_name == 'SYS'
    if data_name == ''
      data_name = 'interfaces'
    elsif data_name == 'docker'
      data_name = "docker.#{index}"
    end
    record.update :time=>time.to_i, :key=>index
    buffered_write data_name, record
  end
end