Debian Codegraph @

src/apps/automation/ansible/cli/mode/discovery.pm

raw
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::cli::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 'host-pattern:s' => { name => 'host_pattern', default => 'all' },
36 'prettify' => { name => 'prettify' },
37 });
38
39 return $self;
40 }
41
42 sub check_options {
43 my ($self, %options) = @_;
44 $self->SUPER::init(%options);
45
46 $self->{option_results}->{host_pattern} = centreon::plugins::misc::sanitize_command_param(value => $self->{option_results}->{host_pattern});
47 }
48
49 sub run {
50 my ($self, %options) = @_;
51
52 my @disco_data;
53 my $disco_stats;
54
55 $disco_stats->{start_time} = time();
56
57 my $result = $options{custom}->execute_command(
58 command => 'ANSIBLE_LOAD_CALLBACK_PLUGINS=true ANSIBLE_STDOUT_CALLBACK=json ansible',
59 command_options => "'" . $self->{option_results}->{host_pattern} . "' --module-name=setup",
60 );
61
62 $disco_stats->{end_time} = time();
63 $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
64
65 foreach my $play (@{$result->{plays}}) {
66 foreach my $task (@{$play->{tasks}}) {
67 foreach my $host (keys %{$task->{hosts}}) {
68 my %host;
69 $host{ip} = $host;
70 $host{hostname} = $task->{hosts}->{$host}->{ansible_facts}->{ansible_hostname};
71 $host{fqdn} = $task->{hosts}->{$host}->{ansible_facts}->{ansible_fqdn};
72 $host{type} = $task->{hosts}->{$host}->{ansible_facts}->{ansible_system};
73 $host{distribution} = $task->{hosts}->{$host}->{ansible_facts}->{ansible_distribution};
74 $host{all_ips} = $task->{hosts}->{$host}->{ansible_facts}->{ansible_all_ipv4_addresses};
75 push @disco_data, \%host;
76 }
77 }
78 }
79
80 $disco_stats->{discovered_items} = @disco_data;
81 $disco_stats->{results} = \@disco_data;
82
83 my $encoded_data;
84 eval {
85 if (defined($self->{option_results}->{prettify})) {
86 $encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats);
87 } else {
88 $encoded_data = JSON::XS->new->utf8->encode($disco_stats);
89 }
90 };
91 if ($@) {
92 $encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
93 }
94
95 $self->{output}->output_add(short_msg => $encoded_data);
96 $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
97 $self->{output}->exit();
98 }
99
100 1;
101
102 __END__
103
104 =head1 MODE
105
106 Resources discovery.
107
108 Command used: ANSIBLE_LOAD_CALLBACK_PLUGINS=true ANSIBLE_STDOUT_CALLBACK=json ansible %(host_pattern) --module-name=setup
109
110 =over 8
111
112 =item B<--host-pattern>
113
114 Specify host pattern to look for (default: 'all').
115
116 =item B<--prettify>
117
118 Prettify JSON output.
119
120 =back
121
122 =cut