1 package PVE
::API2Tools
;
6 use Digest
::MD5
qw(md5_hex);
12 use PVE
::DataCenterConfig
; # so we can cfs-read datacenter.cfg
13 use PVE
::Exception
qw(raise_param_exc);
15 use PVE
::RPCEnvironment
;
17 use PVE
::Storage
::Plugin
;
21 my $hwaddress_st = {};
24 my $fn = '/etc/ssh/ssh_host_rsa_key.pub';
27 if (defined($hwaddress)
28 && $hwaddress_st->{mtime
} == $st->mtime
29 && $hwaddress_st->{ino
} == $st->ino
30 && $hwaddress_st->{dev
} == $st->dev) {
34 my $sshkey = PVE
::Tools
::file_get_contents
($fn);
35 $hwaddress = uc(md5_hex
($sshkey));
36 $hwaddress_st->@{'mtime', 'ino', 'dev'} = ($st->mtime, $st->ino, $st->dev);
41 sub extract_node_stats
{
42 my ($node, $members, $rrd, $exclude_stats) = @_;
51 if (my $d = $rrd->{"pve2-node/$node"}) {
53 if (!$members || # no cluster
54 ($members->{$node} && $members->{$node}->{online
})) {
55 if (!$exclude_stats) {
56 $entry->{uptime
} = ($d->[0] || 0) + 0;
57 $entry->{cpu
} = ($d->[5] || 0) + 0;
58 $entry->{mem
} = ($d->[8] || 0) + 0;
59 $entry->{disk
} = ($d->[12] || 0) + 0;
61 $entry->{status
} = 'online';
63 $entry->{level
} = $d->[1];
64 if (!$exclude_stats) {
65 $entry->{maxcpu
} = ($d->[4] || 0) + 0;
66 $entry->{maxmem
} = ($d->[7] || 0) + 0;
67 $entry->{maxdisk
} = ($d->[11] || 0) + 0;
71 if ($members && $members->{$node} &&
72 !$members->{$node}->{online
}) {
73 $entry->{status
} = 'offline';
79 sub extract_vm_stats
{
80 my ($vmid, $data, $rrd) = @_;
83 id
=> "$data->{type}/$vmid",
85 node
=> $data->{node
},
86 type
=> $data->{type
},
92 if ($d = $rrd->{"pve2-vm/$vmid"}) {
94 $entry->{uptime
} = ($d->[0] || 0) + 0;
95 $entry->{name
} = $d->[1];
96 $entry->{status
} = $entry->{uptime
} ?
'running' : 'stopped';
97 $entry->{maxcpu
} = ($d->[3] || 0) + 0;
98 $entry->{cpu
} = ($d->[4] || 0) + 0;
99 $entry->{maxmem
} = ($d->[5] || 0) + 0;
100 $entry->{mem
} = ($d->[6] || 0) + 0;
101 $entry->{maxdisk
} = ($d->[7] || 0) + 0;
102 $entry->{disk
} = ($d->[8] || 0) + 0;
103 $entry->{netin
} = ($d->[9] || 0) + 0;
104 $entry->{netout
} = ($d->[10] || 0) + 0;
105 $entry->{diskread
} = ($d->[11] || 0) + 0;
106 $entry->{diskwrite
} = ($d->[12] || 0) + 0;
108 } elsif ($d = $rrd->{"pve2.3-vm/$vmid"}) {
110 $entry->{uptime
} = ($d->[0] || 0) + 0;
111 $entry->{name
} = $d->[1];
112 $entry->{status
} = $d->[2];
113 $entry->{template
} = $d->[3] + 0;
115 $entry->{maxcpu
} = ($d->[5] || 0) + 0;
116 $entry->{cpu
} = ($d->[6] || 0) + 0;
117 $entry->{maxmem
} = ($d->[7] || 0) + 0;
118 $entry->{mem
} = ($d->[8] || 0) + 0;
119 $entry->{maxdisk
} = ($d->[9] || 0) + 0;
120 $entry->{disk
} = ($d->[10] || 0) + 0;
121 $entry->{netin
} = ($d->[11] || 0) + 0;
122 $entry->{netout
} = ($d->[12] || 0) + 0;
123 $entry->{diskread
} = ($d->[13] || 0) + 0;
124 $entry->{diskwrite
} = ($d->[14] || 0) + 0;
130 sub extract_storage_stats
{
131 my ($storeid, $scfg, $node, $rrd) = @_;
133 my $content = PVE
::Storage
::Plugin
::content_hash_to_string
($scfg->{content
});
136 id
=> "storage/$node/$storeid",
140 plugintype
=> $scfg->{type
},
142 shared
=> $scfg->{shared
} || 0,
146 if (my $d = $rrd->{"pve2-storage/$node/$storeid"}) {
147 $entry->{maxdisk
} = ($d->[1] || 0) + 0;
148 $entry->{disk
} = ($d->[2] || 0) + 0;
149 $entry->{status
} = 'available';
155 sub parse_http_proxy
{
158 my $uri = URI-
>new($proxyenv);
160 my $scheme = $uri->scheme;
161 my $host = $uri->host;
162 my $port = $uri->port || 3128;
164 my ($username, $password);
166 if (defined(my $p_auth = $uri->userinfo())) {
167 ($username, $password) = map URI
::Escape
::uri_unescape
($_), split(":", $p_auth, 2);
170 return ("$host:$port", $username, $password);
174 my ($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd) = @_;
176 my $rpcenv = PVE
::RPCEnvironment
::get
();
178 my $authuser = $rpcenv->get_user();
180 my $nodename = PVE
::INotify
::nodename
();
181 my $family = PVE
::Tools
::get_host_address_family
($nodename);
182 my $port = PVE
::Tools
::next_spice_port
($family);
184 my ($ticket, undef, $remote_viewer_config) =
185 PVE
::AccessControl
::remote_viewer_config
($authuser, $vmid, $node, $proxy, $title, $port);
189 my $cmd = ['/usr/bin/spiceterm', '--port', $port, '--addr', 'localhost',
190 '--timeout', $timeout, '--authpath', $authpath,
191 '--permissions', $permissions];
193 my $dcconf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
194 push @$cmd, '--keymap', $dcconf->{keyboard
} if $dcconf->{keyboard
};
196 push @$cmd, '--', @$shcmd;
201 syslog
('info', "starting spiceterm $upid - $title\n");
203 my $cmdstr = join (' ', @$cmd);
204 syslog
('info', "launch command: $cmdstr");
207 foreach my $k (keys %ENV) {
208 next if $k eq 'PATH' || $k eq 'TERM' || $k eq 'USER' || $k eq 'HOME' || $k eq 'LANG' || $k eq 'LANGUAGE' ;
212 $ENV{SPICE_TICKET
} = $ticket;
214 PVE
::Tools
::run_command
($cmd, errmsg
=> 'spiceterm failed\n', keeplocale
=> 1);
217 syslog
('err', $err);
224 $rpcenv->fork_worker('spiceproxy', $vmid, $authuser, $realcmd);
226 $rpcenv->fork_worker('spiceshell', undef, $authuser, $realcmd);
229 PVE
::Tools
::wait_for_vnc_port
($port);
231 return $remote_viewer_config;
234 sub resolve_proxyto
{
235 my ($rpcenv, $proxyto_callback, $proxyto, $uri_param) = @_;
238 if ($proxyto_callback) {
239 $node = $proxyto_callback->($rpcenv, $proxyto, $uri_param);
240 die "internal error - proxyto_callback returned nothing\n"
243 $node = $uri_param->{$proxyto};
244 raise_param_exc
({ $proxyto => "proxyto parameter does not exist"})
250 sub get_resource_pool_guest_members
{
253 my $usercfg = PVE
::Cluster
::cfs_read_file
("user.cfg");
255 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
256 my $idlist = $vmlist->{ids
} || {};
258 my $data = $usercfg->{pools
}->{$pool};
260 die "pool '$pool' does not exist\n" if !$data;
262 my $pool_members = [ grep { $idlist->{$_} } keys %{$data->{vms
}} ];
264 return $pool_members;