From 960b1eb167ff89f6633fe4a354f357437fb9206e Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Thu, 30 Jul 2020 10:21:11 +0200 Subject: [PATCH] refacto wip ansible automation --- .../automation/ansible/tower/custom/api.pm | 237 ++++++++++++++++++ .../ansible/tower/custom/towercli.pm | 6 +- .../ansible/tower/mode/dashboard.pm | 170 ------------- .../ansible/tower/mode/discovery.pm | 2 +- .../automation/ansible/tower/mode/hosts.pm | 164 ++++++++++++ ...{inventorystatistics.pm => inventories.pm} | 137 +++++----- .../apps/automation/ansible/tower/plugin.pm | 14 +- 7 files changed, 469 insertions(+), 261 deletions(-) create mode 100644 centreon-plugins/apps/automation/ansible/tower/custom/api.pm delete mode 100644 centreon-plugins/apps/automation/ansible/tower/mode/dashboard.pm create mode 100644 centreon-plugins/apps/automation/ansible/tower/mode/hosts.pm rename centreon-plugins/apps/automation/ansible/tower/mode/{inventorystatistics.pm => inventories.pm} (56%) diff --git a/centreon-plugins/apps/automation/ansible/tower/custom/api.pm b/centreon-plugins/apps/automation/ansible/tower/custom/api.pm new file mode 100644 index 000000000..add5d3648 --- /dev/null +++ b/centreon-plugins/apps/automation/ansible/tower/custom/api.pm @@ -0,0 +1,237 @@ +# +# 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::custom::api; + +use strict; +use warnings; +use centreon::plugins::http; +use JSON::XS; + +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 => { + 'username:s' => { name => 'username' }, + 'password:s' => { name => 'password' }, + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'proto:s' => { name => 'proto' }, + 'timeout:s' => { name => 'timeout' }, + 'api-path:s' => { name => 'api_path' }, + '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 => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{http} = centreon::plugins::http->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->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; + $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 80; + $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'http'; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; + $self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : ''; + $self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : ''; + $self->{api_path} = (defined($self->{option_results}->{api_path})) ? $self->{option_results}->{api_path} : '/api/v2'; + $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->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); + $self->{output}->option_exit(); + } + if ($self->{username} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --username option."); + $self->{output}->option_exit(); + } + if ($self->{password} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --password option."); + $self->{output}->option_exit(); + } + + return 0; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{credentials} = 1; + $self->{option_results}->{basic} = 1; + $self->{option_results}->{username} = $self->{username}; + $self->{option_results}->{password} = $self->{password}; +} + +sub settings { + my ($self, %options) = @_; + + return if (defined($self->{settings_done})); + $self->build_options_for_httplib(); + $self->{http}->add_header(key => 'Accept', value => 'application/json'); + $self->{http}->add_header(key => 'Content-Type', value => 'application/json'); + $self->{http}->set_options(%{$self->{option_results}}); + $self->{settings_done} = 1; +} + +sub get_hostname { + my ($self, %options) = @_; + + return $self->{hostname}; +} + +sub request_api { + my ($self, %options) = @_; + + my $results = []; + my $page = 1; + + $self->settings(); + while (1) { + my $content = $self->{http}->request( + method => defined($options{method}) ? $options{method} : 'GET', + url_path => $self->{api_path} . $options{endpoint} . '?page_size=100&page=' . $page, + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status} + ); + + my $code = $self->{http}->get_code(); + 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->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(); + } + + push @$results, @{$decoded->{results}}; + last if (!defined($decoded->{next})); + } + + return $results; +} + +sub tower_list_hosts { + my ($self, %options) = @_; + + return $self->request_api(endpoint => '/hosts/'); +} + +sub tower_list_inventories { + my ($self, %options) = @_; + + return $self->request_api(endpoint => '/inventories/'); +} + +sub tower_list_projects { + my ($self, %options) = @_; + + return $self->request_api(endpoint => '/projects/'); +} + +1; + +__END__ + +=head1 NAME + +Ansible Tower Rest API + +=head1 REST API OPTIONS + +Ansible Tower Rest API + +=over 8 + +=item B<--hostname> + +Santricity hostname. + +=item B<--port> + +Port used (Default: 80) + +=item B<--proto> + +Specify https if needed (Default: 'http') + +=item B<--username> + +API username. + +=item B<--password> + +API password. + +=item B<--api-path> + +Specify api path (Default: '/api/v2') + +=item B<--timeout> + +Set timeout in seconds (Default: 10). + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/centreon-plugins/apps/automation/ansible/tower/custom/towercli.pm b/centreon-plugins/apps/automation/ansible/tower/custom/towercli.pm index 3541b785c..ecd4df74c 100644 --- a/centreon-plugins/apps/automation/ansible/tower/custom/towercli.pm +++ b/centreon-plugins/apps/automation/ansible/tower/custom/towercli.pm @@ -137,7 +137,7 @@ sub tower_list_hosts { my $cmd_options = $self->tower_list_hosts_set_cmd(%options); my $raw_results = $self->execute(cmd_options => $cmd_options); - return $raw_results; + return $raw_results->{results}; } sub tower_list_inventories_set_cmd { @@ -159,7 +159,7 @@ sub tower_list_inventories { my $cmd_options = $self->tower_list_inventories_set_cmd(%options); my $raw_results = $self->execute(cmd_options => $cmd_options); - return $raw_results; + return $raw_results->{results}; } sub tower_list_projects_set_cmd { @@ -181,7 +181,7 @@ sub tower_list_projects { my $cmd_options = $self->tower_list_projects_set_cmd(%options); my $raw_results = $self->execute(cmd_options => $cmd_options); - return $raw_results; + return $raw_results->{results}; } 1; diff --git a/centreon-plugins/apps/automation/ansible/tower/mode/dashboard.pm b/centreon-plugins/apps/automation/ansible/tower/mode/dashboard.pm deleted file mode 100644 index 1d6f10c6b..000000000 --- a/centreon-plugins/apps/automation/ansible/tower/mode/dashboard.pm +++ /dev/null @@ -1,170 +0,0 @@ -# -# 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::dashboard; - -use base qw(centreon::plugins::templates::counter); - -use strict; -use warnings; - -sub set_counters { - my ($self, %options) = @_; - - $self->{maps_counters_type} = [ - { name => 'global', type => 0, cb_prefix_output => 'prefix_output'}, - ]; - - $self->{maps_counters}->{global} = [ - { label => 'hosts-total', nlabel => 'hosts.total.count', set => { - key_values => [ { name => 'hosts_total' } ], - output_template => 'Hosts Total: %d', - perfdatas => [ - { value => 'hosts_total', template => '%d', min => 0 }, - ], - } - }, - { label => 'hosts-failed', nlabel => 'hosts.failed.count', set => { - key_values => [ { name => 'hosts_failed' },{ name => 'hosts_total' } ], - output_template => 'Hosts Failed: %d', - perfdatas => [ - { value => 'hosts_failed', template => '%d', min => 0, - max => 'hosts_total' }, - ], - } - }, - { label => 'inventories-total', nlabel => 'inventories.total.count', set => { - key_values => [ { name => 'inventories_total' } ], - output_template => 'Inventories Total: %d', - perfdatas => [ - { value => 'inventories_total', template => '%d', min => 0 }, - ], - } - }, - { label => 'inventories-sync-failed', nlabel => 'inventories.sync.failed.count', set => { - key_values => [ { name => 'inventories_sync_failed' }, { name => 'inventories_total' } ], - output_template => 'Inventories Sync Failed: %d', - perfdatas => [ - { value => 'inventories_sync_failed', template => '%d', min => 0, - max => 'inventories_total' }, - ], - } - }, - { label => 'projects-total', nlabel => 'projects.total.count', set => { - key_values => [ { name => 'projects_total' } ], - output_template => 'Projects Total: %d', - perfdatas => [ - { value => 'projects_total', template => '%d', min => 0 }, - ], - } - }, - { label => 'projects-sync-failed', nlabel => 'projects.sync.failed.count', set => { - key_values => [ { name => 'projects_sync_failed' }, { name => 'projects_total' } ], - output_template => 'Projects Sync Failed: %d', - perfdatas => [ - { value => 'projects_sync_failed', template => '%d', min => 0, - max => 'projects_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 => {}); - - return $self; -} - -sub manage_selection { - my ($self, %options) = @_; - - $self->{global} = { - hosts_total => 0, hosts_failed => 0, - inventories_total => 0, inventories_sync_failed => 0, - projects_total => 0, projects_sync_failed => 0 - }; - - my $hosts = $options{custom}->tower_list_hosts(); - $self->{global}->{hosts_total} = $hosts->{count}; - foreach my $host (@{$hosts->{results}}) { - $self->{global}->{hosts_failed}++ if ($host->{has_active_failures}); - } - - my $inventories = $options{custom}->tower_list_inventories(); - $self->{global}->{inventories_total} = $inventories->{count}; - foreach my $inventory (@{$inventories->{results}}) { - $self->{global}->{inventories_sync_failed}++ if ($inventory->{inventory_sources_with_failures} > 0); - } - - my $projects = $options{custom}->tower_list_projects(); - $self->{global}->{projects_total} = $projects->{count}; - foreach my $project (@{$projects->{results}}) { - $self->{global}->{projects_sync_failed}++ if ($project->{status} =~ /failed|canceled/); - } -} - -1; - -__END__ - -=head1 MODE - -Check several counters available through Tower dashboard. - -=over 8 - -=item B<--warning-hosts-*-count> - -Threshold warning. -Can be: 'total', 'failed'. - -=item B<--critical-hosts-*-count> - -Threshold critical. -Can be: 'total', 'failed'. - -=item B<--warning-inventories-*-count> - -Threshold warning. -Can be: 'total', 'sync-failed'. - -=item B<--critical-inventories-*-count> - -Threshold critical. -Can be: 'total', 'sync-failed'. - -=item B<--warning-projects-*-count> - -Threshold warning. -Can be: 'total', 'sync-failed'. - -=item B<--critical-projects-*-count> - -Threshold critical. -Can be: 'total', 'sync-failed'. - -=back - -=cut diff --git a/centreon-plugins/apps/automation/ansible/tower/mode/discovery.pm b/centreon-plugins/apps/automation/ansible/tower/mode/discovery.pm index b187d3705..25c91b470 100644 --- a/centreon-plugins/apps/automation/ansible/tower/mode/discovery.pm +++ b/centreon-plugins/apps/automation/ansible/tower/mode/discovery.pm @@ -61,7 +61,7 @@ sub run { $disco_stats->{end_time} = time(); $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time}; - foreach my $host (@{$hosts->{results}}) { + foreach my $host (@$hosts) { my %host; $host{name} = $host->{name}; $host{id} = $host->{id}; diff --git a/centreon-plugins/apps/automation/ansible/tower/mode/hosts.pm b/centreon-plugins/apps/automation/ansible/tower/mode/hosts.pm new file mode 100644 index 000000000..320145d56 --- /dev/null +++ b/centreon-plugins/apps/automation/ansible/tower/mode/hosts.pm @@ -0,0 +1,164 @@ +# +# 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::hosts; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_prefix_output => 'prefix_output_global' }, + { name => 'hosts', type => 1, cb_prefix_output => 'prefix_output_host', message_multiple => 'All hosts are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'total', nlabel => 'hosts.total.count', set => { + key_values => [ { name => 'total' } ], + output_template => 'total: %d', + perfdatas => [ + { value => 'total', template => '%d', min => 0 } + ] + } + }, + { label => 'failed', nlabel => 'hosts.failed.count', set => { + key_values => [ { name => 'failed' }, { name => 'total' } ], + output_template => 'failed: %d', + perfdatas => [ + { template => '%d', min => 0, max => 'total' } + ] + } + } + ]; + + $self->{maps_counters}->{hosts} = [ + { + label => 'job-status', type => 2, critical_default => '%{last_job_status} !~ /successful/', + 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 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); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'filter-name:s' => { name => 'filter_name' }, + 'display-failed-hosts' => { name => 'display_failed_hosts' } + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{global} = { total => 0, failed => 0 }; + + my $hosts = $options{custom}->tower_list_hosts(); + + $self->{global}->{total} = scalar(@$hosts); + + my $failed_hosts = []; + foreach my $host (@$hosts) { + next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' + && $host->{name} !~ /$self->{option_results}->{filter_name}/); + + $self->{hosts}->{ $host->{id} } = { + display => $host->{name}, + last_job_status => $host->{summary_fields}->{last_job}->{status} + }; + + if ($host->{has_active_failures}) { + $self->{global}->{failed}++; + push @$failed_hosts, $host->{name}; + } + } + + if (defined($self->{option_results}->{display_failed_hosts})) { + $self->{output}->output_add(long_msg => 'Failed hosts list: ' . join(', ', @$failed_hosts)); + } +} + +1; + +__END__ + +=head1 MODE + +Check hosts. + +=over 8 + +=item B<--filter-name> + +Filter host name (Can use regexp). + +=item B<--display-failed-hosts> + +Display failed hosts list in verbose output. + +=item B<--unknown-job-status> + +Set unknown threshold for status. +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} !~ /successful/'). +Can used special variables like: %{last_job_status}, %{display} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'total', 'failed'. + +=back + +=cut diff --git a/centreon-plugins/apps/automation/ansible/tower/mode/inventorystatistics.pm b/centreon-plugins/apps/automation/ansible/tower/mode/inventories.pm similarity index 56% rename from centreon-plugins/apps/automation/ansible/tower/mode/inventorystatistics.pm rename to centreon-plugins/apps/automation/ansible/tower/mode/inventories.pm index 87416e821..0be787ac4 100644 --- a/centreon-plugins/apps/automation/ansible/tower/mode/inventorystatistics.pm +++ b/centreon-plugins/apps/automation/ansible/tower/mode/inventories.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::automation::ansible::tower::mode::inventorystatistics; +package apps::automation::ansible::tower::mode::inventories; use base qw(centreon::plugins::templates::counter); @@ -29,101 +29,91 @@ sub set_counters { my ($self, %options) = @_; $self->{maps_counters_type} = [ - { name => 'global', type => 0, cb_prefix_output => 'prefix_output', cb_init => 'skip_global' }, - { name => 'inventories', type => 1, cb_prefix_output => 'prefix_output_inventories', - message_multiple => 'All inventories statistics are ok' }, + { name => 'global', type => 0, cb_prefix_output => 'prefix_output_global' }, + { name => 'inventories', type => 1, cb_prefix_output => 'prefix_output_inventories', message_multiple => 'All inventories are ok', skipped_code => { -10 => 1 } } ]; $self->{maps_counters}->{global} = [ { label => 'total', nlabel => 'inventories.total.count', set => { key_values => [ { name => 'total' } ], - output_template => 'Total: %d', + output_template => 'total: %d', perfdatas => [ - { value => 'total', template => '%d', min => 0 }, - ], + { value => 'total', template => '%d', min => 0 } + ] } }, { label => 'failed', nlabel => 'inventories.failed.count', set => { key_values => [ { name => 'failed' }, { name => 'total' } ], - output_template => 'Failed: %d', + output_template => 'failed: %d', perfdatas => [ - { value => 'failed', template => '%d', min => 0, - max => 'total' }, - ], + { template => '%d', min => 0, max => 'total' } + ] } - }, + } ]; + $self->{maps_counters}->{inventories} = [ - { label => 'hosts-total', nlabel => 'inventory.hosts.total.count', set => { + { label => 'hosts-total', nlabel => 'inventory.hosts.total.count', display_ok => 0, set => { key_values => [ { name => 'total_hosts' }, { name => 'display' } ], - output_template => 'Hosts total: %d', + output_template => 'hosts total: %d', perfdatas => [ - { value => 'total_hosts', template => '%d', - min => 0, label_extra_instance => 1, instance_use => 'display' }, - ], + { value => 'total_hosts', template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display' } + ] } }, { label => 'hosts-failed', nlabel => 'inventory.hosts.failed.count', set => { - key_values => [ { name => 'hosts_with_active_failures' }, { name => 'total_hosts' }, - { name => 'display' } ], - output_template => 'Hosts failed: %d', - perfdatas => [ - { value => 'hosts_with_active_failures', template => '%d', - min => 0, max => 'total_hosts', label_extra_instance => 1, - instance_use => 'display' }, + key_values => [ + { name => 'hosts_with_active_failures' }, { name => 'total_hosts' }, + { name => 'display' } ], + output_template => 'hosts failed: %d', + perfdatas => [ + { template => '%d', min => 0, max => 'total_hosts', label_extra_instance => 1, instance_use => 'display' } + ] } }, - { label => 'sources-total', nlabel => 'inventory.sources.total.count', set => { + { label => 'sources-total', nlabel => 'inventory.sources.total.count', display_ok => 0, set => { key_values => [ { name => 'total_inventory_sources' }, { name => 'display' } ], - output_template => 'Sources total: %d', + output_template => 'sources total: %d', perfdatas => [ - { value => 'total_inventory_sources', template => '%d', - min => 0, label_extra_instance => 1, instance_use => 'display' }, - ], + { template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display' } + ] } }, { label => 'sources-failed', nlabel => 'inventory.sources.failed.count', set => { - key_values => [ { name => 'inventory_sources_with_failures' }, { name => 'total_inventory_sources' }, - { name => 'display' } ], - output_template => 'Sources failed: %d', - perfdatas => [ - { value => 'inventory_sources_with_failures', template => '%d', - min => 0, max => 'total_inventory_sources', label_extra_instance => 1, - instance_use => 'display' }, + key_values => [ + { name => 'inventory_sources_with_failures' }, { name => 'total_inventory_sources' }, + { name => 'display' } ], + output_template => 'sources failed: %d', + perfdatas => [ + { template => '%d', min => 0, max => 'total_inventory_sources', label_extra_instance => 1, instance_use => 'display' } + ] } }, - { label => 'groups-total', nlabel => 'inventory.groups.total.count', set => { + { label => 'groups-total', nlabel => 'inventory.groups.total.count', display_ok => 0, set => { key_values => [ { name => 'total_groups' }, { name => 'display' } ], - output_template => 'Groups total: %d', + output_template => 'groups total: %d', perfdatas => [ - { value => 'total_groups', template => '%d', - min => 0, label_extra_instance => 1, instance_use => 'display' }, - ], + { template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display' } + ] } }, { label => 'groups-failed', nlabel => 'inventory.groups.failed.count', set => { - key_values => [ { name => 'groups_with_active_failures' }, { name => 'total_groups' }, - { name => 'display' } ], + key_values => [ + { name => 'groups_with_active_failures' }, { name => 'total_groups' }, + { name => 'display' } + ], output_template => 'Groups failed: %d', perfdatas => [ - { value => 'groups_with_active_failures', template => '%d', - min => 0, max => 'total_groups', label_extra_instance => 1, - instance_use => 'display' }, - ], + { template => '%d', min => 0, max => 'total_groups', label_extra_instance => 1, instance_use => 'display' } + ] } - }, + } ]; } -sub skip_global { - my ($self, %options) = @_; - - scalar(keys %{$self->{inventories}}) > 1 ? return(0) : return(1); -} - -sub prefix_output { +sub prefix_output_global { my ($self, %options) = @_; return "Inventories "; @@ -139,9 +129,9 @@ 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-inventory:s' => { name => 'filter_inventory' }, + 'filter-inventory:s' => { name => 'filter_inventory' } }); return $self; @@ -154,20 +144,20 @@ sub manage_selection { my $inventories = $options{custom}->tower_list_inventories(); - $self->{global}->{total} = $inventories->{count}; + $self->{global}->{total} = scalar(@$inventories); - foreach my $inventory (@{$inventories->{results}}) { + 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}} = { + $self->{inventories}->{ $inventory->{id} } = { display => $inventory->{name}, total_hosts => $inventory->{total_hosts}, hosts_with_active_failures => $inventory->{hosts_with_active_failures}, total_inventory_sources => $inventory->{total_inventory_sources}, inventory_sources_with_failures => $inventory->{inventory_sources_with_failures}, total_groups => $inventory->{total_groups}, - groups_with_active_failures => $inventory->{groups_with_active_failures}, + groups_with_active_failures => $inventory->{groups_with_active_failures} }; $self->{global}->{failed}++ if ($inventory->{has_active_failures}); @@ -180,7 +170,7 @@ __END__ =head1 MODE -Check inventories statistics. +Check inventories. =over 8 @@ -188,26 +178,11 @@ Check inventories statistics. Filter inventory name (Can use regexp). -=item B<--warning-inventories-*-count> +=item B<--warning-*> B<--critical-*> -Threshold warning. -Can be: 'total', 'failed'. - -=item B<--critical-inventories-*-count> - -Threshold critical. -Can be: 'total', 'failed'. - -=item B<--warning-instance-inventory.*.*.count> - -Threshold warning. -Can be 'hosts', 'sources', 'groups' and 'total', 'failed'. - -=item B<--critical-instance-inventory.*.*.count> - -Threshold critical. -Can be 'hosts', 'sources', 'groups' and 'total', 'failed' -'indexes'. +Thresholds. +Can be: 'total', 'failed', 'hosts-total', 'hosts-failed', +'sources-total', 'sources-failed', 'groups-total', 'groups-failed'. =back diff --git a/centreon-plugins/apps/automation/ansible/tower/plugin.pm b/centreon-plugins/apps/automation/ansible/tower/plugin.pm index d68a6931e..c5f8c75fa 100644 --- a/centreon-plugins/apps/automation/ansible/tower/plugin.pm +++ b/centreon-plugins/apps/automation/ansible/tower/plugin.pm @@ -30,13 +30,15 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - %{$self->{modes}} = ( - 'dashboard' => 'apps::automation::ansible::tower::mode::dashboard', - 'discovery' => 'apps::automation::ansible::tower::mode::discovery', - 'inventory-statistics' => 'apps::automation::ansible::tower::mode::inventorystatistics', - ); + $self->{modes} = { + 'discovery' => 'apps::automation::ansible::tower::mode::discovery', + 'hosts' => 'apps::automation::ansible::tower::mode::hosts', + 'inventories' => 'apps::automation::ansible::tower::mode::inventories' + }; - $self->{custom_modes}{towercli} = 'apps::automation::ansible::tower::custom::towercli'; + $self->{custom_modes}->{api} = 'apps::automation::ansible::tower::custom::api'; + $self->{custom_modes}->{towercli} = 'apps::automation::ansible::tower::custom::towercli'; + return $self; }