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);
14 use PVE
::HA
::Env
::PVE2
;
16 use PVE
::JSONSchema
qw(get_standard_option);
18 use PVE
::RPCEnvironment
;
21 use PVE
::Tools
qw(extract_param);
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
;
37 require PVE
::API2
::Network
::SDN
;
41 use base
qw(PVE::RESTHandler);
43 __PACKAGE__-
>register_method ({
44 subclass
=> "PVE::API2::ReplicationConfig",
45 path
=> 'replication',
48 __PACKAGE__-
>register_method ({
49 subclass
=> "PVE::API2::Cluster::MetricServer",
53 __PACKAGE__-
>register_method ({
54 subclass
=> "PVE::API2::ClusterConfig",
58 __PACKAGE__-
>register_method ({
59 subclass
=> "PVE::API2::Firewall::Cluster",
63 __PACKAGE__-
>register_method ({
64 subclass
=> "PVE::API2::Backup",
68 __PACKAGE__-
>register_method ({
69 subclass
=> "PVE::API2::Cluster::BackupInfo",
70 path
=> 'backup-info',
73 __PACKAGE__-
>register_method ({
74 subclass
=> "PVE::API2::HAConfig",
78 __PACKAGE__-
>register_method ({
79 subclass
=> "PVE::API2::ACMEAccount",
83 __PACKAGE__-
>register_method ({
84 subclass
=> "PVE::API2::Cluster::Ceph",
88 __PACKAGE__-
>register_method ({
89 subclass
=> "PVE::API2::Cluster::Jobs",
93 __PACKAGE__-
>register_method ({
94 subclass
=> "PVE::API2::Network::SDN",
99 my $dc_schema = PVE
::DataCenterConfig
::get_datacenter_schema
();
100 my $dc_properties = {
102 type
=> 'string', format
=> 'pve-configid-list',
103 description
=> "A list of settings you want to delete.",
107 foreach my $opt (keys %{$dc_schema->{properties
}}) {
108 $dc_properties->{$opt} = $dc_schema->{properties
}->{$opt};
111 __PACKAGE__-
>register_method ({
115 description
=> "Cluster index.",
116 permissions
=> { user
=> 'all' },
118 additionalProperties
=> 0,
127 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
134 { name
=> 'backup' },
135 { name
=> 'backup-info' },
137 { name
=> 'config' },
138 { name
=> 'firewall' },
142 { name
=> 'metrics' },
143 { name
=> 'nextid' },
144 { name
=> 'options' },
145 { name
=> 'replication' },
146 { name
=> 'resources' },
147 { name
=> 'status' },
152 push(@{$result}, { name
=> 'sdn' });
158 __PACKAGE__-
>register_method({
162 description
=> "Read cluster log",
163 permissions
=> { user
=> 'all' },
165 additionalProperties
=> 0,
169 description
=> "Maximum number of entries.",
185 my $rpcenv = PVE
::RPCEnvironment
::get
();
187 my $max = $param->{max
} || 0;
188 my $user = $rpcenv->get_user();
190 my $admin = $rpcenv->check($user, "/", [ 'Sys.Syslog' ], 1);
192 my $loguser = $admin ?
'' : $user;
194 my $res = decode_json
(PVE
::Cluster
::get_cluster_log
($loguser, $max));
196 foreach my $entry (@{$res->{data
}}) {
197 $entry->{id
} = "$entry->{uid}:$entry->{node}";
203 __PACKAGE__-
>register_method({
207 description
=> "Resources index (cluster wide).",
208 permissions
=> { user
=> 'all' },
210 additionalProperties
=> 0,
215 enum
=> ['vm', 'storage', 'node', 'sdn'],
224 id
=> { type
=> 'string' },
226 description
=> "Resource type.",
228 enum
=> ['node', 'storage', 'pool', 'qemu', 'lxc', 'openvz', 'sdn'],
231 description
=> "Resource type dependent status.",
236 description
=> "Name of the resource.",
240 node
=> get_standard_option
('pve-node', {
241 description
=> "The cluster node name (when type in node,storage,qemu,lxc).",
244 storage
=> get_standard_option
('pve-storage-id', {
245 description
=> "The storage identifier (when type == storage).",
249 description
=> "The pool name (when type in pool,qemu,lxc).",
254 description
=> "CPU utilization (when type in node,qemu,lxc).",
258 renderer
=> 'fraction_as_percentage',
261 description
=> "Number of available CPUs (when type in node,qemu,lxc).",
267 description
=> "Used memory in bytes (when type in node,qemu,lxc).",
274 description
=> "Number of available memory in bytes (when type in node,qemu,lxc).",
280 description
=> "Support level (when type == node).",
285 description
=> "Node uptime in seconds (when type in node,qemu,lxc).",
288 renderer
=> 'duration',
291 description
=> "HA service status (for HA managed VMs).",
296 description
=> "Used disk space in bytes (when type in storage), used root image spave for VMs (type in qemu,lxc).",
303 description
=> "Storage size in bytes (when type in storage), root image size for VMs (type in qemu,lxc).",
310 description
=> "Allowed storage content types (when type == storage).",
312 format
=> 'pve-storage-content-list',
316 description
=> "More specific type, if available.",
321 description
=> "The numerical vmid (when type in qemu,lxc).",
327 description
=> "The cgroup mode the node operates under (when type == node).",
337 my $rpcenv = PVE
::RPCEnvironment
::get
();
338 my $authuser = $rpcenv->get_user();
339 my $usercfg = $rpcenv->{user_cfg
};
343 my $nodelist = PVE
::Cluster
::get_nodelist
();
344 my $members = PVE
::Cluster
::get_members
();
346 my $rrd = PVE
::Cluster
::rrd_dump
();
348 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
349 my $idlist = $vmlist->{ids
} || {};
351 my $hastatus = PVE
::HA
::Config
::read_manager_status
();
352 my $haresources = PVE
::HA
::Config
::read_resources_config
();
359 if (!$param->{type
} || $param->{type
} eq 'pool') {
360 for my $pool (sort keys %{$usercfg->{pools
}}) {
361 my $d = $usercfg->{pools
}->{$pool};
363 next if !$rpcenv->check($authuser, "/pool/$pool", [ 'Pool.Audit' ], 1);
371 $pooldata->{$pool} = $entry;
377 # we try to generate 'numbers' by using "$X + 0"
378 if (!$param->{type
} || $param->{type
} eq 'vm') {
379 my $locked_vms = PVE
::Cluster
::get_guest_config_property
('lock');
381 for my $vmid (sort keys %$idlist) {
383 my $data = $idlist->{$vmid};
384 my $entry = PVE
::API2Tools
::extract_vm_stats
($vmid, $data, $rrd);
386 if (my $pool = $usercfg->{vms
}->{$vmid}) {
387 $entry->{pool
} = $pool;
388 if (my $pe = $pooldata->{$pool}) {
389 if ($entry->{uptime
}) {
390 $pe->{uptime
} = $entry->{uptime
} if !$pe->{uptime
} || $entry->{uptime
} > $pe->{uptime
};
391 $pe->{mem
} = 0 if !$pe->{mem
};
392 $pe->{mem
} += $entry->{mem
};
393 $pe->{maxmem
} = 0 if !$pe->{maxmem
};
394 $pe->{maxmem
} += $entry->{maxmem
};
395 $pe->{cpu
} = 0 if !$pe->{cpu
};
396 $pe->{maxcpu
} = 0 if !$pe->{maxcpu
};
398 # we do not know how much cpus there are in the cluster at this moment
399 # so we calculate the current % of the cpu
400 # but we had already the old cpu % before this vm, so:
401 # new% = (old%*oldmax + cur%*curmax) / (oldmax+curmax)
402 $pe->{cpu
} = (($pe->{cpu
} * $pe->{maxcpu
}) + ($entry->{cpu
} * $entry->{maxcpu
})) / ($pe->{maxcpu
} + $entry->{maxcpu
});
403 $pe->{maxcpu
} += $entry->{maxcpu
};
408 # only skip now to next to ensure that the pool stats above are filled, if eligible
409 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
411 if (defined(my $lock = $locked_vms->{$vmid}->{lock})) {
412 $entry->{lock} = $lock;
415 if (defined($entry->{pool
}) &&
416 !$rpcenv->check($authuser, "/pool/$entry->{pool}", ['Pool.Audit'], 1)) {
417 delete $entry->{pool
};
421 if (my $hatype = $hatypemap->{$entry->{type
}}) {
422 my $sid = "$hatype:$vmid";
424 if ($service = $hastatus->{service_status
}->{$sid}) {
425 $entry->{hastate
} = $service->{state};
426 } elsif ($service = $haresources->{ids
}->{$sid}) {
427 $entry->{hastate
} = $service->{state};
435 my $static_node_info = PVE
::Cluster
::get_node_kv
("static-info");
437 if (!$param->{type
} || $param->{type
} eq 'node') {
438 foreach my $node (@$nodelist) {
439 my $can_audit = $rpcenv->check($authuser, "/nodes/$node", [ 'Sys.Audit' ], 1);
440 my $entry = PVE
::API2Tools
::extract_node_stats
($node, $members, $rrd, !$can_audit);
442 my $info = eval { decode_json
($static_node_info->{$node}); };
443 if (defined(my $mode = $info->{'cgroup-mode'})) {
444 $entry->{'cgroup-mode'} = int($mode);
451 if (!$param->{type
} || $param->{type
} eq 'storage') {
453 my $cfg = PVE
::Storage
::config
();
454 my @sids = PVE
::Storage
::storage_ids
($cfg);
456 foreach my $storeid (@sids) {
457 next if !$rpcenv->check($authuser, "/storage/$storeid", [ 'Datastore.Audit' ], 1);
459 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
460 # we create a entry for each node
461 foreach my $node (@$nodelist) {
462 next if !PVE
::Storage
::storage_check_enabled
($cfg, $storeid, $node, 1);
464 my $entry = PVE
::API2Tools
::extract_storage_stats
($storeid, $scfg, $node, $rrd);
471 if (!$param->{type
} || $param->{type
} eq 'sdn') {
473 my $nodes = PVE
::Cluster
::get_node_kv
("sdn");
475 for my $node (sort keys %{$nodes}) {
476 my $sdns = decode_json
($nodes->{$node});
478 for my $id (sort keys %{$sdns}) {
479 next if !$rpcenv->check($authuser, "/sdn/zones/$id", [ 'SDN.Audit' ], 1);
480 my $sdn = $sdns->{$id};
482 id
=> "sdn/$node/$id",
486 status
=> $sdn->{'status'},
497 __PACKAGE__-
>register_method({
501 description
=> "List recent tasks (cluster wide).",
502 permissions
=> { user
=> 'all' },
504 additionalProperties
=> 0,
512 upid
=> { type
=> 'string' },
519 my $rpcenv = PVE
::RPCEnvironment
::get
();
520 my $authuser = $rpcenv->get_user();
522 my $tlist = PVE
::Cluster
::get_tasklist
();
523 return [] if !$tlist;
525 my $all = $rpcenv->check($authuser, "/", [ 'Sys.Audit' ], 1);
528 foreach my $task (@$tlist) {
529 if (PVE
::AccessControl
::pve_verify_tokenid
($task->{user
}, 1)) {
530 ($task->{user
}, $task->{tokenid
}) = PVE
::AccessControl
::split_tokenid
($task->{user
});
532 push @$res, $task if $all || ($task->{user
} eq $authuser);
538 __PACKAGE__-
>register_method({
539 name
=> 'get_options',
542 description
=> "Get datacenter options.",
544 check
=> ['perm', '/', [ 'Sys.Audit' ]],
547 additionalProperties
=> 0,
557 return PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
560 __PACKAGE__-
>register_method({
561 name
=> 'set_options',
564 description
=> "Set datacenter options.",
566 check
=> ['perm', '/', [ 'Sys.Modify' ]],
570 additionalProperties
=> 0,
571 properties
=> $dc_properties,
573 returns
=> { type
=> "null" },
577 my $delete = extract_param
($param, 'delete');
579 cfs_lock_file
('datacenter.cfg', undef, sub {
580 my $conf = cfs_read_file
('datacenter.cfg');
582 $conf->{$_} = $param->{$_} for keys $param->%*;
584 delete $conf->{$_} for PVE
::Tools
::split_list
($delete);
586 cfs_write_file
('datacenter.cfg', $conf);
593 __PACKAGE__-
>register_method({
594 name
=> 'get_status',
597 description
=> "Get cluster status information.",
599 check
=> ['perm', '/', [ 'Sys.Audit' ]],
603 additionalProperties
=> 0,
613 enum
=> ['cluster', 'node'],
614 description
=> 'Indicates the type, either cluster or node. The type defines the object properties e.g. quorate available for type cluster.'
625 description
=> '[cluster] Nodes count, including offline nodes.',
630 description
=> '[cluster] Current version of the corosync configuration file.',
635 description
=> '[cluster] Indicates if there is a majority of nodes online to make decisions',
640 description
=> '[node] ID of the node from the corosync configuration.',
645 description
=> '[node] IP of the resolved nodename.',
650 description
=> '[node] Indicates if this is the responding node.',
655 description
=> '[node] Indicates if the node is online or offline.',
660 description
=> '[node] Proxmox VE Subscription level, indicates if eligible for enterprise support as well as access to the stable Proxmox VE Enterprise Repository.',
668 # make sure we get current info
669 PVE
::Cluster
::cfs_update
();
671 # we also add info from pmxcfs
672 my $clinfo = PVE
::Cluster
::get_clinfo
();
673 my $members = PVE
::Cluster
::get_members
();
674 my $nodename = PVE
::INotify
::nodename
();
675 my $rrd = PVE
::Cluster
::rrd_dump
();
680 if (my $d = $clinfo->{cluster
}) {
684 nodes
=> $d->{nodes
},
685 version
=> $d->{version
},
687 quorate
=> $d->{quorate
},
691 foreach my $node (keys %$members) {
692 my $d = $members->{$node};
698 'local' => ($node eq $nodename) ?
1 : 0,
699 online
=> $d->{online
},
702 if (defined($d->{ip
})) {
703 $entry->{ip
} = $d->{ip
};
706 if (my $d = PVE
::API2Tools
::extract_node_stats
($node, $members, $rrd)) {
707 $entry->{level
} = $d->{level
} || '';
714 # fake entry for local node if no cluster defined
715 my $pmxcfs = ($clinfo && $clinfo->{version
}) ?
1 : 0; # pmxcfs online ?
717 my $subinfo = PVE
::API2
::Subscription
::read_etc_subscription
();
718 my $sublevel = $subinfo->{level
} || '';
722 id
=> "node/$nodename",
724 ip
=> scalar(PVE
::Cluster
::remote_node_ip
($nodename)),
733 __PACKAGE__-
>register_method({
737 description
=> "Get next free VMID. Pass a VMID to assert that its free (at time of check).",
738 permissions
=> { user
=> 'all' },
740 additionalProperties
=> 0,
742 vmid
=> get_standard_option
('pve-vmid', {
749 description
=> "The next free VMID.",
754 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
755 my $idlist = $vmlist->{ids
} || {};
757 if (my $vmid = $param->{vmid
}) {
758 return $vmid if !defined($idlist->{$vmid});
759 raise_param_exc
({ vmid
=> "VM $vmid already exists" });
762 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
763 my $next_id = $dc_conf->{'next-id'} // {};
765 my $lower = $next_id->{lower
} // 100;
766 my $upper = $next_id->{upper
} // (1000 * 1000); # note, lower than the schema-maximum
768 for (my $i = $lower; $i < $upper; $i++) {
769 return $i if !defined($idlist->{$i});
772 die "unable to get any free VMID in range [$lower, $upper]\n";