| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package apps::automation::ansible::tower::mode::discovery; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::mode); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use JSON::XS; |
| 28 |
|
| 29 |
sub new { |
| 30 |
my ($class, %options) = @_; |
| 31 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 32 |
bless $self, $class; |
| 33 |
|
| 34 |
$options{options}->add_options(arguments => { |
| 35 |
'group' => { name => 'group' }, |
| 36 |
'inventory' => { name => 'inventory' }, |
| 37 |
'prettify' => { name => 'prettify' } |
| 38 |
}); |
| 39 |
|
| 40 |
return $self; |
| 41 |
} |
| 42 |
|
| 43 |
sub check_options { |
| 44 |
my ($self, %options) = @_; |
| 45 |
$self->SUPER::init(%options); |
| 46 |
} |
| 47 |
|
| 48 |
sub run { |
| 49 |
my ($self, %options) = @_; |
| 50 |
|
| 51 |
my @disco_data; |
| 52 |
my $disco_stats; |
| 53 |
|
| 54 |
$disco_stats->{start_time} = time(); |
| 55 |
|
| 56 |
my $hosts = $options{custom}->tower_list_hosts( |
| 57 |
group => $self->{option_results}->{group}, |
| 58 |
inventory => $self->{option_results}->{inventory} |
| 59 |
); |
| 60 |
|
| 61 |
$disco_stats->{end_time} = time(); |
| 62 |
$disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time}; |
| 63 |
|
| 64 |
foreach my $host (@$hosts) { |
| 65 |
my %host; |
| 66 |
$host{name} = $host->{name}; |
| 67 |
$host{id} = $host->{id}; |
| 68 |
$host{description} = $host->{description}; |
| 69 |
$host{type} = $host->{type}; |
| 70 |
$host{inventory_id} = $host->{inventory}; |
| 71 |
$host{inventory_name} = $host->{summary_fields}->{inventory}->{name}; |
| 72 |
$host{groups} = $host->{summary_fields}->{groups}->{results}; |
| 73 |
$host{enabled} = $host->{enabled}; |
| 74 |
# Get the ansible host IP address if available |
| 75 |
eval { |
| 76 |
my $json = decode_json($host->{variables}); |
| 77 |
$host{ansible_host} = $json->{ansible_host} || ''; |
| 78 |
}; |
| 79 |
if ($@) { |
| 80 |
$host{ansible_host} = ''; |
| 81 |
} |
| 82 |
push @disco_data, \%host; |
| 83 |
} |
| 84 |
|
| 85 |
$disco_stats->{discovered_items} = @disco_data; |
| 86 |
$disco_stats->{results} = \@disco_data; |
| 87 |
|
| 88 |
my $encoded_data; |
| 89 |
eval { |
| 90 |
if (defined($self->{option_results}->{prettify})) { |
| 91 |
$encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats); |
| 92 |
} else { |
| 93 |
$encoded_data = JSON::XS->new->utf8->encode($disco_stats); |
| 94 |
} |
| 95 |
}; |
| 96 |
if ($@) { |
| 97 |
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}'; |
| 98 |
} |
| 99 |
|
| 100 |
$self->{output}->output_add(short_msg => $encoded_data); |
| 101 |
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1); |
| 102 |
$self->{output}->exit(); |
| 103 |
} |
| 104 |
|
| 105 |
1; |
| 106 |
|
| 107 |
__END__ |
| 108 |
|
| 109 |
=head1 MODE |
| 110 |
|
| 111 |
Resources discovery. |
| 112 |
|
| 113 |
=over 8 |
| 114 |
|
| 115 |
=item B<--group> |
| 116 |
|
| 117 |
Specify host group. |
| 118 |
|
| 119 |
=item B<--inventory> |
| 120 |
|
| 121 |
Specify host inventory. |
| 122 |
|
| 123 |
=item B<--prettify> |
| 124 |
|
| 125 |
Prettify JSON output. |
| 126 |
|
| 127 |
=back |
| 128 |
|
| 129 |
=cut |