(plugin) apps::backup::rubrik::restapi - add mode jobs (#4475)

This commit is contained in:
qgarnier 2023-06-27 10:12:53 +02:00 committed by GitHub
parent fecdbf1404
commit 6143363aac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 585 additions and 38 deletions

View File

@ -287,48 +287,91 @@ sub credentials {
return $creds;
}
sub request_api_paginate {
my ($self, %options) = @_;
my $endpoint = $options{endpoint};
if ($endpoint !~ /^\/api/) {
$endpoint = '/api/internal' . $endpoint;
}
my $full_url;
my $items = [];
$options{get_param} = [] if (!defined($options{get_param}));
my $get_param = $options{get_param};
while (1) {
my $content = $self->{http}->request(
method => $options{method},
full_url => $full_url,
url_path => $endpoint,
get_param => $get_param,
%{$options{creds}}
);
if (!defined($content) || $content eq '') {
$self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
$self->{output}->option_exit();
}
my $decoded;
eval {
$decoded = JSON::XS->new->allow_nonref(1)->utf8->decode($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
$self->{output}->option_exit();
}
return $decoded if (ref($decoded) ne 'HASH');
return $decoded if (!defined($decoded->{hasMore}) || !defined($options{label}));
push @$items, @{$decoded->{ $options{label} }};
last if ($decoded->{hasMore} !~ /true|1/i);
if (defined($decoded->{cursor})) {
$get_param = [@{$options{get_param}}, 'cursor=' . $decoded->{cursor}];
} elsif (defined($decoded->{afterId})) {
$get_param = [@{$options{get_param}}, 'after_id=' . $decoded->{afterId}];
} elsif (defined($decoded->{links}->{next})) {
$full_url = $decoded->{links}->{next}->{href};
}
}
return $items;
}
sub request_api {
my ($self, %options) = @_;
$self->settings();
my $creds = $self->credentials();
my ($content) = $self->{http}->request(
my $result = $self->request_api_paginate(
label => $options{label},
method => 'GET',
url_path => '/api/internal' . $options{endpoint},
endpoint => $options{endpoint},
get_param => $options{get_param},
%$creds
creds => $creds
);
# Maybe token is invalid. so we retry
if (defined($self->{token}) && $self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
$self->clean_token();
$creds = $self->credentials();
$content = $self->{http}->request(
$creds->{unknown_status} = $self->{unknown_http_status};
$creds->{warning_status} = $self->{warning_status};
$creds->{critical_http_status} = $self->{critical_http_status};
$result = $self->request_api_paginate(
label => $options{label},
method => 'GET',
url_path => '/api/internal' . $options{endpoint},
endpoint => $options{endpoint},
get_param => $options{get_param},
%$creds,
unknown_status => $self->{unknown_http_status},
warning_status => $self->{warning_http_status},
critical_status => $self->{critical_http_status}
creds => $creds
);
}
if (!defined($content) || $content eq '') {
$self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
$self->{output}->option_exit();
}
my $decoded;
eval {
$decoded = JSON::XS->new->allow_nonref(1)->utf8->decode($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
$self->{output}->option_exit();
}
return $decoded;
return $result;
}
1;

View File

@ -72,9 +72,12 @@ sub new {
sub manage_selection {
my ($self, %options) = @_;
my $reports = $options{custom}->request_api(endpoint => '/report');
my $reports = $options{custom}->request_api(
endpoint => '/report',
label => 'data'
);
my $report_id;
foreach (@{$reports->{data}}) {
foreach (@$reports) {
if ($_->{name} eq 'SLA Compliance Summary') {
$report_id = $_->{id};
last;

View File

@ -0,0 +1,379 @@
#
# Copyright 2023 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::backup::rubrik::restapi::mode::jobs;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use Digest::MD5;
use DateTime;
use POSIX;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
use centreon::plugins::misc;
use centreon::plugins::statefile;
my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 };
my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' };
sub custom_last_exec_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
nlabel => $self->{nlabel} . '.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} },
instances => $self->{result_values}->{name},
unit => $self->{instance_mode}->{option_results}->{unit},
value => $self->{result_values}->{lastExecSeconds} >= 0 ? floor($self->{result_values}->{lastExecSeconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }) : $self->{result_values}->{lastExecSeconds},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
min => 0
);
}
sub custom_last_exec_threshold {
my ($self, %options) = @_;
return $self->{perfdata}->threshold_check(
value => $self->{result_values}->{lastExecSeconds} >= 0 ? floor($self->{result_values}->{lastExecSeconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }) : $self->{result_values}->{lastExecSeconds},
threshold => [
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
{ label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
{ label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
]
);
}
sub custom_duration_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
nlabel => $self->{nlabel} . '.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} },
instances => $self->{result_values}->{name},
unit => $self->{instance_mode}->{option_results}->{unit},
value => floor($self->{result_values}->{durationSeconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
min => 0
);
}
sub custom_duration_threshold {
my ($self, %options) = @_;
return $self->{perfdata}->threshold_check(
value => floor($self->{result_values}->{durationSeconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
threshold => [
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
{ label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
{ label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
]
);
}
sub job_long_output {
my ($self, %options) = @_;
return sprintf(
"checking job '%s' [type: %s]",
$options{instance_value}->{name},
$options{instance_value}->{jobType}
);
}
sub prefix_job_output {
my ($self, %options) = @_;
return sprintf(
"job '%s' [type: %s] ",
$options{instance_value}->{name},
$options{instance_value}->{jobType}
);
}
sub prefix_global_output {
my ($self, %options) = @_;
return 'Number of jobs ';
}
sub prefix_execution_output {
my ($self, %options) = @_;
return sprintf(
"last execution started: %s ",
$options{instance_value}->{started}
);
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
{
name => 'jobs', type => 3, cb_prefix_output => 'prefix_job_output', cb_long_output => 'job_long_output', indent_long_output => ' ', message_multiple => 'All jobs are ok',
group => [
{ name => 'failed', type => 0 },
{ name => 'timers', type => 0, skipped_code => { -10 => 1 } },
{ name => 'executions', type => 1, cb_prefix_output => 'prefix_execution_output', message_multiple => 'executions are ok', display_long => 1, skipped_code => { -10 => 1 } },
]
}
];
$self->{maps_counters}->{global} = [
{ label => 'jobs-executions-detected', display_ok => 0, nlabel => 'jobs.executions.detected.count', set => {
key_values => [ { name => 'detected' } ],
output_template => 'executions detected: %s',
perfdatas => [
{ template => '%s', min => 0 }
]
}
}
];
$self->{maps_counters}->{failed} = [
{ label => 'job-executions-failed-prct', nlabel => 'job.executions.failed.percentage', set => {
key_values => [ { name => 'failedPrct' } ],
output_template => 'number of failed executions: %.2f %%',
perfdatas => [
{ template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1 }
]
}
}
];
$self->{maps_counters}->{timers} = [
{ label => 'job-execution-last', nlabel => 'job.execution.last', set => {
key_values => [ { name => 'lastExecSeconds' }, { name => 'lastExecHuman' }, { name => 'name' } ],
output_template => 'last execution %s',
output_use => 'lastExecHuman',
closure_custom_perfdata => $self->can('custom_last_exec_perfdata'),
closure_custom_threshold_check => $self->can('custom_last_exec_threshold')
}
},
{ label => 'job-running-duration', nlabel => 'job.running.duration', set => {
key_values => [ { name => 'durationSeconds' }, { name => 'durationHuman' }, { name => 'name' } ],
output_template => 'running duration %s',
output_use => 'durationHuman',
closure_custom_perfdata => $self->can('custom_duration_perfdata'),
closure_custom_threshold_check => $self->can('custom_duration_threshold')
}
}
];
$self->{maps_counters}->{executions} = [
{
label => 'execution-status',
type => 2,
critical_default => '%{status} =~ /failure/i',
set => {
key_values => [
{ name => 'status' }, { name => 'jobName' }
],
output_template => 'status: %s',
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
'filter-job-id:s' => { name => 'filter_job_id' },
'filter-job-name:s' => { name => 'filter_job_name' },
'filter-job-type:s' => { name => 'filter_job_type' },
'filter-location-name:s' => { name => 'filter_location_name' },
'unit:s' => { name => 'unit', default => 's' }
});
$self->{cache_exec} = centreon::plugins::statefile->new(%options);
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) {
$self->{option_results}->{unit} = 's';
}
$self->{cache_exec}->check_options(option_results => $self->{option_results}, default_format => 'json');
}
sub manage_selection {
my ($self, %options) = @_;
my $jobs_exec = $options{custom}->request_api(
endpoint => '/api/v1/job_monitoring',
label => 'jobMonitoringInfoList'
);
$self->{cache_exec}->read(statefile => 'rubrik_' . $self->{mode} . '_' .
Digest::MD5::md5_hex(
(defined($self->{option_results}->{filter_job_id}) ? $self->{option_results}->{filter_job_id} : '') . '_' .
(defined($self->{option_results}->{filter_job_name}) ? $self->{option_results}->{filter_job_name} : '') . '_' .
(defined($self->{option_results}->{filter_job_type}) ? $self->{option_results}->{filter_job_type} : '')
)
);
my $ctime = time();
my $last_exec_times = $self->{cache_exec}->get(name => 'jobs');
$last_exec_times = {} if (!defined($last_exec_times));
$self->{global} = { detected => 0 };
$self->{jobs} = {};
foreach my $job_exec (@$jobs_exec) {
next if (defined($self->{option_results}->{filter_job_id}) && $self->{option_results}->{filter_job_id} ne '' &&
$job_exec->{objectId} !~ /$self->{option_results}->{filter_job_id}/);
next if (defined($self->{option_results}->{filter_job_name}) && $self->{option_results}->{filter_job_name} ne '' &&
$job_exec->{objectName} !~ /$self->{option_results}->{filter_job_name}/);
next if (defined($self->{option_results}->{filter_job_type}) && $self->{option_results}->{filter_job_type} ne '' &&
$job_exec->{jobType} !~ /$self->{option_results}->{filter_job_type}/i);
next if (defined($self->{option_results}->{filter_location_name}) && $self->{option_results}->{filter_location_name} ne '' &&
$job_exec->{locationName} !~ /$self->{option_results}->{filter_location_name}/);
if (!defined($self->{jobs}->{ $job_exec->{objectId} })) {
$self->{jobs}->{ $job_exec->{objectId} } = {
name => $job_exec->{objectName},
jobType => lc($job_exec->{jobType}),
timers => {},
executions => {}
};
}
my ($last_exec, $older_running_exec);
my ($failed, $total) = (0, 0);
foreach (@$jobs_exec) {
next if ($_->{objectId} ne $job_exec->{objectId});
if (!defined($_->{endTime}) && $_->{jobStatus} =~ /Active/i) {
$older_running_exec = $_;
}
if (!defined($last_exec) && $_->{jobStatus} !~ /Scheduled/i) {
$last_exec = $_;
}
$self->{global}->{detected}++;
# Failure, Scheduled, Success, SuccessfulWithWarnings, Active, Canceled
$failed++ if ($_->{jobStatus} =~ /Failure/i);
$total++;
}
$self->{jobs}->{ $job_exec->{objectId} }->{failed} = {
failedPrct => $total > 0 ? $failed * 100 / $total : 0
};
if (defined($last_exec)) {
$self->{jobs}->{ $job_exec->{objectId} }->{executions}->{last} = {
jobName => $job_exec->{objectName},
started => $last_exec->{startTime},
status => $last_exec->{jobStatus}
};
$last_exec->{startTime} =~ /^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/;
my $dt = DateTime->new(year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6);
$last_exec_times->{ $job_exec->{objectId} } = $dt->epoch();
}
$self->{jobs}->{ $job_exec->{objectId} }->{timers} = {
name => $job_exec->{objectName},
lastExecSeconds => defined($last_exec_times->{ $job_exec->{objectId} }) ? $ctime - $last_exec_times->{ $job_exec->{objectId} } : -1,
lastExecHuman => 'never'
};
if (defined($last_exec_times->{ $job_exec->{objectId} })) {
$self->{jobs}->{ $job_exec->{objectId} }->{timers}->{lastExecHuman} = centreon::plugins::misc::change_seconds(value => $ctime - $last_exec_times->{ $job_exec->{objectId} });
}
if (defined($older_running_exec)) {
$older_running_exec->{startTime} =~ /^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/;
my $dt = DateTime->new(year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6);
my $duration = $ctime - $dt->epoch();
$self->{jobs}->{ $job_exec->{objectId} }->{timers}->{durationSeconds} = $duration;
$self->{jobs}->{ $job_exec->{objectId} }->{timers}->{durationHuman} = centreon::plugins::misc::change_seconds(value => $duration);
}
}
$self->{cache_exec}->write(data => {
jobs => $last_exec_times
});
}
1;
__END__
=head1 MODE
Check jobs.
=over 8
=item B<--filter-job-id>
Filter jobs by job ID.
=item B<--filter-job-name>
Filter jobs by job name.
=item B<--filter-job-type>
Filter jobs by job type.
=item B<--filter-location-name>
Filter jobs by location name.
=item B<--unit>
Select the unit for last execution time threshold. May be 's' for seconds, 'm' for minutes,
'h' for hours, 'd' for days, 'w' for weeks. Default is seconds.
=item B<--unknown-execution-status>
Set unknown threshold for last job execution status.
You can use the following variables: %{status}, %{jobName}
=item B<--warning-execution-status>
Set warning threshold for last job execution status.
You can use the following variables: %{status}, %{jobName}
=item B<--critical-execution-status>
Set critical threshold for last job execution status (Default: %{status} =~ /Failure/i).
You can use the following variables: %{status}, %{jobName}
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'jobs-executions-detected', 'job-executions-failed-prct',
'job-execution-last', 'job-running-duration'.
=back
=cut

View File

@ -0,0 +1,113 @@
#
# Copyright 2023 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::backup::rubrik::restapi::mode::listjobs;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $jobs = $options{custom}->request_api(
endpoint => '/api/v1/job_monitoring',
label => 'jobMonitoringInfoList'
);
my $results = {};
foreach (@$jobs) {
$results->{ $_->{objectId} } = $_;
}
return $results;
}
sub run {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (values %$results) {
$self->{output}->output_add(
long_msg => sprintf(
'[jobId: %s][jobName: %s][jobType: %s][locationName: %s]',
$_->{objectId},
$_->{objectName},
$_->{jobType},
$_->{locationName}
)
);
}
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List jobs:'
);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['jobId', 'jobName', 'jobType', 'locationName']);
}
sub disco_show {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (values %$results) {
$self->{output}->add_disco_entry(
jobId => $_->{objectId},
jobName => $_->{objectName},
jobType => $_->{jobType},
locationName => $_->{locationName}
);
}
}
1;
__END__
=head1 MODE
List jobs.
=over 8
=back
=cut

View File

@ -124,8 +124,13 @@ sub check_options {
sub manage_selection {
my ($self, %options) = @_;
my $name = $options{custom}->request_api(endpoint => '/cluster/' . $self->{option_results}->{cluster_id} . '/name');
my $nodes = $options{custom}->request_api(endpoint => '/cluster/' . $self->{option_results}->{cluster_id} . '/node');
my $name = $options{custom}->request_api(
endpoint => '/cluster/' . $self->{option_results}->{cluster_id} . '/name'
);
my $nodes = $options{custom}->request_api(
endpoint => '/cluster/' . $self->{option_results}->{cluster_id} . '/node',
label => 'data'
);
$self->{clusters} = {
$name => {
@ -137,7 +142,7 @@ sub manage_selection {
nodes => {}
}
};
foreach (@{$nodes->{data}}) {
foreach (@$nodes) {
next if (defined($self->{option_results}->{filter_node_id}) && $self->{option_results}->{filter_node_id} ne '' &&
$_->{id} !~ /$self->{option_results}->{filter_node_id}/);

View File

@ -80,9 +80,12 @@ sub new {
sub manage_selection {
my ($self, %options) = @_;
my $reports = $options{custom}->request_api(endpoint => '/report');
my $reports = $options{custom}->request_api(
endpoint => '/report',
label => 'data'
);
my $report_id;
foreach (@{$reports->{data}}) {
foreach (@$reports) {
if ($_->{name} eq 'Protection Tasks Details') {
$report_id = $_->{id};
last;

View File

@ -29,14 +29,15 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '0.1';
$self->{modes} = {
'cluster' => 'apps::backup::rubrik::restapi::mode::cluster',
'compliance' => 'apps::backup::rubrik::restapi::mode::compliance',
'disks' => 'apps::backup::rubrik::restapi::mode::disks',
'nodes' => 'apps::backup::rubrik::restapi::mode::nodes',
'storage' => 'apps::backup::rubrik::restapi::mode::storage',
'tasks' => 'apps::backup::rubrik::restapi::mode::tasks'
'cluster' => 'apps::backup::rubrik::restapi::mode::cluster',
'compliance' => 'apps::backup::rubrik::restapi::mode::compliance',
'disks' => 'apps::backup::rubrik::restapi::mode::disks',
'jobs' => 'apps::backup::rubrik::restapi::mode::jobs',
'list-jobs' => 'apps::backup::rubrik::restapi::mode::listjobs',
'nodes' => 'apps::backup::rubrik::restapi::mode::nodes',
'storage' => 'apps::backup::rubrik::restapi::mode::storage',
'tasks' => 'apps::backup::rubrik::restapi::mode::tasks'
};
$self->{custom_modes}->{api} = 'apps::backup::rubrik::restapi::custom::api';