| 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 network::aruba::orchestrator::restapi::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 |
'resource-type:s' => { name => 'resource_type' }, |
| 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 |
if (!defined($self->{option_results}->{resource_type}) || $self->{option_results}->{resource_type} eq '') { |
| 47 |
$self->{option_results}->{resource_type} = 'appliance'; |
| 48 |
} |
| 49 |
if ($self->{option_results}->{resource_type} !~ /^appliance$/) { |
| 50 |
$self->{output}->add_option_msg(short_msg => 'unknown resource type'); |
| 51 |
$self->{output}->option_exit(); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
sub get_groups { |
| 56 |
my ($self, %options) = @_; |
| 57 |
|
| 58 |
my $results = $options{custom}->request_api( |
| 59 |
endpoint => '/gms/group' |
| 60 |
); |
| 61 |
my $groups = {}; |
| 62 |
foreach (@$results) { |
| 63 |
$groups->{ $_->{id} } = { name => $_->{name}, parentId => $_->{parentId} }; |
| 64 |
} |
| 65 |
|
| 66 |
return $groups; |
| 67 |
} |
| 68 |
|
| 69 |
sub get_group { |
| 70 |
my ($self, %options) = @_; |
| 71 |
|
| 72 |
my @groups = (); |
| 73 |
my $groupId = $options{groupId}; |
| 74 |
while (defined($groupId)) { |
| 75 |
unshift(@groups, $options{groups}->{$groupId}->{name}); |
| 76 |
$groupId = $options{groups}->{$groupId}->{parentId}; |
| 77 |
} |
| 78 |
|
| 79 |
return join(' > ', @groups); |
| 80 |
} |
| 81 |
|
| 82 |
sub discovery_appliance { |
| 83 |
my ($self, %options) = @_; |
| 84 |
|
| 85 |
my $appliances = $options{custom}->request_api(endpoint => '/appliance'); |
| 86 |
my $groups = $self->get_groups(custom => $options{custom}); |
| 87 |
|
| 88 |
my $map_state = { |
| 89 |
0 => 'unknown', 1 => 'normal', 2 => 'unreachable', |
| 90 |
3 => 'unsupportedVersion', 4 => 'outOfSynchronization', 5 => 'synchronizationInProgress' |
| 91 |
}; |
| 92 |
|
| 93 |
my $disco_data = []; |
| 94 |
foreach my $appliance (@$appliances) { |
| 95 |
my $node = {}; |
| 96 |
$node->{uuid} = $appliance->{uuid}; |
| 97 |
$node->{serial} = $appliance->{serial}; |
| 98 |
$node->{hostname} = $appliance->{hostName}; |
| 99 |
$node->{site} = $appliance->{site}; |
| 100 |
$node->{model} = $appliance->{model}; |
| 101 |
$node->{platform} = $appliance->{platform}; |
| 102 |
$node->{ip} = $appliance->{ip}; |
| 103 |
$node->{state} = $map_state->{ $appliance->{state} }; |
| 104 |
$node->{group} = $self->get_group(groups => $groups, groupId => $appliance->{groupId}); |
| 105 |
|
| 106 |
push @$disco_data, $node; |
| 107 |
} |
| 108 |
|
| 109 |
return $disco_data; |
| 110 |
} |
| 111 |
|
| 112 |
sub run { |
| 113 |
my ($self, %options) = @_; |
| 114 |
|
| 115 |
my $disco_stats; |
| 116 |
$disco_stats->{start_time} = time(); |
| 117 |
|
| 118 |
my $results = []; |
| 119 |
if ($self->{option_results}->{resource_type} eq 'appliance') { |
| 120 |
$results = $self->discovery_appliance( |
| 121 |
custom => $options{custom} |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
$disco_stats->{end_time} = time(); |
| 126 |
$disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time}; |
| 127 |
$disco_stats->{discovered_items} = scalar(@$results); |
| 128 |
$disco_stats->{results} = $results; |
| 129 |
|
| 130 |
my $encoded_data; |
| 131 |
eval { |
| 132 |
if (defined($self->{option_results}->{prettify})) { |
| 133 |
$encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats); |
| 134 |
} else { |
| 135 |
$encoded_data = JSON::XS->new->utf8->encode($disco_stats); |
| 136 |
} |
| 137 |
}; |
| 138 |
if ($@) { |
| 139 |
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}'; |
| 140 |
} |
| 141 |
|
| 142 |
$self->{output}->output_add(short_msg => $encoded_data); |
| 143 |
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1); |
| 144 |
$self->{output}->exit(); |
| 145 |
} |
| 146 |
|
| 147 |
1; |
| 148 |
|
| 149 |
__END__ |
| 150 |
|
| 151 |
=head1 MODE |
| 152 |
|
| 153 |
Resources discovery. |
| 154 |
|
| 155 |
=over 8 |
| 156 |
|
| 157 |
=item B<--resource-type> |
| 158 |
|
| 159 |
Choose the type of resources to discover (can be: 'appliance'). |
| 160 |
|
| 161 |
=back |
| 162 |
|
| 163 |
=cut |