From b603c20370c7548739f90f2b92ffa066d59446ab Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Sun, 13 Apr 2014 10:45:36 +0300 Subject: smarter match for IP address in nodes - add 'connnected?' method to ssh/telnet to query if or not we're connected - subclass OxidizedError from StandardError, all future OxidizedErrors should be subclasssed from this. --- lib/oxidized.rb | 1 + lib/oxidized/input/ssh.rb | 4 ++++ lib/oxidized/input/telnet.rb | 4 ++++ lib/oxidized/nodes.rb | 13 +++++++++---- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/oxidized.rb b/lib/oxidized.rb index 99b1ea6..61948ff 100644 --- a/lib/oxidized.rb +++ b/lib/oxidized.rb @@ -1,4 +1,5 @@ module Oxidized + class OxidizedError < StandardError; end Encoding.default_external = 'UTF-8' Directory = File.expand_path File.join File.dirname(__FILE__), '../' require 'oxidized/core' diff --git a/lib/oxidized/input/ssh.rb b/lib/oxidized/input/ssh.rb index a858213..bd0a459 100644 --- a/lib/oxidized/input/ssh.rb +++ b/lib/oxidized/input/ssh.rb @@ -27,6 +27,10 @@ module Oxidized shell_open @ssh @username ? shell_login : expect(@node.prompt) end + connected? + end + + def connected? @ssh and not @ssh.closed? end diff --git a/lib/oxidized/input/telnet.rb b/lib/oxidized/input/telnet.rb index 84d9877..4e80ceb 100644 --- a/lib/oxidized/input/telnet.rb +++ b/lib/oxidized/input/telnet.rb @@ -19,6 +19,10 @@ module Oxidized expect @node.prompt end + def connected? + @telnet and not @telnet.sock.closed? + end + def cmd cmd, expect=@node.prompt Log.debug "Telnet: #{cmd} @#{@node.name}" args = { 'String' => cmd } diff --git a/lib/oxidized/nodes.rb b/lib/oxidized/nodes.rb index 8d49443..42c18cb 100644 --- a/lib/oxidized/nodes.rb +++ b/lib/oxidized/nodes.rb @@ -1,5 +1,6 @@ module Oxidized - require 'oxidized/node' + require 'oxidized/node' + require 'ipaddr' class Oxidized::NotSupported < StandardError; end class Oxidized::NodeNotFound < StandardError; end class Nodes < Array @@ -8,14 +9,18 @@ module Oxidized def load node_want=nil with_lock do new = [] + node_want_ip = (IPaddr.new(node_want) rescue nil) if node_want @source = CFG.source[:default] Oxidized.mgr.add_source @source Oxidized.mgr.source[@source].new.load.each do |node| - # we want to load one specific node(s), not all of them + # we want to load specific node(s), not all of them if node_want - next unless node[:name].to_s.match(node_want) or - node[:ip].to_s.match(node_want) + if node_want_ip + next unless node_want_ip == node[:ip] + else + next unless node[:name].match node_want + end end begin -- cgit v1.2.1