Debian Codegraph @

src/apps/monitoring/iplabel/ekara/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 apps::monitoring::iplabel::ekara::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 'url-path:s' => { name => 'url_path' },
53 'filter-id:s' => { name => 'filter_id' },
54 'filter-name:s' => { name => 'filter_name' },
55 'filter-workspaceid:s' => { name => 'filter_workspaceid' },
56 'filter-siteid:s' => { name => 'filter_siteid' },
57 'filter-status:s@' => { name => 'filter_status' },
58 'authent-endpoint' => { name => 'authent_endpoint' }
59 });
60 }
61 $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
62
63 $self->{output} = $options{output};
64 $self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl');
65 $self->{cache} = centreon::plugins::statefile->new(%options);
66
67 return $self;
68 }
69
70 sub set_options {
71 my ($self, %options) = @_;
72
73 $self->{option_results} = $options{option_results};
74 }
75
76 sub set_defaults {}
77
78 sub check_options {
79 my ($self, %options) = @_;
80
81 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : 'api.ekara.ip-label.net';
82 $self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '';
83 $self->{authent_endpoint} = (defined($self->{option_results}->{authent_endpoint})) ? $self->{option_results}->{authent_endpoint} : '/auth/login';
84 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
85 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
86 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
87 $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300';
88 $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
89 $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
90 $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : undef;
91 $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : undef;
92 $self->{cache}->check_options(option_results => $self->{option_results});
93
94 if (!defined($self->{hostname}) || $self->{hostname} eq '') {
95 $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
96 $self->{output}->option_exit();
97 }
98 if (!defined($self->{api_username}) || $self->{api_username} eq '') {
99 $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
100 $self->{output}->option_exit();
101 }
102 if (!defined($self->{api_password}) || $self->{api_password} eq '') {
103 $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
104 $self->{output}->option_exit();
105 }
106
107 return 0;
108 }
109
110 sub build_options_for_httplib {
111 my ($self, %options) = @_;
112
113 $self->{option_results}->{hostname} = $self->{hostname};
114 $self->{option_results}->{timeout} = $self->{timeout};
115 $self->{option_results}->{proto} = $self->{proto};
116 $self->{option_results}->{warning_status} = '';
117 $self->{option_results}->{critical_status} = '';
118 $self->{option_results}->{unknown_status} = '%{http_code} < 200 or %{http_code} >= 500';
119 }
120
121 sub settings {
122 my ($self, %options) = @_;
123
124 $self->build_options_for_httplib();
125 $self->{http}->add_header(key => 'Accept', value => 'application/json');
126 $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
127 $self->{http}->add_header(key => 'Authorization', value => 'Bearer ' . $self->{access_token}) if (defined($self->{access_token}));
128 $self->{http}->set_options(%{$self->{option_results}});
129 }
130
131 sub get_access_token {
132 my ($self, %options) = @_;
133
134 my $has_cache_file = $options{statefile}->read(statefile => 'iplabel_ekara_api_' . md5_hex($self->{hostname}) . '_' . md5_hex($self->{api_username}));
135 my $expires_on = $options{statefile}->get(name => 'expires_on');
136 my $access_token = $options{statefile}->get(name => 'access_token');
137 if ( $has_cache_file == 0 || !defined($access_token) || (($expires_on - time()) < 10) ) {
138 my $login = { email => $self->{api_username}, password => $self->{api_password} };
139 my $post_json = JSON::XS->new->utf8->encode($login);
140
141 $self->settings();
142
143 my $content = $self->{http}->request(
144 method => 'POST',
145 header => ['Content-type: application/json'],
146 query_form_post => $post_json,
147 url_path => $self->{authent_endpoint}
148 );
149
150 if (!defined($content) || $content eq '' || $self->{http}->get_header(name => 'content-length') == 0) {
151 $self->{output}->add_option_msg(short_msg => "Authentication endpoint returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
152 $self->{output}->option_exit();
153 }
154
155 my $decoded;
156 eval {
157 $decoded = JSON::XS->new->utf8->decode($content);
158 };
159 if ($@) {
160 $self->{output}->output_add(long_msg => $content, debug => 1);
161 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
162 $self->{output}->option_exit();
163 }
164 if (!($decoded->{success})) {
165 $self->{output}->output_add(long_msg => "Error message : " . $decoded->{message}, debug => 1);
166 $self->{output}->add_option_msg(short_msg => "Authentication endpoint returns error code '" . $decoded->{message} . "' (add --debug option for detailed message)");
167 $self->{output}->option_exit();
168 }
169
170 $access_token = $decoded->{token};
171 my $datas = { last_timestamp => time(), access_token => $decoded->{token}, expires_on => time() + 3600 };
172 $options{statefile}->write(data => $datas);
173 }
174
175 return $access_token;
176 }
177
178 sub request_scenarios_status{
179 my ($self, %options) = @_;
180
181 my $status_filter = {};
182 my @get_param;
183
184 if (defined($self->{option_results}->{filter_status}) && $self->{option_results}->{filter_status}[0] ne '') {
185 $status_filter->{statusFilter} = $self->{option_results}->{filter_status};
186 }
187 if (defined($self->{option_results}->{filter_workspaceid}) && $self->{option_results}->{filter_workspaceid} ne '') {
188 push(@get_param, "workspaceId=$self->{option_results}->{filter_workspaceid}");
189 }
190 if (defined($self->{option_results}->{filter_siteid}) && $self->{option_results}->{filter_siteid} ne '') {
191 push(@get_param, "siteId=$self->{option_results}->{filter_siteid}");
192 }
193 my $results = $self->request_api(
194 endpoint => '/results-api/scenarios/status',
195 method => 'POST',
196 post_body => $status_filter,
197 get_param => \@get_param,
198 );
199 if (ref($results) eq "HASH" ) {
200 if (defined($results->{message})) {
201 $self->{output}->add_option_msg(short_msg => "Cannot get scenarios : " . $results->{message});
202 $self->{output}->option_exit();
203 }
204 if (defined($results->{error})) {
205 $self->{output}->add_option_msg(short_msg => "Cannot get scenarios : " . $results->{error});
206 $self->{output}->option_exit();
207 }
208 $self->{output}->add_option_msg(short_msg => "Cannot get scenarios due to an unknown error, please use the --debug option to find more information");
209 $self->{output}->option_exit();
210 }
211
212 my @scenarios;
213 for my $scenario (@$results) {
214 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
215 $scenario->{scenarioName} !~ /$self->{option_results}->{filter_name}/) {
216 $self->{output}->output_add(long_msg => "skipping scenario '" . $scenario->{scenarioName} . "': no matching filter.", debug => 1);
217 next;
218 }
219 if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne '' &&
220 $scenario->{scenarioId} !~ /$self->{option_results}->{filter_id}/) {
221 $self->{output}->output_add(long_msg => "skipping scenario '" . $scenario->{scenarioName} . "': no matching filter.", debug => 1);
222 next;
223 }
224 push(@scenarios, $scenario);
225 }
226 return \@scenarios;
227 }
228
229 sub request_api {
230 my ($self, %options) = @_;
231
232 if (!defined($self->{access_token})) {
233 $self->{access_token} = $self->get_access_token(statefile => $self->{cache});
234 }
235
236 $self->settings();
237
238 my ($json, $response);
239
240 my $post_json = defined($options{post_body}) ? $options{post_body} : {};
241
242 $response = $self->{http}->request(
243 get_param => $options{get_param},
244 method => $options{method},
245 url_path => $self->{url_path} . $options{endpoint},
246 query_form_post => JSON::XS->new->utf8->encode($post_json)
247 );
248 $self->{output}->output_add(long_msg => $response, debug => 1);
249
250 eval {
251 $json = JSON::XS->new->utf8->decode($response);
252 };
253 if ($@) {
254 $self->{output}->add_option_msg(short_msg => "Cannot decode ekara JSON response: $@");
255 $self->{output}->option_exit();
256 };
257
258 return $json;
259 }
260
261
262 1;
263
264 __END__
265
266 =head1 NAME
267
268 ip-label Ekara Rest API
269
270 =head1 REST API OPTIONS
271
272 ip-label Ekara Rest API
273
274 =over 8
275
276 =item B<--hostname>
277
278 Set hostname (default: 'api.ip-label.net').
279
280 =item B<--port>
281
282 Port used (default: 443)
283
284 =item B<--proto>
285
286 Specify https if needed (default: 'https')
287
288 =item B<--api-username>
289
290 Set username.
291
292 =item B<--api-password>
293
294 Set password.
295
296 =item B<--filter-id>
297
298 Filter by monitor ID (can be a regexp).
299
300 =item B<--filter-name>
301
302 Filter by monitor name (can be a regexp).
303
304 =item B<--filter-status>
305
306 Filter by numeric status (can be multiple).
307 0 => 'Unknown',
308 1 => 'Success',
309 2 => 'Failure',
310 3 => 'Aborted',
311 4 => 'No execution',
312 5 => 'No execution',
313 6 => 'Stopped',
314 7 => 'Excluded',
315 8 => 'Degraded'
316
317 Example: --filter-status='1,2'
318
319 =item B<--filter-workspaceid>
320
321 Filter scenario to check by workspace id.
322
323 =item B<--filter-siteid>
324
325 Filter scenario to check by site id.
326
327 =item B<--timeout>
328
329 Set timeout in seconds (default: 10).
330
331 =back
332
333 =head1 DESCRIPTION
334
335 B<custom>.
336
337 =cut