Class: GRI::TraCollector

Inherits:
Collector show all
Defined in:
lib/gri/tra_collector.rb

Constant Summary

Constants inherited from Collector

Collector::TYPES

Class Attribute Summary collapse

Attributes inherited from Collector

#attached_at, #host, #interval, #loop, #options, #timeout

Instance Method Summary collapse

Methods inherited from Collector

#attach, create, #initialize, #on_detach, #on_error, #on_init, #on_retry, #on_timeout, #sync?

Constructor Details

This class inherits a constructor from GRI::Collector

Class Attribute Details

.db_classObject

Returns the value of attribute db_class.



77
78
79
# File 'lib/gri/tra_collector.rb', line 77

def db_class
  @db_class
end

.gra_dirObject

Returns the value of attribute gra_dir.



76
77
78
# File 'lib/gri/tra_collector.rb', line 76

def gra_dir
  @gra_dir
end

.tra_dirObject

Returns the value of attribute tra_dir.



76
77
78
# File 'lib/gri/tra_collector.rb', line 76

def tra_dir
  @tra_dir
end

.tra_uriObject

Returns the value of attribute tra_uri.



77
78
79
# File 'lib/gri/tra_collector.rb', line 77

def tra_uri
  @tra_uri
end

Instance Method Details

#on_attachObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gri/tra_collector.rb', line 8

def on_attach
  # @loop.detach self if @config['nop']
  dir = "#{self.class.gra_dir}/#{host}"
  Dir.mkdir dir unless File.directory? dir

  db_class = self.class.db_class
  db = (db_class <= RemoteLDB) ? db_class.new(self.class.tra_uri, host) :
    db_class.new("#{self.class.tra_dir}/#{host}")

  @loop.next_tick {
    begin
      update_rrd_dir dir, db, options
    ensure
      @loop.detach self
      db.close
    end
  }
end

#recdump(dir, records) ⇒ Object



68
69
70
71
72
73
# File 'lib/gri/tra_collector.rb', line 68

def recdump dir, records
  path = dir + '/.rdump'
  open(path, 'a') {|f|
    records.each {|r| f.puts r.inspect}
  }
end

#update_rrd_dir(dir, db, options) ⇒ Object



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
59
60
61
62
63
64
65
66
# File 'lib/gri/tra_collector.rb', line 27

def update_rrd_dir dir, db, options
  data_names = db.get_data_names
  for data_name, interval in data_names
    lu_path = "#{dir}/.lu_#{data_name}"
    lu_time = nil
    lu_pos = nil
    if File.exist? lu_path
      str = File.read(lu_path)
      if str =~ /\A(\d+) (\d+)\Z/
        lu_time = Time.at $1.to_i
        lu_pos = $2.to_i
      end
    end
    lu_time ||= Time.at(0)
    lu_pos ||= 0

    update_p = false
    t = nil
    records = []
    db.get_after(data_name, lu_time, lu_pos) {|t, record, pos|
      if $debug
        ts = t.strftime '%Y-%m-%d %H:%M:%S'
        puts "update #{ts} #{record['_host']} #{record['_key']}"
      end
      record['_interval'] = interval
      records.push record
      update_p = true
      lu_time = t
      lu_pos = pos
      if records.size > 2000
        @cb.call records
        records.clear
      end
    }
    if update_p
      @cb.call records
      open(lu_path, 'w') {|f| f.print "#{lu_time.to_i} #{lu_pos}"}
    end
  end
end