Class: GRI::RRDUpdater

Inherits:
Updater show all
Defined in:
lib/gri/updater.rb

Instance Attribute Summary collapse

Attributes inherited from Updater

#options

Instance Method Summary collapse

Methods inherited from Updater

get_tag_ary

Methods included from Utils

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

Constructor Details

#initialize(options = {}) ⇒ RRDUpdater

Returns a new instance of RRDUpdater.



55
56
57
58
# File 'lib/gri/updater.rb', line 55

def initialize options={}
  super
  @tagrrds = {}
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



52
53
54
# File 'lib/gri/updater.rb', line 52

def key
  @key
end

#rrdObject (readonly)

Returns the value of attribute rrd.



52
53
54
# File 'lib/gri/updater.rb', line 52

def rrd
  @rrd
end

#rrd_classObject

Returns the value of attribute rrd_class.



53
54
55
# File 'lib/gri/updater.rb', line 53

def rrd_class
  @rrd_class
end

Instance Method Details

#closeObject



195
196
197
198
199
200
# File 'lib/gri/updater.rb', line 195

def close
  if @rrd
    @rrd.flush_buffer
  end
  @tagrrds.each_value {|tagrrd| tagrrd.flush_buffer}
end

#create_rrd(path, time, interval, ds_specs, rra_specs) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/gri/updater.rb', line 60

def create_rrd path, time, interval, ds_specs, rra_specs
  ds_args, @record_keys = mk_ds_args ds_specs, interval
  rra_args = rra_specs || mk_rra_args
  @rrd_args = ds_args + rra_args
  rrd = (@rrd_class || RRD).new @options[:rrdcached_address],
    @options[:base_dir]
  rrd.set_create_args path, (time - interval), interval, *@rrd_args
  rrd
end

#create_tagrrd(group, subdir, descr_v, record) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/gri/updater.rb', line 180

def create_tagrrd group, subdir, descr_v, record
  grp_dir = "#{options[:gra_dir]}/#{group}"
  Dir.mkdir grp_dir unless File.exist? grp_dir
  tag_dir = "#{grp_dir}/#{subdir}"
  Dir.mkdir tag_dir unless File.exist? tag_dir
  open(tag_dir + '/.description', 'w') {|f| f.puts descr_v}

  time = record['_time']
  path = "#{tag_dir}/#{@options[:host]}_#{@key}.rrd"
  tagrrd = (@rrd_class ||RRD).new @options[:rrdcached_address],
    @options[:base_dir]
  tagrrd.set_create_args path, (time - @interval), @interval, *@rrd_args
  tagrrd
end

#default_rra_argsObject



108
109
110
111
# File 'lib/gri/updater.rb', line 108

def default_rra_args
  ['RRA:AVERAGE:0.5:12:9000', 'RRA:MAX:0.5:1:20000',
    'RRA:MAX:0.5:12:9000', 'RRA:MAX:0.5:144:2000']
end

#mk_ds_args(ds_specs, interval) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gri/updater.rb', line 70

def mk_ds_args ds_specs, interval
  ds_args = []
  record_keys = []
  n = 0
  for spec_line in ds_specs
    key, dsname, dst = spec_line.split /,/
    heartbeat = interval * ((dst == 'DERIVE' or dst == 'COUNTER') ? 2.5 : 2)
    key = key[1..-1].intern if key[0] == ?:
    record_keys.push key
    ds_args.push "DS:#{dsname}:#{dst}:#{heartbeat.to_i}:U:U"
    n += 1
  end
  return ds_args, record_keys
end

#mk_key(options) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/gri/updater.rb', line 113

def mk_key options
  unless (key = options[:key])
    data_name = options[:data_name]
    key = (Numeric === (index = options[:index])) ?
    "#{data_name}#{index}" : "#{data_name}_#{index}"
  end
  key
end

#mk_path(options) ⇒ Object



122
123
124
125
126
127
# File 'lib/gri/updater.rb', line 122

def mk_path options
  host = options[:host]
  dir = options[:dir] || options[:gra_dir] + '/' + host
  dkey = mk_key options
  "#{dir}/#{host}_#{dkey}.rrd"
end

#mk_rra_argsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gri/updater.rb', line 85

def mk_rra_args
  rra_args = []
  for cf in ['average', 'max', 'min']
    optrraname = "rra-#{@options[:data_name]}-#{cf}"
    if (optrra = @config.getvar optrraname)
      for line in optrra
        steps, rows = line.split
        rra_args.push "RRA:#{cf.upcase}:0.5:#{steps}:#{rows}"
      end
    else
      optrraname = 'rra-' + cf
      optrra = @config.getvar optrraname
      if optrra
        for line in optrra
          steps, rows = line.split
          rra_args.push "RRA:#{cf.upcase}:0.5:#{steps}:#{rows}"
        end
      end
    end
  end
  rra_args.size.zero? ? default_rra_args : rra_args
end

#mk_update_str(data) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/gri/updater.rb', line 166

def mk_update_str data
  data.map {|item|
    if item =~ /\A0x[\da-f]+\z/i
      Integer(item)
    elsif item == '' or item == nil or
        (item.kind_of?(Float) and not(item.finite?)) or
        (item.kind_of?(String) and item !~ /\A[-+e\.\d]+$/i)
      'U'
    else
      item
    end
  }.join(':')
end

#update(time, record) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/gri/updater.rb', line 129

def update time, record
  unless @rrd
    data_name = options[:data_name]
    @specs = DEFS.get_specs data_name
    Log.debug "update host=#{options[:host]} data=#{data_name}"
    return unless @specs # unknown data_name
    return unless @specs[:ds]
    #if (index_key = (@specs[:index_key] || @specs[:named_index]) and
    #    index = record[index_key])
    #  options[:index] = key_encode index
    #  options.delete :key
    #end
    @key = mk_key options
    return if (ex_proc = @specs[:exclude?]) and ex_proc.call(record)
    path = mk_path options
    ds_specs = @specs[:ds]
    return if !ds_specs or ds_specs.empty?
    @rrd = create_rrd path, time, (@interval || 300),
      ds_specs, @specs[:rra]
  end
  data = @record_keys.map {|key| record[key]}
  s = "#{time.to_i}:#{mk_update_str data}"
  Log.debug "update key=#{@key} rrd data=#{s}"
  @rrd.buffered_update s

  if (prop = @specs[:prop]) and (descr_k = prop[:description]) and
      (description = record[descr_k]) and !description.empty?
    for group, subdir, descr_v in Updater.get_tag_ary(description)
      unless (tagrrd = @tagrrds[group + '/' + subdir])
        tagrrd = create_tagrrd group, subdir, descr_v, record
        @tagrrds[group + '/' + subdir] = tagrrd
      end
      tagrrd.buffered_update s
    end
  end
end