Debian Codegraph @

src/apps/thales/mistral/vs9/restapi/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::thales::mistral::vs9::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} = 'device';
48 }
49 if ($self->{option_results}->{resource_type} !~ /^device|mmc$/) {
50 $self->{output}->add_option_msg(short_msg => 'unknown resource type');
51 $self->{output}->option_exit();
52 }
53 }
54
55 sub discovery_device {
56 my ($self, %options) = @_;
57
58 my $devices = $options{custom}->request_api(
59 endpoint => '/ssIpsecGwHws',
60 get_param => ['projection=gatewayHwData']
61 );
62
63 my $disco_data = [];
64 foreach my $device (@{$devices->{content}}) {
65 my $entry = {};
66 $entry->{id} = $device->{id};
67 $entry->{name} = defined($device->{name}) ? $device->{name} : '';
68 $entry->{serial_number} = $device->{serialNumber};
69 $entry->{firmware_version_current} = $device->{firmwareVersionCurrent};
70 $entry->{firmware_version_other} = $device->{firmwareVersionOther};
71 $entry->{product_name} = $device->{productName};
72 $entry->{platform_model} = $device->{platformModel};
73 $entry->{description} = defined($device->{description}) ? $device->{description} : '';
74
75 $entry->{physical_interfaces} = [];
76 if (defined($device->{physicalInterfaces})) {
77 foreach my $int (@{$device->{physicalInterfaces}}) {
78 push @{$entry->{physical_interfaces}}, {
79 name => $int->{name},
80 physical_address => defined($int->{physicalAddress}) ? $int->{physicalAddress} : '',
81 connection_type => $int->{connectionType},
82 domain => $int->{domain}
83 };
84 }
85 }
86
87 $entry->{gateway_name} = defined($device->{gatewayConf}) ? $device->{gatewayConf}->{name} : '';
88 $entry->{gateway_responder_only} = '';
89 $entry->{gateway_responder_only} = $device->{gatewayConf}->{responderOnly} =~ /true|1/i ? 'yes' : 'no'
90 if (defined($device->{gatewayConf}) && defined($device->{gatewayConf}->{responderOnly}));
91 $entry->{gateway_administration_ip} = defined($device->{gatewayConf}) ? $device->{gatewayConf}->{administrationIp} : '';
92 $entry->{gateway_active} = '';
93 $entry->{gateway_active} = $device->{gatewayConf}->{active} =~ /true|1/i ? 'yes' : 'no' if (defined($device->{gatewayConf}));
94 $entry->{gateway_offline} = '';
95 $entry->{gateway_offline} = $device->{gatewayConf}->{offline} =~ /true|1/i ? 'yes' : 'no' if (defined($device->{gatewayConf}));
96 $entry->{gateway_private_address} = defined($device->{gatewayConf}) ? $device->{gatewayConf}->{privateAddress}->{address} : '';
97 $entry->{gateway_private_address_netmask} = defined($device->{gatewayConf}) ? $device->{gatewayConf}->{privateAddress}->{netmask} : '';
98
99 $entry->{gateway_blackIpRemote_address} = '';
100 $entry->{gateway_blackIpRemote_address_netmask} = '';
101 if (defined($device->{gatewayConf}) && defined($device->{gatewayConf}->{gatewayBlackIpRemote})) {
102 $entry->{gateway_blackIpRemote_address} = $device->{gatewayConf}->{gatewayBlackIpRemote}->{address};
103 $entry->{gateway_blackIpRemote_address_netmask} = $device->{gatewayConf}->{gatewayBlackIpRemote}->{netmask};
104 }
105
106 push @$disco_data, $entry;
107 }
108
109 return $disco_data;
110 }
111
112 sub discovery_mmc {
113 my ($self, %options) = @_;
114
115 my $mmcs = $options{custom}->request_api(
116 endpoint => '/managementCenters',
117 get_param => ['projection=managementCenterSmall']
118 );
119
120 my $disco_data = [];
121 foreach my $mmc (@{$mmcs->{content}}) {
122 my $entry = {};
123 $entry->{id} = $mmc->{id};
124 $entry->{name} = $mmc->{name};
125 $entry->{ip} = $mmc->{ipAddress};
126
127 push @$disco_data, $entry;
128 }
129
130 return $disco_data;
131 }
132
133 sub run {
134 my ($self, %options) = @_;
135
136 my $disco_stats;
137 $disco_stats->{start_time} = time();
138
139 my $results = [];
140 if ($self->{option_results}->{resource_type} eq 'device') {
141 $results = $self->discovery_device(
142 custom => $options{custom}
143 );
144 } elsif ($self->{option_results}->{resource_type} eq 'mmc') {
145 $results = $self->discovery_mmc(
146 custom => $options{custom}
147 );
148 }
149
150 $disco_stats->{end_time} = time();
151 $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
152 $disco_stats->{discovered_items} = scalar(@$results);
153 $disco_stats->{results} = $results;
154
155 my $encoded_data;
156 eval {
157 if (defined($self->{option_results}->{prettify})) {
158 $encoded_data = JSON::XS->new->utf8->canonical(1)->pretty->encode($disco_stats);
159 } else {
160 $encoded_data = JSON::XS->new->utf8->canonical(1)->encode($disco_stats);
161 }
162 };
163 if ($@) {
164 $encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
165 }
166
167 $self->{output}->output_add(short_msg => $encoded_data);
168 $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
169 $self->{output}->exit();
170 }
171
172 1;
173
174 __END__
175
176 =head1 MODE
177
178 Resources discovery.
179
180 =over 8
181
182 =item B<--resource-type>
183
184 Choose the type of resources to discover (can be: 'device', 'mmc').
185
186 =back
187
188 =cut