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.



17
18
19
20
# File 'lib/visage-app/collectd/json.rb', line 17

def initialize(opts={})
  @rrddir = opts[:rrddir] || Visage::Collectd::JSON.rrddir
  @types  = opts[:types]  || Visage::Collectd::JSON.types
end

Class Attribute Details

.rrddirObject



116
117
118
# File 'lib/visage-app/collectd/json.rb', line 116

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

Class Method Details

.hostsObject



124
125
126
127
128
# File 'lib/visage-app/collectd/json.rb', line 124

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

.plugins(opts = {}) ⇒ Object



130
131
132
133
# File 'lib/visage-app/collectd/json.rb', line 130

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

.typesObject



120
121
122
# File 'lib/visage-app/collectd/json.rb', line 120

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

Instance Method Details

#json(opts = {}) ⇒ Object

Entry point.



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
# File 'lib/visage-app/collectd/json.rb', line 34

def json(opts={})
  host      = opts[:host]
  plugin    = opts[:plugin]
  instances = opts[:instances][/\w.*/]
  instances = instances.blank? ? '*' : '{' + instances.split('/').join(',') + '}'
  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 }
  end

  encode(data)
end

#parse_time(time, opts = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/visage-app/collectd/json.rb', line 22

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