Skip to content

Commit

Permalink
worker_command_tester: add more generic filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Aug 24, 2023
1 parent f397708 commit 56eefe4
Showing 1 changed file with 51 additions and 13 deletions.
64 changes: 51 additions & 13 deletions examples/worker_command_tester
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ sub run {
'hostsonly' => undef,
'hostfilter' => undef,
'servicefilter' => undef,
'filter' => undef,
'backend' => undef,
'worker' => 'auto',
'exec' => 'gearman',
Expand All @@ -106,6 +107,7 @@ sub run {
"hostfilter=s" => \$opt->{'hostfilter'},
"hostsonly" => \$opt->{'hostsonly'},
"servicefilter=s" => \$opt->{'servicefilter'},
"filter=s" => \$opt->{'filter'},
"b|backend=s" => \$opt->{'backend'},
"w|worker=i" => \$opt->{'worker'},
"e|exec=s" => \$opt->{'exec'},
Expand Down Expand Up @@ -371,7 +373,24 @@ sub _get_jobs {
if($opt->{'servicefilter'}) {
push @{$servicefilter}, { description => { '~~' => $opt->{'servicefilter'} }};
}
if(!$opt->{'servicefilter'}) {
my $has_hosts = 0;
if($opt->{'filter'}) {
my @names = split(/\s*;\s*/mx, $opt->{'filter'});
my @filter;
for my $name (@names) {
if($name eq '__HOST__') {
$has_hosts = 1;
next;
}
push @filter, { description => $name };
}
if(scalar @filter > 0) {
push @{$servicefilter}, Thruk::Utils::combine_filter( '-or', \@filter );
} else {
$opt->{'hostsonly'} = 1;
}
}
if(!$opt->{'servicefilter'} || $has_hosts) {
my $hosts = $c->db->get_hosts(filter => [$hostfilter], columns => [qw/name address check_command/]);
push @{$jobs}, @{$hosts};
_info("fetched %d hosts", scalar @{$hosts});
Expand Down Expand Up @@ -480,7 +499,8 @@ sub _check_object {
$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)) {
my $comp = $self->_compare_result($name, $chkobj, $rc, $output);
if($comp == 0) {
# everything fine
return;
}
Expand All @@ -490,52 +510,66 @@ sub _check_object {
sprintf("check command: %s", $command->{'line_expanded'}),
sprintf("expected rc: %3s - output: %s (last_check: %s)", $chkobj->{'state'}, _shorten($chkobj->{'plugin_output'}, 80), Thruk::Utils::Filter::date_format($c, $chkobj->{'last_check'})),
sprintf("actual rc: %3s - output: %s", $rc, _shorten($output, 80)),
sprintf("%s: %s has different exit code or output", $chkobj->{'name'} ? 'host' : 'service', $name),
sprintf("%s: %s has different %s",
($chkobj->{'name'} ? 'host' : 'service'),
$name,
($comp == 1 ? 'exit code' : 'plugin output')),
]);
}

##############################################
# returns 0 if no changes were detected, 1 for exit code change and 2 for output changes
sub _compare_result {
my($self, $name, $chkobj, $rc, $output) = @_;
# translate host status
if($chkobj->{'name'}) {
if($chkobj->{'state'} == 0 && $rc != 0) {
return;
return 1;
}
if($chkobj->{'state'} != 0 && $rc == 0) {
return;
return 1;
}
} else {
if($rc != $chkobj->{'state'}) {
return;
return 1;
}
}

my $plugin_output = _make_plugin_output_comparable($chkobj->{'plugin_output'});
my $tmp_output = _make_plugin_output_comparable($output);
if($plugin_output ne $tmp_output) {
return;
return 2;
}

return 1;
return 0;
}

##############################################
sub _make_plugin_output_comparable {
my($output) = @_;

# replace numbers
$output =~ s/\d+/<NUM>/gmx;
# trime whitespace
$output =~ s/^\s+//gmx;
$output =~ s/\s+$//gmx;

# ping check output is a bit different sometimes
$output =~ s/.*\Qrta nan, lost 100%\E.*/<HOST UNREACHABLE>/gmx;

# make time out results more comparable
$output =~ s/.*(timeout\ after\ \d+\ seconds).*/<TIMEOUT RESULT>/gmx;
$output =~ s/.*(timed\ out\ after\ \d+\ seconds).*/<TIMEOUT RESULT>/gmx;
$output =~ s/.*(Plugin\ timed\ out\ while\ executing\ system\ call).*/<TIMEOUT RESULT>/gmx;

# replace dns errors, they might be slightly different
$output =~ s/\QNo address associated with hostname\E/<DNS LOOKUP ERROR>/gmx; # rhel 9
$output =~ s/\QName or service not known\E/<DNS LOOKUP ERROR>/gmx; # rhel 7

$output =~ s/^\s+//gmx;
$output =~ s/\s+$//gmx;

$output =~ s/;/:/gmx; # naemon replaces semicolon with colons

# replace numbers
$output =~ s/\d+/<NUM>/gmx;


return($output);
}

Expand Down Expand Up @@ -680,6 +714,7 @@ exceptions and similar.
--hostsonly only run hostchecks and skip services
--hostfilter filter hostnames by regular expression
--servicefilter filter service names by regular expression
--filter set a list of service names to check
-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
Expand All @@ -695,6 +730,9 @@ exceptions and similar.
# run host checks only and write errors into a logfile
OMD[test]:~$ ./share/thruk/examples/worker_command_tester --hostsonly test@workerhost 2> >(tee errors.log)

# run only the host check and the ping service and write errors into a logfile
OMD[test]:~$ ./share/thruk/examples/worker_command_tester --filter="__HOST__;Ping" test@workerhost 2> >(tee errors.log)

=head1 AUTHOR

2023, Sven Nierlein, <[email protected]>
Expand Down

0 comments on commit 56eefe4

Please sign in to comment.