Skip to content

Commit

Permalink
enh(gorgone-hostdisco): install missing plugins (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
garnier-quentin authored Mar 24, 2023
1 parent baee2e5 commit df543e3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
25 changes: 8 additions & 17 deletions gorgone/gorgone/modules/centreon/autodiscovery/class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,12 @@ sub launchhostdiscovery {
]
});

# plugins attribute format:
# "plugins": {
# "centreon-plugin-Cloud-Aws-Ec2-Api": 20220727,
# ...
# }

$self->send_internal_action({
action => 'COMMAND',
target => $self->{hdisco_jobs_ids}->{$job_id}->{target},
Expand All @@ -490,7 +496,8 @@ sub launchhostdiscovery {
timeout => $timeout,
metadata => {
job_id => $job_id,
source => 'autodiscovery-host-job-discovery'
source => 'autodiscovery-host-job-discovery',
pkg_install => $self->{hdisco_jobs_ids}->{$job_id}->{plugins}
}
}
]
Expand Down Expand Up @@ -678,22 +685,6 @@ sub discovery_add_host_result {
sub discovery_command_result {
my ($self, %options) = @_;

=pod
use Devel::Size;
print "frame = " . (Devel::Size::total_size($options{frame}) / 1024 / 1024) . "==\n";
my $data = $options{frame}->getData();
print "data = " . (Devel::Size::total_size($data) / 1024 / 1024) . "==\n";
my $frame = $options{frame}->getFrame();
print "frame data = " . (Devel::Size::total_size($frame) / 1024 / 1024) . "==\n";
my $raw = $options{frame}->getRawData();
print "raw data = " . (Devel::Size::total_size($raw) / 1024 / 1024) . "==\n";
return 1;
=cut

my $data = $options{frame}->getData();

return 1 if (!defined($data->{data}->{metadata}->{job_id}));
Expand Down
48 changes: 37 additions & 11 deletions gorgone/gorgone/modules/core/action/class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,18 @@ sub validate_plugins_deb {
sub validate_plugins {
my ($self, %options) = @_;

my ($rv, $message, $content) = gorgone::standard::misc::slurp(file => $options{file});
return (1, $message) if (!$rv);
my ($rv, $message, $content);
my $plugins = $options{plugins};
if (!defined($plugins)) {
($rv, $message, $content) = gorgone::standard::misc::slurp(file => $options{file});
return (1, $message) if (!$rv);

my $plugins;
try {
$plugins = JSON::XS->new->decode($content);
} catch {
return (1, 'cannot decode json');
};
try {
$plugins = JSON::XS->new->decode($content);
} catch {
return (1, 'cannot decode json');
};
}

# nothing to validate. so it's ok, show must go on!! :)
if (ref($plugins) ne 'HASH' || scalar(keys %$plugins) <= 0) {
Expand Down Expand Up @@ -412,7 +415,6 @@ sub action_command {
);

my $errors = 0;

foreach my $command (@{$options{data}->{content}}) {
$self->send_log(
socket => $options{socket_log},
Expand All @@ -425,7 +427,31 @@ sub action_command {
metadata => $command->{metadata}
}
);


# check install pkg
if (defined($command->{metadata}) && defined($command->{metadata}->{pkg_install})) {
my ($rv, $message) = $self->validate_plugins(plugins => $command->{metadata}->{pkg_install});
if ($rv && $self->{paranoid_plugins} == 1) {
$self->{logger}->writeLogError("[action] $message");
$self->send_log(
socket => $options{socket_log},
code => GORGONE_ACTION_FINISH_KO,
token => $options{token},
logging => $options{data}->{logging},
data => {
message => "command execution issue",
command => $command->{command},
metadata => $command->{metadata},
result => {
exit_code => $rv,
stdout => $message
}
}
);
next;
}
}

my $start = time();
my ($error, $stdout, $return_code) = gorgone::standard::misc::backtick(
command => $command->{command},
Expand Down Expand Up @@ -495,7 +521,7 @@ sub action_command {
);
}
}

if ($errors) {
$self->send_log(
socket => $options{socket_log},
Expand Down

0 comments on commit df543e3

Please sign in to comment.