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 ++++++++++++++++++++++--- oxidized.gemspec | 2 +- 4 files changed, 33 insertions(+), 8 deletions(-) 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 diff --git a/oxidized.gemspec b/oxidized.gemspec index 38a2e41..53e12c9 100644 --- a/oxidized.gemspec +++ b/oxidized.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'oxidized' - s.version = '0.0.14' + s.version = '0.0.15' s.platform = Gem::Platform::RUBY s.authors = [ 'Saku Ytti' ] s.email = %w( saku@ytti.fi ) -- cgit v1.2.1 From ff5f75ad040fc40484ee186e609e8b5af668d7ae Mon Sep 17 00:00:00 2001 From: nopedial Date: Tue, 15 Oct 2013 22:20:57 +0200 Subject: fix fetch search and broken output --- lib/oxidized/output/file.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/oxidized/output/file.rb b/lib/oxidized/output/file.rb index 08272a5..8a53f91 100644 --- a/lib/oxidized/output/file.rb +++ b/lib/oxidized/output/file.rb @@ -34,15 +34,11 @@ class OxFile < Output 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 + file = Find.find('.').lazy.find{|e|e.match /#{node}/} + if file + open(file, 'r').readlines + else + "not found." end end end -- cgit v1.2.1 From c7d482c514e3d3917cbeb9a95ea8babf22116fe3 Mon Sep 17 00:00:00 2001 From: Samer Abdel-Hafez Date: Tue, 15 Oct 2013 23:17:20 +0200 Subject: Update file.rb --- lib/oxidized/output/file.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/oxidized/output/file.rb b/lib/oxidized/output/file.rb index 8a53f91..6e1a231 100644 --- a/lib/oxidized/output/file.rb +++ b/lib/oxidized/output/file.rb @@ -34,7 +34,7 @@ class OxFile < Output if File.exists?("#{cfg_dir}/#{node}") # node configuration file is stored on base directory IO.readlines File.join(cfg_dir, node) else - file = Find.find('.').lazy.find{|e|e.match /#{node}/} + file = if file open(file, 'r').readlines else -- cgit v1.2.1 From b8150eae04924f2c45cdb0c4b3f60902c8ae3d96 Mon Sep 17 00:00:00 2001 From: nopedial Date: Wed, 23 Oct 2013 23:20:25 +0200 Subject: finalize fetch API call --- lib/oxidized/output/file.rb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/oxidized/output/file.rb b/lib/oxidized/output/file.rb index 6e1a231..2ea2302 100644 --- a/lib/oxidized/output/file.rb +++ b/lib/oxidized/output/file.rb @@ -31,15 +31,11 @@ class OxFile < Output 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 + if File.exists? File.join(cfg_dir, node) # node configuration file is stored on base directory IO.readlines File.join(cfg_dir, node) else - file = - if file - open(file, 'r').readlines - else - "not found." - end + path = Dir.glob File.join(cfg_dir, '**', node) # fetch node in all groups + open(path[0], 'r').readlines end end end -- cgit v1.2.1 From feba4c2b9e0faeb40cad32f16d7b024a2d4327ae Mon Sep 17 00:00:00 2001 From: nopedial Date: Wed, 23 Oct 2013 23:29:20 +0200 Subject: cosmetic update --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ea521fc..15da1fe 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,11 @@ # Pitch * automatically adds/removes threads to meet configured retrieval interval * restful API to move node immediately to head-of-queue - * syslog udp+file example to catch config changg event (ios/junos) and trigger config fetch + * syslog udp+file example to catch config change event (ios/junos) and trigger config fetch * will signal ios/junos user who made change, which output module can (git does) use * 'git blame' will show for each line who and when the change was made * restful API to reload list of nodes (GET /nodes/reload) + * restful API to fetch configurations (/nodes/fetch/[NODE] or /nodes/fetch/group/[NODE]) # Install * early days, but try: -- cgit v1.2.1