Skip to content

Commit

Permalink
agents: add support for custom checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Oct 10, 2024
1 parent e160cca commit dea143a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
13 changes: 13 additions & 0 deletions docs/documentation/plugins/agents/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,21 @@ For example:
name = dns # the actual service description
check = check_dns # snclient check
args = -H thruk.org # check arguments
# add arbitray naemon config attributes here as well
first_notification_delay = 30
</extra_service_checks>
# add custom service checks
<extra_service_checks>
# on which host / sections should this serice be created
host = ANY
section = ANY
name = ping # the actual service description
check_command = check-host-alive!$HOSTADDRESS$
# add arbitray naemon config attributes here as well
first_notification_delay = 30
</extra_service_checks>
</snclient>
</Component>
...................................
63 changes: 44 additions & 19 deletions plugins/plugins-available/agents/lib/Thruk/Agents/SNClient.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use warnings;
use strict;
use Carp qw/confess/;
use Cpanel::JSON::XS qw/decode_json/;
use Storable ();

use Monitoring::Config::Object ();
use Thruk::Utils ();
Expand Down Expand Up @@ -200,6 +201,7 @@ sub get_config_objects {
$svc->{'conf'}->{'use'} = \@templates;
delete $chk->{'svc_conf'}->{'_AGENT_ARGS'};
my $extra = _get_extra_opts_svc($c, $svc->{'conf'}->{'service_description'}, $hostname, $section);
push @{$extra}, $chk->{'extra'} if $chk->{'extra'};
if($args) { # user supplied manual overrides
$chk->{'svc_conf'}->{'_AGENT_ARGS'} = $args;
$chk->{'svc_conf'}->{'check_command'} .= " ".$args;
Expand Down Expand Up @@ -230,6 +232,7 @@ sub get_config_objects {
next if $key eq 'section';
next if $key eq 'host_name';
next if $key eq 'args';
next if $key eq 'check';
$chk->{'svc_conf'}->{$key} = $ex->{$key};
}
}
Expand Down Expand Up @@ -489,7 +492,7 @@ sub _extract_checks {
}
my $proto = "https";
$proto = "http" if($mode && $mode eq 'http');
my $command = sprintf(
my $command = $chk->{'check_command'} // sprintf(
"check_thruk_agent!%s\$USER1\$/check_nsc_web %s %s -p '%s' -u '%s://%s:%s' '%s'",
$proxy_cmd,
_check_nsc_web_extra_options($c, $mode),
Expand All @@ -501,21 +504,23 @@ sub _extract_checks {
$chk->{'check'},
);
my $interval = $c->config->{'Thruk::Agents'}->{'snclient'}->{'check_interval'} // 1;
if($chk->{'check'} eq 'inventory') {
$command = 'check_thruk_agent!'.$proxy_cmd.'$USER4$/bin/thruk agents check inventory \'$HOSTNAME$\'';
$interval = $c->config->{'Thruk::Agents'}->{'snclient'}->{'inventory_interval'} // 60;
}
if($chk->{'check'} eq 'check_os_updates') {
$interval = $c->config->{'Thruk::Agents'}->{'snclient'}->{'os_updates_interval'} // 60;
}
if($chk->{'args'}) {
if(ref $chk->{'args'} eq 'ARRAY') {
for my $arg (@{$chk->{'args'}}) {
$command .= sprintf(" %s", $arg);
}
} else {
for my $arg (sort keys %{$chk->{'args'}}) {
$command .= sprintf(" %s='%s'", $arg, $chk->{'args'}->{$arg});
if($chk->{'check'}) {
if($chk->{'check'} eq 'inventory') {
$command = 'check_thruk_agent!'.$proxy_cmd.'$USER4$/bin/thruk agents check inventory \'$HOSTNAME$\'';
$interval = $c->config->{'Thruk::Agents'}->{'snclient'}->{'inventory_interval'} // 60;
}
if($chk->{'check'} eq 'check_os_updates') {
$interval = $c->config->{'Thruk::Agents'}->{'snclient'}->{'os_updates_interval'} // 60;
}
if($chk->{'args'}) {
if(ref $chk->{'args'} eq 'ARRAY') {
for my $arg (@{$chk->{'args'}}) {
$command .= sprintf(" %s", $arg);
}
} else {
for my $arg (sort keys %{$chk->{'args'}}) {
$command .= sprintf(" %s='%s'", $arg, $chk->{'args'}->{$arg});
}
}
}
}
Expand Down Expand Up @@ -668,12 +673,32 @@ sub _get_extra_service_checks {
next unless Thruk::Utils::Agents::check_wildcard_match($hostname, ($chk->{'host'} // 'ANY'));
next unless Thruk::Utils::Agents::check_wildcard_match($section, ($chk->{'section'} // 'ANY'));

# create a copy, because it will be changed in the process of creating checks
my $svc = Storable::dclone($chk);

# args should be a list
$chk->{'args'} = Thruk::Base::list($chk->{'args'}) if($chk->{'args'});
$svc->{'args'} = Thruk::Base::list($svc->{'args'}) if($svc->{'args'});

# derive id from name
$svc->{'id'} = "extra.".Thruk::Utils::Filter::name2id($svc->{'name'});

my $extra = {};
for my $key (keys %{$svc}) {
next if $key eq 'extra';
next if $key eq 'id';
next if $key eq 'name';
next if $key eq 'host';
next if $key eq 'section';
next if $key eq 'check';
next if $key eq 'args';

$extra->{$key} = $svc->{$key};
}

$chk->{'id'} = "extra.".Thruk::Utils::Filter::name2id($chk->{'name'});
# everything else is a custom attribute
$svc->{'extra'} = $extra;

push @{$res}, $chk;
push @{$res}, $svc;
}

return $res;
Expand Down

0 comments on commit dea143a

Please sign in to comment.