diff options
-rw-r--r-- | Gemfile.lock | 34 | ||||
-rw-r--r-- | Rakefile | 3 | ||||
-rwxr-xr-x[-rw-r--r--] | bin/oxidized | 1 | ||||
-rw-r--r-- | lib/oxidized/config/bootstrap.rb | 7 | ||||
-rw-r--r-- | lib/oxidized/input/input.rb | 7 | ||||
-rw-r--r-- | lib/oxidized/input/ssh.rb | 16 | ||||
-rw-r--r-- | lib/oxidized/input/telnet.rb | 25 | ||||
-rw-r--r-- | lib/oxidized/node.rb | 12 | ||||
-rwxr-xr-x | lib/tst | 19 | ||||
-rw-r--r-- | oxidized.gemspec | 2 |
10 files changed, 62 insertions, 64 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index e1cc091..53f4caf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,31 +1,31 @@ GEM remote: https://rubygems.org/ specs: - coderay (1.0.9) - diff-lcs (1.2.3) + coderay (1.1.0) + diff-lcs (1.2.4) grit (2.5.0) diff-lcs (~> 1.1) mime-types (~> 1.15) posix-spawn (~> 0.3.6) - method_source (0.8.1) - mime-types (1.22) - net-ssh (2.6.7) + method_source (0.8.2) + mime-types (1.25) + net-ssh (2.7.0) posix-spawn (0.3.6) - pry (0.9.12) - coderay (~> 1.0.5) + pry (0.9.12.3) + coderay (~> 1.0) method_source (~> 0.8) slop (~> 3.4) - rspec (2.13.0) - rspec-core (~> 2.13.0) - rspec-expectations (~> 2.13.0) - rspec-mocks (~> 2.13.0) - rspec-core (2.13.1) - rspec-expectations (2.13.0) + rspec (2.14.1) + rspec-core (~> 2.14.0) + rspec-expectations (~> 2.14.0) + rspec-mocks (~> 2.14.0) + rspec-core (2.14.7) + rspec-expectations (2.14.4) diff-lcs (>= 1.1.3, < 2.0) - rspec-mocks (2.13.1) - sequel (3.46.0) - slop (3.4.4) - sqlite3 (1.3.7) + rspec-mocks (2.14.4) + sequel (4.4.0) + slop (3.4.7) + sqlite3 (1.3.8) PLATFORMS ruby @@ -17,7 +17,8 @@ end RSpec::Core::RakeTask.new(:spec) desc "Build gem locally" -task :build => [:spec, :gemspec] do +#task :build => [:spec, :gemspec] do +task :build => [:gemspec] do system "gem build #{gemspec.name}.gemspec" FileUtils.mkdir_p "gems" FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", "gems" diff --git a/bin/oxidized b/bin/oxidized index aee703f..2509279 100644..100755 --- a/bin/oxidized +++ b/bin/oxidized @@ -3,6 +3,7 @@ require 'oxidized' begin + Process.daemon unless $DEBUG Oxidized.new rescue => e open Oxidized::Config::Crash, 'w' do |file| diff --git a/lib/oxidized/config/bootstrap.rb b/lib/oxidized/config/bootstrap.rb index bae2b70..73750de 100644 --- a/lib/oxidized/config/bootstrap.rb +++ b/lib/oxidized/config/bootstrap.rb @@ -14,8 +14,11 @@ module Oxidized CFG.vars = { :enable => 'enablePW', } - CFG.input = { - :default => 'ssh, telnet', + CFG.input = { + :default => 'ssh, telnet', + :ssh => { + :secure => false, + } } CFG.output = { :default => 'git', diff --git a/lib/oxidized/input/input.rb b/lib/oxidized/input/input.rb index e028ce4..d59c4f3 100644 --- a/lib/oxidized/input/input.rb +++ b/lib/oxidized/input/input.rb @@ -1,5 +1,12 @@ module Oxidized class Input + RescueFail = [ + Timeout::Error, + Errno::ECONNREFUSED, + 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 dc90354..b060418 100644 --- a/lib/oxidized/input/ssh.rb +++ b/lib/oxidized/input/ssh.rb @@ -2,6 +2,10 @@ module Oxidized require 'net/ssh' require 'oxidized/input/cli' class SSH < Input + RescueFail = [ + Net::SSH::Disconnect, + Net::SSH::AuthenticationFailed, + ] include CLI class NoShell < StandardError; end @@ -9,14 +13,12 @@ module Oxidized @node = node @output = '' @node.model.cfg['ssh'].each { |cb| instance_exec &cb } - begin - @ssh = Net::SSH.start @node.ip, @node.auth[:username], - :password => @node.auth[:password], :timeout => CFG.timeout - rescue Timeout::Error, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH, Net::SSH::Disconnect - return false - end + secure = CFG.input[:ssh][:secure] + @ssh = Net::SSH.start @node.ip, @node.auth[:username], + :password => @node.auth[:password], :timeout => CFG.timeout, + :paranoid => secure open_shell @ssh unless @exec - not @ssh.closed? + @ssh and not @ssh.closed? end def cmd cmd, expect=@node.prompt diff --git a/lib/oxidized/input/telnet.rb b/lib/oxidized/input/telnet.rb index 26755ca..0ae6877 100644 --- a/lib/oxidized/input/telnet.rb +++ b/lib/oxidized/input/telnet.rb @@ -2,6 +2,7 @@ module Oxidized require 'net/telnet' require 'oxidized/input/cli' class Telnet < Input + RescueFail = [] include CLI attr_reader :telnet @@ -9,28 +10,20 @@ module Oxidized @node = node @timeout = CFG.timeout @node.model.cfg['telnet'].each { |cb| instance_exec &cb } - begin - @telnet = Net::Telnet.new 'Host' => @node.ip, 'Waittime' => @timeout, - 'Model' => @node.model - expect username - @telnet.puts @node.auth[:username] - expect password - @telnet.puts @node.auth[:password] - expect @node.prompt - rescue Timeout::Error, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EPIPE, Errno::EHOSTUNREACH - return false - end + @telnet = Net::Telnet.new 'Host' => @node.ip, 'Waittime' => @timeout, + 'Model' => @node.model + expect username + @telnet.puts @node.auth[:username] + expect password + @telnet.puts @node.auth[:password] + expect @node.prompt end def cmd cmd, expect=@node.prompt Log.debug "Telnet: #{cmd} @#{@node.name}" args = { 'String' => cmd } args.merge!({ 'Match' => expect, 'Timeout' => @timeout }) if expect - begin - @telnet.cmd args - rescue Timeout::Error, Errno::ECONNRESET, Errno::EPIPE - return false - end + @telnet.cmd args end def send data diff --git a/lib/oxidized/node.rb b/lib/oxidized/node.rb index 0b307b7..31f26a9 100644 --- a/lib/oxidized/node.rb +++ b/lib/oxidized/node.rb @@ -21,7 +21,7 @@ module Oxidized status, config = :fail, nil @input.each do |input| @model.input = input = input.new - if input.connect self + if connect input config = input.get status = :success if config break @@ -32,6 +32,16 @@ module Oxidized [status, config] end + def connect input + rescue_fail = input.class::RescueFail + input.class.superclass::RescueFail + begin + input.connect self + rescue *rescue_fail => err + Log.warn '%s raised %s with msg' % [self.ip, err.class, err.message] + return false + end + end + def serialize h = { :name => @name, diff --git a/lib/tst b/lib/tst deleted file mode 100755 index 5e9131f..0000000 --- a/lib/tst +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env ruby20 - -$: << '.' -require 'pry' if ENV['DEV'] -require 'oxidized' - -begin - Oxidized.new -rescue Exception => e - open Oxidized::Config::Crash, 'w' do |file| - file.puts '-' * 50 - file.puts Time.now.utc - file.puts e.message - file.puts '-' * 50 - file.puts e.backtrace - file.puts '-' * 50 - end - raise -end diff --git a/oxidized.gemspec b/oxidized.gemspec index e8db07e..6b0ebfb 100644 --- a/oxidized.gemspec +++ b/oxidized.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'oxidized' - s.version = '0.0.16' + s.version = '0.0.17' s.platform = Gem::Platform::RUBY s.authors = [ 'Saku Ytti' ] s.email = %w( saku@ytti.fi ) |