Debian Codegraph @

src/notification/centreon/opentickets/api/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 notification::centreon::opentickets::api::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-hostname:s' => { name => 'api_hostname' },
47 'api-port:s' => { name => 'api_port' },
48 'api-proto:s' => { name => 'api_proto' },
49 'url-path:s' => { name => 'url_path' },
50 'api-username:s' => { name => 'api_username' },
51 'api-password:s' => { name => 'api_password' },
52 'api-timeout:s' => { name => 'api_timeout' }
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 $self->{cache} = centreon::plugins::statefile->new(%options, default_backend => 'curl');
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->{api_hostname} = (defined($self->{option_results}->{api_hostname})) ? $self->{option_results}->{api_hostname} : '';
76 $self->{api_proto} = (defined($self->{option_results}->{api_proto})) ? $self->{option_results}->{api_proto} : 'http';
77 $self->{api_port} = (defined($self->{option_results}->{api_port})) ? $self->{option_results}->{api_port} : 80;
78 $self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/centreon/api/';
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_timeout} = (defined($self->{option_results}->{api_timeout})) ? $self->{option_results}->{api_timeout} : 10;
82
83 if ($self->{api_hostname} eq '') {
84 $self->{output}->add_option_msg(short_msg => 'Need to specify api-hostname option.');
85 $self->{output}->option_exit();
86 }
87 if ($self->{api_username} eq '') {
88 $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
89 $self->{output}->option_exit();
90 }
91 if ($self->{api_password} eq '') {
92 $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
93 $self->{output}->option_exit();
94 }
95
96 $self->{cache}->check_options(option_results => $self->{option_results});
97 return 0;
98 }
99
100 sub get_connection_infos {
101 my ($self, %options) = @_;
102
103 return $self->{hostname} . '_' . $self->{http}->get_port();
104 }
105
106 sub get_hostname {
107 my ($self, %options) = @_;
108
109 return $self->{hostname};
110 }
111
112 sub get_port {
113 my ($self, %options) = @_;
114
115 return $self->{port};
116 }
117
118 sub json_decode {
119 my ($self, %options) = @_;
120
121 my $decoded;
122 eval {
123 $decoded = JSON::XS->new->utf8->decode($options{content});
124 };
125 if ($@) {
126 $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
127 $self->{output}->option_exit();
128 }
129
130 return $decoded;
131 }
132
133 sub settings {
134 my ($self, %options) = @_;
135
136 $self->{http}->add_header(key => 'Accept', value => 'application/json');
137 $self->{http}->set_options(
138 hostname => $self->{api_hostname},
139 port => $self->{api_port},
140 proto => $self->{api_proto},
141 %{$self->{option_results}}
142 );
143 }
144
145 sub clean_token {
146 my ($self, %options) = @_;
147
148 my $datas = {};
149 $self->{cache}->write(data => $datas);
150 }
151
152 sub get_token {
153 my ($self, %options) = @_;
154
155 my $has_cache_file = $self->{cache}->read(statefile => 'centreon_opentickets_' . md5_hex($self->{api_hostname}) . '_' . md5_hex($self->{api_username}));
156 my $token = $self->{cache}->get(name => 'token');
157
158 if ($has_cache_file == 0 || !defined($token)) {
159 my ($content) = $self->{http}->request(
160 method => 'POST',
161 url_path => $self->{url_path} . 'index.php',
162 get_param => ['action=authenticate'],
163 post_param => [
164 'username=' . $self->{api_username},
165 'password=' . $self->{api_password}
166 ],
167 warning_status => '', unknown_status => '', critical_status => ''
168 );
169
170 if ($self->{http}->get_code() != 200) {
171 $self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
172 $self->{output}->option_exit();
173 }
174
175 my $decoded = $self->json_decode(content => $content);
176 if (!defined($decoded->{authToken})) {
177 $self->{output}->add_option_msg(short_msg => "Cannot get token");
178 $self->{output}->option_exit();
179 }
180
181 $token = $decoded->{authToken};
182 my $datas = { token => $token };
183 $self->{cache}->write(data => $datas);
184 }
185
186 return $token;
187 }
188
189 sub request_api {
190 my ($self, %options) = @_;
191
192 $self->settings();
193 my $token = $self->get_token();
194
195 my $encoded;
196 eval {
197 $encoded = JSON::XS->new->utf8->encode($options{data});
198 };
199 if ($@) {
200 $self->{output}->add_option_msg(short_msg => 'cannot encode json request');
201 $self->{output}->option_exit();
202 }
203
204 my $content = $self->{http}->request(
205 method => 'POST',
206 url_path => $self->{url_path} . 'index.php',
207 get_param => [
208 'object=centreon_openticket',
209 'action=' . $options{action},
210 ],
211 header => [
212 'Content-Type: application/json',
213 'centreon-auth-token: ' . $token
214 ],
215 query_form_post => $encoded,
216 warning_status => '',
217 unknown_status => '',
218 critical_status => ''
219 );
220
221 if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
222 $self->clean_token();
223 $token = $self->get_token();
224 $content = $self->{http}->request(
225 method => 'POST',
226 url_path => $self->{url_path} . 'index.php',
227 get_param => [
228 'object=centreon_openticket',
229 'action=' . $options{action},
230 ],
231 header => [
232 'Content-Type: application/json',
233 'centreon-auth-token: ' . $token
234 ],
235 query_form_post => $encoded,
236 warning_status => '',
237 unknown_status => '',
238 critical_status => ''
239 );
240 }
241
242 if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
243 $self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
244 $self->{output}->option_exit();
245 }
246
247 my $decoded = $self->json_decode(content => $content);
248 if (!defined($decoded)) {
249 $self->{output}->add_option_msg(short_msg => 'Error while retrieving data (add --debug option for detailed message)');
250 $self->{output}->option_exit();
251 }
252
253 return $decoded;
254 }
255
256 1;
257
258 __END__
259
260 =head1 NAME
261
262 Centreon Open-Tickets API
263
264 =head1 SYNOPSIS
265
266 Centreon open-tickets api
267
268 =head1 REST API OPTIONS
269
270 =over 8
271
272 =item B<--api-hostname>
273
274 Centreon address.
275
276 =item B<--url-path>
277
278 API url path (default: '/centreon/api/')
279
280 =item B<--api-port>
281
282 API port (default: 80)
283
284 =item B<--api-proto>
285
286 Specify https if needed (default: 'http')
287
288 =item B<--api-username>
289
290 API username
291
292 =item B<--api-password>
293
294 API password
295
296 =item B<--api-timeout>
297
298 HTTP timeout
299
300 =back
301
302 =head1 DESCRIPTION
303
304 B<custom>.
305
306 =cut