From f5be715f06da3e32557e1b5ecfbe762f7d547a97 Mon Sep 17 00:00:00 2001 From: nopedial Date: Thu, 10 Oct 2013 01:22:11 +0200 Subject: primitive ascii configuration fetch API --- lib/oxidized/api/rest.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib/oxidized') diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb index 783b445..a2ff1ab 100644 --- a/lib/oxidized/api/rest.rb +++ b/lib/oxidized/api/rest.rb @@ -41,6 +41,19 @@ module Oxidized # /nodes/show/node - returns data about node when /show\/(.*)/ send res, @nodes.show($1) + when /fetch\/(.*)/ # primitive node fetch implementation for file config - /nodes/fetch/$ip_address + if CFG.output[:default] == "file" # ugly hack to disable configuration fetch for non ascii configurations + @cfg_root = CFG.output[:file] + File.open("#{@cfg_root[:directory]}/#{$1}", "r") do |pipe| + @res_config = [] + while ( line = pipe.gets ) do + @res_config << line # store configuration into an array to format and send + end + end + send res, @res_config + else + send res, "oxidized 0.0.14 support fetch for ascii configuration files only." # return error to end user + end end end end -- cgit v1.2.1 From 6ab1fbb3f9f551b67a6481aafe3591e837905a8b Mon Sep 17 00:00:00 2001 From: nopedial Date: Thu, 10 Oct 2013 23:48:29 +0200 Subject: add check and rescue for fetch params --- lib/oxidized/api/rest.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib/oxidized') diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb index a2ff1ab..4bf464b 100644 --- a/lib/oxidized/api/rest.rb +++ b/lib/oxidized/api/rest.rb @@ -42,7 +42,7 @@ module Oxidized when /show\/(.*)/ send res, @nodes.show($1) when /fetch\/(.*)/ # primitive node fetch implementation for file config - /nodes/fetch/$ip_address - if CFG.output[:default] == "file" # ugly hack to disable configuration fetch for non ascii configurations + if CFG.output[:default] == "file" and $1 != '' # ugly hack to disable configuration fetch for non ascii configurations and ensure that argument is received @cfg_root = CFG.output[:file] File.open("#{@cfg_root[:directory]}/#{$1}", "r") do |pipe| @res_config = [] @@ -51,8 +51,10 @@ module Oxidized end end send res, @res_config - else + elsif CFG.output[:default] != "file" and $1 != '' send res, "oxidized 0.0.14 support fetch for ascii configuration files only." # return error to end user + else # return error if no argument is received + send res, "missing argument - example: /nodes/fetch/192.0.2.1" end end end -- cgit v1.2.1 From 5bf9df1ebffd85604206d1baf81a5a92158a117a Mon Sep 17 00:00:00 2001 From: nopedial Date: Mon, 14 Oct 2013 11:15:12 +0200 Subject: implement fetch into output model --- lib/oxidized/api/rest.rb | 19 +++++-------------- lib/oxidized/nodes.rb | 6 ++++++ lib/oxidized/output/file.rb | 4 ++++ 3 files changed, 15 insertions(+), 14 deletions(-) (limited to 'lib/oxidized') diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb index 4bf464b..d876917 100644 --- a/lib/oxidized/api/rest.rb +++ b/lib/oxidized/api/rest.rb @@ -41,20 +41,11 @@ module Oxidized # /nodes/show/node - returns data about node when /show\/(.*)/ send res, @nodes.show($1) - when /fetch\/(.*)/ # primitive node fetch implementation for file config - /nodes/fetch/$ip_address - if CFG.output[:default] == "file" and $1 != '' # ugly hack to disable configuration fetch for non ascii configurations and ensure that argument is received - @cfg_root = CFG.output[:file] - File.open("#{@cfg_root[:directory]}/#{$1}", "r") do |pipe| - @res_config = [] - while ( line = pipe.gets ) do - @res_config << line # store configuration into an array to format and send - end - end - send res, @res_config - elsif CFG.output[:default] != "file" and $1 != '' - send res, "oxidized 0.0.14 support fetch for ascii configuration files only." # return error to end user - else # return error if no argument is received - send res, "missing argument - example: /nodes/fetch/192.0.2.1" + when /fetch\/(.*)/ + begin + send res, @nodes.fetch($1) + rescue Oxidized::NotSupported => e + send res, e end end end diff --git a/lib/oxidized/nodes.rb b/lib/oxidized/nodes.rb index e5e87e5..dcebe08 100644 --- a/lib/oxidized/nodes.rb +++ b/lib/oxidized/nodes.rb @@ -1,5 +1,6 @@ module Oxidized require 'oxidized/node' + class Oxidized::NotSupported < StandardError; end class Nodes < Array attr_accessor :source alias :put :unshift @@ -26,6 +27,11 @@ module Oxidized i = find_index node self[i].serialize if i end + def fetch node + i = find_index node + raise Oxidized::NotSupported unless Oxidized.mgr.output.respond_to? :fetch + self[i].output.new.fetch node + end def del node i = find_index node delete_at i if i diff --git a/lib/oxidized/output/file.rb b/lib/oxidized/output/file.rb index 66ad178..8aaae0e 100644 --- a/lib/oxidized/output/file.rb +++ b/lib/oxidized/output/file.rb @@ -25,5 +25,9 @@ class OxFile < Output open(file, 'w') { |fh| fh.write data } end + def fetch node + IO.readlines File.join(@cfg[:directory], node) + end + end end -- cgit v1.2.1