Debian Codegraph @

src/storage/netapp/santricity/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::netapp::santricity::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 'api-path:s' => { name => 'api_path' },
51 'unknown-http-status:s' => { name => 'unknown_http_status' },
52 'warning-http-status:s' => { name => 'warning_http_status' },
53 'critical-http-status:s' => { name => 'critical_http_status' }
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
61 return $self;
62 }
63
64 sub set_options {
65 my ($self, %options) = @_;
66
67 $self->{option_results} = $options{option_results};
68 }
69
70 sub set_defaults {}
71
72 sub check_options {
73 my ($self, %options) = @_;
74
75 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
76 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 8080;
77 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'http';
78 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
79 $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
80 $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
81 $self->{api_path} = (defined($self->{option_results}->{api_path})) ? $self->{option_results}->{api_path} : '/devmgr/v2';
82 $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '(%{http_code} < 200 or %{http_code} >= 300) and %{http_code} != 424';
83 $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
84 $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
85
86 if (!defined($self->{hostname}) || $self->{hostname} eq '') {
87 $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
88 $self->{output}->option_exit();
89 }
90 if (!defined($self->{api_username}) || $self->{api_username} eq '') {
91 $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
92 $self->{output}->option_exit();
93 }
94 if (!defined($self->{api_password}) || $self->{api_password} eq '') {
95 $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
96 $self->{output}->option_exit();
97 }
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}->{credentials} = 1;
111 $self->{option_results}->{basic} = 1;
112 $self->{option_results}->{username} = $self->{api_username};
113 $self->{option_results}->{password} = $self->{api_password};
114 }
115
116 sub settings {
117 my ($self, %options) = @_;
118
119 return if (defined($self->{settings_done}));
120 $self->build_options_for_httplib();
121 $self->{http}->add_header(key => 'Accept', value => 'application/json');
122 $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
123 $self->{http}->set_options(%{$self->{option_results}});
124 $self->{settings_done} = 1;
125 }
126
127 sub get_hostname {
128 my ($self, %options) = @_;
129
130 return $self->{hostname};
131 }
132
133 sub request_api {
134 my ($self, %options) = @_;
135
136 $self->settings();
137 my $content = $self->{http}->request(
138 method => defined($options{method}) ? $options{method} : 'GET',
139 url_path => $self->{api_path} . $options{endpoint},
140 unknown_status => $self->{unknown_http_status},
141 warning_status => $self->{warning_http_status},
142 critical_status => $self->{critical_http_status},
143 cookies_file => '' # in memory
144 );
145
146 # code 424 = storageDevice offline
147 my $code = $self->{http}->get_code();
148 if (!defined($content) || $content eq '') {
149 $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
150 $self->{output}->option_exit();
151 }
152
153 return undef if ($code == 424);
154
155 my $decoded;
156 eval {
157 $decoded = JSON::XS->new->utf8->decode($content);
158 };
159 if ($@) {
160 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
161 $self->{output}->option_exit();
162 }
163
164 return { $options{name} => $decoded };
165 }
166
167 sub execute_storages_request {
168 my ($self, %options) = @_;
169
170 my $storages = $self->request_api(
171 name => 'storages',
172 endpoint => '/storage-systems'
173 );
174 return $storages if (!defined($options{endpoints}));
175
176 for (my $i = 0; $i < scalar(@{$storages->{storages}}); $i++) {
177 next if (defined($options{filter_name}) && $options{filter_name} ne '' &&
178 $storages->{storages}->[$i]->{name} !~ /$options{filter_name}/);
179
180 $storages->{storages}->[$i]->{offline} = 0;
181
182 foreach (@{$options{endpoints}}) {
183 my $info = $self->request_api(
184 name => 'info',
185 endpoint => '/storage-systems/' . $storages->{storages}->[$i]->{wwn} . $_->{endpoint} . (defined($_->{get_param}) ? '?' . $_->{get_param} : '')
186 );
187 if (!defined($info)) {
188 $storages->{storages}->[$i]->{offline} = 1;
189 last;
190 }
191
192 $storages->{storages}->[$i]->{ $_->{endpoint} } = defined($info) ? $info->{info} : undef;
193 }
194 }
195
196 return $storages;
197 }
198
199
200
201 1;
202
203 __END__
204
205 =head1 NAME
206
207 Netapp Santricity Rest API
208
209 =head1 REST API OPTIONS
210
211 Netapp Santricity Rest API
212
213 =over 8
214
215 =item B<--hostname>
216
217 Santricity hostname.
218
219 =item B<--port>
220
221 Port used (default: 8080)
222
223 =item B<--proto>
224
225 Specify https if needed (default: 'http')
226
227 =item B<--api-username>
228
229 Santricity API username.
230
231 =item B<--api-password>
232
233 Santricity API password.
234
235 =item B<--api-path>
236
237 Specify api path (default: '/devmgr/v2')
238
239 =item B<--timeout>
240
241 Set timeout in seconds (default: 10).
242
243 =back
244
245 =head1 DESCRIPTION
246
247 B<custom>.
248
249 =cut