Debian Codegraph @

src/apps/proxmox/ve/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::ve::restapi::custom::api;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::http;
28 use centreon::plugins::statefile;
29 use JSON::XS;
30 use Digest::MD5 qw(md5_hex);
31
32 sub new {
33 my ($class, %options) = @_;
34 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
35 bless $self, $class;
36
37 if (!defined($options{output})) {
38 print "Class Custom: Need to specify 'output' argument.\n";
39 exit 3;
40 }
41 if (!defined($options{options})) {
42 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
43 $options{output}->option_exit();
44 }
45
46 if (!defined($options{noptions})) {
47 $options{options}->add_options(arguments => {
48 'hostname:s' => { name => 'hostname' },
49 'port:s' => { name => 'port'},
50 'proto:s' => { name => 'proto' },
51 'api-username:s' => { name => 'api_username' },
52 'api-password:s' => { name => 'api_password' },
53 'realm:s' => { name => 'realm' },
54 'timeout:s' => { name => 'timeout' },
55 'reload-cache-time:s' => { name => 'reload_cache_time', default => 7200 }
56 });
57 }
58
59 $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
60
61 $self->{output} = $options{output};
62 $self->{http} = centreon::plugins::http->new(%options);
63 $self->{cache} = centreon::plugins::statefile->new(%options);
64
65 return $self;
66 }
67
68 sub set_options {
69 my ($self, %options) = @_;
70
71 $self->{option_results} = $options{option_results};
72 }
73
74 sub set_defaults {}
75
76 sub check_options {
77 my ($self, %options) = @_;
78
79 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
80 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 8006;
81 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
82 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
83 $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
84 $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
85 $self->{realm} = (defined($self->{option_results}->{realm})) ? $self->{option_results}->{realm} : 'pam';
86
87 if ($self->{hostname} eq '') {
88 $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
89 $self->{output}->option_exit();
90 }
91 if ($self->{api_username} eq '') {
92 $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
93 $self->{output}->option_exit();
94 }
95 if ($self->{api_password} eq '') {
96 $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
97 $self->{output}->option_exit();
98 }
99
100 $self->{cache}->check_options(option_results => $self->{option_results});
101
102 return 0;
103 }
104
105 sub get_port {
106 my ($self, %options) = @_;
107
108 return $self->{option_results}->{port};
109 }
110
111 sub get_hostnames {
112 my ($self, %options) = @_;
113
114 return $self->{hostname};
115 }
116
117 sub build_options_for_httplib {
118 my ($self, %options) = @_;
119
120 $self->{option_results}->{hostname} = $self->{hostname};
121 $self->{option_results}->{port} = $self->{port};
122 $self->{option_results}->{proto} = $self->{proto};
123 $self->{option_results}->{timeout} = $self->{timeout};
124 $self->{option_results}->{warning_status} = '';
125 $self->{option_results}->{critical_status} = '';
126 $self->{option_results}->{unknown_status} = '%{http_code} < 200 or %{http_code} >= 300';
127 }
128
129 sub settings {
130 my ($self, %options) = @_;
131
132 $self->build_options_for_httplib();
133 $self->{http}->add_header(key => 'Accept', value => 'application/json');
134 $self->{http}->add_header(key => 'Content-Type', value => 'application/x-www-form-urlencoded');
135 if (defined($self->{ticket})) {
136 $self->{http}->add_header(key => 'Cookie', value => 'PVEAuthCookie=' . $self->{ticket});
137 }
138 $self->{http}->set_options(%{$self->{option_results}});
139 }
140
141 sub get_ticket {
142 my ($self, %options) = @_;
143
144 my $has_cache_file = $options{statefile}->read(statefile => 'proxmox_ve_api_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username}));
145 my $expires_on = $options{statefile}->get(name => 'expires_on');
146 my $ticket = $options{statefile}->get(name => 'ticket');
147
148 if ($has_cache_file == 0 || !defined($ticket) || (($expires_on - time()) < 10)) {
149 my $post_data = 'username=' . $self->{api_username} .
150 '&password=' . $self->{api_password} .
151 '&realm=' . $self->{realm};
152
153 $self->settings();
154
155 my $content = $self->{http}->request(
156 method => 'POST', query_form_post => $post_data,
157 url_path => '/api2/json/access/ticket'
158 );
159
160 my $decoded;
161 eval {
162 $decoded = JSON::XS->new->utf8->decode($content);
163 };
164 if ($@) {
165 $self->{output}->output_add(long_msg => $content, debug => 1);
166 $self->{output}->add_option_msg(short_msg => "Cannot decode json response");
167 $self->{output}->option_exit();
168 }
169 if (!defined($decoded->{data}->{ticket})) {
170 $self->{output}->output_add(long_msg => $content, debug => 1);
171 $self->{output}->add_option_msg(short_msg => "Error retrieving ticket");
172 $self->{output}->option_exit();
173 }
174
175 $ticket = $decoded->{data}->{ticket};
176 my $datas = { last_timestamp => time(), ticket => $ticket, expires_on => time() + 7200 };
177 $options{statefile}->write(data => $datas);
178 }
179
180 return $ticket;
181 }
182
183 sub request_api {
184 my ($self, %options) = @_;
185
186 if (!defined($self->{ticket})) {
187 $self->{ticket} = $self->get_ticket(statefile => $self->{cache});
188 }
189
190 $self->settings();
191
192 my $content = $self->{http}->request(%options);
193
194 my $decoded;
195 eval {
196 $decoded = JSON::XS->new->utf8->decode($content);
197 };
198 if ($@) {
199 $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
200 $self->{output}->option_exit();
201 }
202 if (!defined($decoded->{data})) {
203 $self->{output}->add_option_msg(short_msg => "Error while retrieving data (add --debug option for detailed message)");
204 $self->{output}->option_exit();
205 }
206
207 return $decoded->{data};
208 }
209
210 sub get_version {
211 my ($self, %options) = @_;
212
213 my $content = $self->request_api(method => 'GET', url_path =>'/api2/json/version');
214 return $content->{version};
215 }
216
217 sub internal_api_list_vms {
218 my ($self, %options) = @_;
219
220 my $vm = $self->request_api(method => 'GET', url_path =>'/api2/json/cluster/resources?type=vm');
221 return $vm;
222 }
223
224 sub api_list_vms {
225 my ($self, %options) = @_;
226
227 my $vms = {};
228 my $list_vms = $self->internal_api_list_vms();
229 foreach my $vm (@{$list_vms}) {
230 my ($type, $vmid) = split(/\//,$vm->{id});
231 $vms->{$vm->{id}} = {
232 State => $vm->{status},
233 Type => $type,
234 Vmid => $vmid,
235 Node => $vm->{node},
236 Name => $vm->{name}
237 };
238 }
239
240 return $vms;
241 }
242
243 sub internal_api_list_nodes {
244 my ($self, %options) = @_;
245
246 my $nodes = $self->request_api(method => 'GET', url_path =>'/api2/json/cluster/resources?type=node');
247 return $nodes;
248 }
249
250 sub api_list_nodes {
251 my ($self, %options) = @_;
252
253 my $nodes = {};
254 my $list_nodes = $self->internal_api_list_nodes();
255 foreach my $node (@{$list_nodes}) {
256 $nodes->{$node->{id}} = {
257 State => $node->{status},
258 Name => $node->{node}
259 };
260 }
261
262 return $nodes;
263 }
264
265 sub internal_api_list_storages {
266 my ($self, %options) = @_;
267
268 my $storage = $self->request_api(method => 'GET', url_path =>'/api2/json/cluster/resources?type=storage');
269 return $storage;
270 }
271
272 sub api_list_storages {
273 my ($self, %options) = @_;
274
275 my $storages = {};
276 my $list_storages = $self->internal_api_list_storages();
277 foreach my $storage (@{$list_storages}) {
278 $storages->{$storage->{id}} = {
279 State => $storage->{status},
280 Node => $storage->{node},
281 Name => $storage->{storage}
282 };
283 }
284
285 return $storages;
286 }
287
288 sub cache_vms {
289 my ($self, %options) = @_;
290
291 my $has_cache_file = $options{statefile}->read(statefile => 'cache_proxmox_vm_'.$self->{hostname} . '_' . $self->{port});
292 my $timestamp_cache = $options{statefile}->get(name => 'last_timestamp');
293 my $vms = $options{statefile}->get(name => 'vms');
294 if ($has_cache_file == 0 || !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($options{reload_cache_time})))) {
295 $vms = {};
296 my $list_vms = $self->internal_api_list_vms();
297 foreach my $vm (@$list_vms) {
298 $vms->{$vm->{id}} = {
299 State => $vm->{status},
300 Node => $vm->{node},
301 Name => $vm->{name}
302 };
303 }
304 $options{statefile}->write(data => $vms);
305 }
306
307 return $vms;
308 }
309
310 sub cache_nodes {
311 my ($self, %options) = @_;
312
313 my $has_cache_file = $options{statefile}->read(statefile => 'cache_proxmox_node_' . $self->{hostname} . '_' . $self->{port});
314 my $timestamp_cache = $options{statefile}->get(name => 'last_timestamp');
315 my $nodes = $options{statefile}->get(name => 'nodes');
316 if ($has_cache_file == 0 || !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($options{reload_cache_time})))) {
317 $nodes = {};
318 my $list_nodes = $self->internal_api_list_nodes();
319 foreach my $node (@{$list_nodes}) {
320 $nodes->{ $node->{id} } = {
321 State => $node->{status},
322 Name => $node->{node}
323 };
324 }
325 $options{statefile}->write(data => $nodes);
326 }
327
328 return $nodes;
329 }
330
331 sub cache_storages {
332 my ($self, %options) = @_;
333
334 my $has_cache_file = $options{statefile}->read(statefile => 'cache_proxmox_storage_' . $self->{hostname} . '_' . $self->{port});
335 my $timestamp_cache = $options{statefile}->get(name => 'last_timestamp');
336 my $storages = $options{statefile}->get(name => 'storages');
337 if ($has_cache_file == 0 || !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($options{reload_cache_time})))) {
338 $storages = {};
339 my $list_storages = $self->internal_api_list_storages();
340 foreach my $storage (@{$list_storages}) {
341 $storages->{$storage->{id}} = {
342 State => $storage->{status},
343 Node => $storage->{node},
344 Name => $storage->{storage}
345 };
346 }
347 $options{statefile}->write(data => $storages);
348 }
349
350 return $storages;
351 }
352
353 sub internal_api_get_vm_stats {
354 my ($self, %options) = @_;
355
356 my $vm_stats = $self->request_api(method => 'GET', url_path => '/api2/json/nodes/' . $options{node_id} . '/' . $options{vm_id} . '/status/current');
357 return $vm_stats;
358 }
359
360 sub internal_api_get_node_stats {
361 my ($self, %options) = @_;
362
363 my (undef, $node) = split(/\//, $options{node_id});
364
365 my $node_stats = $self->request_api(method => 'GET', url_path => '/api2/json/nodes/' . $node . '/status');
366 return $node_stats;
367 }
368
369 sub internal_api_get_storage_stats {
370 my ($self, %options) = @_;
371
372 my (undef, $node, $storage) = split(/\//, $options{storage_id});
373
374 my $storage_stats = $self->request_api(method => 'GET', url_path => '/api2/json/nodes/' . $node . '/storage/' . $storage . '/status');
375 return $storage_stats;
376 }
377
378 sub internal_api_get_vm_node {
379 my ($self, %options) = @_;
380
381 my $list_vms = $self->internal_api_list_vms();
382 my $node = '';
383 foreach my $vm (@{$list_vms}) {
384 if ($vm->{id} eq $options{vm_id}) {
385 $node = $vm->{node};
386 last;
387 }
388 }
389 return $node;
390 }
391
392 sub api_get_vms {
393 my ($self, %options) = @_;
394
395 my $content_total = $self->cache_vms(statefile => $options{statefile});
396
397 if (defined($options{vm_id}) && $options{vm_id} ne '') {
398 if (defined($content_total->{ $options{vm_id} })) {
399 $content_total->{ $options{vm_id} }->{Stats} = $self->internal_api_get_vm_stats(
400 node_id => $content_total->{ $options{vm_id} }->{Node},
401 vm_id => $options{vm_id}
402 );
403 }
404 } elsif (defined($options{vm_name}) && $options{vm_name} ne '') {
405 my $vm_id;
406 foreach (keys %$content_total) {
407 if ($content_total->{$_}->{Name} eq $options{vm_name}) {
408 $vm_id = $_;
409 last;
410 }
411 }
412 if (defined($vm_id)) {
413 $content_total->{$vm_id}->{Stats} = $self->internal_api_get_vm_stats(node_id => $content_total->{$vm_id}->{Node}, vm_id => $vm_id);
414 }
415 } else {
416 foreach my $vm_id (keys %$content_total) {
417 $content_total->{$vm_id}->{Stats} = $self->internal_api_get_vm_stats(node_id => $content_total->{$vm_id}->{Node}, vm_id => $vm_id);
418 }
419 }
420
421 return $content_total;
422 }
423
424 sub api_get_nodes {
425 my ($self, %options) = @_;
426
427 my $content_total = $self->cache_nodes(statefile => $options{statefile});
428
429 if (defined($options{node_id}) && $options{node_id} ne '') {
430 if (defined($content_total->{$options{node_id}})) {
431 $content_total->{$options{node_id}}->{Stats} = $self->internal_api_get_node_stats(node_id => $options{node_id});
432 }
433 } elsif (defined($options{node_name}) && $options{node_name} ne '') {
434 my $node_id;
435 foreach (keys %{$content_total}) {
436 if ($content_total->{$_}->{Name} eq $options{node_name}) {
437 $node_id = $_;
438 last;
439 }
440 }
441 if (defined($node_id)) {
442 $content_total->{$node_id}->{Stats} = $self->internal_api_get_node_stats(node_id => $node_id);
443 }
444 } else {
445 foreach my $node_id (keys %{$content_total}) {
446 $content_total->{$node_id}->{Stats} = $self->internal_api_get_node_stats(node_id => $node_id);
447 }
448 }
449
450 return $content_total;
451 }
452
453 sub api_get_storages {
454 my ($self, %options) = @_;
455
456 my $storages = $self->cache_storages(statefile => $options{statefile});
457
458 if (defined($options{storage_id}) && $options{storage_id} ne '') {
459 if (defined($storages->{ $options{storage_id} })) {
460 $storages->{ $options{storage_id} }->{Stats} = $self->internal_api_get_storage_stats(storage_id => $options{storage_id});
461 }
462 } elsif (defined($options{node_id}) && $options{node_id} ne '') {
463 my $nodes = $self->cache_nodes(statefile => $options{statefile});
464 foreach my $node_id (keys %$nodes) {
465 if ($node_id eq $options{node_id}) {
466 foreach my $storage_id (keys %$storages) {
467 if ($storages->{$storage_id}->{Node} eq $nodes->{$node_id}->{Name}) {
468 $storages->{$storage_id}->{Stats} = $self->internal_api_get_storage_stats(storage_id => $storage_id);
469 }
470 }
471 }
472 }
473 } elsif (defined($options{node_name}) && $options{node_name} ne '') {
474 foreach my $storage_id (keys %$storages) {
475 if ($storages->{$storage_id}->{Node} eq $options{node_name}) {
476 $storages->{$storage_id}->{Stats} = $self->internal_api_get_storage_stats(storage_id => $storage_id);
477 }
478 }
479 } elsif (defined($options{storage_name}) && $options{storage_name} ne '') {
480 foreach my $storage_id (keys %$storages) {
481 if ($storages->{$storage_id}->{Name} eq $options{storage_name}) {
482 $storages->{$storage_id}->{Stats} = $self->internal_api_get_storage_stats(storage_id => $storage_id);
483 }
484 }
485 } else {
486 foreach my $storage_id (keys %$storages) {
487 $storages->{$storage_id}->{Stats} = $self->internal_api_get_storage_stats(storage_id => $storage_id);
488 }
489 }
490
491 return $storages;
492 }
493
494 1;
495
496 __END__
497
498 =head1 NAME
499
500 Proxmox VE Rest API
501
502 =head1 REST API OPTIONS
503
504 Proxmox Rest API
505
506 More Info about Proxmox VE API on https://pve.proxmox.com/wiki/Proxmox_VE_API
507
508 =over 8
509
510 =item B<--hostname>
511
512 Set hostname or IP of Proxmox VE Cluster node
513
514 =item B<--port>
515
516 Set Proxmox VE Port (default: '8006')
517
518 =item B<--proto>
519
520 Specify https if needed (default: 'https').
521
522 =item B<--api-username>
523
524 Set Proxmox VE Username
525 API user need to have this privileges
526 'VM.Monitor, VM.Audit, Datastore.Audit, Sys.Audit, Sys.Syslog'
527
528 =item B<--api-password>
529
530 Set Proxmox VE Password
531
532 =item B<--realm>
533
534 Set Proxmox VE Realm (pam, pve or custom) (default: 'pam').
535
536 =item B<--timeout>
537
538 Threshold for HTTP timeout.
539
540 =back
541
542 =cut