This commit is contained in:
garnier-quentin 2020-07-30 14:27:06 +02:00
parent 0571d60fb0
commit dce12c0241
8 changed files with 625 additions and 39 deletions

View File

@ -138,9 +138,10 @@ sub request_api {
$self->settings();
while (1) {
my $path = defined($options{force_endpoint}) ? $self->{api_path} . $options{force_endpoint} : $self->{api_path} . $options{endpoint} . '?page_size=100&page=' . $page;
my $content = $self->{http}->request(
method => defined($options{method}) ? $options{method} : 'GET',
url_path => $self->{api_path} . $options{endpoint} . '?page_size=100&page=' . $page,
url_path => $path,
unknown_status => $self->{unknown_http_status},
warning_status => $self->{warning_http_status},
critical_status => $self->{critical_http_status}
@ -186,6 +187,32 @@ sub tower_list_projects {
return $self->request_api(endpoint => '/projects/');
}
sub tower_list_job_templates {
my ($self, %options) = @_;
return $self->request_api(endpoint => '/job_templates/');
}
sub tower_list_schedules {
my ($self, %options) = @_;
my $schedules = $self->request_api(endpoint => '/schedules/');
if (defined($options{add_job_status})) {
for (my $i = 0; $i < scalar(@$schedules); $i++) {
my $job = $self->request_api(force_endpoint => '/schedules/' . $schedules->[$i]->{id} . '/jobs/?order_by=-id&page_size=1');
$schedules->[$i]->{last_job} = $job->[0];
}
}
return $schedules;
}
sub tower_list_unified_jobs {
my ($self, %options) = @_;
return $self->request_api(endpoint => '/unified_jobs/');
}
1;
__END__

View File

@ -184,6 +184,42 @@ sub tower_list_projects {
return $raw_results->{results};
}
sub tower_list_job_templates_set_cmd {
my ($self, %options) = @_;
return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '');
my $cmd_options = "job_template list --insecure --all-pages --format json";
$cmd_options .= " --tower-host '$self->{hostname}'" if (defined($self->{hostname}));
$cmd_options .= " --tower-username '$self->{username}'" if (defined($self->{username}));
$cmd_options .= " --tower-password '$self->{password}'" if (defined($self->{password}));
return $cmd_options;
}
sub tower_list_job_templates {
my ($self, %options) = @_;
my $cmd_options = $self->tower_list_job_templates_set_cmd(%options);
my $raw_results = $self->execute(cmd_options => $cmd_options);
return $raw_results->{results};
}
sub tower_list_schedules {
my ($self, %options) = @_;
$self->{output}->add_option_msg(short_msg => 'method unsupported (try to use --custommode=api)');
$self->{output}->option_exit();
}
sub tower_list_unified_jobs {
my ($self, %options) = @_;
$self->{output}->add_option_msg(short_msg => 'method unsupported (try to use --custommode=api)');
$self->{output}->option_exit();
}
1;
__END__

View File

@ -26,6 +26,18 @@ use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub prefix_output_global {
my ($self, %options) = @_;
return 'Hosts ';
}
sub prefix_output_host {
my ($self, %options) = @_;
return "Host '" . $options{instance_value}->{display} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
@ -55,7 +67,9 @@ sub set_counters {
$self->{maps_counters}->{hosts} = [
{
label => 'job-status', type => 2, critical_default => '%{last_job_status} !~ /successful/',
label => 'job-status', type => 2,
unknown_default => '%{last_job_status} =~ /default/',
critical_default => '%{last_job_status} =~ /failed/',
set => {
key_values => [ { name => 'last_job_status' }, { name => 'display' } ],
output_template => "last job status is '%s'",
@ -66,18 +80,6 @@ sub set_counters {
];
}
sub prefix_output_global {
my ($self, %options) = @_;
return "Hosts ";
}
sub prefix_output_host {
my ($self, %options) = @_;
return "Host '" . $options{instance_value}->{display} . "' ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
@ -93,12 +95,14 @@ sub new {
sub manage_selection {
my ($self, %options) = @_;
$self->{global} = { total => 0, failed => 0 };
my $hosts = $options{custom}->tower_list_hosts();
$self->{global}->{total} = scalar(@$hosts);
$self->{global} = {
total => scalar(@$hosts),
failed => 0
};
$self->{hosts} = {};
my $failed_hosts = [];
foreach my $host (@$hosts) {
@ -141,7 +145,7 @@ Display failed hosts list in verbose output.
=item B<--unknown-job-status>
Set unknown threshold for status.
Set unknown threshold for status (Default: '%{last_job_status} =~ /default/').
Can used special variables like: %{last_job_status}, %{display}
=item B<--warning-job-status>
@ -151,7 +155,7 @@ Can used special variables like: %{last_job_status}, %{display}
=item B<--critical-job-status>
Set critical threshold for status (Default: '%{last_job_status} !~ /successful/').
Set critical threshold for status (Default: '%{last_job_status} =~ /failed/').
Can used special variables like: %{last_job_status}, %{display}
=item B<--warning-*> B<--critical-*>

View File

@ -25,6 +25,18 @@ use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub prefix_output_global {
my ($self, %options) = @_;
return 'Inventories ';
}
sub prefix_output_inventories {
my ($self, %options) = @_;
return "Inventory '" . $options{instance_value}->{display} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
@ -113,18 +125,6 @@ sub set_counters {
];
}
sub prefix_output_global {
my ($self, %options) = @_;
return "Inventories ";
}
sub prefix_output_inventories {
my ($self, %options) = @_;
return "Inventory '" . $options{instance_value}->{display} . "' ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
@ -139,17 +139,15 @@ sub new {
sub manage_selection {
my ($self, %options) = @_;
$self->{global} = { total => 0, failed => 0 };
my $inventories = $options{custom}->tower_list_inventories();
$self->{global}->{total} = scalar(@$inventories);
$self->{global} = { total => 0, failed => 0 };
$self->{inventories} = {};
foreach my $inventory (@$inventories) {
next if (defined($self->{option_results}->{filter_inventory}) && $self->{option_results}->{filter_inventory} ne ''
&& $inventory->{name} !~ /$self->{option_results}->{filter_inventory}/);
$self->{inventories}->{ $inventory->{id} } = {
display => $inventory->{name},
total_hosts => $inventory->{total_hosts},
@ -159,6 +157,7 @@ sub manage_selection {
total_groups => $inventory->{total_groups},
groups_with_active_failures => $inventory->{groups_with_active_failures}
};
$self->{global}->{total}++;
$self->{global}->{failed}++ if ($inventory->{has_active_failures});
}

View File

@ -0,0 +1,167 @@
#
# Copyright 2020 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::automation::ansible::tower::mode::jobs;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::statefile;
sub prefix_output_global {
my ($self, %options) = @_;
return 'Jobs ';
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_output_global' }
];
$self->{maps_counters}->{global} = [
{ label => 'total', nlabel => 'jobs.total.count', set => {
key_values => [ { name => 'total' } ],
output_template => 'total: %d',
perfdatas => [
{ value => 'total', template => '%d', min => 0 }
]
}
}
];
foreach ((['successful', 1], ['failed', 1], ['running', 1], ['canceled', 0], ['pending', 0], ['default', 0])) {
push @{$self->{maps_counters}->{global}}, {
label => $_->[0], nlabel => 'jobs.' . $_->[0] . '.count', display_ok => $_->[1], set => {
key_values => [ { name => $_->[0] }, { name => 'total' } ],
output_template => $_->[0] . ': %d',
perfdatas => [
{ template => '%d', 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-name:s' => { name => 'filter_name' },
'display-failed-jobs' => { name => 'display_failed_jobs' },
'memory' => { name => 'memory' }
});
$self->{statefile_cache} = centreon::plugins::statefile->new(%options);
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
if (defined($self->{option_results}->{memory})) {
$self->{statefile_cache}->check_options(%options);
centreon::plugins::misc::mymodule_load(
output => $self->{output},
module => 'Date::Parse',
error_msg => "Cannot load module 'Date::Parse'."
);
}
}
sub manage_selection {
my ($self, %options) = @_;
my $last_time;
if (defined($self->{option_results}->{memory})) {
$self->{statefile_cache}->read(statefile => 'cache_ansible_tower_' . $self->{mode} . '_' . $options{custom}->get_hostname());
$last_time = $self->{statefile_cache}->get(name => 'last_time');
}
my $jobs = $options{custom}->tower_list_unified_jobs();
$self->{global} = { total => 0, failed => 0, successful => 0, canceled => 0, default => 0, pending => 0, running => 0 };
my $current_time = time();
my $failed_jobs = {};
foreach my $job (@$jobs) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $job->{name} !~ /$self->{option_results}->{filter_name}/);
if (defined($self->{option_results}->{memory}) && defined($job->{finished})) {
my $finished_time = Date::Parse::str2time($job->{finished});
if (!defined($finished_time)) {
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => "Can't parse date '" . $job->{finished} . "'"
);
next;
}
next if (defined($last_time) && $last_time > $finished_time);
}
$self->{global}->{ $job->{status} }++;
$self->{global}->{total}++;
if ($job->{status} eq 'failed') {
$failed_jobs->{ $job->{name} } = 1;
}
}
if (defined($self->{option_results}->{memory})) {
$self->{statefile_cache}->write(data => { last_time => $current_time });
}
if (defined($self->{option_results}->{display_failed_jobs})) {
$self->{output}->output_add(long_msg => 'Failed jobs list: ' . join(', ', keys %$failed_jobs));
}
}
1;
__END__
=head1 MODE
Check jobs.
=over 8
=item B<--filter-name>
Filter job name (Can use regexp).
=item B<--display-failed-jobs>
Display failed jobs list in verbose output.
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'total', 'successful', 'failed', 'running', 'canceled', 'pending', 'default'.
=back
=cut

View File

@ -0,0 +1,161 @@
#
# Copyright 2020 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::automation::ansible::tower::mode::jobtemplates;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub prefix_output_global {
my ($self, %options) = @_;
return 'Job templates ';
}
sub prefix_output_jobtpl {
my ($self, %options) = @_;
return "Job template '" . $options{instance_value}->{display} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_output_global' },
{ name => 'jobtpl', type => 1, cb_prefix_output => 'prefix_output_jobtpl', message_multiple => 'All job templates are ok', skipped_code => { -10 => 1 } }
];
$self->{maps_counters}->{global} = [
{ label => 'total', nlabel => 'jobtemplates.total.count', set => {
key_values => [ { name => 'total' } ],
output_template => 'total: %d',
perfdatas => [
{ value => 'total', template => '%d', min => 0 }
]
}
}
];
foreach ((['successful', 1], ['failed', 1], ['running', 1], ['canceled', 0], ['pending', 0], ['default', 0], ['never', 0])) {
push @{$self->{maps_counters}->{global}}, {
label => $_->[0], nlabel => 'jobtemplates.' . $_->[0] . '.count', display_ok => $_->[1], set => {
key_values => [ { name => $_->[0] }, { name => 'total' } ],
output_template => $_->[0] . ': %d',
perfdatas => [
{ template => '%d', min => 0, max => 'total' }
]
}
};
}
$self->{maps_counters}->{jobtpl} = [
{
label => 'job-status', type => 2,
unknown_default => '%{last_job_status} =~ /default/',
critical_default => '%{last_job_status} =~ /failed/',
set => {
key_values => [ { name => 'last_job_status' }, { name => 'display' } ],
output_template => "last job status is '%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-name:s' => { name => 'filter_name' }
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $jobs = $options{custom}->tower_list_job_templates();
$self->{global} = { total => 0, failed => 0, successful => 0, canceled => 0, default => 0, pending => 0, running => 0, never => 0 };
$self->{jobtpl} = {};
foreach my $job (@$jobs) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $job->{name} !~ /$self->{option_results}->{filter_name}/);
$self->{jobtpl}->{ $job->{id} } = {
display => $job->{name},
last_job_status => defined($job->{summary_fields}->{last_job}->{status}) ? $job->{summary_fields}->{last_job}->{status} : 'never'
};
$self->{global}->{total}++;
if (defined($job->{summary_fields}->{last_job}->{status})) {
$self->{global}->{ $job->{summary_fields}->{last_job}->{status} }++;
} else {
$self->{global}->{never}++;
}
}
}
1;
__END__
=head1 MODE
Check job templates.
=over 8
=item B<--filter-name>
Filter job template name (Can use regexp).
=item B<--unknown-job-status>
Set unknown threshold for status (Default: '%{last_job_status} =~ /default/').
Can used special variables like: %{last_job_status}, %{display}
=item B<--warning-job-status>
Set warning threshold for status.
Can used special variables like: %{last_job_status}, %{display}
=item B<--critical-job-status>
Set critical threshold for status (Default: '%{last_job_status} =~ /failed/').
Can used special variables like: %{last_job_status}, %{display}
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'total', 'successful', 'failed', 'running', 'canceled', 'pending', 'default', 'never'.
=back
=cut

View File

@ -0,0 +1,189 @@
#
# Copyright 2020 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::automation::ansible::tower::mode::schedules;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
use DateTime;
use centreon::plugins::misc;
sub custom_start_output {
my ($self, %options) = @_;
return sprintf(
'last start: %s',
$self->{result_values}->{start_last_time} >= 0 ? centreon::plugins::misc::change_seconds(value => $self->{result_values}->{start_last_time}) : 'never'
);
}
sub prefix_output_global {
my ($self, %options) = @_;
return 'Schedules ';
}
sub prefix_output_schedule {
my ($self, %options) = @_;
return "Schedule '" . $options{instance_value}->{display} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_output_global' },
{ name => 'schedules', type => 1, cb_prefix_output => 'prefix_output_schedule', message_multiple => 'All schedules are ok', skipped_code => { -10 => 1 } }
];
$self->{maps_counters}->{global} = [
{ label => 'total', nlabel => 'schedules.total.count', set => {
key_values => [ { name => 'total' } ],
output_template => 'total: %d',
perfdatas => [
{ value => 'total', template => '%d', min => 0 }
]
}
}
];
foreach ((['successful', 1], ['failed', 1], ['running', 1], ['canceled', 0], ['pending', 0], ['default', 0], ['never', 0])) {
push @{$self->{maps_counters}->{global}}, {
label => $_->[0], nlabel => 'schedules.' . $_->[0] . '.count', display_ok => $_->[1], set => {
key_values => [ { name => $_->[0] }, { name => 'total' } ],
output_template => $_->[0] . ': %d',
perfdatas => [
{ template => '%d', min => 0, max => 'total' }
]
}
};
}
$self->{maps_counters}->{schedules} = [
{
label => 'job-status', type => 2,
unknown_default => '%{last_job_status} =~ /default/',
critical_default => '%{last_job_status} =~ /failed/',
set => {
key_values => [ { name => 'last_job_status' }, { name => 'display' } ],
output_template => "last job status is '%s'",
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
},
{ label => 'start-last-time', nlabel => 'schedule.start.last.time.seconds', set => {
key_values => [ { name => 'start_last_time' }, { name => 'display' } ],
closure_custom_output => $self->can('custom_start_output'),
perfdatas => [
{ template => '%d', min => 0, unit => 's', label_extra_instance => 1, instance_use => 'display' }
]
}
}
];
}
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-name:s' => { name => 'filter_name' }
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $schedules = $options{custom}->tower_list_schedules(add_job_status => 1);
$self->{global} = { total => 0, failed => 0, successful => 0, canceled => 0, default => 0, pending => 0, running => 0, never => 0 };
$self->{schedules} = {};
foreach my $schedule (@$schedules) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $schedule->{name} !~ /$self->{option_results}->{filter_name}/);
my $start_last_time = -1;
if (defined($schedule->{last_job}->{started})) {
#2020-07-30T09:36:28.806283Z
if ($schedule->{last_job}->{started} =~ /^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/) {
my $dt = DateTime->new(year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6);
$start_last_time = time() - $dt->epoch();
}
}
$self->{schedules}->{ $schedule->{id} } = {
display => $schedule->{name},
last_job_status => defined($schedule->{last_job}->{status}) ? $schedule->{last_job}->{status} : 'never',
start_last_time => $start_last_time
};
$self->{global}->{total}++;
if (defined($schedule->{last_job}->{status})) {
$self->{global}->{ $schedule->{last_job}->{status} }++;
} else {
$self->{global}->{never}++;
}
}
}
1;
__END__
=head1 MODE
Check schedules.
=over 8
=item B<--filter-name>
Filter schedule name (Can use regexp).
=item B<--unknown-job-status>
Set unknown threshold for status (Default: '%{last_job_status} =~ /default/').
Can used special variables like: %{last_job_status}, %{display}
=item B<--warning-job-status>
Set warning threshold for status.
Can used special variables like: %{last_job_status}, %{display}
=item B<--critical-job-status>
Set critical threshold for status (Default: '%{last_job_status} =~ /failed/').
Can used special variables like: %{last_job_status}, %{display}
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'total', 'successful', 'failed', 'running', 'canceled', 'pending', 'default', 'never', 'start-last-time' (s).
=back
=cut

View File

@ -31,9 +31,12 @@ sub new {
$self->{version} = '1.0';
$self->{modes} = {
'discovery' => 'apps::automation::ansible::tower::mode::discovery',
'hosts' => 'apps::automation::ansible::tower::mode::hosts',
'inventories' => 'apps::automation::ansible::tower::mode::inventories'
'discovery' => 'apps::automation::ansible::tower::mode::discovery',
'hosts' => 'apps::automation::ansible::tower::mode::hosts',
'inventories' => 'apps::automation::ansible::tower::mode::inventories',
'jobs' => 'apps::automation::ansible::tower::mode::jobs',
'job-templates' => 'apps::automation::ansible::tower::mode::jobtemplates',
'schedules' => 'apps::automation::ansible::tower::mode::schedules'
};
$self->{custom_modes}->{api} = 'apps::automation::ansible::tower::custom::api';