Parent

Visage::Collectd::JSON

Public Class Methods

new(opts={}) click to toggle source
# File lib/visage-app/collectd/json.rb, line 59
def initialize(opts={})
  @rrddir = opts[:rrddir] || Visage::Collectd::JSON.rrddir
  @types  = opts[:types]  || Visage::Collectd::JSON.types
end

Public Instance Methods

downsample_array(samples, old_resolution, new_resolution) click to toggle source
# File lib/visage-app/collectd/json.rb, line 116
def downsample_array(samples, old_resolution, new_resolution)
  return samples unless samples.length > 0
  timer_start = Time.now
  new_samples = []
  if (new_resolution > 0) and (old_resolution > 0) and (new_resolution % old_resolution == 0)
    groups_of = samples.length / (new_resolution / old_resolution)
    return samples unless groups_of > 0
    samples.in_groups(groups_of, false) {|group|
      new_samples << group.compact.mean
    }
  else
    raise "downsample_array: cowardly refusing to downsample as old_resolution (#{old_resolution.to_s}) doesn't go into new_resolution (#{new_resolution.to_s}) evenly, or new_resolution or old_resolution are zero."
  end
  timer = Time.now - timer_start

  new_samples
end
json(opts={}) click to toggle source

Entry point.

# File lib/visage-app/collectd/json.rb, line 76
def json(opts={})
  host        = opts[:host]
  plugin      = opts[:plugin]
  instances   = opts[:instances][/\w.*/]
  instances   = instances.blank? ? '*' : '{' + instances.split('/').join(',') + '}'
  percentiles = opts[:percentiles] !~ /^$|^false$/ ? true : false
  resolution  = opts[:resolution] || ""
  rrdglob     = "#{@rrddir}/#{host}/#{plugin}/#{instances}.rrd"
  finish      = parse_time(opts[:finish])
  start       = parse_time(opts[:start],  :default => (finish - 3600 || (Time.now - 3600).to_i))
  data        = []

  Dir.glob(rrdglob).map do |rrdname|
    parts         = rrdname.gsub(/#{@rrddir}\//, '').split('/')
    host_name     = parts[0]
    plugin_name   = parts[1]
    instance_name = File.basename(parts[2], '.rrd')
    rrd           = Errand.new(:filename => rrdname)

    data << {  :plugin      => plugin_name, :instance => instance_name,
               :host        => host_name,
               :start       => start,
               :finish      => finish,
               :rrd         => rrd,
               :percentiles => percentiles,
               :resolution  => resolution}

  end

  encode(data)
end
parse_time(time, opts={}) click to toggle source
# File lib/visage-app/collectd/json.rb, line 64
def parse_time(time, opts={})
  case
  when time && time.index('.')
    time.split('.').first.to_i
  when time
    time.to_i
  else
   opts[:default] || Time.now.to_i
  end
end
percentile_of_array(samples, percentage) click to toggle source
# File lib/visage-app/collectd/json.rb, line 108
def percentile_of_array(samples, percentage)
  if samples
    samples.sort[ (samples.length.to_f * ( percentage.to_f / 100.to_f ) ).to_i - 1 ]
  else
    raise "I can't work out percentiles on a nil sample set"
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.