]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2Tools.pm
7b4c425a7555f25996d350bc0cb3e1f156a0b9e5
1 package PVE
::API2Tools
;
7 use PVE
::Exception
qw(raise_param_exc);
11 use Digest
::MD5
qw(md5_hex);
20 return $hwaddress if defined ($hwaddress);
22 my $fn = '/etc/ssh/ssh_host_rsa_key.pub';
23 my $sshkey = PVE
::Tools
::file_get_contents
($fn);
24 $hwaddress = uc(md5_hex
($sshkey));
29 sub extract_node_stats
{
30 my ($node, $members, $rrd) = @_;
39 if (my $d = $rrd->{"pve2-node/$node"}) {
41 if (!$members || # no cluster
42 ($members->{$node} && $members->{$node}->{online
})) {
43 $entry->{uptime
} = ($d->[0] || 0) + 0;
44 $entry->{cpu
} = ($d->[5] || 0) + 0;
45 $entry->{mem
} = ($d->[8] || 0) + 0;
46 $entry->{disk
} = ($d->[12] || 0) + 0;
47 $entry->{status
} = 'online';
49 $entry->{level
} = $d->[1];
50 $entry->{maxcpu
} = ($d->[4] || 0) + 0;
51 $entry->{maxmem
} = ($d->[7] || 0) + 0;
52 $entry->{maxdisk
} = ($d->[11] || 0) + 0;
55 if ($members && $members->{$node} &&
56 !$members->{$node}->{online
}) {
57 $entry->{status
} = 'offline';
63 sub extract_vm_stats
{
64 my ($vmid, $data, $rrd) = @_;
67 id
=> "$data->{type}/$vmid",
69 node
=> $data->{node
},
70 type
=> $data->{type
},
76 if ($d = $rrd->{"pve2-vm/$vmid"}) {
78 $entry->{uptime
} = ($d->[0] || 0) + 0;
79 $entry->{name
} = $d->[1];
80 $entry->{status
} = $entry->{uptime
} ?
'running' : 'stopped';
81 $entry->{maxcpu
} = ($d->[3] || 0) + 0;
82 $entry->{cpu
} = ($d->[4] || 0) + 0;
83 $entry->{maxmem
} = ($d->[5] || 0) + 0;
84 $entry->{mem
} = ($d->[6] || 0) + 0;
85 $entry->{maxdisk
} = ($d->[7] || 0) + 0;
86 $entry->{disk
} = ($d->[8] || 0) + 0;
87 $entry->{netin
} = ($d->[9] || 0) + 0;
88 $entry->{netout
} = ($d->[10] || 0) + 0;
89 $entry->{diskread
} = ($d->[11] || 0) + 0;
90 $entry->{diskwrite
} = ($d->[12] || 0) + 0;
92 } elsif ($d = $rrd->{"pve2.3-vm/$vmid"}) {
94 $entry->{uptime
} = ($d->[0] || 0) + 0;
95 $entry->{name
} = $d->[1];
96 $entry->{status
} = $d->[2];
97 $entry->{template
} = $d->[3] + 0;
99 $entry->{maxcpu
} = ($d->[5] || 0) + 0;
100 $entry->{cpu
} = ($d->[6] || 0) + 0;
101 $entry->{maxmem
} = ($d->[7] || 0) + 0;
102 $entry->{mem
} = ($d->[8] || 0) + 0;
103 $entry->{maxdisk
} = ($d->[9] || 0) + 0;
104 $entry->{disk
} = ($d->[10] || 0) + 0;
105 $entry->{netin
} = ($d->[11] || 0) + 0;
106 $entry->{netout
} = ($d->[12] || 0) + 0;
107 $entry->{diskread
} = ($d->[13] || 0) + 0;
108 $entry->{diskwrite
} = ($d->[14] || 0) + 0;
114 sub extract_storage_stats
{
115 my ($storeid, $scfg, $node, $rrd) = @_;
118 id
=> "storage/$node/$storeid",
125 if (my $d = $rrd->{"pve2-storage/$node/$storeid"}) {
126 $entry->{maxdisk
} = ($d->[1] || 0) + 0;
127 $entry->{disk
} = ($d->[2] || 0) + 0;
128 $entry->{status
} = 'available';
134 sub parse_http_proxy
{
137 my $uri = URI-
>new($proxyenv);
139 my $scheme = $uri->scheme;
140 my $host = $uri->host;
141 my $port = $uri->port || 3128;
143 my ($username, $password);
145 if (defined(my $p_auth = $uri->userinfo())) {
146 ($username, $password) = map URI
::Escape
::uri_unescape
($_), split(":", $p_auth, 2);
149 return ("$host:$port", $username, $password);
153 my ($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd) = @_;
155 my $rpcenv = PVE
::RPCEnvironment
::get
();
157 my $authuser = $rpcenv->get_user();
159 my $nodename = PVE
::INotify
::nodename
();
160 my $family = PVE
::Tools
::get_host_address_family
($nodename);
161 my $port = PVE
::Tools
::next_spice_port
($family);
163 my ($ticket, undef, $remote_viewer_config) =
164 PVE
::AccessControl
::remote_viewer_config
($authuser, $vmid, $node, $proxy, $title, $port);
168 my $cmd = ['/usr/bin/spiceterm', '--port', $port, '--addr', '127.0.0.1',
169 '--timeout', $timeout, '--authpath', $authpath,
170 '--permissions', $permissions];
172 my $dcconf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
173 push @$cmd, '--keymap', $dcconf->{keyboard
} if $dcconf->{keyboard
};
175 push @$cmd, '--', @$shcmd;
180 syslog
('info', "starting spiceterm $upid - $title\n");
182 my $cmdstr = join (' ', @$cmd);
183 syslog
('info', "launch command: $cmdstr");
186 foreach my $k (keys %ENV) {
187 next if $k eq 'PATH' || $k eq 'TERM' || $k eq 'USER' || $k eq 'HOME' || $k eq 'LANG' || $k eq 'LANGUAGE' ;
191 $ENV{SPICE_TICKET
} = $ticket;
193 PVE
::Tools
::run_command
($cmd, errmsg
=> 'spiceterm failed\n', keeplocale
=> 1);
196 syslog
('err', $err);
203 $rpcenv->fork_worker('spiceproxy', $vmid, $authuser, $realcmd);
205 $rpcenv->fork_worker('spiceshell', undef, $authuser, $realcmd);
208 PVE
::Tools
::wait_for_vnc_port
($port);
210 return $remote_viewer_config;
213 sub read_proxy_config
{
215 my $conffile = "/etc/default/pveproxy";
217 # Note: evaluate with bash
218 my $shcmd = ". $conffile;\n";
219 $shcmd .= 'echo \"ALLOW_FROM:\$ALLOW_FROM\";';
220 $shcmd .= 'echo \"DENY_FROM:\$DENY_FROM\";';
221 $shcmd .= 'echo \"POLICY:\$POLICY\";';
222 $shcmd .= 'echo \"CIPHERS:\$CIPHERS\";';
223 $shcmd .= 'echo \"DHPARAMS:\$DHPARAMS\";';
225 my $data = -f
$conffile ?
`bash -c "$shcmd"` : '';
229 while ($data =~ s/^(.*)\n//) {
230 my ($key, $value) = split(/:/, $1, 2);
232 if ($key eq 'ALLOW_FROM' || $key eq 'DENY_FROM') {
234 foreach my $ip (split(/,/, $value)) {
235 $ip = "0/0" if $ip eq 'all';
236 push @$ips, Net
::IP-
>new($ip) || die Net
::IP
::Error
() . "\n";
239 } elsif ($key eq 'POLICY') {
240 die "unknown policy '$value'\n" if $value !~ m/^(allow|deny)$/;
241 $res->{$key} = $value;
242 } elsif ($key eq 'CIPHERS') {
243 $res->{$key} = $value;
244 } elsif ($key eq 'DHPARAMS') {
245 $res->{$key} = $value;
247 # silently skip everythin else?
254 sub resolve_proxyto
{
255 my ($rpcenv, $proxyto_callback, $proxyto, $uri_param) = @_;
258 if ($proxyto_callback) {
259 $node = $proxyto_callback->($rpcenv, $proxyto, $uri_param);
260 die "internal error - proxyto_callback returned nothing\n"
263 $node = $uri_param->{$proxyto};
264 raise_param_exc
({ $proxyto => "proxyto parameter does not exists"})