Debian Codegraph @

src/storage/hp/primera/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::hp::primera::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 'api-username:s' => { name => 'api_username' },
47 'api-password:s' => { name => 'api_password' },
48 'hostname:s' => { name => 'hostname' },
49 'port:s' => { name => 'port', default => 443 },
50 'proto:s' => { name => 'proto', default => 'https' },
51 'timeout:s' => { name => 'timeout', default => 30 },
52 'unknown-http-status:s' => { name => 'unknown_http_status', default => '%{http_code} < 200 or %{http_code} >= 300' },
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 => 'HPE Primera 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 if (centreon::plugins::misc::is_empty($self->{option_results}->{hostname})) {
78 $self->{output}->add_option_msg(short_msg => 'Need to specify --hostname option.');
79 $self->{output}->option_exit();
80 }
81 if (centreon::plugins::misc::is_empty($self->{option_results}->{api_username})) {
82 $self->{output}->add_option_msg(short_msg => 'Need to specify --api-username option.');
83 $self->{output}->option_exit();
84 }
85 if (centreon::plugins::misc::is_empty($self->{option_results}->{api_password})) {
86 $self->{output}->add_option_msg(short_msg => 'Need to specify --api-password option.');
87 $self->{output}->option_exit();
88 }
89 $self->{http}->set_options(%{$self->{option_results}});
90 $self->{http}->add_header(key => 'Accept', value => 'application/json');
91
92 $self->{cache}->check_options(option_results => $self->{option_results});
93
94 return 0;
95 }
96
97 sub get_connection_info {
98 my ($self, %options) = @_;
99
100 return $self->{option_results}->{hostname} . ':' . $self->{option_results}->{port};
101 }
102
103 sub get_token {
104 my ($self, %options) = @_;
105
106 my $has_cache_file = $self->{cache}->read(statefile => 'hpe_primera_' . md5_hex($self->get_connection_info() . '_' . $self->{option_results}->{api_username}));
107 my $auth_key = $self->{cache}->get(name => 'auth_key');
108
109 if ($has_cache_file == 0 || !defined($auth_key) || $auth_key eq '' ) {
110 my $json_request = {
111 user => $self->{option_results}->{api_username},
112 password => $self->{option_results}->{api_password}
113 };
114 my $encoded;
115 eval {
116 $encoded = encode_json($json_request);
117 };
118 if ($@) {
119 $self->{output}->add_option_msg(short_msg => 'An error occurred while encoding the credentials to a JSON string.');
120 $self->{output}->option_exit();
121 }
122
123 my $content = $self->{http}->request(
124 method => 'POST',
125 url_path => '/api/v1/credentials',
126 query_form_post => $encoded,
127 unknown_status => $self->{option_results}->{unknown_http_status},
128 warning_status => $self->{option_results}->{warning_http_status},
129 critical_status => $self->{option_results}->{critical_http_status},
130 header => ['Content-Type: application/json']
131 );
132
133 my $decoded;
134 eval {
135 $decoded = JSON::XS->new->utf8->decode($content);
136 };
137 if ($@) {
138 $self->{output}->add_option_msg(short_msg => "An error occurred while decoding the response ('$content').");
139 $self->{output}->option_exit();
140 }
141
142 $auth_key = $decoded->{key};
143 my $data = {
144 updated => time(),
145 auth_key => $auth_key
146 };
147 $self->{cache}->write(data => $data);
148 }
149
150 return $auth_key;
151 }
152
153 sub clean_token {
154 my ($self, %options) = @_;
155
156 my $data = { updated => time() };
157 $self->{cache}->write(data => $data);
158 }
159
160 sub request_api {
161 my ($self, %options) = @_;
162
163 my $get_param = [];
164 if (defined($options{get_param})) {
165 $get_param = $options{get_param};
166 }
167
168 my $token = $self->get_token();
169 my ($content) = $self->{http}->request(
170 url_path => $options{endpoint},
171 get_param => $get_param,
172 header => [ 'X-HP3PAR-WSAPI-SessionKey: ' . $token ],
173 unknown_status => '',
174 warning_status => '',
175 critical_status => ''
176 );
177
178 # Maybe token is invalid. so we retry
179 if (!defined($token) || $self->{http}->get_code() >= 400) {
180 $self->clean_token();
181 $token = $self->get_token();
182
183 $content = $self->{http}->request(
184 url_path => $options{endpoint},
185 get_param => $get_param,
186 header => [ 'X-HP3PAR-WSAPI-SessionKey: ' . $token ],
187 unknown_status => $self->{unknown_http_status},
188 warning_status => $self->{warning_http_status},
189 critical_status => $self->{critical_http_status}
190 );
191 }
192
193 if (!defined($content) || $content eq '') {
194 $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
195 $self->{output}->option_exit();
196 }
197
198 my $decoded;
199 eval {
200 $decoded = JSON::XS->new->allow_nonref(1)->utf8->decode($content);
201 };
202 if ($@) {
203 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
204 $self->{output}->option_exit();
205 }
206
207 return $decoded;
208 }
209
210 1;
211
212 __END__
213
214 =head1 NAME
215
216 HPE Primera REST API
217
218 =head1 HPE Primera API OPTIONS
219
220 HPE Primera REST API
221
222 =over 8
223
224 =item B<--hostname>
225
226 Address of the server that hosts the API.
227
228 =item B<--port>
229
230 Define the TCP port to use to reach the API (default: 443).
231
232 =item B<--proto>
233
234 Define the protocol to reach the API (default: 'https').
235
236 =item B<--api-username>
237
238 Define the username for authentication.
239
240 =item B<--api-password>
241
242 Define the password associated with the username.
243
244 =item B<--timeout>
245
246 Define the timeout in seconds for HTTP requests (default: 30).
247
248 =back
249
250 =head1 DESCRIPTION
251
252 B<custom>.
253
254 =cut