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
::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::Cluster::BackupInfo",
69 path
=> 'backup-info',
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' },
134 { name
=> 'backup-info' },
136 { name
=> 'status' },
137 { name
=> 'nextid' },
138 { name
=> 'firewall' },
139 { name
=> 'config' },
142 { name
=> 'metrics' },
146 push(@{$result}, { name
=> 'sdn' });
152 __PACKAGE__-
>register_method({
156 description
=> "Read cluster log",
157 permissions
=> { user
=> 'all' },
159 additionalProperties
=> 0,
163 description
=> "Maximum number of entries.",
179 my $rpcenv = PVE
::RPCEnvironment
::get
();
181 my $max = $param->{max
} || 0;
182 my $user = $rpcenv->get_user();
184 my $admin = $rpcenv->check($user, "/", [ 'Sys.Syslog' ], 1);
186 my $loguser = $admin ?
'' : $user;
188 my $res = decode_json
(PVE
::Cluster
::get_cluster_log
($loguser, $max));
190 foreach my $entry (@{$res->{data
}}) {
191 $entry->{id
} = "$entry->{uid}:$entry->{node}";
197 __PACKAGE__-
>register_method({
201 description
=> "Resources index (cluster wide).",
202 permissions
=> { user
=> 'all' },
204 additionalProperties
=> 0,
209 enum
=> ['vm', 'storage', 'node', 'sdn'],
218 id
=> { type
=> 'string' },
220 description
=> "Resource type.",
222 enum
=> ['node', 'storage', 'pool', 'qemu', 'lxc', 'openvz', 'sdn'],
225 description
=> "Resource type dependent status.",
230 description
=> "Name of the resource.",
234 node
=> get_standard_option
('pve-node', {
235 description
=> "The cluster node name (when type in node,storage,qemu,lxc).",
238 storage
=> get_standard_option
('pve-storage-id', {
239 description
=> "The storage identifier (when type == storage).",
243 description
=> "The pool name (when type in pool,qemu,lxc).",
248 description
=> "CPU utilization (when type in node,qemu,lxc).",
251 renderer
=> 'fraction_as_percentage',
254 description
=> "Number of available CPUs (when type in node,qemu,lxc).",
259 description
=> "Used memory in bytes (when type in node,qemu,lxc).",
265 description
=> "Number of available memory in bytes (when type in node,qemu,lxc).",
271 description
=> "Support level (when type == node).",
276 description
=> "Node uptime in seconds (when type in node,qemu,lxc).",
279 renderer
=> 'duration',
282 description
=> "HA service status (for HA managed VMs).",
287 description
=> "Used disk space in bytes (when type in storage), used root image spave for VMs (type in qemu,lxc).",
293 description
=> "Storage size in bytes (when type in storage), root image size for VMs (type in qemu,lxc).",
299 description
=> "Allowed storage content types (when type == storage).",
301 format
=> 'pve-storage-content-list',
305 description
=> "More specific type, if available.",
315 my $rpcenv = PVE
::RPCEnvironment
::get
();
316 my $authuser = $rpcenv->get_user();
317 my $usercfg = $rpcenv->{user_cfg
};
321 my $nodelist = PVE
::Cluster
::get_nodelist
();
322 my $members = PVE
::Cluster
::get_members
();
324 my $rrd = PVE
::Cluster
::rrd_dump
();
326 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
327 my $idlist = $vmlist->{ids
} || {};
329 my $hastatus = PVE
::HA
::Config
::read_manager_status
();
330 my $haresources = PVE
::HA
::Config
::read_resources_config
();
337 if (!$param->{type
} || $param->{type
} eq 'pool') {
338 for my $pool (sort keys %{$usercfg->{pools
}}) {
339 my $d = $usercfg->{pools
}->{$pool};
341 next if !$rpcenv->check($authuser, "/pool/$pool", [ 'Pool.Audit' ], 1);
349 $pooldata->{$pool} = $entry;
355 # we try to generate 'numbers' by using "$X + 0"
356 if (!$param->{type
} || $param->{type
} eq 'vm') {
357 my $locked_vms = PVE
::Cluster
::get_guest_config_property
('lock');
359 for my $vmid (sort keys %$idlist) {
361 my $data = $idlist->{$vmid};
362 my $entry = PVE
::API2Tools
::extract_vm_stats
($vmid, $data, $rrd);
364 if (my $pool = $usercfg->{vms
}->{$vmid}) {
365 $entry->{pool
} = $pool;
366 if (my $pe = $pooldata->{$pool}) {
367 if ($entry->{uptime
}) {
368 $pe->{uptime
} = $entry->{uptime
} if !$pe->{uptime
} || $entry->{uptime
} > $pe->{uptime
};
369 $pe->{mem
} = 0 if !$pe->{mem
};
370 $pe->{mem
} += $entry->{mem
};
371 $pe->{maxmem
} = 0 if !$pe->{maxmem
};
372 $pe->{maxmem
} += $entry->{maxmem
};
373 $pe->{cpu
} = 0 if !$pe->{cpu
};
374 $pe->{maxcpu
} = 0 if !$pe->{maxcpu
};
376 # we do not know how much cpus there are in the cluster at this moment
377 # so we calculate the current % of the cpu
378 # but we had already the old cpu % before this vm, so:
379 # new% = (old%*oldmax + cur%*curmax) / (oldmax+curmax)
380 $pe->{cpu
} = (($pe->{cpu
} * $pe->{maxcpu
}) + ($entry->{cpu
} * $entry->{maxcpu
})) / ($pe->{maxcpu
} + $entry->{maxcpu
});
381 $pe->{maxcpu
} += $entry->{maxcpu
};
386 # only skip now to next to ensure that the pool stats above are filled, if eligible
387 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
389 if (defined(my $lock = $locked_vms->{$vmid}->{lock})) {
390 $entry->{lock} = $lock;
393 if (defined($entry->{pool
}) &&
394 !$rpcenv->check($authuser, "/pool/$entry->{pool}", ['Pool.Audit'], 1)) {
395 delete $entry->{pool
};
399 if (my $hatype = $hatypemap->{$entry->{type
}}) {
400 my $sid = "$hatype:$vmid";
402 if ($service = $hastatus->{service_status
}->{$sid}) {
403 $entry->{hastate
} = $service->{state};
404 } elsif ($service = $haresources->{ids
}->{$sid}) {
405 $entry->{hastate
} = $service->{state};
413 if (!$param->{type
} || $param->{type
} eq 'node') {
414 foreach my $node (@$nodelist) {
415 my $can_audit = $rpcenv->check($authuser, "/nodes/$node", [ 'Sys.Audit' ], 1);
416 my $entry = PVE
::API2Tools
::extract_node_stats
($node, $members, $rrd, !$can_audit);
421 if (!$param->{type
} || $param->{type
} eq 'storage') {
423 my $cfg = PVE
::Storage
::config
();
424 my @sids = PVE
::Storage
::storage_ids
($cfg);
426 foreach my $storeid (@sids) {
427 next if !$rpcenv->check($authuser, "/storage/$storeid", [ 'Datastore.Audit' ], 1);
429 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
430 # we create a entry for each node
431 foreach my $node (@$nodelist) {
432 next if !PVE
::Storage
::storage_check_enabled
($cfg, $storeid, $node, 1);
434 my $entry = PVE
::API2Tools
::extract_storage_stats
($storeid, $scfg, $node, $rrd);
441 if (!$param->{type
} || $param->{type
} eq 'sdn') {
443 my $nodes = PVE
::Cluster
::get_node_kv
("sdn");
445 for my $node (sort keys %{$nodes}) {
446 my $sdns = decode_json
($nodes->{$node});
448 for my $id (sort keys %{$sdns}) {
449 next if !$rpcenv->check($authuser, "/sdn/zones/$id", [ 'SDN.Audit' ], 1);
450 my $sdn = $sdns->{$id};
452 id
=> "sdn/$node/$id",
456 status
=> $sdn->{'status'},
467 __PACKAGE__-
>register_method({
471 description
=> "List recent tasks (cluster wide).",
472 permissions
=> { user
=> 'all' },
474 additionalProperties
=> 0,
482 upid
=> { type
=> 'string' },
489 my $rpcenv = PVE
::RPCEnvironment
::get
();
490 my $authuser = $rpcenv->get_user();
492 my $tlist = PVE
::Cluster
::get_tasklist
();
496 return $res if !$tlist;
498 my $all = $rpcenv->check($authuser, "/", [ 'Sys.Audit' ], 1);
500 foreach my $task (@$tlist) {
501 if (PVE
::AccessControl
::pve_verify_tokenid
($task->{user
}, 1)) {
502 ($task->{user
}, $task->{tokenid
}) = PVE
::AccessControl
::split_tokenid
($task->{user
});
504 push @$res, $task if $all || ($task->{user
} eq $authuser);
510 __PACKAGE__-
>register_method({
511 name
=> 'get_options',
514 description
=> "Get datacenter options.",
516 check
=> ['perm', '/', [ 'Sys.Audit' ]],
519 additionalProperties
=> 0,
529 return PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
532 __PACKAGE__-
>register_method({
533 name
=> 'set_options',
536 description
=> "Set datacenter options.",
538 check
=> ['perm', '/', [ 'Sys.Modify' ]],
542 additionalProperties
=> 0,
543 properties
=> $dc_properties,
545 returns
=> { type
=> "null" },
549 my $filename = 'datacenter.cfg';
551 my $delete = extract_param
($param, 'delete');
555 my $conf = cfs_read_file
($filename);
557 foreach my $opt (keys %$param) {
558 $conf->{$opt} = $param->{$opt};
561 foreach my $opt (PVE
::Tools
::split_list
($delete)) {
562 delete $conf->{$opt};
565 cfs_write_file
($filename, $conf);
568 cfs_lock_file
($filename, undef, $code);
574 __PACKAGE__-
>register_method({
575 name
=> 'get_status',
578 description
=> "Get cluster status information.",
580 check
=> ['perm', '/', [ 'Sys.Audit' ]],
584 additionalProperties
=> 0,
594 enum
=> ['cluster', 'node'],
595 description
=> 'Indicates the type, either cluster or node. The type defines the object properties e.g. quorate available for type cluster.'
606 description
=> '[cluster] Nodes count, including offline nodes.',
611 description
=> '[cluster] Current version of the corosync configuration file.',
616 description
=> '[cluster] Indicates if there is a majority of nodes online to make decisions',
621 description
=> '[node] ID of the node from the corosync configuration.',
626 description
=> '[node] IP of the resolved nodename.',
631 description
=> '[node] Indicates if this is the responding node.',
636 description
=> '[node] Indicates if the node is online or offline.',
641 description
=> '[node] Proxmox VE Subscription level, indicates if eligible for enterprise support as well as access to the stable Proxmox VE Enterprise Repository.',
649 # make sure we get current info
650 PVE
::Cluster
::cfs_update
();
652 # we also add info from pmxcfs
653 my $clinfo = PVE
::Cluster
::get_clinfo
();
654 my $members = PVE
::Cluster
::get_members
();
655 my $nodename = PVE
::INotify
::nodename
();
656 my $rrd = PVE
::Cluster
::rrd_dump
();
661 if (my $d = $clinfo->{cluster
}) {
665 nodes
=> $d->{nodes
},
666 version
=> $d->{version
},
668 quorate
=> $d->{quorate
},
672 foreach my $node (keys %$members) {
673 my $d = $members->{$node};
679 'local' => ($node eq $nodename) ?
1 : 0,
680 online
=> $d->{online
},
683 if (defined($d->{ip
})) {
684 $entry->{ip
} = $d->{ip
};
687 if (my $d = PVE
::API2Tools
::extract_node_stats
($node, $members, $rrd)) {
688 $entry->{level
} = $d->{level
} || '';
695 # fake entry for local node if no cluster defined
696 my $pmxcfs = ($clinfo && $clinfo->{version
}) ?
1 : 0; # pmxcfs online ?
698 my $subinfo = PVE
::INotify
::read_file
('subscription');
699 my $sublevel = $subinfo->{level
} || '';
703 id
=> "node/$nodename",
705 ip
=> scalar(PVE
::Cluster
::remote_node_ip
($nodename)),
714 __PACKAGE__-
>register_method({
718 description
=> "Get next free VMID. If you pass an VMID it will raise an error if the ID is already used.",
719 permissions
=> { user
=> 'all' },
721 additionalProperties
=> 0,
723 vmid
=> get_standard_option
('pve-vmid', {optional
=> 1}),
728 description
=> "The next free VMID.",
733 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
734 my $idlist = $vmlist->{ids
} || {};
736 if (my $vmid = $param->{vmid
}) {
737 return $vmid if !defined($idlist->{$vmid});
738 raise_param_exc
({ vmid
=> "VM $vmid already exists" });
741 for (my $i = 100; $i < 10000; $i++) {
742 return $i if !defined($idlist->{$i});
745 die "unable to get any free VMID\n";