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