2007-12-12 13:39:51 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
|
|
|
package xCAT::DSHCore;
|
|
|
|
|
2010-02-23 11:26:19 +00:00
|
|
|
BEGIN
|
|
|
|
{
|
|
|
|
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr';
|
|
|
|
}
|
2007-12-12 13:39:51 +00:00
|
|
|
use locale;
|
2008-07-18 12:02:22 +00:00
|
|
|
use strict;
|
2007-12-12 13:39:51 +00:00
|
|
|
use Socket;
|
|
|
|
|
|
|
|
use xCAT::MsgUtils;
|
|
|
|
use xCAT::Utils;
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3
|
|
|
|
fork_no_output
|
|
|
|
|
|
|
|
Forks a process for the given command array and returns the process
|
|
|
|
ID for the forked process. Since no I/O is needed for the pipes, no
|
|
|
|
STDOUT/STDERR pipes are returned to the caller.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$fork_id - unique identifer to use for tracking the forked process
|
|
|
|
@command - command and parameter array to execute in the forkec process
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
$pid - process identifer for the forked process
|
|
|
|
|
|
|
|
Globals:
|
|
|
|
None
|
|
|
|
|
|
|
|
Error:
|
|
|
|
None
|
|
|
|
|
|
|
|
Example:
|
|
|
|
$pid = xCAT::DSHCore->fork_no_output('hostname1PID', @command_array);
|
|
|
|
|
|
|
|
Comments:
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub fork_no_output
|
|
|
|
{
|
|
|
|
my ($class, $fork_id, @command) = @_;
|
|
|
|
|
|
|
|
my $pid;
|
|
|
|
|
2008-01-21 19:49:59 +00:00
|
|
|
if ($pid = xCAT::Utils->xfork)
|
2007-12-12 13:39:51 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
elsif (defined $pid)
|
|
|
|
{
|
|
|
|
open(STDOUT, ">/dev/null");
|
|
|
|
open(STDERR, ">/dev/null");
|
|
|
|
|
|
|
|
select(STDOUT);
|
|
|
|
$| = 1;
|
|
|
|
select(STDERR);
|
|
|
|
$| = 1;
|
|
|
|
|
|
|
|
if (!(exec {$command[0]} @command))
|
|
|
|
{
|
|
|
|
return (-4, undef);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (-3, undef);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($pid, undef, undef, undef, undef);
|
|
|
|
}
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3
|
|
|
|
fork_output
|
|
|
|
|
|
|
|
Forks a process for the given command array and returns the process
|
|
|
|
ID for the forked process and references to all I/O pipes for STDOUT
|
|
|
|
and STDERR.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$fork_id - unique identifer to use for tracking the forked process
|
|
|
|
@command - command and parameter array to execute in the forkec process
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
$pid - process identifer for the forked process
|
|
|
|
|
|
|
|
Globals:
|
|
|
|
None
|
|
|
|
|
|
|
|
Error:
|
|
|
|
None
|
|
|
|
|
|
|
|
Example:
|
|
|
|
$pid = xCAT::DSHCore->fork_no_output('hostname1PID', @command_array);
|
|
|
|
|
|
|
|
Comments:
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub fork_output
|
|
|
|
{
|
|
|
|
my ($class, $fork_id, @command) = @_;
|
2008-07-18 12:02:22 +00:00
|
|
|
no strict;
|
2007-12-12 13:39:51 +00:00
|
|
|
my $pid;
|
|
|
|
my %pipes = ();
|
|
|
|
|
|
|
|
my $rout_fh = "rout_$fork_id";
|
|
|
|
my $rerr_fh = "rerr_$fork_id";
|
|
|
|
my $wout_fh = "wout_$fork_id";
|
|
|
|
my $werr_fh = "werr_$fork_id";
|
|
|
|
|
|
|
|
(pipe($rout_fh, $wout_fh) == -1) && return (-1, undef);
|
|
|
|
(pipe($rerr_fh, $werr_fh) == -1) && return (-2, undef);
|
|
|
|
|
|
|
|
if ($pid = fork)
|
|
|
|
{
|
|
|
|
close($wout_fh);
|
|
|
|
close($werr_fh);
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif (defined $pid)
|
|
|
|
{
|
|
|
|
close($rout_fh);
|
|
|
|
close($rerr_fh);
|
|
|
|
|
|
|
|
!(open(STDOUT, ">&$wout_fh")) && return (-5, undef);
|
|
|
|
!(open(STDERR, ">&$werr_fh")) && return (-6, undef);
|
|
|
|
|
|
|
|
select(STDOUT);
|
|
|
|
$| = 1;
|
|
|
|
select(STDERR);
|
|
|
|
$| = 1;
|
|
|
|
|
|
|
|
if (!(exec {$command[0]} @command))
|
|
|
|
{
|
|
|
|
return (-4, undef);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (-3, undef);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($pid, *$rout_fh, *$rerr_fh, *$wout_fh, *$werr_fh);
|
2008-07-18 12:02:22 +00:00
|
|
|
use strict;
|
2007-12-12 13:39:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-13 05:27:24 +00:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3
|
|
|
|
fork_output_for_commands
|
|
|
|
|
|
|
|
Forks a process for the given command array and returns the process
|
|
|
|
ID for the forked process and references to all I/O pipes for STDOUT
|
|
|
|
and STDERR. In the child process, it will invoke the xCAT::DSHCore->fork_no_output()
|
|
|
|
for the first command which is a no-output command and waitpid(). And then execute
|
|
|
|
the left commands in the child process.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$fork_id - unique identifer to use for tracking the forked process
|
|
|
|
@command - command and parameter array to execute in the forkec process
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
$pid - process identifer for the forked process
|
|
|
|
|
|
|
|
Globals:
|
|
|
|
None
|
|
|
|
|
|
|
|
Error:
|
|
|
|
None
|
|
|
|
|
|
|
|
Example:
|
|
|
|
$pid = xCAT::DSHCore->fork_output_for_commands('hostname1PID', @command_array);
|
|
|
|
|
|
|
|
Comments:
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
sub fork_output_for_commands
|
|
|
|
{
|
|
|
|
my ($class, $fork_id, @commands) = @_;
|
|
|
|
no strict;
|
|
|
|
my $pid;
|
|
|
|
my %pipes = ();
|
|
|
|
|
|
|
|
my $rout_fh = "rout_$fork_id";
|
|
|
|
my $rerr_fh = "rerr_$fork_id";
|
|
|
|
my $wout_fh = "wout_$fork_id";
|
|
|
|
my $werr_fh = "werr_$fork_id";
|
|
|
|
|
|
|
|
(pipe($rout_fh, $wout_fh) == -1) && return (-1, undef);
|
|
|
|
(pipe($rerr_fh, $werr_fh) == -1) && return (-2, undef);
|
|
|
|
|
|
|
|
if ($pid = fork)
|
|
|
|
{
|
|
|
|
close($wout_fh);
|
|
|
|
close($werr_fh);
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif (defined $pid)
|
|
|
|
{
|
|
|
|
close($rout_fh);
|
|
|
|
close($rerr_fh);
|
|
|
|
|
|
|
|
!(open(STDOUT, ">&$wout_fh")) && return (-5, undef);
|
|
|
|
!(open(STDERR, ">&$werr_fh")) && return (-6, undef);
|
|
|
|
|
|
|
|
select(STDOUT);
|
|
|
|
$| = 1;
|
|
|
|
select(STDERR);
|
|
|
|
$| = 1;
|
2012-12-14 09:35:18 +00:00
|
|
|
if ( @commands > 1 ) {
|
|
|
|
my $command0 = shift(@commands);
|
|
|
|
my @exe_command0_process = xCAT::DSHCore->fork_no_output($fork_id, @$command0);
|
|
|
|
waitpid($exe_command0_process[0], undef);
|
|
|
|
}
|
|
|
|
|
|
|
|
my $t_command = shift(@commands);
|
|
|
|
my @command = @$t_command;
|
2012-12-13 05:27:24 +00:00
|
|
|
if (!(exec {$command[0]} @command))
|
|
|
|
{
|
|
|
|
return (-4, undef);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (-3, undef);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($pid, *$rout_fh, *$rerr_fh, *$wout_fh, *$werr_fh);
|
|
|
|
use strict;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-12 13:39:51 +00:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3
|
|
|
|
pipe_handler
|
|
|
|
|
|
|
|
Handles and processes dsh output from a given read pipe handle. The output
|
|
|
|
is immediately written to each output file handle as it is available.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$options - options hash table describing dsh configuration options
|
|
|
|
$target_properties - property information of the target related to the pipe handle
|
|
|
|
$read_fh - reference to the read pipe handle
|
|
|
|
$buffer_size - local buffer size to read data from the handle
|
|
|
|
$label - prefix label to use for dsh output
|
|
|
|
$write_buffer - buffer of data that is yet to be written (must wait until \n is read)
|
|
|
|
@write_fhs - array of output file handles where output will be written
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
1 if the EOF reached on $read_fh
|
|
|
|
undef otherwise
|
|
|
|
|
|
|
|
Globals:
|
|
|
|
None
|
|
|
|
|
|
|
|
Error:
|
|
|
|
None
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
Comments:
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
2009-10-22 12:49:48 +00:00
|
|
|
# NOTE: global environment $::__DSH_LAST_LINE} only can be used in DSHCore::pipe_handler and DSHCore::pipe_handler_buffer
|
2009-01-08 12:57:02 +00:00
|
|
|
$::__DSH_LAST_LINE = undef;
|
2007-12-12 13:39:51 +00:00
|
|
|
sub pipe_handler
|
|
|
|
{
|
|
|
|
my ($class, $options, $target_properties, $read_fh, $buffer_size, $label,
|
|
|
|
$write_buffer, @write_fhs)
|
|
|
|
= @_;
|
|
|
|
|
|
|
|
my $line;
|
|
|
|
my $target_hostname;
|
|
|
|
my $eof_reached = undef;
|
2009-01-08 12:57:02 +00:00
|
|
|
my $cust_rc_deal =0;
|
|
|
|
|
|
|
|
if ($::USER_POST_CMD)
|
|
|
|
{
|
|
|
|
# If user provide post-command to display return code,
|
|
|
|
# the keyword 'DSH_RC' will be searched,
|
|
|
|
# the return code is gotten in another way as shown like below:
|
|
|
|
# ...
|
|
|
|
# <output>
|
|
|
|
# <return_code>
|
|
|
|
# DSH_RC
|
|
|
|
#
|
|
|
|
# The last two lines are needed to be moved out from output
|
|
|
|
$cust_rc_deal = 1;
|
|
|
|
}
|
2007-12-12 13:39:51 +00:00
|
|
|
|
|
|
|
while (sysread($read_fh, $line, $buffer_size) != 0
|
|
|
|
|| ($eof_reached = 1))
|
|
|
|
{
|
2009-11-10 12:27:20 +00:00
|
|
|
last if ($eof_reached && (!defined($::__DSH_LAST_LINE->{$label})));
|
2007-12-12 13:39:51 +00:00
|
|
|
|
2009-11-10 12:27:20 +00:00
|
|
|
if ($line =~ /^\n$/ && scalar(@$write_buffer) == 0)
|
2007-12-12 13:39:51 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
# need to preserve blank lines in the output.
|
|
|
|
$line = $label . $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @lines = split "\n", $line;
|
|
|
|
|
|
|
|
if (@$write_buffer)
|
|
|
|
{
|
|
|
|
my $buffered_line = shift @$write_buffer;
|
|
|
|
my $next_line = shift @lines;
|
|
|
|
$next_line = $buffered_line . $next_line;
|
|
|
|
unshift @lines, $next_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($line !~ /\n$/)
|
|
|
|
{
|
|
|
|
push @$write_buffer, (pop @lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (@lines)
|
|
|
|
{
|
2009-01-08 12:57:02 +00:00
|
|
|
if ($cust_rc_deal)
|
|
|
|
{
|
|
|
|
# Dump the last line at the beginning of current buffer
|
2009-10-22 12:49:48 +00:00
|
|
|
if ($::__DSH_LAST_LINE->{$label})
|
2009-01-08 12:57:02 +00:00
|
|
|
{
|
2009-10-22 12:49:48 +00:00
|
|
|
unshift @lines, "$::__DSH_LAST_LINE->{$label}" ;
|
2009-01-08 12:57:02 +00:00
|
|
|
}
|
2009-10-22 12:49:48 +00:00
|
|
|
# Pop current buffer to $::__DSH_LAST_LINE->{$label}
|
2009-11-10 12:27:20 +00:00
|
|
|
if($line)
|
2009-01-08 12:57:02 +00:00
|
|
|
{
|
2009-11-10 12:27:20 +00:00
|
|
|
$::__DSH_LAST_LINE->{$label} = $lines[scalar @lines - 1];
|
|
|
|
pop @lines;
|
|
|
|
# Skip this loop if array @lines is empty.
|
|
|
|
if (scalar @lines == 0)
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
2009-01-08 12:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-12 13:39:51 +00:00
|
|
|
|
|
|
|
$line = join "\n", @lines;
|
|
|
|
$line .= "\n";
|
|
|
|
|
|
|
|
if ($line =~ /:DSH_TARGET_RC=/)
|
|
|
|
{
|
|
|
|
my $start_offset = index($line, ':DSH_TARGET_RC');
|
|
|
|
my $end_offset = index($line, ':', $start_offset + 1);
|
|
|
|
my $target_rc_string =
|
|
|
|
substr($line, $start_offset, $end_offset - $start_offset);
|
|
|
|
my ($discard, $target_rc) = split '=', $target_rc_string;
|
|
|
|
$line =~ s/:DSH_TARGET_RC=$target_rc:\n//g;
|
|
|
|
$$target_properties{'target-rc'} = $target_rc;
|
|
|
|
}
|
2009-10-22 12:49:48 +00:00
|
|
|
if ( $::__DSH_LAST_LINE->{$label} =~ /DSH_RC/ && $cust_rc_deal) {
|
2009-01-08 12:57:02 +00:00
|
|
|
my $target_rc = undef;
|
|
|
|
# Get the number in the last line
|
|
|
|
$line =~ /[\D]*([0-9]+)\s*$/ ;
|
|
|
|
$target_rc = $1;
|
|
|
|
$$target_properties{'target-rc'} = $target_rc;
|
|
|
|
# Remove the last line
|
|
|
|
$line =~ s/$target_rc\s*\n$//g;
|
|
|
|
#$line = $line . "## ret=$target_rc";
|
2009-10-22 12:49:48 +00:00
|
|
|
# Clean up $::__DSH_LAST_LINE->{$label}
|
|
|
|
undef $::__DSH_LAST_LINE->{$label} ;
|
2009-01-08 12:57:02 +00:00
|
|
|
# when '-z' is specified, display return code
|
|
|
|
$::DSH_EXIT_STATUS &&
|
|
|
|
($line .="Remote_command_rc = $target_rc");
|
|
|
|
}
|
2007-12-12 13:39:51 +00:00
|
|
|
|
|
|
|
if ($line ne '')
|
|
|
|
{
|
|
|
|
if ($line !~ /^$label\n$/)
|
|
|
|
{
|
|
|
|
$line = $label . $line;
|
|
|
|
}
|
|
|
|
$line =~ s/$/\n/ if $line !~ /\n$/;
|
|
|
|
}
|
|
|
|
|
|
|
|
$line =~ s/\n/\n$label/g;
|
|
|
|
($line =~ /\n$label$/) && ($line =~ s/\n$label$/\n/);
|
2008-09-29 14:10:16 +00:00
|
|
|
chomp $line;
|
2007-12-12 13:39:51 +00:00
|
|
|
|
|
|
|
my @output_files = ();
|
|
|
|
my @output_file_nos = ();
|
|
|
|
|
2008-07-18 12:02:22 +00:00
|
|
|
foreach my $write_fh (@write_fhs)
|
2007-12-12 13:39:51 +00:00
|
|
|
{
|
|
|
|
my $file_no = fileno($write_fh);
|
|
|
|
if (grep /$file_no/, @output_file_nos)
|
|
|
|
{
|
|
|
|
$line =~ s/$label//g;
|
|
|
|
}
|
|
|
|
|
2008-09-29 14:10:16 +00:00
|
|
|
my $rsp={};
|
|
|
|
$rsp->{data}->[0] = $line;
|
2009-09-16 14:21:44 +00:00
|
|
|
xCAT::MsgUtils->message("D", $rsp, $::CALLBACK);
|
2008-09-29 14:10:16 +00:00
|
|
|
#print $write_fh $line;
|
2007-12-12 13:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (@output_files)
|
|
|
|
{
|
2008-07-18 12:02:22 +00:00
|
|
|
foreach my $output_file (@output_files)
|
2007-12-12 13:39:51 +00:00
|
|
|
{
|
|
|
|
pop @write_fhs;
|
|
|
|
close $output_file
|
|
|
|
|| print STDOUT
|
|
|
|
"dsh> Error_file_closed $$target_properties{hostname} $output_file\n";
|
2008-07-18 12:02:22 +00:00
|
|
|
my $rsp={};
|
2012-06-07 13:19:01 +00:00
|
|
|
$rsp->{error}->[0] =
|
2007-12-12 13:39:51 +00:00
|
|
|
"Error_file_closed $$target_properties{hostname $output_file}.\n";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
($output_file == $$target_properties{'output-fh'})
|
|
|
|
&& delete $$target_properties{'output-fh'};
|
|
|
|
($output_file == $$target_properties{'output-fh'})
|
|
|
|
&& delete $$target_properties{'error-fh'};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $rin = '';
|
|
|
|
vec($rin, fileno($read_fh), 1) = 1;
|
|
|
|
my $fh_count = select($rin, undef, undef, 0);
|
|
|
|
last if ($fh_count == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $eof_reached;
|
|
|
|
}
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3
|
|
|
|
pipe_handler_buffer
|
|
|
|
|
|
|
|
Handles and processes dsh output from a given read pipe handle. The output
|
|
|
|
is stored in a buffer supplied by the caller.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$target_properties - property information of the target related to the pipe handle
|
|
|
|
$read_fh - reference to the read pipe handle
|
|
|
|
$buffer_size - local buffer size to read data from the handle
|
|
|
|
$label - prefix label to use for dsh output
|
|
|
|
$write_buffer - buffer of data that is yet to be written (must wait until \n is read)
|
|
|
|
$output_buffer - buffer where output will be written
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
1 if the EOF reached on $read_fh
|
|
|
|
undef otherwise
|
|
|
|
|
|
|
|
Globals:
|
|
|
|
None
|
|
|
|
|
|
|
|
Error:
|
|
|
|
None
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
Comments:
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
2009-01-08 12:57:02 +00:00
|
|
|
# NOTE: global environment $::__DSH_LAST_LINE only can be used in DSHCore::pipe_handler and DSHCore::pipe_handler_buffer
|
2007-12-12 13:39:51 +00:00
|
|
|
|
|
|
|
sub pipe_handler_buffer
|
|
|
|
{
|
|
|
|
my ($class, $target_properties, $read_fh, $buffer_size, $label,
|
|
|
|
$write_buffer, $output_buffer)
|
|
|
|
= @_;
|
|
|
|
|
|
|
|
my $line;
|
|
|
|
my $eof_reached = undef;
|
|
|
|
|
2009-01-08 12:57:02 +00:00
|
|
|
my $cust_rc_deal =0;
|
|
|
|
if ($::USER_POST_CMD)
|
|
|
|
{
|
|
|
|
# If user provide post-command to display return code,
|
|
|
|
# the keyword 'DSH_RC' will be searched,
|
|
|
|
# the return code is gotten in another way as shown like below:
|
|
|
|
# ...
|
|
|
|
# <output>
|
|
|
|
# <return_code>
|
|
|
|
# DSH_RC
|
|
|
|
#
|
|
|
|
# The last two lines are needed to be moved out from output
|
|
|
|
$cust_rc_deal = 1;
|
|
|
|
}
|
|
|
|
|
2007-12-12 13:39:51 +00:00
|
|
|
while ( (sysread($read_fh, $line, $buffer_size) != 0)
|
|
|
|
|| ($eof_reached = 1))
|
|
|
|
{
|
2009-11-10 12:27:20 +00:00
|
|
|
last if ($eof_reached && (!defined($::__DSH_LAST_LINE->{$label})));
|
|
|
|
if ($line =~ /^\n$/ && scalar(@$write_buffer) == 0)
|
2007-12-12 13:39:51 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
# need to preserve blank lines in the output.
|
|
|
|
$line = $label . $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @lines = split "\n", $line;
|
|
|
|
|
|
|
|
if (@$write_buffer)
|
|
|
|
{
|
|
|
|
my $buffered_line = shift @$write_buffer;
|
|
|
|
my $next_line = shift @lines;
|
|
|
|
$next_line = $buffered_line . $next_line;
|
|
|
|
unshift @lines, $next_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($line !~ /\n$/)
|
|
|
|
{
|
|
|
|
push @$write_buffer, (pop @lines);
|
|
|
|
}
|
|
|
|
|
2009-11-10 12:27:20 +00:00
|
|
|
if (@lines || $::__DSH_LAST_LINE->{$label})
|
2007-12-12 13:39:51 +00:00
|
|
|
{
|
2009-01-08 12:57:02 +00:00
|
|
|
if ($cust_rc_deal)
|
|
|
|
{
|
|
|
|
# Dump the last line at the beginning of current buffer
|
2009-10-22 12:49:48 +00:00
|
|
|
if ($::__DSH_LAST_LINE->{$label})
|
2009-01-08 12:57:02 +00:00
|
|
|
{
|
2009-10-22 12:49:48 +00:00
|
|
|
unshift @lines, "$::__DSH_LAST_LINE->{$label}" ;
|
2009-11-10 12:27:20 +00:00
|
|
|
undef $::__DSH_LAST_LINE->{$label}
|
2009-01-08 12:57:02 +00:00
|
|
|
}
|
2009-11-10 12:27:20 +00:00
|
|
|
if ($line) {
|
|
|
|
# Pop current buffer to $::__DSH_LAST_LINE->{$label}
|
|
|
|
$::__DSH_LAST_LINE->{$label} = $lines[scalar @lines - 1];
|
|
|
|
pop @lines;
|
|
|
|
# Skip this loop if array @lines is empty.
|
|
|
|
if (scalar @lines == 0)
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
2009-01-08 12:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-12 13:39:51 +00:00
|
|
|
|
|
|
|
$line = join "\n", @lines;
|
|
|
|
$line .= "\n";
|
|
|
|
|
|
|
|
if ($line =~ /:DSH_TARGET_RC=/)
|
|
|
|
{
|
|
|
|
my $start_offset = index($line, ':DSH_TARGET_RC');
|
|
|
|
my $end_offset = index($line, ':', $start_offset + 1);
|
|
|
|
my $target_rc_string =
|
|
|
|
substr($line, $start_offset, $end_offset - $start_offset);
|
|
|
|
my ($discard, $target_rc) = split '=', $target_rc_string;
|
|
|
|
$line =~ s/:DSH_TARGET_RC=$target_rc:\n//g;
|
|
|
|
$$target_properties{'target-rc'} = $target_rc;
|
|
|
|
}
|
2009-10-22 12:49:48 +00:00
|
|
|
if ( $::__DSH_LAST_LINE->{$label} =~ /DSH_RC/ && $cust_rc_deal) {
|
2009-01-08 12:57:02 +00:00
|
|
|
my $target_rc = undef;
|
|
|
|
# Get the number in the last line
|
|
|
|
$line =~ /[\D]*([0-9]+)\s*$/ ;
|
|
|
|
$target_rc = $1;
|
|
|
|
$$target_properties{'target-rc'} = $target_rc;
|
|
|
|
# Remove the last line
|
|
|
|
$line =~ s/$target_rc\s*\n$//g;
|
|
|
|
#$line = $line . "## ret=$target_rc";
|
2009-10-22 12:49:48 +00:00
|
|
|
# Clean up $::__DSH_LAST_LINE->{$label}
|
|
|
|
undef $::__DSH_LAST_LINE->{$label} ;
|
2009-01-08 12:57:02 +00:00
|
|
|
# when '-z' is specified, display return code
|
|
|
|
$::DSH_EXIT_STATUS &&
|
|
|
|
($line .="Remote_command_rc = $target_rc");
|
|
|
|
}
|
2007-12-12 13:39:51 +00:00
|
|
|
|
|
|
|
if ($line ne '')
|
|
|
|
{
|
|
|
|
if ($line !~ /^$label\n$/)
|
|
|
|
{
|
|
|
|
$line = $label . $line;
|
|
|
|
}
|
|
|
|
$line =~ s/$/\n/ if $line !~ /\n$/;
|
|
|
|
}
|
|
|
|
|
|
|
|
$line =~ s/\n/\n$label/g;
|
|
|
|
($line =~ /\n$label$/) && ($line =~ s/\n$label$/\n/);
|
|
|
|
|
|
|
|
push @$output_buffer, $line;
|
|
|
|
|
|
|
|
my $rin = '';
|
|
|
|
vec($rin, fileno($read_fh), 1) = 1;
|
|
|
|
my $fh_count = select($rin, undef, undef, 0);
|
|
|
|
last if ($fh_count == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $eof_reached;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3
|
|
|
|
ping_hostnames
|
|
|
|
|
|
|
|
Executes ping on a given list of hostnames and returns a list of those
|
|
|
|
hostnames that did not respond
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
@hostnames - list of hostnames to execute for fping
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
@no_response - list of hostnames that did not respond
|
|
|
|
undef if fping is not installed
|
|
|
|
|
|
|
|
Globals:
|
|
|
|
None
|
|
|
|
|
|
|
|
Error:
|
|
|
|
None
|
|
|
|
|
|
|
|
Example:
|
|
|
|
@bad_hosts = xCAT::DSHCore->ping_hostnames(@host_list);
|
|
|
|
|
|
|
|
Comments:
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub ping_hostnames
|
|
|
|
{
|
|
|
|
my ($class, @hostnames) = @_;
|
|
|
|
|
|
|
|
my $ping = (($^O eq 'aix') && '/usr/sbin/ping')
|
|
|
|
|| (($^O eq 'linux') && '/bin/ping')
|
|
|
|
|| undef;
|
|
|
|
!$ping && return undef;
|
|
|
|
|
|
|
|
my @no_response = ();
|
2008-07-18 12:02:22 +00:00
|
|
|
foreach my $hostname (@hostnames)
|
2007-12-12 13:39:51 +00:00
|
|
|
{
|
|
|
|
(system("$ping -c 1 -w 1 $hostname > /dev/null 2>&1") != 0)
|
|
|
|
&& (push @no_response, $hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return @no_response;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3
|
2007-12-19 16:04:20 +00:00
|
|
|
pping_hostnames
|
2007-12-12 13:39:51 +00:00
|
|
|
|
2007-12-19 16:04:20 +00:00
|
|
|
Executes pping on a given list of hostnames and returns a list of those
|
2007-12-12 13:39:51 +00:00
|
|
|
hostnames that did not respond
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
@hostnames - list of hostnames to execute for fping
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
@no_response - list of hostnames that did not respond
|
|
|
|
|
|
|
|
Globals:
|
|
|
|
None
|
|
|
|
|
|
|
|
Error:
|
|
|
|
None
|
|
|
|
|
|
|
|
Example:
|
2007-12-19 16:04:20 +00:00
|
|
|
@bad_hosts = xCAT::DSHCore->pping_hostnames(@host_list);
|
2007-12-12 13:39:51 +00:00
|
|
|
|
|
|
|
Comments:
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2007-12-19 16:04:20 +00:00
|
|
|
sub pping_hostnames
|
2007-12-12 13:39:51 +00:00
|
|
|
{
|
|
|
|
my ($class, @hostnames) = @_;
|
|
|
|
|
|
|
|
my $hostname_list = join ",", @hostnames;
|
2013-04-23 18:27:51 +00:00
|
|
|
# read site table, usefping attribute
|
|
|
|
# if set then run pping -f to use fping
|
|
|
|
# this fixes a broken nmap in Redhat 6.2 with ip alias (3512)
|
|
|
|
my $cmd="$::XCATROOT/bin/pping $hostname_list"; # default
|
|
|
|
my @usefping=xCAT::TableUtils->get_site_attribute("usefping");
|
|
|
|
if ((defined($usefping[0])) && ($usefping[0] eq "1")) {
|
|
|
|
$cmd = "$::XCATROOT/bin/pping -f $hostname_list";
|
|
|
|
}
|
|
|
|
#my $rsp={};
|
|
|
|
#$rsp->{data}->[0] = "running command $cmd";
|
|
|
|
#xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
|
|
|
|
|
2007-12-12 13:39:51 +00:00
|
|
|
my @output =
|
2013-04-23 18:27:51 +00:00
|
|
|
xCAT::Utils->runcmd($cmd, -1);
|
2009-12-15 23:55:12 +00:00
|
|
|
if ($::RUNCMD_RC !=0) {
|
|
|
|
my $rsp={};
|
2012-06-07 13:19:01 +00:00
|
|
|
$rsp->{error}->[0] = "Error from pping";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
2009-12-15 23:55:12 +00:00
|
|
|
}
|
2008-10-06 19:32:22 +00:00
|
|
|
$::RUNCMD_RC =0; # reset
|
2007-12-12 13:39:51 +00:00
|
|
|
my @no_response = ();
|
2008-07-18 12:02:22 +00:00
|
|
|
foreach my $line (@output)
|
2007-12-12 13:39:51 +00:00
|
|
|
{
|
|
|
|
my ($hostname, $result) = split ':', $line;
|
|
|
|
my ($token, $status) = split ' ', $result;
|
|
|
|
chomp($token);
|
2008-10-06 19:32:22 +00:00
|
|
|
if ($token ne 'ping') {
|
|
|
|
push @no_response, $hostname;
|
|
|
|
}
|
2007-12-12 13:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return @no_response;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|