[Cluster-devel] [PATCH 3/3] fence_scsi: properly log errors for all commands

Ryan O'Hara rohara at redhat.com
Tue Jan 25 17:39:51 UTC 2011


There are a number of places in fence_scsi that call sg_persist commands
via Perl's qx command. If an error occurs, the script simply dies and
reports the name of the subroutine that the script died in. This can be
improved by replacing the die call with log_error, such that any errors
get properly written to the logfile, if any. This patch also logs the
error code returned from the failed command.

This patch also redirects stderr to /dev/null for all commands executed
via qx.

Signed-off-by: Ryan O'Hara <rohara at redhat.com>
---
 fence/agents/scsi/fence_scsi.pl |   99 ++++++++++++++++++++++++++++++--------
 1 files changed, 78 insertions(+), 21 deletions(-)

diff --git a/fence/agents/scsi/fence_scsi.pl b/fence/agents/scsi/fence_scsi.pl
index efeeba3..ae658ab 100644
--- a/fence/agents/scsi/fence_scsi.pl
+++ b/fence/agents/scsi/fence_scsi.pl
@@ -185,14 +185,20 @@ sub do_register ($$$)
 
     my $cmd;
     my $out;
+    my $err;
 
     do_reset ($dev);
 
     $cmd = "sg_persist -n -o -G -K $host_key -S $node_key -d $dev";
     $cmd .= " -Z" if (defined $opt_a);
-    $out = qx { $cmd };
+    $out = qx { $cmd 2> /dev/null };
+    $err = ($?>>8);
 
-    die "[error]: $self\n" if ($?>>8);
+    if ($err != 0) {
+	log_error ("$self (err=$err)");
+    }
+
+    # die "[error]: $self\n" if ($?>>8);
 
     return;
 }
@@ -216,14 +222,20 @@ sub do_register_ignore ($$)
 
     my $cmd;
     my $out;
+    my $err;
 
     do_reset ($dev);
 
     $cmd = "sg_persist -n -o -I -S $node_key -d $dev";
     $cmd .= " -Z" if (defined $opt_a);
-    $out = qx { $cmd };
+    $out = qx { $cmd 2> /dev/null };
+    $err = ($?>>8);
 
-    die "[error]: $self ($dev)\n" if ($?>>8);
+    if ($err != 0) {
+	log_error ("$self (err=$err)");
+    }
+
+    # die "[error]: $self ($dev)\n" if ($?>>8);
 
     return;
 }
@@ -236,9 +248,14 @@ sub do_reserve ($$)
     log_debug ("$self (host_key=$host_key, dev=$dev)");
 
     my $cmd = "sg_persist -n -o -R -T 5 -K $host_key -d $dev";
-    my $out = qx { $cmd };
+    my $out = qx { $cmd 2> /dev/null };
+    my $err = ($?>>8);
 
-    die "[error]: $self\n" if ($?>>8);
+    if ($err != 0) {
+	log_error ("$self (err=$err)");
+    }
+
+    # die "[error]: $self\n" if ($?>>8);
 
     return;
 }
@@ -251,9 +268,14 @@ sub do_release ($$)
     log_debug ("$self (host_key=$host_key, dev=$dev)");
 
     my $cmd = "sg_persist -n -o -L -T 5 -K $host_key -d $dev";
-    my $out = qx { $cmd };
+    my $out = qx { $cmd 2> /dev/null };
+    my $err = ($?>>8);
 
-    die "[error]: $self\n" if ($?>>8);
+    if ($err != 0) {
+	log_error ("$self (err=$err)");
+    }
+
+    # die "[error]: $self\n" if ($?>>8);
 
     return;
 }
@@ -266,9 +288,14 @@ sub do_preempt ($$$)
     log_debug ("$self (host_key=$host_key, node_key=$node_key, dev=$dev)");
 
     my $cmd = "sg_persist -n -o -P -T 5 -K $host_key -S $node_key -d $dev";
-    my $out = qx { $cmd };
+    my $out = qx { $cmd 2> /dev/null };
+    my $err = ($?>>8);
+
+    if ($err != 0) {
+	log_error ("$self (err=$err)");
+    }
 
-    die "[error]: $self\n" if ($?>>8);
+    # die "[error]: $self\n" if ($?>>8);
 
     return;
 }
@@ -281,9 +308,14 @@ sub do_preempt_abort ($$$)
     log_debug ("$self (host_key=$host_key, node_key=$node_key, dev=$dev)");
 
     my $cmd = "sg_persist -n -o -A -T 5 -K $host_key -S $node_key -d $dev";
-    my $out = qx { $cmd };
+    my $out = qx { $cmd 2> /dev/null };
+    my $err = ($?>>8);
+
+    if ($err != 0) {
+	log_error ("$self (err=$err)");
+    }
 
-    die "[error]: $self\n" if ($?>>8);
+    # die "[error]: $self\n" if ($?>>8);
 
     return;
 }
@@ -343,9 +375,14 @@ sub get_node_id ($)
     my $node_id;
 
     my $cmd = "cman_tool nodes -n $_[0] -F id";
-    my $out = qx { $cmd };
+    my $out = qx { $cmd 2> /dev/null };
+    my $err = ($?>>8);
 
-    die "[error]: $self\n" if ($?>>8);
+    if ($err != 0) {
+	log_error ("$self (err=$err)");
+    }
+
+    # die "[error]: $self\n" if ($?>>8);
 
     chomp ($out);
 
@@ -360,9 +397,14 @@ sub get_cluster_id ()
     my $cluster_id;
 
     my $cmd = "cman_tool status";
-    my @out = qx { $cmd };
+    my @out = qx { $cmd 2> /dev/null };
+    my $err = ($?>>8);
 
-    die "[error]: $self\n" if ($?>>8);
+    if ($err != 0) {
+	log_error ("$self (err=$err)");
+    }
+
+    # die "[error]: $self\n" if ($?>>8);
 
     foreach (@out) {
 	chomp;
@@ -388,8 +430,13 @@ sub get_devices_clvm ()
 	"              devices { preferred_names = [ \"^/dev/dm\" ] }'";
 
     my @out = qx { $cmd 2> /dev/null };
+    my $err = ($?>>8);
+
+    if ($err != 0) {
+	log_error ("$self (err=$err)");
+    }
 
-    die "[error]: $self\n" if ($?>>8);
+    # die "[error]: $self\n" if ($?>>8);
 
     foreach (@out) {
 	chomp;
@@ -479,9 +526,14 @@ sub get_registration_keys ($)
     my @keys;
 
     my $cmd = "sg_persist -n -i -k -d $dev";
-    my @out = qx { $cmd };
+    my @out = qx { $cmd 2> /dev/null };
+    my $err = ($?>>8);
+
+    if ($err != 0) {
+	log_error ("$self (err=$err)");
+    }
 
-    die "[error]: $self\n" if ($?>>8);
+    # die "[error]: $self\n" if ($?>>8);
 
     foreach (@out) {
 	chomp;
@@ -500,9 +552,14 @@ sub get_reservation_key ($)
     my $key;
 
     my $cmd = "sg_persist -n -i -r -d $dev";
-    my @out = qx { $cmd };
+    my @out = qx { $cmd 2> /dev/null };
+    my $err = ($?>>8);
+
+    if ($err != 0) {
+	log_error ("$self (err=$err)");
+    }
 
-    die "[error]: $self\n" if ($?>>8);
+    # die "[error]: $self\n" if ($?>>8);
 
     foreach (@out) {
 	chomp;
-- 
1.7.3.4




More information about the Cluster-devel mailing list