Debian Codegraph @

src/apps/proxmox/mg/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::proxmox::mg::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 'hostname:s' => { name => 'hostname' },
47 'port:s' => { name => 'port' },
48 'proto:s' => { name => 'proto' },
49 'url-path:s' => { name => 'url_path' },
50 'api-username:s' => { name => 'api_username' },
51 'api-password:s' => { name => 'api_password' },
52 'realm:s' => { name => 'realm' },
53 'timeout:s' => { name => 'timeout' }
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
62 return $self;
63 }
64
65 sub set_options {
66 my ($self, %options) = @_;
67
68 $self->{option_results} = $options{option_results};
69 }
70
71 sub set_defaults {}
72
73 sub check_options {
74 my ($self, %options) = @_;
75
76 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
77 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
78 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 8006;
79 $self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/api2/json';
80 $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
81 $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
82 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30;
83 $self->{realm} = (defined($self->{option_results}->{realm})) ? $self->{option_results}->{realm} : 'pmg';
84 $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300' ;
85 $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
86 $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
87
88 if ($self->{hostname} eq '') {
89 $self->{output}->add_option_msg(short_msg => 'Need to specify hostname option.');
90 $self->{output}->option_exit();
91 }
92 if ($self->{api_username} eq '') {
93 $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
94 $self->{output}->option_exit();
95 }
96 if ($self->{api_password} eq '') {
97 $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
98 $self->{output}->option_exit();
99 }
100
101 $self->{cache}->check_options(option_results => $self->{option_results});
102 return 0;
103 }
104
105 sub get_connection_infos {
106 my ($self, %options) = @_;
107
108 return $self->{hostname} . '_' . $self->{http}->get_port();
109 }
110
111 sub get_hostname {
112 my ($self, %options) = @_;
113
114 return $self->{hostname};
115 }
116
117 sub get_port {
118 my ($self, %options) = @_;
119
120 return $self->{port};
121 }
122
123 sub json_decode {
124 my ($self, %options) = @_;
125
126 $options{content} =~ s/\r//mg;
127 my $decoded;
128 eval {
129 $decoded = JSON::XS->new->utf8->decode($options{content});
130 };
131 if ($@) {
132 $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
133 $self->{output}->option_exit();
134 }
135
136 return $decoded;
137 }
138
139 sub build_options_for_httplib {
140 my ($self, %options) = @_;
141
142 $self->{option_results}->{hostname} = $self->{hostname};
143 $self->{option_results}->{port} = $self->{port};
144 $self->{option_results}->{proto} = $self->{proto};
145 }
146
147 sub settings {
148 my ($self, %options) = @_;
149
150 $self->build_options_for_httplib();
151 $self->{http}->add_header(key => 'Accept', value => 'application/json');
152 $self->{http}->set_options(%{$self->{option_results}});
153 }
154
155 sub clean_session {
156 my ($self, %options) = @_;
157
158 my $datas = {};
159 $options{statefile}->write(data => $datas);
160 $self->{cookie_id} = undef;
161 $self->{cookie_user} = undef;
162 }
163
164 sub get_session {
165 my ($self, %options) = @_;
166
167 my $has_cache_file = $options{statefile}->read(statefile => 'proxmox_mg_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username}));
168 my $cookie_id = $options{statefile}->get(name => 'cookie_id');
169
170 if ($has_cache_file == 0 || !defined($cookie_id)) {
171 my ($content) = $self->{http}->request(
172 method => 'POST',
173 url_path => $self->{url_path} . '/access/ticket',
174 post_param => [
175 'username=' . $self->{api_username},
176 'password=' . $self->{api_password},
177 'realm=' . $self->{realm}
178 ],
179 warning_status => '', unknown_status => '', critical_status => ''
180 );
181
182 if ($self->{http}->get_code() != 200) {
183 $self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
184 $self->{output}->option_exit();
185 }
186
187 my $decoded = $self->json_decode(content => $content);
188 if (!defined($decoded->{data}->{ticket})) {
189 $self->{output}->add_option_msg(short_msg => "Cannot get token");
190 $self->{output}->option_exit();
191 }
192
193 $cookie_id = $decoded->{data}->{ticket};
194 $options{statefile}->write(data => { cookie_id => $cookie_id });
195 }
196
197 $self->{cookie_id} = $cookie_id;
198 }
199
200 sub request {
201 my ($self, %options) = @_;
202
203 my $endpoint = $options{full_endpoint};
204 if (!defined($endpoint)) {
205 $endpoint = $self->{url_path} . $options{endpoint};
206 }
207
208 $self->settings();
209 if (!defined($self->{access_token})) {
210 $self->get_session(statefile => $self->{cache});
211 }
212
213 my $content = $self->{http}->request(
214 method => 'GET',
215 url_path => $endpoint,
216 get_param => $options{get_param},
217 header => [
218 'Cookie: PMGAuthCookie=' . $self->{cookie_id}
219 ],
220 warning_status => '',
221 unknown_status => '',
222 critical_status => ''
223 );
224
225 # Maybe there is an issue with the token. So we retry.
226 if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
227 $self->clean_session(statefile => $self->{cache});
228 $self->get_session(statefile => $self->{cache});
229 $content = $self->{http}->request(
230 url_path => $endpoint,
231 get_param => $options{get_param},
232 header => [
233 'Cookie: PMGAuthCookie=' . $self->{cookie_id}
234 ],
235 unknown_status => $self->{unknown_http_status},
236 warning_status => $self->{warning_http_status},
237 critical_status => $self->{critical_http_status}
238 );
239 }
240
241 my $decoded = $self->json_decode(content => $content);
242 if (!defined($decoded)) {
243 $self->{output}->add_option_msg(short_msg => 'Error while retrieving data (add --debug option for detailed message)');
244 $self->{output}->option_exit();
245 }
246
247 return $decoded;
248 }
249
250 1;
251
252 __END__
253
254 =head1 NAME
255
256 Proxmog Media Gateway API
257
258 =head1 SYNOPSIS
259
260 Proxmog Media Gateway api
261
262 =head1 REST API OPTIONS
263
264 =over 8
265
266 =item B<--hostname>
267
268 API hostname.
269
270 =item B<--url-path>
271
272 API url path (default: '/api2/json')
273
274 =item B<--port>
275
276 API port (default: 8806)
277
278 =item B<--proto>
279
280 Specify https if needed (default: 'https')
281
282 =item B<--api-username>
283
284 Set API username
285
286 =item B<--api-password>
287
288 Set API password
289
290 =item B<--realm>
291
292 Set user realm (default: 'pmg')
293
294 =item B<--timeout>
295
296 Set HTTP timeout
297
298 =back
299
300 =head1 DESCRIPTION
301
302 B<custom>.
303
304 =cut