Skip to content

Commit

Permalink
agents: improve excludes
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Mar 5, 2025
1 parent 35c9543 commit 18b7388
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docs/documentation/plugins/agents/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ tags = tag2
tags = tag1 && tag2
...................................

#### Excludes
#### Exclude Match

...................................
# exclude something
Expand Down Expand Up @@ -327,6 +327,7 @@ Apply this option to all inventory checks:
#host !~ \.win\. # apply this exclude only to specific hosts, only hosts not matching ".win."
#host ~ ^l # apply this exclude only to hosts starting with an "l"
#section ~ test # apply this exclude only to sections containing "test"
#tags = prod # apply this exclude only to tag "prod"
</exclude>
# include services in discovery
Expand Down
33 changes: 23 additions & 10 deletions plugins/plugins-available/agents/lib/Thruk/Utils/Agents.pm
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ sub get_services_checks {

my $agent = build_agent($agenttype // $hostobj);
$checks = $agent->get_services_checks($c, $hostname, $hostobj, $password, $cli_opts, $section, $mode, $options, $tags);
_set_checks_category($c, $hostname, $hostobj, $checks, $type, $cli_opts);
_set_checks_category($c, $hostname, $hostobj, $checks, $type, $cli_opts, $section, $tags);

return($checks);
}
Expand Down Expand Up @@ -366,7 +366,7 @@ sub set_object_model {
# - obsolete: exists as services but not in inventory anymore
# - disabled: exists in inventory but is disabled by user config
sub _set_checks_category {
my($c, $hostname, $hostobj, $checks, $agenttype, $fresh) = @_;
my($c, $hostname, $hostobj, $checks, $agenttype, $fresh, $section, $tags) = @_;

my $services = $hostobj ? get_host_agent_services($c, $hostobj) : {};
my $services_by_id = get_host_agent_services_by_id($services);
Expand All @@ -381,6 +381,7 @@ sub _set_checks_category {
my $name = $chk->{'name'};
$existing->{$chk->{'id'}} = 1;
my $svc = $services_by_id->{$chk->{'id'}} // $services->{$name};
delete $chk->{'exclude_reason'};
if($svc && $svc->{'conf'}->{'_AGENT_AUTO_CHECK'}) {
$chk->{'exists'} = 'exists';
$chk->{'_svc'} = $svc;
Expand All @@ -397,9 +398,10 @@ sub _set_checks_category {
# disabled by 'disable' configuration
$chk->{'exists'} = 'disabled';
}
elsif(_is_excluded($hostname, $chk, $excludes)) {
elsif(my $res = _is_excluded($hostname, $section, $tags, $chk, $excludes)) {
# disabled by 'exclude' configuration
$chk->{'exists'} = 'disabled';
$chk->{'exclude_reason'} = $res;
} else {
$chk->{'exists'} = 'new';
}
Expand Down Expand Up @@ -921,18 +923,29 @@ sub _find_agent_modules {
##########################################################
# returns true if check matches any of the given excludes
sub _is_excluded {
my($hostname, $chk, $excludes) = @_;
my($hostname, $section, $tags, $chk, $excludes) = @_;
return unless $excludes;
$excludes = Thruk::Base::list($excludes);
for my $ex (@{$excludes}) {
$ex->{'host'} = "ANY" unless defined $ex->{'host'};
if(!_check_pattern($hostname, $ex->{'host'})) {
next;
next unless check_wildcard_match($hostname, ($ex->{'host'}//'ANY'));
if($section) {
next unless check_wildcard_match($section, ($ex->{'section'}//'ANY'));
}
if($tags) {
my $list = Thruk::Base::comma_separated_list($tags // '');
next unless check_wildcard_match($list, ($ex->{'tag'}//'ANY'));
}

my $names = [@{Thruk::Utils::list($ex->{'name'})}, @{Thruk::Utils::list($ex->{'service'})}];
if(scalar @{$names} > 0) {
return $ex if check_wildcard_match($chk->{'name'}, $names);
}
my $types = Thruk::Utils::list($ex->{'type'});
if(scalar @{$types} > 0) {
return $ex if check_wildcard_match($chk->{'id'}, $types);
}
return 1 if _check_pattern($chk->{'name'}, $ex->{'name'});
return 1 if _check_pattern($chk->{'id'}, $ex->{'type'});
}
return(0);
return;
}

##########################################################
Expand Down
20 changes: 15 additions & 5 deletions plugins/plugins-available/agents/lib/Thruk/Utils/CLI/Agents.pm
Original file line number Diff line number Diff line change
Expand Up @@ -972,12 +972,22 @@ sub _log_changes_diff {
$diff =~ s/\Q$filename2\E.*/old/mx;
$diff =~ s/\Q$filename1\E.*/new/mx;

if($force) {
_info("changes in service '%s'", $obj->{'conf'}->{'service_description'});
_info($diff);
if($obj->{'conf'}->{'service_description'}) {
if($force) {
_info("changes in service '%s'", $obj->{'conf'}->{'service_description'});
_info($diff);
} else {
_debug("changes in service '%s'", $obj->{'conf'}->{'service_description'});
_debug($diff);
}
} else {
_debug("changes in service '%s'", $obj->{'conf'}->{'service_description'});
_debug($diff);
if($force) {
_info("changes in host '%s'", $obj->{'conf'}->{'host_name'});
_info($diff);
} else {
_debug("changes in host '%s'", $obj->{'conf'}->{'host_name'});
_debug($diff);
}
}

return($diff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BEGIN {

$ENV{'THRUK_TEST_AUTH'} = 'omdadmin:omd';
$ENV{'PLACK_TEST_EXTERNALSERVER_URI'} = 'http://127.0.0.1/demo';
plan tests => 145;
plan tests => 147;

use_ok("Thruk::Utils::IO");

Expand Down

0 comments on commit 18b7388

Please sign in to comment.