lib/visage-app/collectd/json.rb in visage-app-0.9.4 vs lib/visage-app/collectd/json.rb in visage-app-0.9.5

- old
+ new

@@ -12,36 +12,53 @@ # class CollectdJSON def initialize(opts={}) @rrddir = opts[:rrddir] || CollectdJSON.rrddir + @types = opts[:types] || CollectdJSON.types end # Entry point. def json(opts={}) host = opts[:host] plugin = opts[:plugin] plugin_instances = opts[:plugin_instances][/\w.*/] instances = plugin_instances.blank? ? '*' : '{' + plugin_instances.split('/').join(',') + '}' - @plugin_names = [] + rrdglob = "#{@rrddir}/#{host}/#{plugin}/#{instances}.rrd" - rrdglob = "#{@rrddir}/#{host}/#{plugin}/#{instances}.rrd" - plugin_offset = @rrddir.size + 1 + host.size + 1 + start = case + when opts[:start] && opts[:start].index('.') + opts[:start].split('.').first + when opts[:start] + opts[:start] + else + (Time.now - 3600).to_i + end + finish = case + when opts[:finish] && opts[:finish].index('.') + opts[:finish].split('.').first + when opts[:finish] + opts[:finish] + else + Time.now.to_i + end + data = [] Dir.glob(rrdglob).map do |rrdname| parts = rrdname.gsub(/#{@rrddir}\//, '').split('/') - host = parts[0] + host_name = parts[0] plugin_name = parts[1] - instance_name = parts[2].split('.').first + instance_name = File.basename(parts[2], '.rrd') rrd = Errand.new(:filename => rrdname) + data << { :plugin => plugin_name, :instance => instance_name, - :host => host, - :start => opts[:start] || (Time.now - 3600).to_i, - :finish => opts[:finish] || Time.now.to_i, + :host => host_name, + :start => start, + :finish => finish, :rrd => rrd } end encode(data) end @@ -101,9 +118,13 @@ class << self attr_writer :rrddir def rrddir @rrddir ||= Visage::Config.rrddir + end + + def types + @types ||= Visage::Config.types end def hosts if @rrddir Dir.glob("#{@rrddir}/*").map {|e| e.split('/').last }.sort