1 package PVE
::API2Tools
;
7 use PVE
::Exception
qw(raise_param_exc);
11 use PVE
::DataCenterConfig
;
12 use PVE
::RPCEnvironment
;
13 use Digest
::MD5
qw(md5_hex);
17 use PVE
::Storage
::Plugin
;
20 my $hwaddress_st = {};
23 my $fn = '/etc/ssh/ssh_host_rsa_key.pub';
26 if (defined($hwaddress)
27 && $hwaddress_st->{mtime
} == $st->mtime
28 && $hwaddress_st->{ino
} == $st->ino
29 && $hwaddress_st->{dev
} == $st->dev) {
33 my $sshkey = PVE
::Tools
::file_get_contents
($fn);
34 $hwaddress = uc(md5_hex
($sshkey));
35 $hwaddress_st->@{'mtime', 'ino', 'dev'} = ($st->mtime, $st->ino, $st->dev);
40 sub extract_node_stats
{
41 my ($node, $members, $rrd, $exclude_stats) = @_;
50 if (my $d = $rrd->{"pve2-node/$node"}) {
52 if (!$members || # no cluster
53 ($members->{$node} && $members->{$node}->{online
})) {
54 if (!$exclude_stats) {
55 $entry->{uptime
} = ($d->[0] || 0) + 0;
56 $entry->{cpu
} = ($d->[5] || 0) + 0;
57 $entry->{mem
} = ($d->[8] || 0) + 0;
58 $entry->{disk
} = ($d->[12] || 0) + 0;
60 $entry->{status
} = 'online';
62 $entry->{level
} = $d->[1];
63 if (!$exclude_stats) {
64 $entry->{maxcpu
} = ($d->[4] || 0) + 0;
65 $entry->{maxmem
} = ($d->[7] || 0) + 0;
66 $entry->{maxdisk
} = ($d->[11] || 0) + 0;
70 if ($members && $members->{$node} &&
71 !$members->{$node}->{online
}) {
72 $entry->{status
} = 'offline';
78 sub extract_vm_stats
{
79 my ($vmid, $data, $rrd) = @_;
82 id
=> "$data->{type}/$vmid",
84 node
=> $data->{node
},
85 type
=> $data->{type
},
91 if ($d = $rrd->{"pve2-vm/$vmid"}) {
93 $entry->{uptime
} = ($d->[0] || 0) + 0;
94 $entry->{name
} = $d->[1];
95 $entry->{status
} = $entry->{uptime
} ?
'running' : 'stopped';
96 $entry->{maxcpu
} = ($d->[3] || 0) + 0;
97 $entry->{cpu
} = ($d->[4] || 0) + 0;
98 $entry->{maxmem
} = ($d->[5] || 0) + 0;
99 $entry->{mem
} = ($d->[6] || 0) + 0;
100 $entry->{maxdisk
} = ($d->[7] || 0) + 0;
101 $entry->{disk
} = ($d->[8] || 0) + 0;
102 $entry->{netin
} = ($d->[9] || 0) + 0;
103 $entry->{netout
} = ($d->[10] || 0) + 0;
104 $entry->{diskread
} = ($d->[11] || 0) + 0;
105 $entry->{diskwrite
} = ($d->[12] || 0) + 0;
107 } elsif ($d = $rrd->{"pve2.3-vm/$vmid"}) {
109 $entry->{uptime
} = ($d->[0] || 0) + 0;
110 $entry->{name
} = $d->[1];
111 $entry->{status
} = $d->[2];
112 $entry->{template
} = $d->[3] + 0;
114 $entry->{maxcpu
} = ($d->[5] || 0) + 0;
115 $entry->{cpu
} = ($d->[6] || 0) + 0;
116 $entry->{maxmem
} = ($d->[7] || 0) + 0;
117 $entry->{mem
} = ($d->[8] || 0) + 0;
118 $entry->{maxdisk
} = ($d->[9] || 0) + 0;
119 $entry->{disk
} = ($d->[10] || 0) + 0;
120 $entry->{netin
} = ($d->[11] || 0) + 0;
121 $entry->{netout
} = ($d->[12] || 0) + 0;
122 $entry->{diskread
} = ($d->[13] || 0) + 0;
123 $entry->{diskwrite
} = ($d->[14] || 0) + 0;
129 sub extract_storage_stats
{
130 my ($storeid, $scfg, $node, $rrd) = @_;
132 my $content = PVE
::Storage
::Plugin
::content_hash_to_string
($scfg->{content
});
135 id
=> "storage/$node/$storeid",
139 plugintype
=> $scfg->{type
},
141 shared
=> $scfg->{shared
} || 0,
145 if (my $d = $rrd->{"pve2-storage/$node/$storeid"}) {
146 $entry->{maxdisk
} = ($d->[1] || 0) + 0;
147 $entry->{disk
} = ($d->[2] || 0) + 0;
148 $entry->{status
} = 'available';
154 sub parse_http_proxy
{
157 my $uri = URI-
>new($proxyenv);
159 my $scheme = $uri->scheme;
160 my $host = $uri->host;
161 my $port = $uri->port || 3128;
163 my ($username, $password);
165 if (defined(my $p_auth = $uri->userinfo())) {
166 ($username, $password) = map URI
::Escape
::uri_unescape
($_), split(":", $p_auth, 2);
169 return ("$host:$port", $username, $password);
173 my ($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd) = @_;
175 my $rpcenv = PVE
::RPCEnvironment
::get
();
177 my $authuser = $rpcenv->get_user();
179 my $nodename = PVE
::INotify
::nodename
();
180 my $family = PVE
::Tools
::get_host_address_family
($nodename);
181 my $port = PVE
::Tools
::next_spice_port
($family);
183 my ($ticket, undef, $remote_viewer_config) =
184 PVE
::AccessControl
::remote_viewer_config
($authuser, $vmid, $node, $proxy, $title, $port);
188 my $cmd = ['/usr/bin/spiceterm', '--port', $port, '--addr', 'localhost',
189 '--timeout', $timeout, '--authpath', $authpath,
190 '--permissions', $permissions];
192 my $dcconf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
193 push @$cmd, '--keymap', $dcconf->{keyboard
} if $dcconf->{keyboard
};
195 push @$cmd, '--', @$shcmd;
200 syslog
('info', "starting spiceterm $upid - $title\n");
202 my $cmdstr = join (' ', @$cmd);
203 syslog
('info', "launch command: $cmdstr");
206 foreach my $k (keys %ENV) {
207 next if $k eq 'PATH' || $k eq 'TERM' || $k eq 'USER' || $k eq 'HOME' || $k eq 'LANG' || $k eq 'LANGUAGE' ;
211 $ENV{SPICE_TICKET
} = $ticket;
213 PVE
::Tools
::run_command
($cmd, errmsg
=> 'spiceterm failed\n', keeplocale
=> 1);
216 syslog
('err', $err);
223 $rpcenv->fork_worker('spiceproxy', $vmid, $authuser, $realcmd);
225 $rpcenv->fork_worker('spiceshell', undef, $authuser, $realcmd);
228 PVE
::Tools
::wait_for_vnc_port
($port);
230 return $remote_viewer_config;
233 sub resolve_proxyto
{
234 my ($rpcenv, $proxyto_callback, $proxyto, $uri_param) = @_;
237 if ($proxyto_callback) {
238 $node = $proxyto_callback->($rpcenv, $proxyto, $uri_param);
239 die "internal error - proxyto_callback returned nothing\n"
242 $node = $uri_param->{$proxyto};
243 raise_param_exc
({ $proxyto => "proxyto parameter does not exist"})
249 sub get_resource_pool_guest_members
{
252 my $usercfg = PVE
::Cluster
::cfs_read_file
("user.cfg");
254 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
255 my $idlist = $vmlist->{ids
} || {};
257 my $data = $usercfg->{pools
}->{$pool};
259 die "pool '$pool' does not exist\n" if !$data;
261 my $pool_members = [ grep { $idlist->{$_} } keys %{$data->{vms
}} ];
263 return $pool_members;