Debian Codegraph @

src/storage/dell/powerstore/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::dell::powerstore::restapi::custom::api;
22
23 use strict;
24 use warnings;
25 use centreon::plugins::http;
26 use JSON::XS;
27
28 sub new {
29 my ($class, %options) = @_;
30 my $self = {};
31 bless $self, $class;
32
33 if (!defined($options{output})) {
34 print "Class Custom: Need to specify 'output' argument.\n";
35 exit 3;
36 }
37 if (!defined($options{options})) {
38 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
39 $options{output}->option_exit();
40 }
41
42 if (!defined($options{noptions})) {
43 $options{options}->add_options(arguments => {
44 'api-username:s' => { name => 'api_username' },
45 'api-password:s' => { name => 'api_password' },
46 'hostname:s' => { name => 'hostname' },
47 'port:s' => { name => 'port' },
48 'proto:s' => { name => 'proto' },
49 'timeout:s' => { name => 'timeout' },
50 'unknown-http-status:s' => { name => 'unknown_http_status' },
51 'warning-http-status:s' => { name => 'warning_http_status' },
52 'critical-http-status:s' => { name => 'critical_http_status' }
53 });
54 }
55 $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
56
57 $self->{output} = $options{output};
58 $self->{http} = centreon::plugins::http->new(%options);
59
60 return $self;
61 }
62
63 sub set_options {
64 my ($self, %options) = @_;
65
66 $self->{option_results} = $options{option_results};
67 }
68
69 sub set_defaults {}
70
71 sub check_options {
72 my ($self, %options) = @_;
73
74 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
75 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
76 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
77 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 50;
78 $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
79 $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
80 $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300';
81 $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
82 $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
83
84 if ($self->{hostname} eq '') {
85 $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
86 $self->{output}->option_exit();
87 }
88 if ($self->{api_username} eq '') {
89 $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
90 $self->{output}->option_exit();
91 }
92 if ($self->{api_password} eq '') {
93 $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
94 $self->{output}->option_exit();
95 }
96
97 return 0;
98 }
99
100 sub build_options_for_httplib {
101 my ($self, %options) = @_;
102
103 $self->{option_results}->{hostname} = $self->{hostname};
104 $self->{option_results}->{timeout} = $self->{timeout};
105 $self->{option_results}->{port} = $self->{port};
106 $self->{option_results}->{proto} = $self->{proto};
107 $self->{option_results}->{timeout} = $self->{timeout};
108 $self->{option_results}->{credentials} = 1;
109 $self->{option_results}->{basic} = 1;
110 $self->{option_results}->{username} = $self->{api_username};
111 $self->{option_results}->{password} = $self->{api_password};
112 }
113
114 sub settings {
115 my ($self, %options) = @_;
116
117 return if (defined($self->{settings_done}));
118 $self->build_options_for_httplib();
119 $self->{http}->add_header(key => 'Accept', value => 'application/json');
120 $self->{http}->set_options(%{$self->{option_results}});
121 $self->{settings_done} = 1;
122 }
123
124 sub get_hostname {
125 my ($self, %options) = @_;
126
127 return $self->{hostname};
128 }
129
130 sub get_port {
131 my ($self, %options) = @_;
132
133 return $self->{port};
134 }
135
136 sub get_performance_metrics_by_clusters {
137 my ($self, %options) = @_;
138
139 my $clusters = $self->request_api(endpoint => '/api/rest/cluster');
140
141 my $results = {};
142 foreach (@$clusters) {
143 my $post_json = JSON::XS->new->utf8->encode(
144 {
145 entity => 'performance_metrics_by_cluster',
146 entity_id => $_->{id},
147 interval => 'Five_Mins'
148 }
149 );
150
151 my $perfs = $self->request_api(
152 method => 'POST',
153 endpoint => '/api/rest/metrics/generate',
154 headers => ['Content-Type: application/json'],
155 query_form_post => $post_json
156 );
157 $results->{ $_->{id} } = $perfs;
158 }
159
160 return $results;
161 }
162
163 sub get_space_metrics_by_appliance {
164 my ($self, %options) = @_;
165
166 my $appliances = $self->request_api(endpoint => '/api/rest/appliance');
167
168 my $results = {};
169 foreach (@$appliances) {
170 my $post_json = JSON::XS->new->utf8->encode(
171 {
172 entity => 'space_metrics_by_appliance',
173 entity_id => $_->{id},
174 interval => 'Five_Mins'
175 }
176 );
177
178 my $perfs = $self->request_api(
179 method => 'POST',
180 endpoint => '/api/rest/metrics/generate',
181 headers => ['Content-Type: application/json'],
182 query_form_post => $post_json
183 );
184 $results->{ $_->{id} } = $perfs;
185 }
186
187 return $results;
188 }
189
190 sub request_api {
191 my ($self, %options) = @_;
192
193 $self->settings();
194 my $content = $self->{http}->request(
195 method => defined($options{method}) ? $options{method} : 'GET',
196 url_path => $options{endpoint},
197 get_param => $options{get_param},
198 header => $options{headers},
199 query_form_post => $options{query_form_post},
200 unknown_status => $self->{unknown_http_status},
201 warning_status => $self->{warning_http_status},
202 critical_status => $self->{critical_http_status}
203 );
204
205 if (!defined($content) || $content eq '') {
206 $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
207 $self->{output}->option_exit();
208 }
209
210 my $decoded;
211 eval {
212 $decoded = JSON::XS->new->utf8->decode($content);
213 };
214 if ($@) {
215 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
216 $self->{output}->option_exit();
217 }
218
219 return $decoded;
220 }
221
222 1;
223
224 __END__
225
226 =head1 NAME
227
228 Dell PowerStore Rest API
229
230 =head1 REST API OPTIONS
231
232 Dell PowerStore Rest API
233
234 =over 8
235
236 =item B<--hostname>
237
238 Set hostname.
239
240 =item B<--port>
241
242 Port used (default: 443)
243
244 =item B<--proto>
245
246 Specify https if needed (default: 'https')
247
248 =item B<--api-username>
249
250 API username.
251
252 =item B<--api-password>
253
254 API password.
255
256 =item B<--timeout>
257
258 Set timeout in seconds (default: 50).
259
260 =back
261
262 =head1 DESCRIPTION
263
264 B<custom>.
265
266 =cut