diff options
author | Saku Ytti <saku@ytti.fi> | 2014-02-10 12:26:59 +0200 |
---|---|---|
committer | Saku Ytti <saku@ytti.fi> | 2014-02-10 12:26:59 +0200 |
commit | 227ea783d3598c2bd974cdaf4aac8c255870e264 (patch) | |
tree | ac3af5a9473fb06bda8bbbb6a30c01c453f0dfe8 | |
parent | 92fbaa712c99ae5b523be0ebbc46fca9cdddef4e (diff) |
Handle exceptions with different log levels
-rw-r--r-- | lib/oxidized/input/input.rb | 18 | ||||
-rw-r--r-- | lib/oxidized/input/ssh.rb | 14 | ||||
-rw-r--r-- | lib/oxidized/input/telnet.rb | 4 | ||||
-rw-r--r-- | lib/oxidized/node.rb | 25 | ||||
-rw-r--r-- | oxidized.gemspec | 2 |
5 files changed, 40 insertions, 23 deletions
diff --git a/lib/oxidized/input/input.rb b/lib/oxidized/input/input.rb index d59c4f3..61f4634 100644 --- a/lib/oxidized/input/input.rb +++ b/lib/oxidized/input/input.rb @@ -1,12 +1,16 @@ module Oxidized class Input - RescueFail = [ - Timeout::Error, - Errno::ECONNREFUSED, - Errno::ECONNRESET, - Errno::EHOSTUNREACH, - Errno::EPIPE, - ] + RescueFail = { + :debug => [ + Errno::ECONNREFUSED, + ], + :warn => [ + Timeout::Error, + Errno::ECONNRESET, + Errno::EHOSTUNREACH, + Errno::EPIPE, + ], + } class << self def inherited klass Oxidized.mgr.loader = { :class => klass } diff --git a/lib/oxidized/input/ssh.rb b/lib/oxidized/input/ssh.rb index 3fdaa70..67be180 100644 --- a/lib/oxidized/input/ssh.rb +++ b/lib/oxidized/input/ssh.rb @@ -2,11 +2,15 @@ module Oxidized require 'net/ssh' require 'oxidized/input/cli' class SSH < Input - RescueFail = [ - Net::SSH::Disconnect, - Net::SSH::AuthenticationFailed, - RuntimeError, - ] + RescueFail = { + :debug => [ + Net::SSH::Disconnect, + ], + :warn => [ + RuntimeError, + Net::SSH::AuthenticationFailed, + ], + } include CLI class NoShell < StandardError; end diff --git a/lib/oxidized/input/telnet.rb b/lib/oxidized/input/telnet.rb index 0ae6877..31ed8d0 100644 --- a/lib/oxidized/input/telnet.rb +++ b/lib/oxidized/input/telnet.rb @@ -2,8 +2,8 @@ module Oxidized require 'net/telnet' require 'oxidized/input/cli' class Telnet < Input - RescueFail = [] - include CLI + RescueFail = {} + include CLI attr_reader :telnet def connect node diff --git a/lib/oxidized/node.rb b/lib/oxidized/node.rb index b7dc134..5f09096 100644 --- a/lib/oxidized/node.rb +++ b/lib/oxidized/node.rb @@ -21,9 +21,8 @@ module Oxidized status, config = :fail, nil @input.each do |input| @model.input = input = input.new - if connect input - config = input.get - status = :success if config + if config=run_input(input) + status = :success break else status = :no_connection @@ -32,12 +31,22 @@ module Oxidized [status, config] end - def connect input - rescue_fail = input.class::RescueFail + input.class.superclass::RescueFail + def run_input input + rescue_fail = {} + [input.class::RescueFail, input.class.superclass::RescueFail].each do |hash| + hash.each do |level,errors| + errors.each do |err| + rescue_fail[err] = level + end + end + end begin - input.connect self - rescue *rescue_fail => err - Log.warn '%s raised %s with msg "%s"' % [self.ip, err.class, err.message] + if input.connect self + input.get + end + rescue *rescue_fail.keys.flatten => err + level = rescue_fail[err.class] + Log.send(level, '%s raised %s with msg "%s"' % [self.ip, err.class, err.message]) return false end end diff --git a/oxidized.gemspec b/oxidized.gemspec index 48eee08..1394f77 100644 --- a/oxidized.gemspec +++ b/oxidized.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'oxidized' - s.version = '0.0.18' + s.version = '0.0.19' s.platform = Gem::Platform::RUBY s.authors = [ 'Saku Ytti' ] s.email = %w( saku@ytti.fi ) |