Debian Codegraph @

src/storage/dell/me4/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::me4::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 use Digest::SHA;
30
31 sub new {
32 my ($class, %options) = @_;
33 my $self = {};
34 bless $self, $class;
35
36 if (!defined($options{output})) {
37 print "Class Custom: Need to specify 'output' argument.\n";
38 exit 3;
39 }
40 if (!defined($options{options})) {
41 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
42 $options{output}->option_exit();
43 }
44
45 if (!defined($options{noptions})) {
46 $options{options}->add_options(arguments => {
47 'api-username:s' => { name => 'api_username' },
48 'api-password:s' => { name => 'api_password' },
49 'hostname:s' => { name => 'hostname' },
50 'port:s' => { name => 'port' },
51 'proto:s' => { name => 'proto' },
52 'timeout:s' => { name => 'timeout' },
53 'digest-sha256' => { name => 'digest_sha256' }
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 $self->{set_lang} = 0;
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->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
78 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
79 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
80 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
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
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 $self->{cache}->check_options(option_results => $self->{option_results});
98
99 return 0;
100 }
101
102 sub build_options_for_httplib {
103 my ($self, %options) = @_;
104
105 $self->{option_results}->{hostname} = $self->{hostname};
106 $self->{option_results}->{timeout} = $self->{timeout};
107 $self->{option_results}->{port} = $self->{port};
108 $self->{option_results}->{proto} = $self->{proto};
109 $self->{option_results}->{timeout} = $self->{timeout};
110 $self->{option_results}->{warning_status} = '';
111 $self->{option_results}->{critical_status} = '';
112 $self->{option_results}->{unknown_status} = '%{http_code} < 200 or %{http_code} > 400';
113 }
114
115 sub settings {
116 my ($self, %options) = @_;
117
118 $self->build_options_for_httplib();
119 $self->{http}->add_header(key => 'Accept', value => 'application/json');
120 $self->{http}->add_header(key => 'datatype', value => 'json');
121 if (defined($self->{session_key})) {
122 $self->{http}->add_header(key => 'sessionKey', value => $self->{session_key});
123 }
124 $self->{http}->set_options(%{$self->{option_results}});
125 }
126
127 sub get_hostname {
128 my ($self, %options) = @_;
129
130 return $self->{hostname};
131 }
132
133 sub get_session_key {
134 my ($self, %options) = @_;
135
136 my $has_cache_file = $options{statefile}->read(statefile => 'dell_me4_api_' . md5_hex($self->{hostname}) . '_' . md5_hex($self->{api_username}));
137 my $expires_on = $options{statefile}->get(name => 'expires_on');
138 my $session_key = $options{statefile}->get(name => 'session_key');
139
140 if ($has_cache_file == 0 || !defined($session_key) || (($expires_on - time()) < 10)) {
141 my $digest_data = $self->{api_username} . '_' . $self->{api_password};
142 my $digest_hash = defined($self->{option_results}->{digest_sha256}) ? Digest::SHA::sha256_hex($digest_data) : md5_hex($digest_data);
143
144 $self->settings();
145
146 my $content = $self->{http}->request(
147 method => 'GET',
148 url_path => '/api/login/' . $digest_hash
149 );
150
151 if (!defined($content) || $content eq '') {
152 $self->{output}->add_option_msg(short_msg => "Login endpoint returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
153 $self->{output}->option_exit();
154 }
155
156 my $decoded;
157 eval {
158 $decoded = JSON::XS->new->utf8->decode($content);
159 };
160 if ($@) {
161 $self->{output}->output_add(long_msg => $content, debug => 1);
162 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
163 $self->{output}->option_exit();
164 }
165 if ($decoded->{status}[0]->{'response-type'} ne 'Success') {
166 $self->{output}->add_option_msg(short_msg => "Login endpoint returns an error: '" . $decoded->{status}[0]->{response} . "'");
167 $self->{output}->option_exit();
168 }
169
170 $session_key = $decoded->{status}[0]->{response};
171 my $datas = { last_timestamp => time(), session_key => $decoded->{status}[0]->{response}, expires_on => 3600 };
172 $options{statefile}->write(data => $datas);
173 }
174
175 return $session_key;
176 }
177
178 sub request_api {
179 my ($self, %options) = @_;
180
181 if (!defined($self->{session_key})) {
182 $self->{session_key} = $self->get_session_key(statefile => $self->{cache});
183 }
184
185 $self->settings();
186
187 if ($self->{set_lang} == 0) {
188 $self->{http}->request(method => 'GET', url_path => '/api/set/cli-parameters/locale/English');
189 $self->{set_lang} = 1;
190 }
191 my $content = $self->{http}->request(%options);
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->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 Dell ME4 Rest API
217
218 =head1 REST API OPTIONS
219
220 Dell ME4 Rest API
221
222 =over 8
223
224 =item B<--hostname>
225
226 Dell ME4 hostname.
227
228 =item B<--port>
229
230 Port used (default: 443)
231
232 =item B<--proto>
233
234 Specify https if needed (default: 'https')
235
236 =item B<--api-username>
237
238 API username.
239
240 =item B<--api-password>
241
242 API password.
243
244 =item B<--digest-sha256>
245
246 New digest to use since firmware GT280R010-01 (md5 deprecated).
247
248 =item B<--timeout>
249
250 Set timeout in seconds (default: 10).
251
252 =back
253
254 =head1 DESCRIPTION
255
256 B<custom>.
257
258 =cut