Debian Codegraph @

src/network/aruba/aoscx/restapi/custom/api.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 network::aruba::aoscx::restapi::custom::api;
22
23 use strict;
24 use warnings;
25 use centreon::plugins::http;
26 use centreon::plugins::statefile;
27 use JSON::XS;
28 use Digest::MD5 qw(md5_hex);
29
30 sub new {
31 my ($class, %options) = @_;
32 my $self = {};
33 bless $self, $class;
34
35 if (!defined($options{output})) {
36 print "Class Custom: Need to specify 'output' argument.\n";
37 exit 3;
38 }
39 if (!defined($options{options})) {
40 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
41 $options{output}->option_exit();
42 }
43
44 if (!defined($options{noptions})) {
45 $options{options}->add_options(arguments => {
46 'hostname:s' => { name => 'hostname' },
47 'port:s' => { name => 'port' },
48 'proto:s' => { name => 'proto' },
49 'url-path:s' => { name => 'url_path' },
50 'api-username:s' => { name => 'api_username' },
51 'api-password:s' => { name => 'api_password' },
52 'user-domain:s' => { name => 'user_domain' },
53 'timeout:s' => { name => 'timeout' }
54 });
55 }
56 $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
57
58 $self->{output} = $options{output};
59 $self->{http} = centreon::plugins::http->new(%options);
60 $self->{cache} = centreon::plugins::statefile->new(%options);
61
62 return $self;
63 }
64
65 sub set_options {
66 my ($self, %options) = @_;
67
68 $self->{option_results} = $options{option_results};
69 }
70
71 sub set_defaults {}
72
73 sub check_options {
74 my ($self, %options) = @_;
75
76 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
77 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
78 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
79 $self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/rest/v1';
80 $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
81 $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
82 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30;
83 $self->{user_domain} = (defined($self->{option_results}->{user_domain})) ? $self->{option_results}->{user_domain} : '';
84
85 if ($self->{hostname} eq '') {
86 $self->{output}->add_option_msg(short_msg => 'Need to specify hostname option.');
87 $self->{output}->option_exit();
88 }
89 if ($self->{api_username} eq '') {
90 $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
91 $self->{output}->option_exit();
92 }
93 if ($self->{api_password} eq '') {
94 $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
95 $self->{output}->option_exit();
96 }
97
98 $self->{cache}->check_options(option_results => $self->{option_results});
99 return 0;
100 }
101
102 sub get_connection_infos {
103 my ($self, %options) = @_;
104
105 return $self->{hostname} . '_' . $self->{http}->get_port();
106 }
107
108 sub get_hostname {
109 my ($self, %options) = @_;
110
111 return $self->{hostname};
112 }
113
114 sub get_port {
115 my ($self, %options) = @_;
116
117 return $self->{port};
118 }
119
120 sub json_decode {
121 my ($self, %options) = @_;
122
123 $options{content} =~ s/\r//mg;
124 my $decoded;
125 eval {
126 $decoded = JSON::XS->new->utf8->decode($options{content});
127 };
128 if ($@) {
129 $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
130 $self->{output}->option_exit();
131 }
132
133 return $decoded;
134 }
135
136 sub build_options_for_httplib {
137 my ($self, %options) = @_;
138
139 $self->{option_results}->{hostname} = $self->{hostname};
140 $self->{option_results}->{port} = $self->{port};
141 $self->{option_results}->{proto} = $self->{proto};
142 }
143
144 sub settings {
145 my ($self, %options) = @_;
146
147 $self->build_options_for_httplib();
148 $self->{http}->add_header(key => 'Accept', value => 'application/json');
149 $self->{http}->set_options(%{$self->{option_results}});
150 }
151
152 sub clean_session {
153 my ($self, %options) = @_;
154
155 my $datas = {};
156 $options{statefile}->write(data => $datas);
157 $self->{cookie_id} = undef;
158 $self->{cookie_user} = undef;
159 }
160
161 sub get_session {
162 my ($self, %options) = @_;
163
164 my $has_cache_file = $options{statefile}->read(statefile => 'aruba_aoscx_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username}));
165 my $cookie_id = $options{statefile}->get(name => 'cookie_id');
166 my $cookie_user = $options{statefile}->get(name => 'cookie_user');
167
168 if ($has_cache_file == 0 || !defined($cookie_id)) {
169 my ($content) = $self->{http}->request(
170 method => 'POST',
171 url_path => $self->{url_path} . '/login',
172 post_param => [
173 'username=' . $self->{api_username},
174 'password=' . $self->{api_password}
175 ],
176 warning_status => '', unknown_status => '', critical_status => ''
177 );
178
179 if ($self->{http}->get_code() != 200) {
180 $self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
181 $self->{output}->option_exit();
182 }
183
184 my (@cookies) = $self->{http}->get_header(name => 'Set-Cookie');
185 foreach my $cookie (@cookies) {
186 $cookie_id = $1 if ($cookie =~ /id=(.+?);/);
187 $cookie_user = $1 if ($cookie =~ /user=(.+?);/);
188 }
189
190 if (!defined($cookie_id)) {
191 $self->{output}->add_option_msg(short_msg => "Cannot get token");
192 $self->{output}->option_exit();
193 }
194
195 $options{statefile}->write(data => { cookie_id => $cookie_id, cookie_user => $cookie_user });
196 }
197
198 $self->{cookie_id} = $cookie_id;
199 $self->{cookie_user} = $cookie_user;
200 }
201
202 sub request {
203 my ($self, %options) = @_;
204
205 my $endpoint = $options{full_endpoint};
206 if (!defined($endpoint)) {
207 $endpoint = $self->{url_path} . $options{endpoint};
208 }
209
210 $self->settings();
211 if (!defined($self->{access_token})) {
212 $self->get_session(statefile => $self->{cache});
213 }
214
215 my $content = $self->{http}->request(
216 method => 'GET',
217 url_path => $endpoint,
218 get_param => $options{get_param},
219 header => [
220 'Cookie: ' . $self->{cookie_id},
221 'Cookie: ' . $self->{cookie_user}
222 ],
223 warning_status => '',
224 unknown_status => '',
225 critical_status => ''
226 );
227
228 # Maybe there is an issue with the token. So we retry.
229 if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
230 $self->clean_session(statefile => $self->{cache});
231 $self->get_session(statefile => $self->{cache});
232 $content = $self->{http}->request(
233 url_path => $endpoint,
234 get_param => $options{get_param},
235 header => [
236 'Cookie: ' . $self->{cookie_id},
237 'Cookie: ' . $self->{cookie_user}
238 ],
239 unknown_status => $self->{unknown_http_status},
240 warning_status => $self->{warning_http_status},
241 critical_status => $self->{critical_http_status}
242 );
243 }
244
245 my $decoded = $self->json_decode(content => $content);
246 if (!defined($decoded)) {
247 $self->{output}->add_option_msg(short_msg => 'Error while retrieving data (add --debug option for detailed message)');
248 $self->{output}->option_exit();
249 }
250
251 return $decoded;
252 }
253
254 1;
255
256 __END__
257
258 =head1 NAME
259
260 ArubaOS-CX API
261
262 =head1 SYNOPSIS
263
264 ArubaOS-CX api
265
266 =head1 REST API OPTIONS
267
268 =over 8
269
270 =item B<--hostname>
271
272 API hostname.
273
274 =item B<--url-path>
275
276 API url path (default: '/rest/v1')
277
278 =item B<--port>
279
280 API port (default: 443)
281
282 =item B<--proto>
283
284 Specify https if needed (default: 'https')
285
286 =item B<--api-username>
287
288 Set API username
289
290 =item B<--api-password>
291
292 Set API password
293
294 =item B<--timeout>
295
296 Set HTTP timeout
297
298 =back
299
300 =head1 DESCRIPTION
301
302 B<custom>.
303
304 =cut