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
::Exception
qw(raise_param_exc);
13 use PVE
::HA
::Env
::PVE2
;
15 use PVE
::JSONSchema
qw(get_standard_option);
17 use PVE
::RPCEnvironment
;
20 use PVE
::Tools
qw(extract_param);
22 use PVE
::API2
::ACMEAccount
;
23 use PVE
::API2
::Backup
;
24 use PVE
::API2
::Cluster
::Ceph
;
25 use PVE
::API2
::ClusterConfig
;
26 use PVE
::API2
::Firewall
::Cluster
;
27 use PVE
::API2
::HAConfig
;
28 use PVE
::API2
::ReplicationConfig
;
32 require PVE
::API2
::Network
::SDN
;
36 use base
qw(PVE::RESTHandler);
38 __PACKAGE__-
>register_method ({
39 subclass
=> "PVE::API2::ReplicationConfig",
40 path
=> 'replication',
43 __PACKAGE__-
>register_method ({
44 subclass
=> "PVE::API2::ClusterConfig",
48 __PACKAGE__-
>register_method ({
49 subclass
=> "PVE::API2::Firewall::Cluster",
53 __PACKAGE__-
>register_method ({
54 subclass
=> "PVE::API2::Backup",
58 __PACKAGE__-
>register_method ({
59 subclass
=> "PVE::API2::HAConfig",
63 __PACKAGE__-
>register_method ({
64 subclass
=> "PVE::API2::ACMEAccount",
68 __PACKAGE__-
>register_method ({
69 subclass
=> "PVE::API2::Cluster::Ceph",
74 __PACKAGE__-
>register_method ({
75 subclass
=> "PVE::API2::Network::SDN",
80 my $dc_schema = PVE
::Cluster
::get_datacenter_schema
();
83 type
=> 'string', format
=> 'pve-configid-list',
84 description
=> "A list of settings you want to delete.",
88 foreach my $opt (keys %{$dc_schema->{properties
}}) {
89 $dc_properties->{$opt} = $dc_schema->{properties
}->{$opt};
92 __PACKAGE__-
>register_method ({
96 description
=> "Cluster index.",
97 permissions
=> { user
=> 'all' },
99 additionalProperties
=> 0,
108 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
115 { name
=> 'options' },
116 { name
=> 'resources' },
117 { name
=> 'replication' },
119 { name
=> 'backup' },
121 { name
=> 'status' },
122 { name
=> 'nextid' },
123 { name
=> 'firewall' },
124 { name
=> 'config' },
130 push(@{$result}, { name
=> 'sdn' });
136 __PACKAGE__-
>register_method({
140 description
=> "Read cluster log",
141 permissions
=> { user
=> 'all' },
143 additionalProperties
=> 0,
147 description
=> "Maximum number of entries.",
163 my $rpcenv = PVE
::RPCEnvironment
::get
();
165 my $max = $param->{max
} || 0;
166 my $user = $rpcenv->get_user();
168 my $admin = $rpcenv->check($user, "/", [ 'Sys.Syslog' ], 1);
170 my $loguser = $admin ?
'' : $user;
172 my $res = decode_json
(PVE
::Cluster
::get_cluster_log
($loguser, $max));
174 foreach my $entry (@{$res->{data
}}) {
175 $entry->{id
} = "$entry->{uid}:$entry->{node}";
181 __PACKAGE__-
>register_method({
185 description
=> "Resources index (cluster wide).",
186 permissions
=> { user
=> 'all' },
188 additionalProperties
=> 0,
193 enum
=> ['vm', 'storage', 'node', 'sdn'],
202 id
=> { type
=> 'string' },
204 description
=> "Resource type.",
206 enum
=> ['node', 'storage', 'pool', 'qemu', 'lxc', 'openvz', 'sdn'],
209 description
=> "Resource type dependent status.",
213 node
=> get_standard_option
('pve-node', {
214 description
=> "The cluster node name (when type in node,storage,qemu,lxc).",
217 storage
=> get_standard_option
('pve-storage-id', {
218 description
=> "The storage identifier (when type == storage).",
222 description
=> "The pool name (when type in pool,qemu,lxc).",
227 description
=> "CPU utilization (when type in node,qemu,lxc).",
230 renderer
=> 'fraction_as_percentage',
233 description
=> "Number of available CPUs (when type in node,qemu,lxc).",
238 description
=> "Used memory in bytes (when type in node,qemu,lxc).",
244 description
=> "Number of available memory in bytes (when type in node,qemu,lxc).",
250 description
=> "Support level (when type == node).",
255 description
=> "Node uptime in seconds (when type in node,qemu,lxc).",
258 renderer
=> 'duration',
261 description
=> "HA service status (for HA managed VMs).",
266 description
=> "Used disk space in bytes (when type in storage), used root image spave for VMs (type in qemu,lxc).",
272 description
=> "Storage size in bytes (when type in storage), root image size for VMs (type in qemu,lxc).",
283 my $rpcenv = PVE
::RPCEnvironment
::get
();
284 my $authuser = $rpcenv->get_user();
285 my $usercfg = $rpcenv->{user_cfg
};
289 my $nodelist = PVE
::Cluster
::get_nodelist
();
290 my $members = PVE
::Cluster
::get_members
();
292 my $rrd = PVE
::Cluster
::rrd_dump
();
294 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
295 my $idlist = $vmlist->{ids
} || {};
297 my $hastatus = PVE
::HA
::Config
::read_manager_status
();
298 my $haresources = PVE
::HA
::Config
::read_resources_config
();
305 if (!$param->{type
} || $param->{type
} eq 'pool') {
306 foreach my $pool (keys %{$usercfg->{pools
}}) {
307 my $d = $usercfg->{pools
}->{$pool};
309 next if !$rpcenv->check($authuser, "/pool/$pool", [ 'Pool.Allocate' ], 1);
317 $pooldata->{$pool} = $entry;
323 # we try to generate 'numbers' by using "$X + 0"
324 if (!$param->{type
} || $param->{type
} eq 'vm') {
325 my $locked_vms = PVE
::Cluster
::get_guest_config_property
('lock');
327 foreach my $vmid (keys %$idlist) {
329 my $data = $idlist->{$vmid};
330 my $entry = PVE
::API2Tools
::extract_vm_stats
($vmid, $data, $rrd);
332 if (defined(my $lock = $locked_vms->{$vmid}->{lock})) {
333 $entry->{lock} = $lock;
336 if (my $pool = $usercfg->{vms
}->{$vmid}) {
337 $entry->{pool
} = $pool;
338 if (my $pe = $pooldata->{$pool}) {
339 if ($entry->{uptime
}) {
340 $pe->{uptime
} = $entry->{uptime
} if !$pe->{uptime
} || $entry->{uptime
} > $pe->{uptime
};
341 $pe->{mem
} = 0 if !$pe->{mem
};
342 $pe->{mem
} += $entry->{mem
};
343 $pe->{maxmem
} = 0 if !$pe->{maxmem
};
344 $pe->{maxmem
} += $entry->{maxmem
};
345 $pe->{cpu
} = 0 if !$pe->{cpu
};
346 $pe->{maxcpu
} = 0 if !$pe->{maxcpu
};
348 # we do not know how much cpus there are in the cluster at this moment
349 # so we calculate the current % of the cpu
350 # but we had already the old cpu % before this vm, so:
351 # new% = (old%*oldmax + cur%*curmax) / (oldmax+curmax)
352 $pe->{cpu
} = (($pe->{cpu
} * $pe->{maxcpu
}) + ($entry->{cpu
} * $entry->{maxcpu
})) / ($pe->{maxcpu
} + $entry->{maxcpu
});
353 $pe->{maxcpu
} += $entry->{maxcpu
};
358 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
361 if (my $hatype = $hatypemap->{$entry->{type
}}) {
362 my $sid = "$hatype:$vmid";
364 if ($service = $hastatus->{service_status
}->{$sid}) {
365 $entry->{hastate
} = $service->{state};
366 } elsif ($service = $haresources->{ids
}->{$sid}) {
367 $entry->{hastate
} = $service->{state};
375 if (!$param->{type
} || $param->{type
} eq 'node') {
376 foreach my $node (@$nodelist) {
377 my $can_audit = $rpcenv->check($authuser, "/nodes/$node", [ 'Sys.Audit' ], 1);
378 my $entry = PVE
::API2Tools
::extract_node_stats
($node, $members, $rrd, !$can_audit);
383 if (!$param->{type
} || $param->{type
} eq 'storage') {
385 my $cfg = PVE
::Storage
::config
();
386 my @sids = PVE
::Storage
::storage_ids
($cfg);
388 foreach my $storeid (@sids) {
389 next if !$rpcenv->check($authuser, "/storage/$storeid", [ 'Datastore.Audit' ], 1);
391 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
392 # we create a entry for each node
393 foreach my $node (@$nodelist) {
394 next if !PVE
::Storage
::storage_check_enabled
($cfg, $storeid, $node, 1);
396 my $entry = PVE
::API2Tools
::extract_storage_stats
($storeid, $scfg, $node, $rrd);
403 if (!$param->{type
} || $param->{type
} eq 'sdn') {
405 my $nodes = PVE
::Cluster
::get_node_kv
("sdn");
407 foreach my $node (keys %{$nodes}) {
408 my $sdns = decode_json
($nodes->{$node});
410 foreach my $id (keys %{$sdns}) {
411 my $sdn = $sdns->{$id};
412 #next if !$rpcenv->check($authuser, "/sdn/$id", [ 'SDN.Audit' ], 1);
414 id
=> "sdn/$node/$id",
418 status
=> $sdn->{'status'},
429 __PACKAGE__-
>register_method({
433 description
=> "List recent tasks (cluster wide).",
434 permissions
=> { user
=> 'all' },
436 additionalProperties
=> 0,
444 upid
=> { type
=> 'string' },
451 my $rpcenv = PVE
::RPCEnvironment
::get
();
452 my $authuser = $rpcenv->get_user();
454 my $tlist = PVE
::Cluster
::get_tasklist
();
458 return $res if !$tlist;
460 my $all = $rpcenv->check($authuser, "/", [ 'Sys.Audit' ], 1);
462 foreach my $task (@$tlist) {
463 push @$res, $task if $all || ($task->{user
} eq $authuser);
469 __PACKAGE__-
>register_method({
470 name
=> 'get_options',
473 description
=> "Get datacenter options.",
475 check
=> ['perm', '/', [ 'Sys.Audit' ]],
478 additionalProperties
=> 0,
488 return PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
491 __PACKAGE__-
>register_method({
492 name
=> 'set_options',
495 description
=> "Set datacenter options.",
497 check
=> ['perm', '/', [ 'Sys.Modify' ]],
501 additionalProperties
=> 0,
502 properties
=> $dc_properties,
504 returns
=> { type
=> "null" },
508 my $filename = 'datacenter.cfg';
510 my $delete = extract_param
($param, 'delete');
514 my $conf = cfs_read_file
($filename);
516 foreach my $opt (keys %$param) {
517 $conf->{$opt} = $param->{$opt};
520 foreach my $opt (PVE
::Tools
::split_list
($delete)) {
521 delete $conf->{$opt};
524 cfs_write_file
($filename, $conf);
527 cfs_lock_file
($filename, undef, $code);
533 __PACKAGE__-
>register_method({
534 name
=> 'get_status',
537 description
=> "Get cluster status information.",
539 check
=> ['perm', '/', [ 'Sys.Audit' ]],
543 additionalProperties
=> 0,
560 # make sure we get current info
561 PVE
::Cluster
::cfs_update
();
563 # we also add info from pmxcfs
564 my $clinfo = PVE
::Cluster
::get_clinfo
();
565 my $members = PVE
::Cluster
::get_members
();
566 my $nodename = PVE
::INotify
::nodename
();
567 my $rrd = PVE
::Cluster
::rrd_dump
();
572 if (my $d = $clinfo->{cluster
}) {
576 nodes
=> $d->{nodes
},
577 version
=> $d->{version
},
579 quorate
=> $d->{quorate
},
583 foreach my $node (keys %$members) {
584 my $d = $members->{$node};
591 'local' => ($node eq $nodename) ?
1 : 0,
592 online
=> $d->{online
},
595 if (my $d = PVE
::API2Tools
::extract_node_stats
($node, $members, $rrd)) {
596 $entry->{level
} = $d->{level
};
603 # fake entry for local node if no cluster defined
604 my $pmxcfs = ($clinfo && $clinfo->{version
}) ?
1 : 0; # pmxcfs online ?
606 my $subinfo = PVE
::INotify
::read_file
('subscription');
607 my $sublevel = $subinfo->{level
} || '';
611 id
=> "node/$nodename",
613 ip
=> scalar(PVE
::Cluster
::remote_node_ip
($nodename)),
622 __PACKAGE__-
>register_method({
626 description
=> "Get next free VMID. If you pass an VMID it will raise an error if the ID is already used.",
627 permissions
=> { user
=> 'all' },
629 additionalProperties
=> 0,
631 vmid
=> get_standard_option
('pve-vmid', {optional
=> 1}),
636 description
=> "The next free VMID.",
641 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
642 my $idlist = $vmlist->{ids
} || {};
644 if (my $vmid = $param->{vmid
}) {
645 return $vmid if !defined($idlist->{$vmid});
646 raise_param_exc
({ vmid
=> "VM $vmid already exists" });
649 for (my $i = 100; $i < 10000; $i++) {
650 return $i if !defined($idlist->{$i});
653 die "unable to get any free VMID\n";