From 39432f29e54e71478ef570dceccc98a6c868a71e Mon Sep 17 00:00:00 2001 From: Vasu1105 Date: Wed, 11 Aug 2021 14:58:37 +0530 Subject: [PATCH] IBM Db2 connection failure fix for error Reason Code 3 Signed-off-by: Vasu1105 --- lib/inspec/resources/ibmdb2_conf.rb | 9 +++++++-- lib/inspec/resources/ibmdb2_session.rb | 10 +++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/inspec/resources/ibmdb2_conf.rb b/lib/inspec/resources/ibmdb2_conf.rb index b9bf0c5e1..f293e6c84 100644 --- a/lib/inspec/resources/ibmdb2_conf.rb +++ b/lib/inspec/resources/ibmdb2_conf.rb @@ -29,13 +29,18 @@ module Inspec::Resources private def run_command + cmd = inspec.command("#{@db2_executable_file_path} attach to #{@db_instance}\;") out = cmd.stdout + "\n" + cmd.stderr - if cmd.exit_status != 0 || out =~ /Can't connect to IBM Db2 instance/ || out.downcase =~ /^error:.*/ + # check if following specific error is there. Sourcing the db2profile to resolve the error. + if cmd.exit_status != 0 && out =~ /SQL10007N Message "-1390" could not be retrieved. Reason code: "3"/ + cmd = inspec.command(". ~/sqllib/db2profile\; #{@db2_executable_file_path} attach to #{@db_instance}\; #{@db2_executable_file_path} get database manager configuration") + elsif cmd.exit_status != 0 || out =~ /Can't connect to IBM Db2 instance/ || out.downcase =~ /^error:.*/ raise Inspec::Exceptions::ResourceFailed, "IBM Db2 connection error: #{out}" + else + cmd = inspec.command("#{@db2_executable_file_path} get database manager configuration") end - cmd = inspec.command("#{@db2_executable_file_path} get database manager configuration") out = cmd.stdout + "\n" + cmd.stderr if cmd.exit_status != 0 || out =~ /Can't connect to IBM Db2 server/ || out.downcase =~ /^error:.*/ raise Inspec::Exceptions::ResourceFailed, "IBM Db2 query with error: #{out}" diff --git a/lib/inspec/resources/ibmdb2_session.rb b/lib/inspec/resources/ibmdb2_session.rb index df261837e..31a34c727 100644 --- a/lib/inspec/resources/ibmdb2_session.rb +++ b/lib/inspec/resources/ibmdb2_session.rb @@ -38,12 +38,16 @@ module Inspec::Resources # connect to the db cmd = inspec.command("#{@db2_executable_file_path} attach to #{@db_instance}\; #{@db2_executable_file_path} connect to #{@db_name}\;") out = cmd.stdout + "\n" + cmd.stderr - if cmd.exit_status != 0 || out =~ /Can't connect to IBM Db2 / || out.downcase =~ /^error:.*/ + # check if following specific error is there. Sourcing the db2profile to resolve the error. + if cmd.exit_status != 0 && out =~ /SQL10007N Message "-1390" could not be retrieved. Reason code: "3"/ + cmd = inspec.command(". ~/sqllib/db2profile\; #{@db2_executable_file_path} attach to #{@db_instance}\; #{@db2_executable_file_path} connect to #{@db_name}\; #{@db2_executable_file_path} #{q}\;") + elsif cmd.exit_status != 0 || out =~ /Can't connect to IBM Db2 / || out.downcase =~ /^error:.*/ raise Inspec::Exceptions::ResourceFailed, "IBM Db2 connection error: #{out}" + else + # query on the database + cmd = inspec.command("#{@db2_executable_file_path} #{q}\;") end - # query on the database - cmd = inspec.command("#{@db2_executable_file_path} #{q}\;") out = cmd.stdout + "\n" + cmd.stderr if cmd.exit_status != 0 || out =~ /Can't connect to IBM Db2 server/ || out.downcase =~ /^error:.*/ raise Inspec::Exceptions::ResourceFailed, "IBM Db2 query with error: #{out}"