From e9ec48a85006b481007db28d0b1fb7344f1d1224 Mon Sep 17 00:00:00 2001 From: nopedial Date: Tue, 15 Oct 2013 00:13:03 +0200 Subject: enhance fetch support --- lib/oxidized/api/rest.rb | 8 +++++++- lib/oxidized/nodes.rb | 6 +++--- lib/oxidized/output/file.rb | 25 ++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 7 deletions(-) (limited to 'lib/oxidized') diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb index d876917..d1bd7dc 100644 --- a/lib/oxidized/api/rest.rb +++ b/lib/oxidized/api/rest.rb @@ -41,9 +41,15 @@ module Oxidized # /nodes/show/node - returns data about node when /show\/(.*)/ send res, @nodes.show($1) + # /nodes/fetch/ or /nodes/fetch/group// - returns json formatted configuration file for when /fetch\/(.*)/ begin - send res, @nodes.fetch($1) + if $1.include? '/' + group, node = $1.split("/")[1..2] + else + group, node = 0, $1 + end + send res, @nodes.fetch(node, group) rescue Oxidized::NotSupported => e send res, e end diff --git a/lib/oxidized/nodes.rb b/lib/oxidized/nodes.rb index dcebe08..d878d89 100644 --- a/lib/oxidized/nodes.rb +++ b/lib/oxidized/nodes.rb @@ -27,10 +27,10 @@ module Oxidized i = find_index node self[i].serialize if i end - def fetch node - i = find_index node + def fetch node, group raise Oxidized::NotSupported unless Oxidized.mgr.output.respond_to? :fetch - self[i].output.new.fetch node + i = find_index node + self[i].output.new.fetch node, group end def del node i = find_index node diff --git a/lib/oxidized/output/file.rb b/lib/oxidized/output/file.rb index 8aaae0e..08272a5 100644 --- a/lib/oxidized/output/file.rb +++ b/lib/oxidized/output/file.rb @@ -25,9 +25,28 @@ class OxFile < Output open(file, 'w') { |fh| fh.write data } end - def fetch node - IO.readlines File.join(@cfg[:directory], node) + + def fetch node, group + cfg_dir = @cfg[:directory] + if group != 0 # group is explicitly defined by user + IO.readlines File.join(cfg_dir, group, node) + else + if File.exists?("#{cfg_dir}/#{node}") # node configuration file is stored on base directory + IO.readlines File.join(cfg_dir, node) + else + Dir.foreach(cfg_dir) do |sub_dir| + next if sub_dir == '.' or sub_dir == '..' + if File.directory?("#{cfg_dir}/#{sub_dir}") + files = Dir.entries("#{cfg_dir}/#{sub_dir}") + files.each do |f| + next if File.directory?("#{cfg_dir}/#{sub_dir}/#{f}") or f == '.' or f == '..' + IO.readlines File.join(cfg_dir, sub_dir, f) if f == node # node configuration file found on sub directory + end + end + end + end + end end - + end end -- cgit v1.2.1