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).",
257 renderer
=> 'fraction_as_percentage',
260 description
=> "Number of available CPUs (when type in node,qemu,lxc).",
265 description
=> "Used memory in bytes (when type in node,qemu,lxc).",
271 description
=> "Number of available memory in bytes (when type in node,qemu,lxc).",
277 description
=> "Support level (when type == node).",
282 description
=> "Node uptime in seconds (when type in node,qemu,lxc).",
285 renderer
=> 'duration',
288 description
=> "HA service status (for HA managed VMs).",
293 description
=> "Used disk space in bytes (when type in storage), used root image spave for VMs (type in qemu,lxc).",
299 description
=> "Storage size in bytes (when type in storage), root image size for VMs (type in qemu,lxc).",
305 description
=> "Allowed storage content types (when type == storage).",
307 format
=> 'pve-storage-content-list',
311 description
=> "More specific type, if available.",
321 my $rpcenv = PVE
::RPCEnvironment
::get
();
322 my $authuser = $rpcenv->get_user();
323 my $usercfg = $rpcenv->{user_cfg
};
327 my $nodelist = PVE
::Cluster
::get_nodelist
();
328 my $members = PVE
::Cluster
::get_members
();
330 my $rrd = PVE
::Cluster
::rrd_dump
();
332 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
333 my $idlist = $vmlist->{ids
} || {};
335 my $hastatus = PVE
::HA
::Config
::read_manager_status
();
336 my $haresources = PVE
::HA
::Config
::read_resources_config
();
343 if (!$param->{type
} || $param->{type
} eq 'pool') {
344 for my $pool (sort keys %{$usercfg->{pools
}}) {
345 my $d = $usercfg->{pools
}->{$pool};
347 next if !$rpcenv->check($authuser, "/pool/$pool", [ 'Pool.Audit' ], 1);
355 $pooldata->{$pool} = $entry;
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');
365 for my $vmid (sort keys %$idlist) {
367 my $data = $idlist->{$vmid};
368 my $entry = PVE
::API2Tools
::extract_vm_stats
($vmid, $data, $rrd);
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
};
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
};
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);
395 if (defined(my $lock = $locked_vms->{$vmid}->{lock})) {
396 $entry->{lock} = $lock;
399 if (defined($entry->{pool
}) &&
400 !$rpcenv->check($authuser, "/pool/$entry->{pool}", ['Pool.Audit'], 1)) {
401 delete $entry->{pool
};
405 if (my $hatype = $hatypemap->{$entry->{type
}}) {
406 my $sid = "$hatype:$vmid";
408 if ($service = $hastatus->{service_status
}->{$sid}) {
409 $entry->{hastate
} = $service->{state};
410 } elsif ($service = $haresources->{ids
}->{$sid}) {
411 $entry->{hastate
} = $service->{state};
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);
427 if (!$param->{type
} || $param->{type
} eq 'storage') {
429 my $cfg = PVE
::Storage
::config
();
430 my @sids = PVE
::Storage
::storage_ids
($cfg);
432 foreach my $storeid (@sids) {
433 next if !$rpcenv->check($authuser, "/storage/$storeid", [ 'Datastore.Audit' ], 1);
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);
440 my $entry = PVE
::API2Tools
::extract_storage_stats
($storeid, $scfg, $node, $rrd);
447 if (!$param->{type
} || $param->{type
} eq 'sdn') {
449 my $nodes = PVE
::Cluster
::get_node_kv
("sdn");
451 for my $node (sort keys %{$nodes}) {
452 my $sdns = decode_json
($nodes->{$node});
454 for my $id (sort keys %{$sdns}) {
455 next if !$rpcenv->check($authuser, "/sdn/zones/$id", [ 'SDN.Audit' ], 1);
456 my $sdn = $sdns->{$id};
458 id
=> "sdn/$node/$id",
462 status
=> $sdn->{'status'},
473 __PACKAGE__-
>register_method({
477 description
=> "List recent tasks (cluster wide).",
478 permissions
=> { user
=> 'all' },
480 additionalProperties
=> 0,
488 upid
=> { type
=> 'string' },
495 my $rpcenv = PVE
::RPCEnvironment
::get
();
496 my $authuser = $rpcenv->get_user();
498 my $tlist = PVE
::Cluster
::get_tasklist
();
499 return [] if !$tlist;
501 my $all = $rpcenv->check($authuser, "/", [ 'Sys.Audit' ], 1);
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
});
508 push @$res, $task if $all || ($task->{user
} eq $authuser);
514 __PACKAGE__-
>register_method({
515 name
=> 'get_options',
518 description
=> "Get datacenter options.",
520 check
=> ['perm', '/', [ 'Sys.Audit' ]],
523 additionalProperties
=> 0,
533 return PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
536 __PACKAGE__-
>register_method({
537 name
=> 'set_options',
540 description
=> "Set datacenter options.",
542 check
=> ['perm', '/', [ 'Sys.Modify' ]],
546 additionalProperties
=> 0,
547 properties
=> $dc_properties,
549 returns
=> { type
=> "null" },
553 my $delete = extract_param
($param, 'delete');
555 cfs_lock_file
('datacenter.cfg', undef, sub {
556 my $conf = cfs_read_file
('datacenter.cfg');
558 $conf->{$_} = $param->{$_} for keys $param->%*;
560 delete $conf->{$_} for PVE
::Tools
::split_list
($delete);
562 cfs_write_file
('datacenter.cfg', $conf);
569 __PACKAGE__-
>register_method({
570 name
=> 'get_status',
573 description
=> "Get cluster status information.",
575 check
=> ['perm', '/', [ 'Sys.Audit' ]],
579 additionalProperties
=> 0,
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.'
601 description
=> '[cluster] Nodes count, including offline nodes.',
606 description
=> '[cluster] Current version of the corosync configuration file.',
611 description
=> '[cluster] Indicates if there is a majority of nodes online to make decisions',
616 description
=> '[node] ID of the node from the corosync configuration.',
621 description
=> '[node] IP of the resolved nodename.',
626 description
=> '[node] Indicates if this is the responding node.',
631 description
=> '[node] Indicates if the node is online or offline.',
636 description
=> '[node] Proxmox VE Subscription level, indicates if eligible for enterprise support as well as access to the stable Proxmox VE Enterprise Repository.',
644 # make sure we get current info
645 PVE
::Cluster
::cfs_update
();
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
();
656 if (my $d = $clinfo->{cluster
}) {
660 nodes
=> $d->{nodes
},
661 version
=> $d->{version
},
663 quorate
=> $d->{quorate
},
667 foreach my $node (keys %$members) {
668 my $d = $members->{$node};
674 'local' => ($node eq $nodename) ?
1 : 0,
675 online
=> $d->{online
},
678 if (defined($d->{ip
})) {
679 $entry->{ip
} = $d->{ip
};
682 if (my $d = PVE
::API2Tools
::extract_node_stats
($node, $members, $rrd)) {
683 $entry->{level
} = $d->{level
} || '';
690 # fake entry for local node if no cluster defined
691 my $pmxcfs = ($clinfo && $clinfo->{version
}) ?
1 : 0; # pmxcfs online ?
693 my $subinfo = PVE
::API2
::Subscription
::read_etc_subscription
();
694 my $sublevel = $subinfo->{level
} || '';
698 id
=> "node/$nodename",
700 ip
=> scalar(PVE
::Cluster
::remote_node_ip
($nodename)),
709 __PACKAGE__-
>register_method({
713 description
=> "Get next free VMID. Pass a VMID to assert that its free (at time of check).",
714 permissions
=> { user
=> 'all' },
716 additionalProperties
=> 0,
718 vmid
=> get_standard_option
('pve-vmid', {
725 description
=> "The next free VMID.",
730 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
731 my $idlist = $vmlist->{ids
} || {};
733 if (my $vmid = $param->{vmid
}) {
734 return $vmid if !defined($idlist->{$vmid});
735 raise_param_exc
({ vmid
=> "VM $vmid already exists" });
738 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
739 my $next_id = $dc_conf->{'next-id'} // {};
741 my $lower = $next_id->{lower
} // 100;
742 my $upper = $next_id->{upper
} // (1000 * 1000); # note, lower than the schema-maximum
744 for (my $i = $lower; $i < $upper; $i++) {
745 return $i if !defined($idlist->{$i});
748 die "unable to get any free VMID in range [$lower, $upper]\n";