(plugin) apps::controlm::restapi - new (#4266)

This commit is contained in:
qgarnier 2023-03-08 10:06:58 +00:00 committed by GitHub
parent 9c4e15e1f1
commit 97570afbe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 767 additions and 0 deletions

View File

@ -0,0 +1,5 @@
{
"dependencies": [
"libdatetime-perl"
]
}

View File

@ -0,0 +1,9 @@
{
"pkg_name": "centreon-plugin-Applications-Contoolm-Restapi",
"pkg_summary": "Centreon Plugin to monitor Control-M throught Automation API",
"plugin_name": "centreon_controlm_restapi.pl",
"files": [
"centreon/plugins/script_custom.pm",
"apps/controlm/restapi/"
]
}

View File

@ -0,0 +1,5 @@
{
"dependencies": [
"perl(DateTime)"
]
}

View File

@ -0,0 +1,279 @@
#
# 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::controlm::restapi::custom::api;
use strict;
use warnings;
use centreon::plugins::http;
use centreon::plugins::statefile;
use JSON::XS;
use Digest::MD5 qw(md5_hex);
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
if (!defined($options{output})) {
print "Class Custom: Need to specify 'output' argument.\n";
exit 3;
}
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit();
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments => {
'api-username:s' => { name => 'api_username' },
'api-password:s' => { name => 'api_password' },
'hostname:s' => { name => 'hostname' },
'port:s' => { name => 'port' },
'proto:s' => { name => 'proto' },
'timeout:s' => { name => 'timeout' },
'unknown-http-status:s' => { name => 'unknown_http_status' },
'warning-http-status:s' => { name => 'warning_http_status' },
'critical-http-status:s' => { name => 'critical_http_status' }
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'AUTOMATION API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl');
$self->{cache} = centreon::plugins::statefile->new(%options);
return $self;
}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
}
sub set_defaults {}
sub check_options {
my ($self, %options) = @_;
$self->{option_results}->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
$self->{option_results}->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 8443;
$self->{option_results}->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{option_results}->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30;
$self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
$self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
$self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300';
$self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
$self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
if ($self->{option_results}->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => 'Need to specify --hostname option.');
$self->{output}->option_exit();
}
if ($self->{api_username} eq '') {
$self->{output}->add_option_msg(short_msg => 'Need to specify --api-username option.');
$self->{output}->option_exit();
}
if ($self->{api_password} eq '') {
$self->{output}->add_option_msg(short_msg => 'Need to specify --api-password option.');
$self->{output}->option_exit();
}
$self->{cache}->check_options(option_results => $self->{option_results});
return 0;
}
sub settings {
my ($self, %options) = @_;
return if (defined($self->{settings_done}));
$self->{http}->set_options(%{$self->{option_results}});
$self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{settings_done} = 1;
}
sub get_connection_info {
my ($self, %options) = @_;
return $self->{option_results}->{hostname} . ':' . $self->{option_results}->{port};
}
sub get_token {
my ($self, %options) = @_;
my $has_cache_file = $self->{cache}->read(statefile => 'controlm_' . md5_hex($self->get_connection_info() . '_' . $self->{api_username}));
my $token = $self->{cache}->get(name => 'token');
my $md5_secret_cache = $self->{cache}->get(name => 'md5_secret');
my $md5_secret = md5_hex($self->{api_username} . $self->{api_password});
if ($has_cache_file == 0 ||
!defined($token) ||
(defined($md5_secret_cache) && $md5_secret_cache ne $md5_secret)
) {
my $json_request = {
username => $self->{api_username},
password => $self->{api_password}
};
my $encoded;
eval {
$encoded = encode_json($json_request);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => 'cannot encode json request');
$self->{output}->option_exit();
}
$self->settings();
my $content = $self->{http}->request(
method => 'POST',
url_path => '/automation-api',
query_form_post => $encoded,
unknown_status => $self->{unknown_http_status},
warning_status => $self->{warning_http_status},
critical_status => $self->{critical_http_status},
header => ['Content-Type: application/json']
);
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response");
$self->{output}->option_exit();
}
$token = $decoded->{token};
my $datas = {
updated => time(),
token => $token,
md5_secret => $md5_secret
};
$self->{cache}->write(data => $datas);
}
return $token;
}
sub clean_token {
my ($self, %options) = @_;
my $datas = { updated => time() };
$self->{cache}->write(data => $datas);
}
sub request_api {
my ($self, %options) = @_;
my $get_param = [];
if (defined($options{get_param})) {
$get_param = $options{get_param};
}
push @$get_param, 'limit=10000';
$self->settings();
my $token = $self->get_token();
my ($content) = $self->{http}->request(
url_path => $options{endpoint},
get_param => $get_param,
header => ['Authorization: Bearer ' . $token],
unknown_status => '',
warning_status => '',
critical_status => ''
);
# Maybe token is invalid. so we retry
if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
$self->clean_token();
$token = $self->get_token();
$content = $self->{http}->request(
url_path => $options{endpoint},
get_param => $get_param,
header => ['Authorization: Bearer ' . $token],
unknown_status => $self->{unknown_http_status},
warning_status => $self->{warning_http_status},
critical_status => $self->{critical_http_status}
);
}
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;
}
1;
__END__
=head1 NAME
Control-M Automation API
=head1 AUTOMATION API OPTIONS
Control-M API
=over 8
=item B<--hostname>
Set hostname.
=item B<--port>
Port used (Default: 8443)
=item B<--proto>
Specify https if needed (Default: 'https')
=item B<--api-username>
API username.
=item B<--api-password>
API password.
=item B<--timeout>
Set timeout in seconds (Default: 30).
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,301 @@
#
# 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::controlm::restapi::mode::jobs;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::misc;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
use Digest::MD5;
use DateTime;
sub custom_status_output {
my ($self, %options) = @_;
return 'status: ' . $self->{result_values}->{status};
}
sub custom_long_output {
my ($self, %options) = @_;
return 'started since: ' . centreon::plugins::misc::change_seconds(value => $self->{result_values}->{elapsed});
}
sub custom_failed_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
nlabel => $self->{nlabel},
instances => [$self->{result_values}->{application}, $self->{result_values}->{name}],
value => $self->{result_values}->{failed},
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 prefix_global_output {
my ($self, %options) = @_;
return 'Number of jobs ';
}
sub prefix_job_output {
my ($self, %options) = @_;
return sprintf(
"job '%s/%s' ",
$options{instance_value}->{application},
$options{instance_value}->{name}
);
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', },
{ name => 'jobs', type => 1, cb_prefix_output => 'prefix_job_output', message_multiple => 'All jobs are ok', , skipped_code => { -10 => 1, -11 => 1 } }
];
$self->{maps_counters}->{jobs} = [
{
label => 'status',
type => 2,
critical_default => '%{status} =~ /ended not ok/i',
set => {
key_values => [
{ name => 'status' }, { name => 'name' }, { name => 'application' },
{ name => 'type' }, { name => 'folder' },
],
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
},
{ label => 'long', type => 2, set => {
key_values => [
{ name => 'status' }, { name => 'name' }, { name => 'application' },
{ name => 'type' }, { name => 'folder' }, { name => 'elapsed' }
],
closure_custom_output => $self->can('custom_long_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
},
{ label => 'job-failed', nlabel => 'job.failed.count', display_ok => 0, set => {
key_values => [
{ name => 'failed' }, { name => 'name' }, { name => 'application' }
],
output_template => 'failed: %s',
closure_custom_perfdata => $self->can('custom_failed_perfdata')
}
}
];
$self->{maps_counters}->{global} = [
{ label => 'jobs-succeeded', nlabel => 'jobs.succeeded.count', set => {
key_values => [ { name => 'succeeded' }, { name => 'total' } ],
output_template => 'succeeded: %s',
perfdatas => [
{ template => '%s', min => 0, max => 'total' }
]
}
},
{ label => 'jobs-failed', nlabel => 'jobs.failed.count', set => {
key_values => [ { name => 'failed' }, { name => 'total' } ],
output_template => 'errors: %s',
perfdatas => [
{ template => '%s', min => 0, max => 'total' }
]
}
},
{ label => 'jobs-executing', nlabel => 'jobs.executing.count', set => {
key_values => [ { name => 'running' }, { name => 'total' } ],
output_template => 'executing: %s',
perfdatas => [
{ template => '%s', min => 0, max => 'total' }
]
}
},
{ label => 'jobs-waiting', nlabel => 'jobs.waiting.count', set => {
key_values => [ { name => 'waiting' }, { name => 'total' } ],
output_template => 'waiting: %s',
perfdatas => [
{ template => '%s', min => 0, max => 'total' }
]
}
}
];
}
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-application:s' => { name => 'filter_application' },
'filter-folder:s' => { name => 'filter_folder' },
'filter-type:s' => { name => 'filter_type' },
'filter-name:s' => { name => 'filter_name' },
'timezone:s' => { name => 'timezone' }
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $jobs = $options{custom}->request_api(
endpoint => '/run/jobs/status',
get_param => ['jobname=*', 'application=*']
);
my $current_time = time();
$self->{global} = { total => 0, failed => 0, waiting => 0, succeeded => 0, executing => 0 };
$self->{jobs} = {};
foreach my $job (@{$jobs->{statuses}}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
$job->{name} !~ /$self->{option_results}->{filter_name}/);
next if (defined($self->{option_results}->{filter_folder}) && $self->{option_results}->{filter_folder} ne '' &&
$job->{folder} !~ /$self->{option_results}->{filter_folder}/);
next if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne '' &&
$job->{type} !~ /$self->{option_results}->{filter_type}/);
next if (defined($self->{option_results}->{filter_application}) && $self->{option_results}->{filter_application} ne '' &&
$job->{application} !~ /$self->{option_results}->{filter_application}/);
my $elapsed;
# 20230214050004
if ($job->{status} eq 'Executing' && $job->{startTime} =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/) {
my $tz = {};
if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') {
$tz = centreon::plugins::misc::set_timezone(name => $self->{option_results}->{timezone});
}
my $dt = DateTime->new(
year => $1,
month => $2,
day => $3,
hour => $4,
minute => $5,
second => $6,
%$tz
);
$elapsed = $current_time - $dt->epoch();
}
my $failed = 0;
$self->{global}->{total}++;
if ($job->{status} eq 'Executing' ) {
$self->{global}->{executing}++;
} elsif ($job->{status} =~ /Ended Not OK/i) {
$self->{global}->{failed}++;
$failed = 1;
} elsif ($job->{status} =~ /Ended OK/i) {
$self->{global}->{succeeded}++;
} elsif ($job->{status} =~ /Wait/i) {
# Wait User — waiting for user confirmation.
# Wait Resource — waiting on a resource to be available.
# Wait Host — waiting on an agent or remote host to be available.
# Wait Workload — waiting due to a workload limit.
# Wait Condition — waiting for a condition.
$self->{global}->{waiting}++;
}
$self->{jobs}->{ $job->{jobId} } = {
name => $job->{name},
folder => $job->{folder},
application => $job->{application},
type => $job->{type},
status => lc($job->{status}),
elapsed => $elapsed,
failed => 1
};
}
}
1;
__END__
=head1 MODE
Check jobs.
=over 8
=item B<--filter-counters>
Only display some counters (regexp can be used).
Example: --filter-counters='failed'
=item B<--filter-folder>
Filter jobs by folder name (cannot be a regexp).
=item B<--filter-application>
Filter jobs by application name (cannot be a regexp).
=item B<--filter-type>
Filter jobs by type (cannot be a regexp).
=item B<--filter-name>
Filter jobs by job name (can be a regexp).
=item B<--timezone>
Set date timezone.
Can use format: 'Europe/London' or '+0100'.
=item B<--warning-status>
Set warning threshold for status.
Can used special variables like: %{name}, %{status}, %{application}, %{folder}, %{type}
=item B<--critical-status>
Set critical threshold for status (Default: '%{status} =~ /ended not ok/i').
Can used special variables like: %{name}, %{status}, %{application}, %{folder}, %{type}
=item B<--warning-long>
Set warning threshold for long jobs.
Can used special variables like: %{name}, %{status}, %{elapsed}, %{application}, %{folder}, %{type}
=item B<--critical-long>
Set critical threshold for long jobs.
Can used special variables like: %{name}, %{status}, %{elapsed}, %{application}, %{folder}, %{type}
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'jobs-succeeded', 'jobs-failed', 'jobs-executing', 'jobs-waiting',
'job-failed'.
=back
=cut

