Debian Codegraph @

src/storage/hp/storeonce/4/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::storeonce::4::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' },
50 'proto:s' => { name => 'proto' },
51 'timeout:s' => { name => 'timeout' },
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 => 'REST 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_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
82 $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
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_username} eq '') {
92 $self->{output}->add_option_msg(short_msg => 'Need to specify --api-username option.');
93 $self->{output}->option_exit();
94 }
95 if ($self->{api_password} eq '') {
96 $self->{output}->add_option_msg(short_msg => 'Need to specify --api-password 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 => 'Accept', value => 'application/json');
111 $self->{settings_done} = 1;
112 }
113
114 sub get_connection_info {
115 my ($self, %options) = @_;
116
117 return $self->{option_results}->{hostname} . ':' . $self->{option_results}->{port};
118 }
119
120 sub get_token {
121 my ($self, %options) = @_;
122
123 my $has_cache_file = $self->{cache}->read(statefile => 'storeonce_' . md5_hex($self->get_connection_info() . '_' . $self->{api_username}));
124 my $token = $self->{cache}->get(name => 'token');
125 my $md5_secret_cache = $self->{cache}->get(name => 'md5_secret');
126 my $md5_secret = md5_hex($self->{api_username} . $self->{api_password});
127
128 if ($has_cache_file == 0 ||
129 !defined($token) ||
130 (defined($md5_secret_cache) && $md5_secret_cache ne $md5_secret)
131 ) {
132 my $json_request = {
133 username => $self->{api_username},
134 password => $self->{api_password},
135 grant_type => 'password'
136 };
137 my $encoded;
138 eval {
139 $encoded = encode_json($json_request);
140 };
141 if ($@) {
142 $self->{output}->add_option_msg(short_msg => 'cannot encode json request');
143 $self->{output}->option_exit();
144 }
145
146 $self->settings();
147 my $content = $self->{http}->request(
148 method => 'POST',
149 url_path => '/pml/login/authenticatewithobject',
150 query_form_post => $encoded,
151 unknown_status => $self->{unknown_http_status},
152 warning_status => $self->{warning_http_status},
153 critical_status => $self->{critical_http_status},
154 header => ['Content-Type: application/json']
155 );
156
157 my $decoded;
158 eval {
159 $decoded = JSON::XS->new->utf8->decode($content);
160 };
161 if ($@) {
162 $self->{output}->add_option_msg(short_msg => "Cannot decode json response");
163 $self->{output}->option_exit();
164 }
165
166 $token = $decoded->{access_token};
167 my $datas = {
168 updated => time(),
169 token => $token,
170 md5_secret => $md5_secret
171 };
172 $self->{cache}->write(data => $datas);
173 }
174
175 return $token;
176 }
177
178 sub clean_token {
179 my ($self, %options) = @_;
180
181 my $datas = { updated => time() };
182 $self->{cache}->write(data => $datas);
183 }
184
185 sub request_api {
186 my ($self, %options) = @_;
187
188 $self->settings();
189 my $token = $self->get_token();
190 my ($content) = $self->{http}->request(
191 url_path => $options{endpoint},
192 get_param => $options{get_param},
193 header => ['Authorization: Bearer ' . $token],
194 unknown_status => '',
195 warning_status => '',
196 critical_status => ''
197 );
198
199 # Maybe token is invalid. so we retry
200 if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
201 $self->clean_token();
202 $token = $self->get_token();
203 $content = $self->{http}->request(
204 url_path => $options{endpoint},
205 get_param => $options{get_param},
206 header => ['Authorization: Bearer ' . $token],
207 unknown_status => $self->{unknown_http_status},
208 warning_status => $self->{warning_http_status},
209 critical_status => $self->{critical_http_status}
210 );
211 }
212
213 if (!defined($content) || $content eq '') {
214 $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
215 $self->{output}->option_exit();
216 }
217
218 my $decoded;
219 eval {
220 $decoded = JSON::XS->new->allow_nonref(1)->utf8->decode($content);
221 };
222 if ($@) {
223 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
224 $self->{output}->option_exit();
225 }
226
227 return $decoded;
228 }
229
230 1;
231
232 __END__
233
234 =head1 NAME
235
236 StoreOnce API
237
238 =head1 REST API OPTIONS
239
240 StoreOnce API
241
242 =over 8
243
244 =item B<--hostname>
245
246 Set hostname.
247
248 =item B<--port>
249
250 Port used (default: 443)
251
252 =item B<--proto>
253
254 Specify https if needed (default: 'https')
255
256 =item B<--api-username>
257
258 API username.
259
260 =item B<--api-password>
261
262 API password.
263
264 =item B<--timeout>
265
266 Set timeout in seconds (default: 30).
267
268 =back
269
270 =head1 DESCRIPTION
271
272 B<custom>.
273
274 =cut