Debian Codegraph @

src/storage/purestorage/flashblade/v2/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 storage::purestorage::flashblade::v2::restapi::custom::api;
22
23 use strict;
24 use warnings;
25 use centreon::plugins::http;
26 use centreon::plugins::statefile;
27 use Digest::MD5 qw(md5_hex);
28 use JSON::XS;
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 'api-token:s' => { name => 'api_token' },
47 'hostname:s' => { name => 'hostname' },
48 'port:s' => { name => 'port' },
49 'proto:s' => { name => 'proto' },
50 'timeout:s' => { name => 'timeout' },
51 'api-version:s' => { name => 'api_version' },
52 'unknown-http-status:s' => { name => 'unknown_http_status' },
53 'warning-http-status:s' => { name => 'warning_http_status' },
54 'critical-http-status:s' => { name => 'critical_http_status' }
55 });
56 }
57 $options{options}->add_help(package => __PACKAGE__, sections => 'API OPTIONS', once => 1);
58
59 $self->{output} = $options{output};
60 $self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl');
61 $self->{cache} = centreon::plugins::statefile->new(%options);
62
63 return $self;
64 }
65
66 sub set_options {
67 my ($self, %options) = @_;
68
69 $self->{option_results} = $options{option_results};
70 }
71
72 sub set_defaults {}
73
74 sub check_options {
75 my ($self, %options) = @_;
76
77 $self->{option_results}->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
78 $self->{option_results}->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
79 $self->{option_results}->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
80 $self->{option_results}->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30;
81 $self->{api_token} = (defined($self->{option_results}->{api_token})) ? $self->{option_results}->{api_token} : '';
82 $self->{api_version} = (defined($self->{option_results}->{api_version})) ? $self->{option_results}->{api_version} : '2.4';
83 $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300';
84 $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
85 $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
86
87 if ($self->{option_results}->{hostname} eq '') {
88 $self->{output}->add_option_msg(short_msg => 'Need to specify --hostname option.');
89 $self->{output}->option_exit();
90 }
91 if ($self->{api_token} eq '') {
92 $self->{output}->add_option_msg(short_msg => 'Need to specify --api-token option.');
93 $self->{output}->option_exit();
94 }
95 if ($self->{api_version} eq '') {
96 $self->{output}->add_option_msg(short_msg => 'Need to specify --api-version option.');
97 $self->{output}->option_exit();
98 }
99
100 $self->{cache}->check_options(option_results => $self->{option_results});
101
102 return 0;
103 }
104
105 sub settings {
106 my ($self, %options) = @_;
107
108 return if (defined($self->{settings_done}));
109 $self->{http}->set_options(%{$self->{option_results}});
110 $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
111 $self->{http}->add_header(key => 'Accept', value => 'application/json');
112 $self->{settings_done} = 1;
113 }
114
115 sub get_connection_info {
116 my ($self, %options) = @_;
117
118 return $self->{option_results}->{hostname} . ':' . $self->{option_results}->{port};
119 }
120
121 sub get_token {
122 my ($self, %options) = @_;
123
124 my $has_cache_file = $self->{cache}->read(statefile => 'purestorage_' . md5_hex($self->get_connection_info() . '_' . $self->{api_token}));
125 my $token = $self->{cache}->get(name => 'token');
126 my $md5_secret_cache = $self->{cache}->get(name => 'md5_secret');
127 my $md5_secret = md5_hex($self->{api_token});
128
129 if ($has_cache_file == 0 ||
130 !defined($token) ||
131 (defined($md5_secret_cache) && $md5_secret_cache ne $md5_secret)
132 ) {
133 my ($content) = $self->{http}->request(
134 method => 'POST',
135 url_path => '/api/login',
136 query_form_post => '',
137 header => [
138 'api-token: ' . $self->{api_token}
139 ],
140 unknown_status => $self->{unknown_http_status},
141 warning_status => $self->{warning_http_status},
142 critical_status => $self->{critical_http_status}
143 );
144
145 $token = $self->{http}->get_header(name => 'x-auth-token');
146
147 if (!defined($token) || $token eq '') {
148 $self->{output}->add_option_msg(short_msg => 'Cannot get token');
149 $self->{output}->option_exit();
150 }
151
152 my $datas = {
153 updated => time(),
154 token => $token,
155 md5_secret => $md5_secret
156 };
157 $self->{cache}->write(data => $datas);
158 }
159
160 return $token;
161 }
162
163 sub clean_token {
164 my ($self, %options) = @_;
165
166 my $datas = { updated => time() };
167 $self->{cache}->write(data => $datas);
168 }
169
170 sub request {
171 my ($self, %options) = @_;
172
173 $self->settings();
174 my $token = $self->get_token();
175
176 my $decoded;
177 my $items = [];
178 my $num_items = 0;
179 while (1) {
180 my $get_param = ['limit=1000'];
181 push @$get_param, @{$options{get_param}} if (defined($options{get_param}));
182 push @$get_param, 'continuation_token=' . $decoded->{continuation_token} if (defined($decoded) && defined($decoded->{continuation_token}));
183
184 my ($content) = $self->{http}->request(
185 url_path => '/api/' . $self->{api_version} . $options{endpoint},
186 get_param => $options{get_param},
187 header => ['X-Auth-Token: ' . $token],
188 unknown_status => '',
189 warning_status => '',
190 critical_status => ''
191 );
192
193 # Maybe token is invalid. so we retry
194 if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
195 $self->clean_token();
196 $token = $self->get_token();
197 ($content) = $self->{http}->request(
198 url_path => '/api/' . $self->{api_version} . $options{endpoint},
199 get_param => $options{get_param},
200 header => ['X-Auth-Token: ' . $token],
201 unknown_status => $self->{unknown_http_status},
202 warning_status => $self->{warning_http_status},
203 critical_status => $self->{critical_http_status}
204 );
205 }
206
207 eval {
208 $decoded = decode_json($content);
209 };
210 if ($@) {
211 $self->{output}->add_option_msg(short_msg => "Cannot decode json response");
212 $self->{output}->option_exit();
213 }
214
215 $num_items += scalar(@{$decoded->{items}});
216 push @$items, @{$decoded->{items}};
217
218 last if (
219 !defined($decoded->{total_item_count}) ||
220 $num_items >= $decoded->{total_item_count} ||
221 !defined($decoded->{continuation_token})
222 );
223 }
224
225 return $items;
226 }
227
228 1;
229
230 __END__
231
232 =head1 NAME
233
234 Pure Storage API
235
236 =head1 API OPTIONS
237
238 Pure Storage API
239
240 =over 8
241
242 =item B<--hostname>
243
244 Set hostname.
245
246 =item B<--port>
247
248 Port used (default: 443)
249
250 =item B<--proto>
251
252 Specify https if needed (default: 'https')
253
254 =item B<--api-token>
255
256 API token.
257
258 =item B<--api-version>
259
260 API version (default: 2.4).
261
262 =item B<--timeout>
263
264 Set timeout in seconds (default: 30).
265
266 =back
267
268 =head1 DESCRIPTION
269
270 B<custom>.
271
272 =cut