Debian Codegraph @

src/storage/emc/unisphere/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::emc::unisphere::restapi::custom::api;
22
23 use strict;
24 use warnings;
25 use centreon::plugins::http;
26 use JSON::XS;
27 use Digest::MD5 qw(md5_hex);
28
29 sub new {
30 my ($class, %options) = @_;
31 my $self = {};
32 bless $self, $class;
33
34 if (!defined($options{output})) {
35 print "Class Custom: Need to specify 'output' argument.\n";
36 exit 3;
37 }
38 if (!defined($options{options})) {
39 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
40 $options{output}->option_exit();
41 }
42
43 if (!defined($options{noptions})) {
44 $options{options}->add_options(arguments => {
45 'api-username:s' => { name => 'api_username' },
46 'api-password:s' => { name => 'api_password' },
47 'hostname:s' => { name => 'hostname' },
48 'port:s' => { name => 'port' },
49 'proto:s' => { name => 'proto' },
50 'timeout:s' => { name => 'timeout' },
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} : 443;
77 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
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->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300';
82 $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
83 $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
84
85 if (!defined($self->{hostname}) || $self->{hostname} eq '') {
86 $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
87 $self->{output}->option_exit();
88 }
89 if (!defined($self->{api_username}) || $self->{api_username} eq '') {
90 $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
91 $self->{output}->option_exit();
92 }
93 if (!defined($self->{api_password}) || $self->{api_password} eq '') {
94 $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
95 $self->{output}->option_exit();
96 }
97
98 return 0;
99 }
100
101 sub build_options_for_httplib {
102 my ($self, %options) = @_;
103
104 $self->{option_results}->{hostname} = $self->{hostname};
105 $self->{option_results}->{timeout} = $self->{timeout};
106 $self->{option_results}->{port} = $self->{port};
107 $self->{option_results}->{proto} = $self->{proto};
108 $self->{option_results}->{timeout} = $self->{timeout};
109 $self->{option_results}->{credentials} = 1;
110 $self->{option_results}->{basic} = 1;
111 $self->{option_results}->{username} = $self->{api_username};
112 $self->{option_results}->{password} = $self->{api_password};
113 }
114
115 sub settings {
116 my ($self, %options) = @_;
117
118 return if (defined($self->{settings_done}));
119 $self->build_options_for_httplib();
120 $self->{http}->add_header(key => 'Accept', value => 'application/json');
121 $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
122 $self->{http}->add_header(key => 'X-EMC-REST-CLIENT', value => 'true');
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 => $options{url_path},
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 if (!defined($content) || $content eq '') {
147 $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
148 $self->{output}->option_exit();
149 }
150
151 my $decoded;
152 eval {
153 $decoded = JSON::XS->new->utf8->decode($content);
154 };
155 if ($@) {
156 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
157 $self->{output}->option_exit();
158 }
159
160 return $decoded;
161 }
162
163 1;
164
165 __END__
166
167 =head1 NAME
168
169 EMC Unisphere Rest API
170
171 =head1 REST API OPTIONS
172
173 EMC Unisphere Rest API
174
175 =over 8
176
177 =item B<--hostname>
178
179 EMC Unisphere hostname.
180
181 =item B<--port>
182
183 Port used (default: 443)
184
185 =item B<--proto>
186
187 Specify https if needed (default: 'https')
188
189 =item B<--api-username>
190
191 EMC Unisphere API username.
192
193 =item B<--api-password>
194
195 EMC Unisphere API password.
196
197 =item B<--timeout>
198
199 Set timeout in seconds (default: 10).
200
201 =back
202
203 =head1 DESCRIPTION
204
205 B<custom>.
206
207 =cut