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
::BackupInfo
;
27 use PVE
::API2
::Cluster
::Ceph
;
28 use PVE
::API2
::Cluster
::MetricServer
;
29 use PVE
::API2
::ClusterConfig
;
30 use PVE
::API2
::Firewall
::Cluster
;
31 use PVE
::API2
::HAConfig
;
32 use PVE
::API2
::ReplicationConfig
;
36 require PVE
::API2
::Network
::SDN
;
40 use base
qw(PVE::RESTHandler);
42 __PACKAGE__-
>register_method ({
43 subclass
=> "PVE::API2::ReplicationConfig",
44 path
=> 'replication',
47 __PACKAGE__-
>register_method ({
48 subclass
=> "PVE::API2::Cluster::MetricServer",
52 __PACKAGE__-
>register_method ({
53 subclass
=> "PVE::API2::ClusterConfig",
57 __PACKAGE__-
>register_method ({
58 subclass
=> "PVE::API2::Firewall::Cluster",
62 __PACKAGE__-
>register_method ({
63 subclass
=> "PVE::API2::Backup",
67 __PACKAGE__-
>register_method ({
68 subclass
=> "PVE::API2::BackupInfo",
72 __PACKAGE__-
>register_method ({
73 subclass
=> "PVE::API2::HAConfig",
77 __PACKAGE__-
>register_method ({
78 subclass
=> "PVE::API2::ACMEAccount",
82 __PACKAGE__-
>register_method ({
83 subclass
=> "PVE::API2::Cluster::Ceph",
88 __PACKAGE__-
>register_method ({
89 subclass
=> "PVE::API2::Network::SDN",
94 my $dc_schema = PVE
::DataCenterConfig
::get_datacenter_schema
();
97 type
=> 'string', format
=> 'pve-configid-list',
98 description
=> "A list of settings you want to delete.",
102 foreach my $opt (keys %{$dc_schema->{properties
}}) {
103 $dc_properties->{$opt} = $dc_schema->{properties
}->{$opt};
106 __PACKAGE__-
>register_method ({
110 description
=> "Cluster index.",
111 permissions
=> { user
=> 'all' },
113 additionalProperties
=> 0,
122 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
129 { name
=> 'options' },
130 { name
=> 'resources' },
131 { name
=> 'replication' },
133 { name
=> 'backup' },
135 { name
=> 'status' },
136 { name
=> 'nextid' },
137 { name
=> 'firewall' },
138 { name
=> 'config' },
141 { name
=> 'metrics' },
145 push(@{$result}, { name
=> 'sdn' });
151 __PACKAGE__-
>register_method({
155 description
=> "Read cluster log",
156 permissions
=> { user
=> 'all' },
158 additionalProperties
=> 0,
162 description
=> "Maximum number of entries.",
178 my $rpcenv = PVE
::RPCEnvironment
::get
();
180 my $max = $param->{max
} || 0;
181 my $user = $rpcenv->get_user();
183 my $admin = $rpcenv->check($user, "/", [ 'Sys.Syslog' ], 1);
185 my $loguser = $admin ?
'' : $user;
187 my $res = decode_json
(PVE
::Cluster
::get_cluster_log
($loguser, $max));
189 foreach my $entry (@{$res->{data
}}) {
190 $entry->{id
} = "$entry->{uid}:$entry->{node}";
196 __PACKAGE__-
>register_method({
200 description
=> "Resources index (cluster wide).",
201 permissions
=> { user
=> 'all' },
203 additionalProperties
=> 0,
208 enum
=> ['vm', 'storage', 'node', 'sdn'],
217 id
=> { type
=> 'string' },
219 description
=> "Resource type.",
221 enum
=> ['node', 'storage', 'pool', 'qemu', 'lxc', 'openvz', 'sdn'],
224 description
=> "Resource type dependent status.",
228 node
=> get_standard_option
('pve-node', {
229 description
=> "The cluster node name (when type in node,storage,qemu,lxc).",
232 storage
=> get_standard_option
('pve-storage-id', {
233 description
=> "The storage identifier (when type == storage).",
237 description
=> "The pool name (when type in pool,qemu,lxc).",
242 description
=> "CPU utilization (when type in node,qemu,lxc).",
245 renderer
=> 'fraction_as_percentage',
248 description
=> "Number of available CPUs (when type in node,qemu,lxc).",
253 description
=> "Used memory in bytes (when type in node,qemu,lxc).",
259 description
=> "Number of available memory in bytes (when type in node,qemu,lxc).",
265 description
=> "Support level (when type == node).",
270 description
=> "Node uptime in seconds (when type in node,qemu,lxc).",
273 renderer
=> 'duration',
276 description
=> "HA service status (for HA managed VMs).",
281 description
=> "Used disk space in bytes (when type in storage), used root image spave for VMs (type in qemu,lxc).",
287 description
=> "Storage size in bytes (when type in storage), root image size for VMs (type in qemu,lxc).",
293 description
=> "Allowed storage content types (when type == storage).",
295 format
=> 'pve-storage-content-list',
299 description
=> "More specific type, if available.",
309 my $rpcenv = PVE
::RPCEnvironment
::get
();
310 my $authuser = $rpcenv->get_user();
311 my $usercfg = $rpcenv->{user_cfg
};
315 my $nodelist = PVE
::Cluster
::get_nodelist
();
316 my $members = PVE
::Cluster
::get_members
();
318 my $rrd = PVE
::Cluster
::rrd_dump
();
320 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
321 my $idlist = $vmlist->{ids
} || {};
323 my $hastatus = PVE
::HA
::Config
::read_manager_status
();
324 my $haresources = PVE
::HA
::Config
::read_resources_config
();
331 if (!$param->{type
} || $param->{type
} eq 'pool') {
332 for my $pool (sort keys %{$usercfg->{pools
}}) {
333 my $d = $usercfg->{pools
}->{$pool};
335 next if !$rpcenv->check($authuser, "/pool/$pool", [ 'Pool.Allocate' ], 1);
343 $pooldata->{$pool} = $entry;
349 # we try to generate 'numbers' by using "$X + 0"
350 if (!$param->{type
} || $param->{type
} eq 'vm') {
351 my $locked_vms = PVE
::Cluster
::get_guest_config_property
('lock');
353 for my $vmid (sort keys %$idlist) {
355 my $data = $idlist->{$vmid};
356 my $entry = PVE
::API2Tools
::extract_vm_stats
($vmid, $data, $rrd);
358 if (my $pool = $usercfg->{vms
}->{$vmid}) {
359 $entry->{pool
} = $pool;
360 if (my $pe = $pooldata->{$pool}) {
361 if ($entry->{uptime
}) {
362 $pe->{uptime
} = $entry->{uptime
} if !$pe->{uptime
} || $entry->{uptime
} > $pe->{uptime
};
363 $pe->{mem
} = 0 if !$pe->{mem
};
364 $pe->{mem
} += $entry->{mem
};
365 $pe->{maxmem
} = 0 if !$pe->{maxmem
};
366 $pe->{maxmem
} += $entry->{maxmem
};
367 $pe->{cpu
} = 0 if !$pe->{cpu
};
368 $pe->{maxcpu
} = 0 if !$pe->{maxcpu
};
370 # we do not know how much cpus there are in the cluster at this moment
371 # so we calculate the current % of the cpu
372 # but we had already the old cpu % before this vm, so:
373 # new% = (old%*oldmax + cur%*curmax) / (oldmax+curmax)
374 $pe->{cpu
} = (($pe->{cpu
} * $pe->{maxcpu
}) + ($entry->{cpu
} * $entry->{maxcpu
})) / ($pe->{maxcpu
} + $entry->{maxcpu
});
375 $pe->{maxcpu
} += $entry->{maxcpu
};
380 # only skip now to next to ensure that the pool stats above are filled, if eligible
381 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
383 if (defined(my $lock = $locked_vms->{$vmid}->{lock})) {
384 $entry->{lock} = $lock;
388 if (my $hatype = $hatypemap->{$entry->{type
}}) {
389 my $sid = "$hatype:$vmid";
391 if ($service = $hastatus->{service_status
}->{$sid}) {
392 $entry->{hastate
} = $service->{state};
393 } elsif ($service = $haresources->{ids
}->{$sid}) {
394 $entry->{hastate
} = $service->{state};
402 if (!$param->{type
} || $param->{type
} eq 'node') {
403 foreach my $node (@$nodelist) {
404 my $can_audit = $rpcenv->check($authuser, "/nodes/$node", [ 'Sys.Audit' ], 1);
405 my $entry = PVE
::API2Tools
::extract_node_stats
($node, $members, $rrd, !$can_audit);
410 if (!$param->{type
} || $param->{type
} eq 'storage') {
412 my $cfg = PVE
::Storage
::config
();
413 my @sids = PVE
::Storage
::storage_ids
($cfg);
415 foreach my $storeid (@sids) {
416 next if !$rpcenv->check($authuser, "/storage/$storeid", [ 'Datastore.Audit' ], 1);
418 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
419 # we create a entry for each node
420 foreach my $node (@$nodelist) {
421 next if !PVE
::Storage
::storage_check_enabled
($cfg, $storeid, $node, 1);
423 my $entry = PVE
::API2Tools
::extract_storage_stats
($storeid, $scfg, $node, $rrd);
430 if (!$param->{type
} || $param->{type
} eq 'sdn') {
432 my $nodes = PVE
::Cluster
::get_node_kv
("sdn");
434 for my $node (sort keys %{$nodes}) {
435 my $sdns = decode_json
($nodes->{$node});
437 for my $id (sort keys %{$sdns}) {
438 next if !$rpcenv->check($authuser, "/sdn/zones/$id", [ 'SDN.Audit' ], 1);
439 my $sdn = $sdns->{$id};
441 id
=> "sdn/$node/$id",
445 status
=> $sdn->{'status'},
456 __PACKAGE__-
>register_method({
460 description
=> "List recent tasks (cluster wide).",
461 permissions
=> { user
=> 'all' },
463 additionalProperties
=> 0,
471 upid
=> { type
=> 'string' },
478 my $rpcenv = PVE
::RPCEnvironment
::get
();
479 my $authuser = $rpcenv->get_user();
481 my $tlist = PVE
::Cluster
::get_tasklist
();
485 return $res if !$tlist;
487 my $all = $rpcenv->check($authuser, "/", [ 'Sys.Audit' ], 1);
489 foreach my $task (@$tlist) {
490 if (PVE
::AccessControl
::pve_verify_tokenid
($task->{user
}, 1)) {
491 ($task->{user
}, $task->{tokenid
}) = PVE
::AccessControl
::split_tokenid
($task->{user
});
493 push @$res, $task if $all || ($task->{user
} eq $authuser);
499 __PACKAGE__-
>register_method({
500 name
=> 'get_options',
503 description
=> "Get datacenter options.",
505 check
=> ['perm', '/', [ 'Sys.Audit' ]],
508 additionalProperties
=> 0,
518 return PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
521 __PACKAGE__-
>register_method({
522 name
=> 'set_options',
525 description
=> "Set datacenter options.",
527 check
=> ['perm', '/', [ 'Sys.Modify' ]],
531 additionalProperties
=> 0,
532 properties
=> $dc_properties,
534 returns
=> { type
=> "null" },
538 my $filename = 'datacenter.cfg';
540 my $delete = extract_param
($param, 'delete');
544 my $conf = cfs_read_file
($filename);
546 foreach my $opt (keys %$param) {
547 $conf->{$opt} = $param->{$opt};
550 foreach my $opt (PVE
::Tools
::split_list
($delete)) {
551 delete $conf->{$opt};
554 cfs_write_file
($filename, $conf);
557 cfs_lock_file
($filename, undef, $code);
563 __PACKAGE__-
>register_method({
564 name
=> 'get_status',
567 description
=> "Get cluster status information.",
569 check
=> ['perm', '/', [ 'Sys.Audit' ]],
573 additionalProperties
=> 0,
583 enum
=> ['cluster', 'node'],
584 description
=> 'Indicates the type, either cluster or node. The type defines the object properties e.g. quorate available for type cluster.'
595 description
=> '[cluster] Nodes count, including offline nodes.',
600 description
=> '[cluster] Current version of the corosync configuration file.',
605 description
=> '[cluster] Indicates if there is a majority of nodes online to make decisions',
610 description
=> '[node] ID of the node from the corosync configuration.',
615 description
=> '[node] IP of the resolved nodename.',
620 description
=> '[node] Indicates if this is the responding node.',
625 description
=> '[node] Indicates if the node is online or offline.',
630 description
=> '[node] Proxmox VE Subscription level, indicates if eligible for enterprise support as well as access to the stable Proxmox VE Enterprise Repository.',
638 # make sure we get current info
639 PVE
::Cluster
::cfs_update
();
641 # we also add info from pmxcfs
642 my $clinfo = PVE
::Cluster
::get_clinfo
();
643 my $members = PVE
::Cluster
::get_members
();
644 my $nodename = PVE
::INotify
::nodename
();
645 my $rrd = PVE
::Cluster
::rrd_dump
();
650 if (my $d = $clinfo->{cluster
}) {
654 nodes
=> $d->{nodes
},
655 version
=> $d->{version
},
657 quorate
=> $d->{quorate
},
661 foreach my $node (keys %$members) {
662 my $d = $members->{$node};
668 'local' => ($node eq $nodename) ?
1 : 0,
669 online
=> $d->{online
},
672 if (defined($d->{ip
})) {
673 $entry->{ip
} = $d->{ip
};
676 if (my $d = PVE
::API2Tools
::extract_node_stats
($node, $members, $rrd)) {
677 $entry->{level
} = $d->{level
} || '';
684 # fake entry for local node if no cluster defined
685 my $pmxcfs = ($clinfo && $clinfo->{version
}) ?
1 : 0; # pmxcfs online ?
687 my $subinfo = PVE
::INotify
::read_file
('subscription');
688 my $sublevel = $subinfo->{level
} || '';
692 id
=> "node/$nodename",
694 ip
=> scalar(PVE
::Cluster
::remote_node_ip
($nodename)),
703 __PACKAGE__-
>register_method({
707 description
=> "Get next free VMID. If you pass an VMID it will raise an error if the ID is already used.",
708 permissions
=> { user
=> 'all' },
710 additionalProperties
=> 0,
712 vmid
=> get_standard_option
('pve-vmid', {optional
=> 1}),
717 description
=> "The next free VMID.",
722 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
723 my $idlist = $vmlist->{ids
} || {};
725 if (my $vmid = $param->{vmid
}) {
726 return $vmid if !defined($idlist->{$vmid});
727 raise_param_exc
({ vmid
=> "VM $vmid already exists" });
730 for (my $i = 100; $i < 10000; $i++) {
731 return $i if !defined($idlist->{$i});
734 die "unable to get any free VMID\n";