Skip to content

Commit

Permalink
worker_command_tester: implement shell and gearman exec strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Jul 28, 2023
1 parent bc1c016 commit cc4d5ca
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions examples/worker_command_tester
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ $|=1; #enable autoflush
our $max_session_per_worker = 10;
our @sshpids;
my $sockets_queue = Thread::Queue->new();
our $exec;
our $terminal = -t STDOUT;

##############################################
sub END {
Expand Down Expand Up @@ -90,6 +92,7 @@ sub run {
'servicefilter' => undef,
'backend' => undef,
'worker' => 'auto',
'exec' => 'gearman',
'target' => [],
'verbose' => 0,
'timeout' => 30,
Expand All @@ -105,6 +108,7 @@ sub run {
"servicefilter=s" => \$opt->{'servicefilter'},
"b|backend=s" => \$opt->{'backend'},
"w|worker=i" => \$opt->{'worker'},
"e|exec=s" => \$opt->{'exec'},
"t|timeout=i" => \$opt->{'timeout'},
"c|continue" => \$opt->{'continue'},
"r|retry=s" => \$opt->{'retry'},
Expand All @@ -117,6 +121,11 @@ sub run {
_error("retry file and continue option set, use either of them, not both.");
exit(1);
}
if($opt->{'exec'} ne 'gearman' && $opt->{'exec'} ne 'shell') {
_error("exec can be either 'gearman' or 'shell'");
exit(1);
}
$exec = $opt->{'exec'};

$ENV{'THRUK_VERBOSE'} = $opt->{'verbose'};
Thruk::Config::set_config_env();
Expand Down Expand Up @@ -234,7 +243,7 @@ sub _run_checks {
$rate,
$rem,
$end,
);
) if $terminal;

# clean up and save continue file information
my $host_name;
Expand Down Expand Up @@ -288,6 +297,7 @@ sub _save_retry_file {
my($self) = @_;
_debug("saving retry file");

return if scalar keys %{$self->{'summary'}} == 0;
my $retry_file = $self->{'opt'}->{'retry'} || sprintf("/tmp/.worker.test.retry.%d.%s.json", $>, (POSIX::strftime("%Y%m%d_%H%M%S", localtime)));
Thruk::Utils::IO::json_store($retry_file, {
summary => $self->{'summary'},
Expand Down Expand Up @@ -467,6 +477,7 @@ sub _check_object {
return $err if $err;
$output = "" unless $output;
$output =~ s/\n.*$//sgmx; # limit to first line
$output =~ s/\\n.*$//sgmx; # limit to first line
$output =~ s/^(.*?)\|.*$/$1/gmx; # strip perf data
if($self->_compare_result($name, $chkobj, $rc, $output)) {
# everything fine
Expand Down Expand Up @@ -543,7 +554,8 @@ sub _check_command {
my $ctrl_path = $sockets_queue->dequeue_timed(3); # get next free ctrl path
die("got no ctrl path in time") unless $ctrl_path;
my $target = $self->{'opt'}->{'target'}->[0];
my $cmd = [
my $cmd = [];
my $ssh_options = [
"ssh",
"-o", "PasswordAuthentication=no",
"-o", "PreferredAuthentications=publickey",
Expand All @@ -552,10 +564,27 @@ sub _check_command {
"-o", "ControlPath=".$ctrl_path,
"-o", "StrictHostKeyChecking=no",
$target,
"./bin/mod_gearman_worker --job_timeout=".$self->{'opt'}->{'timeout'}." testcmd ".$command->{'line_expanded'},
];

my $line_expanded = $command->{'line_expanded'};
if($exec eq 'gearman') {
$line_expanded =~ s/"/\\"/gmx;
$line_expanded = '"'.$line_expanded.'"';
$cmd = [
@{$ssh_options},
"./bin/mod_gearman_worker --job_timeout=".$self->{'opt'}->{'timeout'}." testcmd ".$line_expanded,
];
}
elsif($exec eq 'shell') {
$cmd = [
"timeout", "--kill-after=".($self->{'opt'}->{'timeout'}+2), $self->{'opt'}->{'timeout'},
@{$ssh_options},
$line_expanded,
];
}

my($rc, $output) = Thruk::Utils::IO::cmd($self->{'c'}, $cmd, undef, undef, undef, undef, undef, 1);
if($rc == 2 && $output =~ m/\Qtest run into timeout after\E/) {
if(($rc == 2 && $output =~ m/\Qtest run into timeout after\E/) || $rc == 124) {
$rc = 3;
$output = sprintf("check timed out after %d seconds", $self->{'opt'}->{'timeout'});
}
Expand Down Expand Up @@ -651,6 +680,7 @@ exceptions and similar.
--hostfilter filter hostnames by regular expression
--servicefilter filter service names by regular expression
-w|--worker specify the number of parallel checks
-e|--exec specify plugin execution, supported values: gearman, shell
-t|--timeout specify timeout (in seconds) for each check
-c|--continue try to continue previuous run
-r|--retry=<file> use retry file and verify failed runs
Expand Down

0 comments on commit cc4d5ca

Please sign in to comment.