-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker_command_tester: add more generic filter
- Loading branch information
Showing
1 changed file
with
51 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,7 @@ sub run { | |
'hostsonly' => undef, | ||
'hostfilter' => undef, | ||
'servicefilter' => undef, | ||
'filter' => undef, | ||
'backend' => undef, | ||
'worker' => 'auto', | ||
'exec' => 'gearman', | ||
|
@@ -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'}, | ||
|
@@ -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}); | ||
|
@@ -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; | ||
} | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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 | ||
|
@@ -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]> | ||
|