]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Cluster.pm
subscription: switch to Proxmox::RS::Subscription
[pve-manager.git] / PVE / API2 / Cluster.pm
1 package PVE::API2::Cluster;
2
3 use strict;
4 use warnings;
5
6 use JSON;
7
8 use PVE::API2Tools;
9 use PVE::Cluster qw(cfs_register_file cfs_lock_file cfs_read_file cfs_write_file);
10 use PVE::DataCenterConfig;
11 use PVE::Exception qw(raise_param_exc);
12 use PVE::Firewall;
13 use PVE::HA::Config;
14 use PVE::HA::Env::PVE2;
15 use PVE::INotify;
16 use PVE::JSONSchema qw(get_standard_option);
17 use PVE::RESTHandler;
18 use PVE::RPCEnvironment;
19 use PVE::SafeSyslog;
20 use PVE::Storage;
21 use PVE::Tools qw(extract_param);
22
23 use PVE::API2::ACMEAccount;
24 use PVE::API2::ACMEPlugin;
25 use PVE::API2::Backup;
26 use PVE::API2::Cluster::BackupInfo;
27 use PVE::API2::Cluster::Ceph;
28 use PVE::API2::Cluster::Jobs;
29 use PVE::API2::Cluster::MetricServer;
30 use PVE::API2::ClusterConfig;
31 use PVE::API2::Firewall::Cluster;
32 use PVE::API2::HAConfig;
33 use PVE::API2::ReplicationConfig;
34
35 my $have_sdn;
36 eval {
37 require PVE::API2::Network::SDN;
38 $have_sdn = 1;
39 };
40
41 use base qw(PVE::RESTHandler);
42
43 __PACKAGE__->register_method ({
44 subclass => "PVE::API2::ReplicationConfig",
45 path => 'replication',
46 });
47
48 __PACKAGE__->register_method ({
49 subclass => "PVE::API2::Cluster::MetricServer",
50 path => 'metrics',
51 });
52
53 __PACKAGE__->register_method ({
54 subclass => "PVE::API2::ClusterConfig",
55 path => 'config',
56 });
57
58 __PACKAGE__->register_method ({
59 subclass => "PVE::API2::Firewall::Cluster",
60 path => 'firewall',
61 });
62
63 __PACKAGE__->register_method ({
64 subclass => "PVE::API2::Backup",
65 path => 'backup',
66 });
67
68 __PACKAGE__->register_method ({
69 subclass => "PVE::API2::Cluster::BackupInfo",
70 path => 'backup-info',
71 });
72
73 __PACKAGE__->register_method ({
74 subclass => "PVE::API2::HAConfig",
75 path => 'ha',
76 });
77
78 __PACKAGE__->register_method ({
79 subclass => "PVE::API2::ACMEAccount",
80 path => 'acme',
81 });
82
83 __PACKAGE__->register_method ({
84 subclass => "PVE::API2::Cluster::Ceph",
85 path => 'ceph',
86 });
87
88 __PACKAGE__->register_method ({
89 subclass => "PVE::API2::Cluster::Jobs",
90 path => 'jobs',
91 });
92 if ($have_sdn) {
93 __PACKAGE__->register_method ({
94 subclass => "PVE::API2::Network::SDN",
95 path => 'sdn',
96 });
97 }
98
99 my $dc_schema = PVE::DataCenterConfig::get_datacenter_schema();
100 my $dc_properties = {
101 delete => {
102 type => 'string', format => 'pve-configid-list',
103 description => "A list of settings you want to delete.",
104 optional => 1,
105 }
106 };
107 foreach my $opt (keys %{$dc_schema->{properties}}) {
108 $dc_properties->{$opt} = $dc_schema->{properties}->{$opt};
109 }
110
111 __PACKAGE__->register_method ({
112 name => 'index',
113 path => '',
114 method => 'GET',
115 description => "Cluster index.",
116 permissions => { user => 'all' },
117 parameters => {
118 additionalProperties => 0,
119 properties => {},
120 },
121 returns => {
122 type => 'array',
123 items => {
124 type => "object",
125 properties => {},
126 },
127 links => [ { rel => 'child', href => "{name}" } ],
128 },
129 code => sub {
130 my ($param) = @_;
131
132 my $result = [
133 { name => 'acme' },
134 { name => 'backup' },
135 { name => 'backup-info' },
136 { name => 'ceph' },
137 { name => 'config' },
138 { name => 'firewall' },
139 { name => 'ha' },
140 { name => 'jobs' },
141 { name => 'log' },
142 { name => 'metrics' },
143 { name => 'nextid' },
144 { name => 'options' },
145 { name => 'replication' },
146 { name => 'resources' },
147 { name => 'status' },
148 { name => 'tasks' },
149 ];
150
151 if ($have_sdn) {
152 push(@{$result}, { name => 'sdn' });
153 }
154
155 return $result;
156 }});
157
158 __PACKAGE__->register_method({
159 name => 'log',
160 path => 'log',
161 method => 'GET',
162 description => "Read cluster log",
163 permissions => { user => 'all' },
164 parameters => {
165 additionalProperties => 0,
166 properties => {
167 max => {
168 type => 'integer',
169 description => "Maximum number of entries.",
170 optional => 1,
171 minimum => 1,
172 }
173 },
174 },
175 returns => {
176 type => 'array',
177 items => {
178 type => "object",
179 properties => {},
180 },
181 },
182 code => sub {
183 my ($param) = @_;
184
185 my $rpcenv = PVE::RPCEnvironment::get();
186
187 my $max = $param->{max} || 0;
188 my $user = $rpcenv->get_user();
189
190 my $admin = $rpcenv->check($user, "/", [ 'Sys.Syslog' ], 1);
191
192 my $loguser = $admin ? '' : $user;
193
194 my $res = decode_json(PVE::Cluster::get_cluster_log($loguser, $max));
195
196 foreach my $entry (@{$res->{data}}) {
197 $entry->{id} = "$entry->{uid}:$entry->{node}";
198 }
199
200 return $res->{data};
201 }});
202
203 __PACKAGE__->register_method({
204 name => 'resources',
205 path => 'resources',
206 method => 'GET',
207 description => "Resources index (cluster wide).",
208 permissions => { user => 'all' },
209 parameters => {
210 additionalProperties => 0,
211 properties => {
212 type => {
213 type => 'string',
214 optional => 1,
215 enum => ['vm', 'storage', 'node', 'sdn'],
216 },
217 },
218 },
219 returns => {
220 type => 'array',
221 items => {
222 type => "object",
223 properties => {
224 id => { type => 'string' },
225 type => {
226 description => "Resource type.",
227 type => 'string',
228 enum => ['node', 'storage', 'pool', 'qemu', 'lxc', 'openvz', 'sdn'],
229 },
230 status => {
231 description => "Resource type dependent status.",
232 type => 'string',
233 optional => 1,
234 },
235 name => {
236 description => "Name of the resource.",
237 type => 'string',
238 optional => 1,
239 },
240 node => get_standard_option('pve-node', {
241 description => "The cluster node name (when type in node,storage,qemu,lxc).",
242 optional => 1,
243 }),
244 storage => get_standard_option('pve-storage-id', {
245 description => "The storage identifier (when type == storage).",
246 optional => 1,
247 }),
248 pool => {
249 description => "The pool name (when type in pool,qemu,lxc).",
250 type => 'string',
251 optional => 1,
252 },
253 cpu => {
254 description => "CPU utilization (when type in node,qemu,lxc).",
255 type => 'number',
256 optional => 1,
257 renderer => 'fraction_as_percentage',
258 },
259 maxcpu => {
260 description => "Number of available CPUs (when type in node,qemu,lxc).",
261 type => 'number',
262 optional => 1,
263 },
264 mem => {
265 description => "Used memory in bytes (when type in node,qemu,lxc).",
266 type => 'string',
267 optional => 1,
268 renderer => 'bytes',
269 },
270 maxmem => {
271 description => "Number of available memory in bytes (when type in node,qemu,lxc).",
272 type => 'integer',
273 optional => 1,
274 renderer => 'bytes',
275 },
276 level => {
277 description => "Support level (when type == node).",
278 type => 'string',
279 optional => 1,
280 },
281 uptime => {
282 description => "Node uptime in seconds (when type in node,qemu,lxc).",
283 type => 'integer',
284 optional => 1,
285 renderer => 'duration',
286 },
287 hastate => {
288 description => "HA service status (for HA managed VMs).",
289 type => 'string',
290 optional => 1,
291 },
292 disk => {
293 description => "Used disk space in bytes (when type in storage), used root image spave for VMs (type in qemu,lxc).",
294 type => 'string',
295 optional => 1,
296 renderer => 'bytes',
297 },
298 maxdisk => {
299 description => "Storage size in bytes (when type in storage), root image size for VMs (type in qemu,lxc).",
300 type => 'integer',
301 optional => 1,
302 renderer => 'bytes',
303 },
304 content => {
305 description => "Allowed storage content types (when type == storage).",
306 type => 'string',
307 format => 'pve-storage-content-list',
308 optional => 1,
309 },
310 plugintype => {
311 description => "More specific type, if available.",
312 type => 'string',
313 optional => 1,
314 },
315 },
316 },
317 },
318 code => sub {
319 my ($param) = @_;
320
321 my $rpcenv = PVE::RPCEnvironment::get();
322 my $authuser = $rpcenv->get_user();
323 my $usercfg = $rpcenv->{user_cfg};
324
325 my $res = [];
326
327 my $nodelist = PVE::Cluster::get_nodelist();
328 my $members = PVE::Cluster::get_members();
329
330 my $rrd = PVE::Cluster::rrd_dump();
331
332 my $vmlist = PVE::Cluster::get_vmlist() || {};
333 my $idlist = $vmlist->{ids} || {};
334
335 my $hastatus = PVE::HA::Config::read_manager_status();
336 my $haresources = PVE::HA::Config::read_resources_config();
337 my $hatypemap = {
338 'qemu' => 'vm',
339 'lxc' => 'ct'
340 };
341
342 my $pooldata = {};
343 if (!$param->{type} || $param->{type} eq 'pool') {
344 for my $pool (sort keys %{$usercfg->{pools}}) {
345 my $d = $usercfg->{pools}->{$pool};
346
347 next if !$rpcenv->check($authuser, "/pool/$pool", [ 'Pool.Audit' ], 1);
348
349 my $entry = {
350 id => "/pool/$pool",
351 pool => $pool,
352 type => 'pool',
353 };
354
355 $pooldata->{$pool} = $entry;
356
357 push @$res, $entry;
358 }
359 }
360
361 # we try to generate 'numbers' by using "$X + 0"
362 if (!$param->{type} || $param->{type} eq 'vm') {
363 my $locked_vms = PVE::Cluster::get_guest_config_property('lock');
364
365 for my $vmid (sort keys %$idlist) {
366
367 my $data = $idlist->{$vmid};
368 my $entry = PVE::API2Tools::extract_vm_stats($vmid, $data, $rrd);
369
370 if (my $pool = $usercfg->{vms}->{$vmid}) {
371 $entry->{pool} = $pool;
372 if (my $pe = $pooldata->{$pool}) {
373 if ($entry->{uptime}) {
374 $pe->{uptime} = $entry->{uptime} if !$pe->{uptime} || $entry->{uptime} > $pe->{uptime};
375 $pe->{mem} = 0 if !$pe->{mem};
376 $pe->{mem} += $entry->{mem};
377 $pe->{maxmem} = 0 if !$pe->{maxmem};
378 $pe->{maxmem} += $entry->{maxmem};
379 $pe->{cpu} = 0 if !$pe->{cpu};
380 $pe->{maxcpu} = 0 if !$pe->{maxcpu};
381 # explanation:
382 # we do not know how much cpus there are in the cluster at this moment
383 # so we calculate the current % of the cpu
384 # but we had already the old cpu % before this vm, so:
385 # new% = (old%*oldmax + cur%*curmax) / (oldmax+curmax)
386 $pe->{cpu} = (($pe->{cpu} * $pe->{maxcpu}) + ($entry->{cpu} * $entry->{maxcpu})) / ($pe->{maxcpu} + $entry->{maxcpu});
387 $pe->{maxcpu} += $entry->{maxcpu};
388 }
389 }
390 }
391
392 # only skip now to next to ensure that the pool stats above are filled, if eligible
393 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
394
395 if (defined(my $lock = $locked_vms->{$vmid}->{lock})) {
396 $entry->{lock} = $lock;
397 }
398
399 if (defined($entry->{pool}) &&
400 !$rpcenv->check($authuser, "/pool/$entry->{pool}", ['Pool.Audit'], 1)) {
401 delete $entry->{pool};
402 }
403
404 # get ha status
405 if (my $hatype = $hatypemap->{$entry->{type}}) {
406 my $sid = "$hatype:$vmid";
407 my $service;
408 if ($service = $hastatus->{service_status}->{$sid}) {
409 $entry->{hastate} = $service->{state};
410 } elsif ($service = $haresources->{ids}->{$sid}) {
411 $entry->{hastate} = $service->{state};
412 }
413 }
414
415 push @$res, $entry;
416 }
417 }
418
419 if (!$param->{type} || $param->{type} eq 'node') {
420 foreach my $node (@$nodelist) {
421 my $can_audit = $rpcenv->check($authuser, "/nodes/$node", [ 'Sys.Audit' ], 1);
422 my $entry = PVE::API2Tools::extract_node_stats($node, $members, $rrd, !$can_audit);
423 push @$res, $entry;
424 }
425 }
426
427 if (!$param->{type} || $param->{type} eq 'storage') {
428
429 my $cfg = PVE::Storage::config();
430 my @sids = PVE::Storage::storage_ids ($cfg);
431
432 foreach my $storeid (@sids) {
433 next if !$rpcenv->check($authuser, "/storage/$storeid", [ 'Datastore.Audit' ], 1);
434
435 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
436 # we create a entry for each node
437 foreach my $node (@$nodelist) {
438 next if !PVE::Storage::storage_check_enabled($cfg, $storeid, $node, 1);
439
440 my $entry = PVE::API2Tools::extract_storage_stats($storeid, $scfg, $node, $rrd);
441 push @$res, $entry;
442 }
443 }
444 }
445
446 if ($have_sdn) {
447 if (!$param->{type} || $param->{type} eq 'sdn') {
448
449 my $nodes = PVE::Cluster::get_node_kv("sdn");
450
451 for my $node (sort keys %{$nodes}) {
452 my $sdns = decode_json($nodes->{$node});
453
454 for my $id (sort keys %{$sdns}) {
455 next if !$rpcenv->check($authuser, "/sdn/zones/$id", [ 'SDN.Audit' ], 1);
456 my $sdn = $sdns->{$id};
457 my $entry = {
458 id => "sdn/$node/$id",
459 sdn => $id,
460 node => $node,
461 type => 'sdn',
462 status => $sdn->{'status'},
463 };
464 push @$res, $entry;
465 }
466 }
467 }
468 }
469
470 return $res;
471 }});
472
473 __PACKAGE__->register_method({
474 name => 'tasks',
475 path => 'tasks',
476 method => 'GET',
477 description => "List recent tasks (cluster wide).",
478 permissions => { user => 'all' },
479 parameters => {
480 additionalProperties => 0,
481 properties => {},
482 },
483 returns => {
484 type => 'array',
485 items => {
486 type => "object",
487 properties => {
488 upid => { type => 'string' },
489 },
490 },
491 },
492 code => sub {
493 my ($param) = @_;
494
495 my $rpcenv = PVE::RPCEnvironment::get();
496 my $authuser = $rpcenv->get_user();
497
498 my $tlist = PVE::Cluster::get_tasklist();
499 return [] if !$tlist;
500
501 my $all = $rpcenv->check($authuser, "/", [ 'Sys.Audit' ], 1);
502
503 my $res = [];
504 foreach my $task (@$tlist) {
505 if (PVE::AccessControl::pve_verify_tokenid($task->{user}, 1)) {
506 ($task->{user}, $task->{tokenid}) = PVE::AccessControl::split_tokenid($task->{user});
507 }
508 push @$res, $task if $all || ($task->{user} eq $authuser);
509 }
510
511 return $res;
512 }});
513
514 __PACKAGE__->register_method({
515 name => 'get_options',
516 path => 'options',
517 method => 'GET',
518 description => "Get datacenter options.",
519 permissions => {
520 check => ['perm', '/', [ 'Sys.Audit' ]],
521 },
522 parameters => {
523 additionalProperties => 0,
524 properties => {},
525 },
526 returns => {
527 type => "object",
528 properties => {},
529 },
530 code => sub {
531 my ($param) = @_;
532
533 return PVE::Cluster::cfs_read_file('datacenter.cfg');
534 }});
535
536 __PACKAGE__->register_method({
537 name => 'set_options',
538 path => 'options',
539 method => 'PUT',
540 description => "Set datacenter options.",
541 permissions => {
542 check => ['perm', '/', [ 'Sys.Modify' ]],
543 },
544 protected => 1,
545 parameters => {
546 additionalProperties => 0,
547 properties => $dc_properties,
548 },
549 returns => { type => "null" },
550 code => sub {
551 my ($param) = @_;
552
553 my $delete = extract_param($param, 'delete');
554
555 cfs_lock_file('datacenter.cfg', undef, sub {
556 my $conf = cfs_read_file('datacenter.cfg');
557
558 $conf->{$_} = $param->{$_} for keys $param->%*;
559
560 delete $conf->{$_} for PVE::Tools::split_list($delete);
561
562 cfs_write_file('datacenter.cfg', $conf);
563 });
564 die $@ if $@;
565
566 return undef;
567 }});
568
569 __PACKAGE__->register_method({
570 name => 'get_status',
571 path => 'status',
572 method => 'GET',
573 description => "Get cluster status information.",
574 permissions => {
575 check => ['perm', '/', [ 'Sys.Audit' ]],
576 },
577 protected => 1,
578 parameters => {
579 additionalProperties => 0,
580 properties => {},
581 },
582 returns => {
583 type => 'array',
584 items => {
585 type => "object",
586 properties => {
587 type => {
588 type => 'string',
589 enum => ['cluster', 'node'],
590 description => 'Indicates the type, either cluster or node. The type defines the object properties e.g. quorate available for type cluster.'
591 },
592 id => {
593 type => 'string',
594 },
595 name => {
596 type => 'string',
597 },
598 nodes => {
599 type => 'integer',
600 optional => 1,
601 description => '[cluster] Nodes count, including offline nodes.',
602 },
603 version => {
604 type => 'integer',
605 optional => 1,
606 description => '[cluster] Current version of the corosync configuration file.',
607 },
608 quorate => {
609 type => 'boolean',
610 optional => 1,
611 description => '[cluster] Indicates if there is a majority of nodes online to make decisions',
612 },
613 nodeid => {
614 type => 'integer',
615 optional => 1,
616 description => '[node] ID of the node from the corosync configuration.',
617 },
618 ip => {
619 type => 'string',
620 optional => 1,
621 description => '[node] IP of the resolved nodename.',
622 },
623 'local' => {
624 type => 'boolean',
625 optional => 1,
626 description => '[node] Indicates if this is the responding node.',
627 },
628 online => {
629 type => 'boolean',
630 optional => 1,
631 description => '[node] Indicates if the node is online or offline.',
632 },
633 level => {
634 type => 'string',
635 optional => 1,
636 description => '[node] Proxmox VE Subscription level, indicates if eligible for enterprise support as well as access to the stable Proxmox VE Enterprise Repository.',
637 }
638 },
639 },
640 },
641 code => sub {
642 my ($param) = @_;
643
644 # make sure we get current info
645 PVE::Cluster::cfs_update();
646
647 # we also add info from pmxcfs
648 my $clinfo = PVE::Cluster::get_clinfo();
649 my $members = PVE::Cluster::get_members();
650 my $nodename = PVE::INotify::nodename();
651 my $rrd = PVE::Cluster::rrd_dump();
652
653 if ($members) {
654 my $res = [];
655
656 if (my $d = $clinfo->{cluster}) {
657 push @$res, {
658 type => 'cluster',
659 id => 'cluster',
660 nodes => $d->{nodes},
661 version => $d->{version},
662 name => $d->{name},
663 quorate => $d->{quorate},
664 };
665 }
666
667 foreach my $node (keys %$members) {
668 my $d = $members->{$node};
669 my $entry = {
670 type => 'node',
671 id => "node/$node",
672 name => $node,
673 nodeid => $d->{id},
674 'local' => ($node eq $nodename) ? 1 : 0,
675 online => $d->{online},
676 };
677
678 if (defined($d->{ip})) {
679 $entry->{ip} = $d->{ip};
680 }
681
682 if (my $d = PVE::API2Tools::extract_node_stats($node, $members, $rrd)) {
683 $entry->{level} = $d->{level} || '';
684 }
685
686 push @$res, $entry;
687 }
688 return $res;
689 } else {
690 # fake entry for local node if no cluster defined
691 my $pmxcfs = ($clinfo && $clinfo->{version}) ? 1 : 0; # pmxcfs online ?
692
693 my $subinfo = PVE::API2::Subscription::read_etc_subscription();
694 my $sublevel = $subinfo->{level} || '';
695
696 return [{
697 type => 'node',
698 id => "node/$nodename",
699 name => $nodename,
700 ip => scalar(PVE::Cluster::remote_node_ip($nodename)),
701 'local' => 1,
702 nodeid => 0,
703 online => 1,
704 level => $sublevel,
705 }];
706 }
707 }});
708
709 __PACKAGE__->register_method({
710 name => 'nextid',
711 path => 'nextid',
712 method => 'GET',
713 description => "Get next free VMID. Pass a VMID to assert that its free (at time of check).",
714 permissions => { user => 'all' },
715 parameters => {
716 additionalProperties => 0,
717 properties => {
718 vmid => get_standard_option('pve-vmid', {
719 optional => 1,
720 }),
721 },
722 },
723 returns => {
724 type => 'integer',
725 description => "The next free VMID.",
726 },
727 code => sub {
728 my ($param) = @_;
729
730 my $vmlist = PVE::Cluster::get_vmlist() || {};
731 my $idlist = $vmlist->{ids} || {};
732
733 if (my $vmid = $param->{vmid}) {
734 return $vmid if !defined($idlist->{$vmid});
735 raise_param_exc({ vmid => "VM $vmid already exists" });
736 }
737
738 my $dc_conf = PVE::Cluster::cfs_read_file('datacenter.cfg');
739 my $next_id = $dc_conf->{'next-id'} // {};
740
741 my $lower = $next_id->{lower} // 100;
742 my $upper = $next_id->{upper} // (1000 * 1000); # note, lower than the schema-maximum
743
744 for (my $i = $lower; $i < $upper; $i++) {
745 return $i if !defined($idlist->{$i});
746 }
747
748 die "unable to get any free VMID in range [$lower, $upper]\n";
749 }});
750
751 1;