Skip to content

Commit

Permalink
agents: fix saving options
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Apr 7, 2024
1 parent d4360ab commit 4141654
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
35 changes: 18 additions & 17 deletions plugins/plugins-available/agents/lib/Thruk/Agents/SNClient.pm
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,20 @@ sub get_config_objects {
my $services = Thruk::Utils::Agents::get_host_agent_services($c, $hostobj);
my $services_by_id = Thruk::Utils::Agents::get_host_agent_services_by_id($services);

my $settings = $hostdata->{'_AGENT_CONFIG'} ? decode_json($hostdata->{'_AGENT_CONFIG'}) : {};
for my $key (sort keys %{$checks_config}) {
if($key =~ /^options\.(.*)$/mx) {
my $opt_name = $1;
if($checks_config->{$key} ne '') {
$settings->{'options'}->{$opt_name} = $checks_config->{$key};
} else {
delete $settings->{'options'}->{$opt_name};
}
}
}

# save services
my $checks = Thruk::Utils::Agents::get_services_checks($c, $backend, $hostname, $hostobj, "snclient", $password, $fresh, $section, $mode);
my $checks = Thruk::Utils::Agents::get_services_checks($c, $backend, $hostname, $hostobj, "snclient", $password, $fresh, $section, $mode, $settings->{'options'});
my $checks_hash = Thruk::Base::array2hash($checks, "id");

if(!$checks || scalar @{$checks} == 0) {
Expand All @@ -137,17 +149,6 @@ sub get_config_objects {
if($mode && $mode ne 'https') {
$hostdata->{'_AGENT_MODE'} = $mode;
}
my $settings = $hostdata->{'_AGENT_CONFIG'} ? decode_json($hostdata->{'_AGENT_CONFIG'}) : {};
for my $key (sort keys %{$checks_config}) {
if($key =~ /^options\.(.*)$/mx) {
my $opt_name = $1;
if($checks_config->{$key} ne '') {
$settings->{'options'}->{$opt_name} = $checks_config->{$key};
} else {
delete $settings->{'options'}->{$opt_name};
}
}
}

my $template = $section ? _make_section_template("service", $section) : 'generic-thruk-agent-service';

Expand Down Expand Up @@ -340,23 +341,23 @@ sub _add_templates {

=head2 get_services_checks
get_services_checks($c, $hostname, $hostobj, $password, $fresh, $section, $mode)
get_services_checks($c, $hostname, $hostobj, $password, $fresh, $section, $mode, $options)
returns list of Monitoring::Objects for the host / services
=cut
sub get_services_checks {
my($self, $c, $hostname, $hostobj, $password, $fresh, $section, $mode) = @_;
my($self, $c, $hostname, $hostobj, $password, $fresh, $section, $mode, $options) = @_;
my $datafile = $c->config->{'var_path'}.'/agents/hosts/'.$hostname.'.json';
if(!-r $datafile) {
return([]);
}

my $checks = [];
my $data = Thruk::Utils::IO::json_lock_retrieve($datafile);
my $options = {};
$settings = {};
if($hostobj && $hostobj->{'conf'}->{'_AGENT_CONFIG'}) {
$options = decode_json($hostobj->{'conf'}->{'_AGENT_CONFIG'});
$settings = decode_json($hostobj->{'conf'}->{'_AGENT_CONFIG'});
}
$checks = _extract_checks(
$c,
Expand All @@ -366,7 +367,7 @@ sub get_services_checks {
$fresh,
$section,
$mode,
$options->{'options'} // {},
$options // $settings->{'options'} // {},
) if $data->{'inventory'};

return($checks);
Expand Down
12 changes: 6 additions & 6 deletions plugins/plugins-available/agents/lib/Thruk/Utils/Agents.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ returns list of checks for this host grouped by type (new, exists, obsolete, dis
=cut
sub get_agent_checks_for_host {
my($c, $backend, $hostname, $hostobj, $agenttype, $fresh, $section, $mode) = @_;
my($c, $backend, $hostname, $hostobj, $agenttype, $fresh, $section, $mode, $options) = @_;
$section = $section // $hostobj->{'conf'}->{'_AGENT_SECTION'};

# extract checks and group by type
my $flat = get_services_checks($c, $backend, $hostname, $hostobj, $agenttype, undef, $fresh, $section, $mode);
my $flat = get_services_checks($c, $backend, $hostname, $hostobj, $agenttype, undef, $fresh, $section, $mode, $options);
my $checks = Thruk::Base::array_group_by($flat, "exists");
for my $key (qw/new exists obsolete disabled/) {
$checks->{$key} = [] unless defined $checks->{$key};
Expand Down Expand Up @@ -103,13 +103,13 @@ sub update_inventory {

=head2 get_services_checks
get_services_checks($c, $backend, $hostname, $hostobj, $agenttype, $password, $fresh, $section)
get_services_checks($c, $backend, $hostname, $hostobj, $agenttype, $password, $fresh, $section, $mode, $options)
returns list of checks as flat list.
=cut
sub get_services_checks {
my($c, $backend, $hostname, $hostobj, $agenttype, $password, $fresh, $section, $mode) = @_;
my($c, $backend, $hostname, $hostobj, $agenttype, $password, $fresh, $section, $mode, $options) = @_;
my $checks = [];
return($checks) unless $hostname;

Expand All @@ -121,7 +121,7 @@ sub get_services_checks {
confess("no peer found by name: ".$backend) unless $peer;
confess("no remotekey") unless $peer->remotekey();
confess("need agenttype") unless $agenttype;
my @res = $c->db->rpc($backend, __PACKAGE__."::get_services_checks", [$c, $peer->remotekey(), $hostname, undef, $agenttype, $password, $fresh, $section, $mode], 1);
my @res = $c->db->rpc($backend, __PACKAGE__."::get_services_checks", [$c, $peer->remotekey(), $hostname, undef, $agenttype, $password, $fresh, $section, $mode, $options], 1);
return($res[0]);
}

Expand All @@ -140,7 +140,7 @@ sub get_services_checks {
$password = $password || $c->config->{'Thruk::Agents'}->{lc($type)}->{'default_password'};

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

return($checks);
Expand Down

0 comments on commit 4141654

Please sign in to comment.