1 package PVE
::API2
::Cluster
;
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);
13 use PVE
::GuestHelpers
;
15 use PVE
::HA
::Env
::PVE2
;
17 use PVE
::JSONSchema
qw(get_standard_option);
19 use PVE
::RPCEnvironment
;
22 use PVE
::Tools
qw(extract_param);
24 use PVE
::API2
::ACMEAccount
;
25 use PVE
::API2
::ACMEPlugin
;
26 use PVE
::API2
::Backup
;
27 use PVE
::API2
::Cluster
::BackupInfo
;
28 use PVE
::API2
::Cluster
::Ceph
;
29 use PVE
::API2
::Cluster
::Jobs
;
30 use PVE
::API2
::Cluster
::MetricServer
;
31 use PVE
::API2
::ClusterConfig
;
32 use PVE
::API2
::Firewall
::Cluster
;
33 use PVE
::API2
::HAConfig
;
34 use PVE
::API2
::ReplicationConfig
;
38 require PVE
::API2
::Network
::SDN
;
42 use base
qw(PVE::RESTHandler);
44 __PACKAGE__-
>register_method ({
45 subclass
=> "PVE::API2::ReplicationConfig",
46 path
=> 'replication',
49 __PACKAGE__-
>register_method ({
50 subclass
=> "PVE::API2::Cluster::MetricServer",
54 __PACKAGE__-
>register_method ({
55 subclass
=> "PVE::API2::ClusterConfig",
59 __PACKAGE__-
>register_method ({
60 subclass
=> "PVE::API2::Firewall::Cluster",
64 __PACKAGE__-
>register_method ({
65 subclass
=> "PVE::API2::Backup",
69 __PACKAGE__-
>register_method ({
70 subclass
=> "PVE::API2::Cluster::BackupInfo",
71 path
=> 'backup-info',
74 __PACKAGE__-
>register_method ({
75 subclass
=> "PVE::API2::HAConfig",
79 __PACKAGE__-
>register_method ({
80 subclass
=> "PVE::API2::ACMEAccount",
84 __PACKAGE__-
>register_method ({
85 subclass
=> "PVE::API2::Cluster::Ceph",
89 __PACKAGE__-
>register_method ({
90 subclass
=> "PVE::API2::Cluster::Jobs",
94 __PACKAGE__-
>register_method ({
95 subclass
=> "PVE::API2::Network::SDN",
100 my $dc_schema = PVE
::DataCenterConfig
::get_datacenter_schema
();
101 my $dc_properties = {
103 type
=> 'string', format
=> 'pve-configid-list',
104 description
=> "A list of settings you want to delete.",
108 foreach my $opt (keys %{$dc_schema->{properties
}}) {
109 $dc_properties->{$opt} = $dc_schema->{properties
}->{$opt};
112 __PACKAGE__-
>register_method ({
116 description
=> "Cluster index.",
117 permissions
=> { user
=> 'all' },
119 additionalProperties
=> 0,
128 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
135 { name
=> 'backup' },
136 { name
=> 'backup-info' },
138 { name
=> 'config' },
139 { name
=> 'firewall' },
143 { name
=> 'metrics' },
144 { name
=> 'nextid' },
145 { name
=> 'options' },
146 { name
=> 'replication' },
147 { name
=> 'resources' },
148 { name
=> 'status' },
153 push(@{$result}, { name
=> 'sdn' });
159 __PACKAGE__-
>register_method({
163 description
=> "Read cluster log",
164 permissions
=> { user
=> 'all' },
166 additionalProperties
=> 0,
170 description
=> "Maximum number of entries.",
186 my $rpcenv = PVE
::RPCEnvironment
::get
();
188 my $max = $param->{max
} || 0;
189 my $user = $rpcenv->get_user();
191 my $admin = $rpcenv->check($user, "/", [ 'Sys.Syslog' ], 1);
193 my $loguser = $admin ?
'' : $user;
195 my $res = decode_json
(PVE
::Cluster
::get_cluster_log
($loguser, $max));
197 foreach my $entry (@{$res->{data
}}) {
198 $entry->{id
} = "$entry->{uid}:$entry->{node}";
204 __PACKAGE__-
>register_method({
208 description
=> "Resources index (cluster wide).",
209 permissions
=> { user
=> 'all' },
211 additionalProperties
=> 0,
216 enum
=> ['vm', 'storage', 'node', 'sdn'],
226 description
=> "Resource id.",
230 description
=> "Resource type.",
232 enum
=> ['node', 'storage', 'pool', 'qemu', 'lxc', 'openvz', 'sdn'],
235 description
=> "Resource type dependent status.",
240 description
=> "Name of the resource.",
244 node
=> get_standard_option
('pve-node', {
245 description
=> "The cluster node name (when type in node,storage,qemu,lxc).",
248 storage
=> get_standard_option
('pve-storage-id', {
249 description
=> "The storage identifier (when type == storage).",
253 description
=> "The pool name (when type in pool,qemu,lxc).",
258 description
=> "CPU utilization (when type in node,qemu,lxc).",
262 renderer
=> 'fraction_as_percentage',
265 description
=> "Number of available CPUs (when type in node,qemu,lxc).",
271 description
=> "Used memory in bytes (when type in node,qemu,lxc).",
278 description
=> "Number of available memory in bytes (when type in node,qemu,lxc).",
284 description
=> "Support level (when type == node).",
289 description
=> "Node uptime in seconds (when type in node,qemu,lxc).",
292 renderer
=> 'duration',
295 description
=> "HA service status (for HA managed VMs).",
300 description
=> "Used disk space in bytes (when type in storage), used root image spave for VMs (type in qemu,lxc).",
307 description
=> "Storage size in bytes (when type in storage), root image size for VMs (type in qemu,lxc).",
314 description
=> "Allowed storage content types (when type == storage).",
316 format
=> 'pve-storage-content-list',
320 description
=> "More specific type, if available.",
325 description
=> "The numerical vmid (when type in qemu,lxc).",
331 description
=> "The cgroup mode the node operates under (when type == node).",
341 my $rpcenv = PVE
::RPCEnvironment
::get
();
342 my $authuser = $rpcenv->get_user();
343 my $usercfg = $rpcenv->{user_cfg
};
347 my $nodelist = PVE
::Cluster
::get_nodelist
();
348 my $members = PVE
::Cluster
::get_members
();
350 my $rrd = PVE
::Cluster
::rrd_dump
();
352 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
353 my $idlist = $vmlist->{ids
} || {};
355 my $hastatus = PVE
::HA
::Config
::read_manager_status
();
356 my $haresources = PVE
::HA
::Config
::read_resources_config
();
363 if (!$param->{type
} || $param->{type
} eq 'pool') {
364 for my $pool (sort keys %{$usercfg->{pools
}}) {
365 my $d = $usercfg->{pools
}->{$pool};
367 next if !$rpcenv->check($authuser, "/pool/$pool", [ 'Pool.Audit' ], 1);
375 $pooldata->{$pool} = $entry;
381 # we try to generate 'numbers' by using "$X + 0"
382 if (!$param->{type
} || $param->{type
} eq 'vm') {
383 my $prop_list = [qw(lock tags)];
384 my $props = PVE
::Cluster
::get_guest_config_properties
($prop_list);
386 for my $vmid (sort keys %$idlist) {
388 my $data = $idlist->{$vmid};
389 my $entry = PVE
::API2Tools
::extract_vm_stats
($vmid, $data, $rrd);
391 if (my $pool = $usercfg->{vms
}->{$vmid}) {
392 $entry->{pool
} = $pool;
393 if (my $pe = $pooldata->{$pool}) {
394 if ($entry->{uptime
}) {
395 $pe->{uptime
} = $entry->{uptime
} if !$pe->{uptime
} || $entry->{uptime
} > $pe->{uptime
};
396 $pe->{mem
} = 0 if !$pe->{mem
};
397 $pe->{mem
} += $entry->{mem
};
398 $pe->{maxmem
} = 0 if !$pe->{maxmem
};
399 $pe->{maxmem
} += $entry->{maxmem
};
400 $pe->{cpu
} = 0 if !$pe->{cpu
};
401 $pe->{maxcpu
} = 0 if !$pe->{maxcpu
};
403 # we do not know how much cpus there are in the cluster at this moment
404 # so we calculate the current % of the cpu
405 # but we had already the old cpu % before this vm, so:
406 # new% = (old%*oldmax + cur%*curmax) / (oldmax+curmax)
407 $pe->{cpu
} = (($pe->{cpu
} * $pe->{maxcpu
}) + ($entry->{cpu
} * $entry->{maxcpu
})) / ($pe->{maxcpu
} + $entry->{maxcpu
});
408 $pe->{maxcpu
} += $entry->{maxcpu
};
413 # only skip now to next to ensure that the pool stats above are filled, if eligible
414 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
416 for my $prop (@$prop_list) {
417 if (defined(my $value = $props->{$vmid}->{$prop})) {
418 $entry->{$prop} = $value;
422 if (defined($entry->{pool
}) &&
423 !$rpcenv->check($authuser, "/pool/$entry->{pool}", ['Pool.Audit'], 1)) {
424 delete $entry->{pool
};
428 if (my $hatype = $hatypemap->{$entry->{type
}}) {
429 my $sid = "$hatype:$vmid";
431 if ($service = $hastatus->{service_status
}->{$sid}) {
432 $entry->{hastate
} = $service->{state};
433 } elsif ($service = $haresources->{ids
}->{$sid}) {
434 $entry->{hastate
} = $service->{state};
442 my $static_node_info = PVE
::Cluster
::get_node_kv
("static-info");
444 if (!$param->{type
} || $param->{type
} eq 'node') {
445 foreach my $node (@$nodelist) {
446 my $can_audit = $rpcenv->check($authuser, "/nodes/$node", [ 'Sys.Audit' ], 1);
447 my $entry = PVE
::API2Tools
::extract_node_stats
($node, $members, $rrd, !$can_audit);
449 my $info = eval { decode_json
($static_node_info->{$node}); };
450 if (defined(my $mode = $info->{'cgroup-mode'})) {
451 $entry->{'cgroup-mode'} = int($mode);
458 if (!$param->{type
} || $param->{type
} eq 'storage') {
460 my $cfg = PVE
::Storage
::config
();
461 my @sids = PVE
::Storage
::storage_ids
($cfg);
463 foreach my $storeid (@sids) {
464 next if !$rpcenv->check($authuser, "/storage/$storeid", [ 'Datastore.Audit' ], 1);
466 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
467 # we create a entry for each node
468 foreach my $node (@$nodelist) {
469 next if !PVE
::Storage
::storage_check_enabled
($cfg, $storeid, $node, 1);
471 my $entry = PVE
::API2Tools
::extract_storage_stats
($storeid, $scfg, $node, $rrd);
478 if (!$param->{type
} || $param->{type
} eq 'sdn') {
480 my $nodes = PVE
::Cluster
::get_node_kv
("sdn");
482 for my $node (sort keys %{$nodes}) {
483 my $sdns = decode_json
($nodes->{$node});
485 for my $id (sort keys %{$sdns}) {
486 next if !$rpcenv->check($authuser, "/sdn/zones/$id", [ 'SDN.Audit' ], 1);
487 my $sdn = $sdns->{$id};
489 id
=> "sdn/$node/$id",
493 status
=> $sdn->{'status'},
504 __PACKAGE__-
>register_method({
508 description
=> "List recent tasks (cluster wide).",
509 permissions
=> { user
=> 'all' },
511 additionalProperties
=> 0,
519 upid
=> { type
=> 'string' },
526 my $rpcenv = PVE
::RPCEnvironment
::get
();
527 my $authuser = $rpcenv->get_user();
529 my $tlist = PVE
::Cluster
::get_tasklist
();
530 return [] if !$tlist;
532 my $all = $rpcenv->check($authuser, "/", [ 'Sys.Audit' ], 1);
535 foreach my $task (@$tlist) {
536 if (PVE
::AccessControl
::pve_verify_tokenid
($task->{user
}, 1)) {
537 ($task->{user
}, $task->{tokenid
}) = PVE
::AccessControl
::split_tokenid
($task->{user
});
539 push @$res, $task if $all || ($task->{user
} eq $authuser);
545 __PACKAGE__-
>register_method({
546 name
=> 'get_options',
549 description
=> "Get datacenter options. Without 'Sys.Audit' on '/' not all options are returned.",
552 check
=> ['perm', '/', [ 'Sys.Audit' ]],
555 additionalProperties
=> 0,
567 my $rpcenv = PVE
::RPCEnvironment
::get
();
568 my $authuser = $rpcenv->get_user();
570 my $datacenter_config = eval { PVE
::Cluster
::cfs_read_file
('datacenter.cfg') } // {};
572 if ($rpcenv->check($authuser, '/', ['Sys.Audit'], 1)) {
573 $res = $datacenter_config;
575 for my $k (qw(console tag-style)) {
576 $res->{$k} = $datacenter_config->{$k} if exists $datacenter_config->{$k};
580 my $tags = PVE
::GuestHelpers
::get_allowed_tags
($rpcenv, $authuser);
581 $res->{'allowed-tags'} = [sort keys $tags->%*];
586 __PACKAGE__-
>register_method({
587 name
=> 'set_options',
590 description
=> "Set datacenter options.",
592 check
=> ['perm', '/', [ 'Sys.Modify' ]],
596 additionalProperties
=> 0,
597 properties
=> $dc_properties,
599 returns
=> { type
=> "null" },
603 my $delete = extract_param
($param, 'delete');
605 cfs_lock_file
('datacenter.cfg', undef, sub {
606 my $conf = cfs_read_file
('datacenter.cfg');
608 $conf->{$_} = $param->{$_} for keys $param->%*;
610 delete $conf->{$_} for PVE
::Tools
::split_list
($delete);
612 cfs_write_file
('datacenter.cfg', $conf);
619 __PACKAGE__-
>register_method({
620 name
=> 'get_status',
623 description
=> "Get cluster status information.",
625 check
=> ['perm', '/', [ 'Sys.Audit' ]],
629 additionalProperties
=> 0,
639 enum
=> ['cluster', 'node'],
640 description
=> 'Indicates the type, either cluster or node. The type defines the object properties e.g. quorate available for type cluster.'
651 description
=> '[cluster] Nodes count, including offline nodes.',
656 description
=> '[cluster] Current version of the corosync configuration file.',
661 description
=> '[cluster] Indicates if there is a majority of nodes online to make decisions',
666 description
=> '[node] ID of the node from the corosync configuration.',
671 description
=> '[node] IP of the resolved nodename.',
676 description
=> '[node] Indicates if this is the responding node.',
681 description
=> '[node] Indicates if the node is online or offline.',
686 description
=> '[node] Proxmox VE Subscription level, indicates if eligible for enterprise support as well as access to the stable Proxmox VE Enterprise Repository.',
694 # make sure we get current info
695 PVE
::Cluster
::cfs_update
();
697 # we also add info from pmxcfs
698 my $clinfo = PVE
::Cluster
::get_clinfo
();
699 my $members = PVE
::Cluster
::get_members
();
700 my $nodename = PVE
::INotify
::nodename
();
701 my $rrd = PVE
::Cluster
::rrd_dump
();
706 if (my $d = $clinfo->{cluster
}) {
710 nodes
=> $d->{nodes
},
711 version
=> $d->{version
},
713 quorate
=> $d->{quorate
},
717 foreach my $node (keys %$members) {
718 my $d = $members->{$node};
724 'local' => ($node eq $nodename) ?
1 : 0,
725 online
=> $d->{online
},
728 if (defined($d->{ip
})) {
729 $entry->{ip
} = $d->{ip
};
732 if (my $d = PVE
::API2Tools
::extract_node_stats
($node, $members, $rrd)) {
733 $entry->{level
} = $d->{level
} || '';
740 # fake entry for local node if no cluster defined
741 my $pmxcfs = ($clinfo && $clinfo->{version
}) ?
1 : 0; # pmxcfs online ?
743 my $subinfo = PVE
::API2
::Subscription
::read_etc_subscription
();
744 my $sublevel = $subinfo->{level
} || '';
748 id
=> "node/$nodename",
750 ip
=> scalar(PVE
::Cluster
::remote_node_ip
($nodename)),
759 __PACKAGE__-
>register_method({
763 description
=> "Get next free VMID. Pass a VMID to assert that its free (at time of check).",
764 permissions
=> { user
=> 'all' },
766 additionalProperties
=> 0,
768 vmid
=> get_standard_option
('pve-vmid', {
775 description
=> "The next free VMID.",
780 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
781 my $idlist = $vmlist->{ids
} || {};
783 if (my $vmid = $param->{vmid
}) {
784 return $vmid if !defined($idlist->{$vmid});
785 raise_param_exc
({ vmid
=> "VM $vmid already exists" });
788 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
789 my $next_id = $dc_conf->{'next-id'} // {};
791 my $lower = $next_id->{lower
} // 100;
792 my $upper = $next_id->{upper
} // (1000 * 1000); # note, lower than the schema-maximum
794 for (my $i = $lower; $i < $upper; $i++) {
795 return $i if !defined($idlist->{$i});
798 die "unable to get any free VMID in range [$lower, $upper]\n";