View File

@ -0,0 +1,119 @@
#
# 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::controlm::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 => '/run/jobs/status',
get_param => ['jobname=*', 'application=*']
);
my $results = [];
foreach my $job (@{$jobs->{statuses}}) {
push @$results, {
name => $job->{name},
type => $job->{type},
folder => $job->{folder},
application => $job->{application},
ctm => $job->{ctm},
status => lc($job->{status})
};
}
return $results;
}
sub run {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (@$results) {
$self->{output}->output_add(
long_msg => sprintf(
'[name: %s][type: %s][folder: %s][application: %s][ctm: %s][status: %s]',
$_->{name},
$_->{type},
$_->{folder},
$_->{application},
$_->{ctm},
$_->{status}
)
);
}
$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 => ['name', 'type', 'folder', 'application', 'ctm', 'status']);
}
sub disco_show {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (@$results) {
$self->{output}->add_disco_entry(%$_);
}
}
1;
__END__
=head1 MODE
List jobs.
=over 8
=back
=cut

View File

@ -0,0 +1,49 @@
#
# 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::controlm::restapi::plugin;
use strict;
use warnings;
use base qw(centreon::plugins::script_custom);
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{modes} = {
'jobs' => 'apps::controlm::restapi::mode::jobs',
'list-jobs' => 'apps::controlm::restapi::mode::listjobs'
};
$self->{custom_modes}->{api} = 'apps::controlm::restapi::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Control-M through Automation API.
=cut