]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2Tools.pm
hw-address: check if source file changed so cache needs update
[pve-manager.git] / PVE / API2Tools.pm
1 package PVE::API2Tools;
2
3 use strict;
4 use warnings;
5 use Net::IP;
6
7 use PVE::Exception qw(raise_param_exc);
8 use PVE::Tools;
9 use PVE::INotify;
10 use PVE::Cluster;
11 use PVE::DataCenterConfig;
12 use PVE::RPCEnvironment;
13 use Digest::MD5 qw(md5_hex);
14 use URI;
15 use URI::Escape;
16 use PVE::SafeSyslog;
17 use PVE::Storage::Plugin;
18
19 my $hwaddress;
20 my $hwaddress_st = {};
21
22 sub get_hwaddress {
23 my $fn = '/etc/ssh/ssh_host_rsa_key.pub';
24 my $st = stat($fn);
25
26 if (defined($hwaddress)
27 && $hwaddress_st->{mtime} == $st->mtime
28 && $hwaddress_st->{ino} == $st->ino
29 && $hwaddress_st->{dev} == $st->dev) {
30 return $hwaddress;
31 }
32
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);
36
37 return $hwaddress;
38 }
39
40 sub extract_node_stats {
41 my ($node, $members, $rrd, $exclude_stats) = @_;
42
43 my $entry = {
44 id => "node/$node",
45 node => $node,
46 type => "node",
47 status => 'unknown',
48 };
49
50 if (my $d = $rrd->{"pve2-node/$node"}) {
51
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;
59 }
60 $entry->{status} = 'online';
61 }
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;
67 }
68 }
69
70 if ($members && $members->{$node} &&
71 !$members->{$node}->{online}) {
72 $entry->{status} = 'offline';
73 }
74
75 return $entry;
76 }
77
78 sub extract_vm_stats {
79 my ($vmid, $data, $rrd) = @_;
80
81 my $entry = {
82 id => "$data->{type}/$vmid",
83 vmid => $vmid + 0,
84 node => $data->{node},
85 type => $data->{type},
86 status => 'unknown',
87 };
88
89 my $d;
90
91 if ($d = $rrd->{"pve2-vm/$vmid"}) {
92
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;
106
107 } elsif ($d = $rrd->{"pve2.3-vm/$vmid"}) {
108
109 $entry->{uptime} = ($d->[0] || 0) + 0;
110 $entry->{name} = $d->[1];
111 $entry->{status} = $d->[2];
112 $entry->{template} = $d->[3] + 0;
113
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;
124 };
125
126 return $entry;
127 }
128
129 sub extract_storage_stats {
130 my ($storeid, $scfg, $node, $rrd) = @_;
131
132 my $content = PVE::Storage::Plugin::content_hash_to_string($scfg->{content});
133
134 my $entry = {
135 id => "storage/$node/$storeid",
136 storage => $storeid,
137 node => $node,
138 type => 'storage',
139 plugintype => $scfg->{type},
140 status => 'unknown',
141 shared => $scfg->{shared} || 0,
142 content => $content,
143 };
144
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';
149 }
150
151 return $entry;
152 }
153
154 sub parse_http_proxy {
155 my ($proxyenv) = @_;
156
157 my $uri = URI->new($proxyenv);
158
159 my $scheme = $uri->scheme;
160 my $host = $uri->host;
161 my $port = $uri->port || 3128;
162
163 my ($username, $password);
164
165 if (defined(my $p_auth = $uri->userinfo())) {
166 ($username, $password) = map URI::Escape::uri_unescape($_), split(":", $p_auth, 2);
167 }
168
169 return ("$host:$port", $username, $password);
170 }
171
172 sub run_spiceterm {
173 my ($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd) = @_;
174
175 my $rpcenv = PVE::RPCEnvironment::get();
176
177 my $authuser = $rpcenv->get_user();
178
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);
182
183 my ($ticket, undef, $remote_viewer_config) =
184 PVE::AccessControl::remote_viewer_config($authuser, $vmid, $node, $proxy, $title, $port);
185
186 my $timeout = 40;
187
188 my $cmd = ['/usr/bin/spiceterm', '--port', $port, '--addr', 'localhost',
189 '--timeout', $timeout, '--authpath', $authpath,
190 '--permissions', $permissions];
191
192 my $dcconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
193 push @$cmd, '--keymap', $dcconf->{keyboard} if $dcconf->{keyboard};
194
195 push @$cmd, '--', @$shcmd;
196
197 my $realcmd = sub {
198 my $upid = shift;
199
200 syslog ('info', "starting spiceterm $upid - $title\n");
201
202 my $cmdstr = join (' ', @$cmd);
203 syslog ('info', "launch command: $cmdstr");
204
205 eval {
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' ;
208 delete $ENV{$k};
209 }
210 $ENV{PWD} = '/';
211 $ENV{SPICE_TICKET} = $ticket;
212
213 PVE::Tools::run_command($cmd, errmsg => 'spiceterm failed\n', keeplocale => 1);
214 };
215 if (my $err = $@) {
216 syslog ('err', $err);
217 }
218
219 return;
220 };
221
222 if ($vmid) {
223 $rpcenv->fork_worker('spiceproxy', $vmid, $authuser, $realcmd);
224 } else {
225 $rpcenv->fork_worker('spiceshell', undef, $authuser, $realcmd);
226 }
227
228 PVE::Tools::wait_for_vnc_port($port);
229
230 return $remote_viewer_config;
231 }
232
233 sub resolve_proxyto {
234 my ($rpcenv, $proxyto_callback, $proxyto, $uri_param) = @_;
235
236 my $node;
237 if ($proxyto_callback) {
238 $node = $proxyto_callback->($rpcenv, $proxyto, $uri_param);
239 die "internal error - proxyto_callback returned nothing\n"
240 if !$node;
241 } else {
242 $node = $uri_param->{$proxyto};
243 raise_param_exc({ $proxyto => "proxyto parameter does not exist"})
244 if !$node;
245 }
246 return $node;
247 }
248
249 sub get_resource_pool_guest_members {
250 my ($pool) = @_;
251
252 my $usercfg = PVE::Cluster::cfs_read_file("user.cfg");
253
254 my $vmlist = PVE::Cluster::get_vmlist() || {};
255 my $idlist = $vmlist->{ids} || {};
256
257 my $data = $usercfg->{pools}->{$pool};
258
259 die "pool '$pool' does not exist\n" if !$data;
260
261 my $pool_members = [ grep { $idlist->{$_} } keys %{$data->{vms}} ];
262
263 return $pool_members;
264 }
265
266 1;