Skip to content

Commit

Permalink
improve error out from downtime tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Nov 12, 2024
1 parent e0a6fd4 commit a4f5f52
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 30 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ next:
- reworked status page column editor
- improve performance when having custom column layout on status pages
- reduce required queries when using lmd
- improve error out from downtime tasks
- Rest:
- add support for host/service note commands
ex.: /hosts/<name>/cmd/note
Expand Down
56 changes: 30 additions & 26 deletions lib/Thruk/Controller/cmd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,19 @@ sub index {
}

# service quick commands
for my $servicedata ( split /,/mx, $c->req->parameters->{'selected_services'} ) {
for my $servicedata ( split(/,/mx, $c->req->parameters->{'selected_services'}) ) {
if( defined $service_quick_commands->{$quick_command} ) {
$cmd_typ = $service_quick_commands->{$quick_command};
}
else {
return $c->detach('/error/index/7');
}
my($host, $service, $backend) = split /;/mx, $servicedata;
my($host, $service, $backend) = split(/;/mx, $servicedata);
if(!defined $service) {
$c->error("invalid data, no host or service received");
return $c->detach('/error/index/100');
}
my @backends = split /\|/mx, $backend;
my @backends = split(/\|/mx, $backend);
$c->stash->{'lasthost'} = $host;
$c->stash->{'lastservice'} = $service;
$c->req->parameters->{'cmd_typ'} = $cmd_typ;
Expand Down Expand Up @@ -298,9 +298,12 @@ sub index {
_check_for_commands($c);
}

if($c->req->parameters->{'json'} and $c->stash->{'form_errors'}) {
my $json = {'success' => ($c->stash->{'form_errors'} && scalar @{$c->stash->{'form_errors'}}) > 0 ? 0 : 1, errors => $c->stash->{'form_errors'} };
return $c->render(json => $json);
if($c->req->parameters->{'json'}) {
return 1 if $c->{'rendered'};
if(scalar @{$c->stash->{'form_errors'}} > 0) {
return $c->render(json => {'success' => 0, 'error' => $c->stash->{'form_errors'} });
}
return $c->render(json => {'success' => 1 });
}

return 1;
Expand Down Expand Up @@ -556,8 +559,8 @@ sub redirect_or_success {

return if $just_return;
if($c->req->parameters->{'json'}) {
my $json = {'success' => 1};
return $c->render(json => $json);
return $c->render(json => {'success' => 0, 'error' => $c->stash->{'last_command_error'} }) if $c->stash->{'last_command_error'};
return $c->render(json => {'success' => 1});
}
else {
$c->redirect_to($referer);
Expand All @@ -566,8 +569,8 @@ sub redirect_or_success {
else {
return if $just_return;
if($c->req->parameters->{'json'}) {
my $json = {'success' => 1};
return $c->render(json => $json);
return $c->render(json => {'success' => 0, 'error' => $c->stash->{'last_command_error'} }) if $c->stash->{'last_command_error'};
return $c->render(json => {'success' => 1});
}
$c->stash->{template} = 'cmd_success.tt';
}
Expand Down Expand Up @@ -862,23 +865,24 @@ sub _bulk_send_backend {
$c->stash->{'last_command_lines'} = [] unless $c->stash->{'last_command_lines'};
push @{$c->stash->{'last_command_lines'}}, sprintf("%s%s", $cmd, ($c->stash->{'extra_log_comment'}->{$cmd} || ''));
}
if(!$testmode) {
eval {
$c->db->send_command(%{$options});
};
my $err = $@;
if($err) {
$err =~ s/(\ at\ .*?\.pm\ line\ \d+).*$//gsmx;
$c->stash->{'last_command_error'} = $err;
Thruk::Utils::set_message($c, 'fail_message', "sending command failed: ".$err);
return;
}
my $cached_proc = $c->cache->get->{'global'} || {};
for my $key (split(/,/mx, $backends)) {
delete $cached_proc->{'processinfo'}->{$key};
}
$c->cache->set('global', $cached_proc);

return 1 if $testmode;

eval {
$c->db->send_command(%{$options});
};
my $err = $@;
if($err) {
$err =~ s/(\ at\ .*?\.pm\ line\ \d+).*$//gsmx;
$c->stash->{'last_command_error'} = $err;
Thruk::Utils::set_message($c, 'fail_message', "sending command failed: ".$err);
return;
}
my $cached_proc = $c->cache->get->{'global'} || {};
for my $key (split(/,/mx, $backends)) {
delete $cached_proc->{'processinfo'}->{$key};
}
$c->cache->set('global', $cached_proc);

return 1;
}
Expand Down
27 changes: 23 additions & 4 deletions lib/Thruk/Utils/CLI/Downtimetask.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The downtimetask command executes recurring downtimes tasks.

use warnings;
use strict;
use Cpanel::JSON::XS ();
use Getopt::Long ();

use Thruk::Action::AddDefaults ();
Expand Down Expand Up @@ -118,6 +119,7 @@ sub _handle_file {

my $retries;
my $total_retries = 5;
my $retry_delay = 10;

my $nr = $file;
$file = $c->config->{'var_path'}.'/downtimes/'.$file.'.tsk';
Expand Down Expand Up @@ -150,7 +152,7 @@ sub _handle_file {
my($backends, $cmd_typ) = Thruk::Utils::RecurringDowntimes::get_downtime_backends($c, $downtime);
my $errors = 0;
for($retries = 0; $retries < $total_retries; $retries++) {
sleep(10) if $retries > 0;
sleep($retry_delay * $retries) if $retries > 0;
if($downtime->{'target'} eq 'host') {
my $hosts = $downtime->{'host'};
for my $hst (@{$hosts}) {
Expand Down Expand Up @@ -222,7 +224,7 @@ sub _handle_file {
last unless $errors;
}

return("recurring downtime ".$file." failed after $retries retries, find details in the thruk.log file.\n", 1) if $errors; # error is already printed
return("recurring downtime ".$file." failed after $retries retries.\n", 1) if $errors; # error is already printed

my $output = '';
$output = '['.$nr.'.tsk]';
Expand Down Expand Up @@ -284,9 +286,26 @@ sub set_downtime {
service => $downtime->{'service'},
hostgroup => $downtime->{'hostgroup'},
servicegroup => $downtime->{'servicegroup'},
json => 1,
});
return 0 if $res[0] != 200; # error is already printed
return 1;

if(scalar @res >= 2 && ref $res[1] eq 'HASH' && defined $res[1]->{'result'}) {
my $data;
my $jsonreader = Cpanel::JSON::XS->new->utf8;
$jsonreader->relaxed();
eval {
$data = $jsonreader->decode($res[1]->{'result'});
};
if($@) {
die("failed to parse json: ".$@);
}
return 1 if $data->{'success'};
_warn($data->{'error'});
return 0;
}
return 1 if $res[0] == 200;
# error is already printed?
return 0;
}

##############################################
Expand Down

0 comments on commit a4f5f52

Please sign in to comment.