Class: GRI::RRDWriter

Inherits:
Writer show all
Defined in:
lib/gri/writer.rb

Constant Summary

Constants inherited from Writer

Writer::TYPES

Instance Attribute Summary

Attributes inherited from Writer

#loop

Instance Method Summary collapse

Methods inherited from Writer

create

Constructor Details

#initialize(options = {}) ⇒ RRDWriter

Returns a new instance of RRDWriter.



39
40
41
42
43
# File 'lib/gri/writer.rb', line 39

def initialize options={}
  @options = options
  @gra_dir = options[:gra_dir]
  @sysinfos = {}
end

Instance Method Details

#finalizeObject



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gri/writer.rb', line 92

def finalize
  unless @sysinfos.empty?
    if @options[:merge_p]
      path = "#{@gra_dir}/.sysdb/sysdb.tmp.#{$$}"
      FileUtils.mkdir_p File.dirname(path) unless File.exist? path
      open(path, 'w') {|f| LTSV.dump_to_io @sysinfos, f}
    else
      path = "#{@gra_dir}/.sysdb/sysdb.txt"
      FileUtils.mkdir_p File.dirname(path) unless File.exist? path
      Utils.update_ltsv_file path, '_host', @sysinfos
    end
  end
end

#mergeObject



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gri/writer.rb', line 106

def merge
  sysdb_dir = "#{@gra_dir}/.sysdb"
  path0 = sysdb_dir + '/sysdb.txt'
  values = LTSV.load_from_file(path0) rescue {}
  nvalues = values.inject({}) {|h, v| h[v['_host']] = v; h}
  for path in Dir.glob(sysdb_dir + '/sysdb.tmp.*')
    (other = LTSV.load_from_file(path); File.unlink path) rescue next
    other = other.inject({}) {|h, v| h[v['_host']] = v; h}
    nvalues.merge! other
  end
  LTSV.dump_to_file nvalues, path0
end

#write(records) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gri/writer.rb', line 45

def write records
  now = Time.now.to_i
  hrecords = {}
  updaters = {}
  hosts = {}
  for record in records
    if (host = record['_host']) and (key = record['_key'])
      hosts[host] = true
      if key == 'SYS'
        @sysinfos[host] = record
      else
        data_name, index = key.scan(/\A([^_\d]*)(_?.*)/).first
        basename = "#{host}_#{key}"
        unless (updater = updaters[basename])
          hdir = "#{@gra_dir}/#{host}"
          FileUtils.mkdir_p hdir unless File.directory? hdir
          opts = {:data_name=>data_name, :gra_dir=>@gra_dir,
            :host=>host, :key=>key,
            :interval=>(record['_interval'] || @options[:interval])}
          if @options[:rrdcached_address]
            opts[:rrdcached_address] = @options[:rrdcached_address]
          end
          if @options[:base_dir]
            opts[:base_dir] = @options[:base_dir]
          end
          updater = updaters[basename] = RRDUpdater.new(opts)
        end
        time = (record['_time'] || now).to_i
        updater.update time, record
        key = updater.key
      end
      (r = record.dup)['_key'] = key
      r['_mtime'] = now
      (hrecords[host] ||= {})[key] = r
    end
  end
  hrecords.each {|host, h|
    path = "#{@gra_dir}/#{host}/.records.txt"
    FileUtils.mkdir_p File.dirname(path) unless File.exist? path
    Utils.update_ltsv_file path, '_key', h
  }
  updaters.each {|k, u| u.close}
  hosts.each {|host,|
    @sysinfos[host] ||= {'_host'=>host.dup, '_time'=>now, '_mtime'=>now}
  }
end