Class: Visage::Collectd::JSON

Inherits:
Object
  • Object
show all
Defined in:
lib/visage-app/collectd/json.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ JSON

Returns a new instance of JSON.



59
60
61
62
# 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

Class Attribute Details

.rrddirObject



216
217
218
# File 'lib/visage-app/collectd/json.rb', line 216

def rrddir
  @rrddir ||= Visage::Config.rrddir
end

Class Method Details

.hostsObject



224
225
226
227
228
# File 'lib/visage-app/collectd/json.rb', line 224

def hosts
  if @rrddir
    Dir.glob("#{@rrddir}/*").map {|e| e.split('/').last }.sort
  end
end

.plugins(opts = {}) ⇒ Object



230
231
232
233
# File 'lib/visage-app/collectd/json.rb', line 230

def plugins(opts={})
  host = opts[:host] || '*'
  Dir.glob("#{@rrddir}/#{host}/*").map {|e| e.split('/').last }.sort
end

.typesObject



220
221
222
# File 'lib/visage-app/collectd/json.rb', line 220

def types
  @types  ||= Visage::Config.types
end

Instance Method Details

#downsample_array(samples, old_resolution, new_resolution) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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 = {}) ⇒ Object

Entry point.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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 = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



108
109
110
111
112
113
114
# 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