Skip to content

Commit

Permalink
node-control: improve hook output
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Sep 23, 2024
1 parent f7b38bb commit 322e573
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 120 deletions.
12 changes: 6 additions & 6 deletions docs/documentation/plugins/node-control/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ The pre hooks can abort the update process by exiting != 0.

All hooks can make use of the following environment variables:

- `PEER_NAME`: name of the backend as set in the thruk.conf.
- `PEER_KEY`: internal id of the backend.
- `HOST_NAME`: remote host name of this backend.
- `SITE_NAME`: site name which will be updated.
- `OMD_VERSION`: current omd version that site is running.
- `OMD_UPDATE`: omd version that'll be used for the update.
- `PEER_NAME`: name of the backend as set in the thruk.conf.
- `PEER_KEY`: internal id of the backend.
- `OMD_HOST_NAME`: remote host name of this backend.
- `SITE_NAME`: site name which will be updated.
- `FROM_OMD_VERSION`: current omd version that site is running.
- `OMD_UPDATE`: omd version that'll be used for the update.
2 changes: 1 addition & 1 deletion lib/Thruk/Backend/Peer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ sub cmd {
if($background_options) {
$background_options->{"background"} = 1;
$background_options->{"cmd"} = $cmd;
$background_options->{"env"} = $env;
$background_options->{"env"} = $env if $env;
}
if($self->{'type'} eq 'http') {
# forward by http federation
Expand Down
27 changes: 21 additions & 6 deletions lib/Thruk/Utils/External.pm
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ sub cmd {
clean => "remove job after displaying it if true"
render => "set to a true value to render page immediatly"
show_output => show console with output
log_archive => store stdout/stderr with this filename
}
)
Expand Down Expand Up @@ -200,6 +201,7 @@ sub perl {
$err = $@ unless $err;
$c->stats->profile(end => 'External::perl');
save_profile($c, $dir);
_save_log_archive($c, $dir, $conf->{'log_archive'}) if $conf->{'log_archive'};
if($err) {
eval {
Thruk::Utils::IO::write($dir."/stderr", "ERROR: perl eval failed:\n".$err, undef, 1);
Expand Down Expand Up @@ -338,7 +340,7 @@ sub read_job {

my $start = -e $job_dir.'/start' ? (stat(_))[9] : 0;
my $end = -e $job_dir.'/rc' ? (stat(_))[9] : 0;
my $rc = Thruk::Utils::IO::saferead($job_dir.'/rc') // '';
my $rc = Thruk::Utils::IO::saferead($job_dir.'/rc') // ''; # 0 is OK, everything else is an error (exit code)
my $res = Thruk::Utils::IO::saferead($job_dir.'/perl_res') // '';
my $out = Thruk::Utils::IO::saferead($job_dir.'/stdout') // '';
my $err = Thruk::Utils::IO::saferead($job_dir.'/stderr') // '';
Expand All @@ -350,6 +352,7 @@ sub read_job {
$cmd =~ s%^\$VAR1\s*=\s*%%gmx;
$cmd =~ s%\n$%%gmx;
}
chomp($rc);
if($rc !~ m/^\d*$/mx) { $rc = -1; }
my($hostid, $hostname) = split(/\n/mx, $host);

Expand All @@ -358,20 +361,20 @@ sub read_job {
'id' => $id,
'pid' => $pid,
'user' => $user,
'host_id' => $hostid // "",
'host_id' => $hostid // "",
'host_name' => $hostname // "",
'cmd' => $cmd,
'rc' => $rc // '',
'rc' => $rc // '',
'perl_res' => $res // '',
'stdout' => $out // '',
'stderr' => $err // '',
'is_running' => 0+$is_running,
'time' => 0+$time,
'start' => 0+($start || 0),
'end' => 0+($end || 0),
'end' => 0+($end || 0),
'percent' => 0+$percent,
'message' => $message // '',
'forward' => $forward // '',
'message' => $message // '',
'forward' => $forward // '',
'show_output'=> $show_output // 0,
'remaining' => 0+$remaining,
};
Expand Down Expand Up @@ -772,6 +775,18 @@ sub save_profile {
return;
}

##############################################
# save stdout and stderr into new logfile
sub _save_log_archive {
my($c, $job_dir, $logfile) = @_;

my $out = Thruk::Utils::IO::saferead($job_dir.'/stdout') // '';
my $err = Thruk::Utils::IO::saferead($job_dir.'/stderr') // '';
Thruk::Utils::IO::write($logfile, $out.$err);

return;
}

##############################################

=head2 do_child_stuff
Expand Down
42 changes: 23 additions & 19 deletions lib/Thruk/Utils/IO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,10 @@ sub cmd {

require Thruk::Utils::Encode unless $options->{'no_decode'};

if(ref $cmd ne 'ARRAY' && $cmd !~ m/&\s*$/mx) {
$cmd = ["/bin/sh", "-c", $cmd];
}

my($rc, $output);
if(ref $cmd eq 'ARRAY') {
my $prog = shift @{$cmd};
Expand All @@ -895,15 +899,21 @@ sub cmd {
$sel->remove($fh);
next;
} else {
my $prefix = "";
if($options->{'output_prefix'}) {
my $prefix = $options->{'output_prefix'};
$prefix = $options->{'output_prefix'};
if(ref $prefix eq 'CODE') {
$prefix = &{$prefix};
$prefix = &{$prefix}();
}
}
if($options->{'print_prefix'} || $options->{'output_prefix'}) {
for my $l (split/\n/mx, $line) {
push @lines, $prefix.$l."\n";
print $options->{'print_prefix'}, $l, "\n" if defined $options->{'print_prefix'};
}
$line = $prefix.$line;
} else {
push @lines, $line;
}
push @lines, $line;
print $options->{'print_prefix'}, $line if defined $options->{'print_prefix'};
}
}
}
Expand All @@ -920,21 +930,15 @@ sub cmd {
&timing_breakpoint('IO::cmd: '.$cmd);
_debug( "running cmd: ". $cmd ) if $c;

# background process?
if($cmd =~ m/&\s*$/mx) {
local $SIG{CHLD} = 'IGNORE'; # let the system reap the childs, we don't care
if($cmd !~ m|2>&1|mx) {
_warn(longmess("cmd does not redirect output but wants to run in the background, add >/dev/null 2>&1 to: ".$cmd)) if $c;
}
$output = `$cmd`;
$rc = $?;
# rc will be -1 otherwise when ignoring SIGCHLD
$rc = 0 if $rc == -1;
} else {
$output = `$cmd`;
$rc = $?;
$output = Thruk::Utils::Encode::decode_any($output) unless $options->{'no_decode'};
# background command
local $SIG{CHLD} = 'IGNORE'; # let the system reap the childs, we don't care
if($cmd !~ m|2>&1|mx) {
_warn(longmess("cmd does not redirect output but wants to run in the background, add >/dev/null 2>&1 to: ".$cmd)) if $c;
}
$output = `$cmd`;
$rc = $?;
# rc will be -1 otherwise when ignoring SIGCHLD
$rc = 0 if $rc == -1;
}

if($rc == -1) {
Expand Down
35 changes: 34 additions & 1 deletion lib/Thruk/Utils/Log.pm
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ sub _audit_log {
=cut
sub time_prefix {
my($seconds, $microseconds) = Time::HiRes::gettimeofday;
return(sprintf("[%s,%s]",
return(sprintf("[%s,%s] ",
POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime($seconds)),
substr(sprintf("%06s", $microseconds), 0, 3),
));
Expand Down Expand Up @@ -340,6 +340,39 @@ sub wrap_stdout2log_stop {
return;
}

##############################################

=head2 wrap_stderr2log
wrap stderr to warn logger. everything printed to stderr will be logged
with info level to stdout.
=cut
sub wrap_stderr2log {
my($capture, $tmp);
## no critic
open($capture, '>', \$tmp) or die("cannot open stdout capture: $!");
tie *$capture, 'Thruk::Utils::Log', (*STDERR);
select $capture;
STDERR->autoflush(1);
## use critic
return($capture);
}

##############################################

=head2 wrap_stderr2log_stop
stop wrapping stderr
=cut
sub wrap_stderr2log_stop {
## no critic
select *STERR;
## use critic
return;
}

##############################################
sub TIEHANDLE {
my($class, $fh) = @_;
Expand Down
Loading

0 comments on commit 322e573

Please sign in to comment.