From 90aa9e68ed16c96f54ecd3e3e197d85850531f6e Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Sun, 29 Apr 2018 19:55:35 +0200 Subject: refactor telnet input logging for consistently with ssh --- .rubocop_todo.yml | 2 +- lib/oxidized/input/telnet.rb | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 77c1026..6598a37 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -140,7 +140,7 @@ Metrics/CyclomaticComplexity: # Offense count: 53 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 72 + Max: 75 # Offense count: 2 # Configuration parameters: CountKeywordArgs. diff --git a/lib/oxidized/input/telnet.rb b/lib/oxidized/input/telnet.rb index b1b3222..e7c2ee3 100644 --- a/lib/oxidized/input/telnet.rb +++ b/lib/oxidized/input/telnet.rb @@ -10,15 +10,15 @@ module Oxidized @node = node @timeout = Oxidized.config.timeout @node.model.cfg['telnet'].each { |cb| instance_exec(&cb) } + @log = File.open(Oxidized::Config::Log + "/#{@node.ip}-telnet", 'w') if Oxidized.config.input.debug? port = vars(:telnet_port) || 23 - opt = { 'Host' => @node.ip, - 'Port' => port.to_i, - 'Timeout' => @timeout, - 'Model' => @node.model } - opt['Output_log'] = Oxidized::Config::Log + "/#{@node.ip}-telnet" if Oxidized.config.input.debug? + telnet_opts = { 'Host' => @node.ip, + 'Port' => port.to_i, + 'Timeout' => @timeout, + 'Model' => @node.model } - @telnet = Net::Telnet.new opt + @telnet = Net::Telnet.new telnet_opts if @node.auth[:username] and @node.auth[:username].length > 0 expect username @telnet.puts @node.auth[:username] @@ -62,6 +62,9 @@ module Oxidized disconnect_cli @telnet.close rescue Errno::ECONNRESET + ensure + @log.close if Oxidized.config.input.debug? + (@telnet.close rescue true) unless @telnet.closed? end end end @@ -137,7 +140,10 @@ class Net::Telnet buf.gsub!(/#{EOL}/no, "\n") end end - @log.print(buf) if @options.has_key?("Output_log") + if Oxidized.config.input.debug? + @log.print buf + @log.flush + end line += buf line = model.expects line line = yield line if block_given? -- cgit v1.2.1