]>
Commit | Line | Data |
---|---|---|
e143e9d8 DM |
1 | package PVE::ProcFSTools; |
2 | ||
3 | use strict; | |
c36f332e | 4 | use warnings; |
e143e9d8 DM |
5 | use POSIX; |
6 | use Time::HiRes qw (gettimeofday); | |
7 | use IO::File; | |
ddcdc5ee | 8 | use List::Util qw(sum); |
e143e9d8 | 9 | use PVE::Tools; |
b78e40c0 | 10 | use Cwd qw(); |
e143e9d8 | 11 | |
f0d1b04f WB |
12 | use Socket qw(PF_INET PF_INET6 SOCK_DGRAM IPPROTO_IP); |
13 | ||
14 | use constant IFF_UP => 1; | |
15 | use constant IFNAMSIZ => 16; | |
16 | use constant SIOCGIFFLAGS => 0x8913; | |
17 | ||
e143e9d8 DM |
18 | my $clock_ticks = POSIX::sysconf(&POSIX::_SC_CLK_TCK); |
19 | ||
20 | my $cpuinfo; | |
21 | ||
e143e9d8 DM |
22 | sub read_cpuinfo { |
23 | my $fn = '/proc/cpuinfo'; | |
24 | ||
25 | return $cpuinfo if $cpuinfo; | |
26 | ||
27 | my $res = { | |
8104dfe3 | 28 | user_hz => $clock_ticks, |
e143e9d8 DM |
29 | model => 'unknown', |
30 | mhz => 0, | |
31 | cpus => 1, | |
66bda4e0 | 32 | sockets => 1, |
568abb4b | 33 | flags => '', |
e143e9d8 DM |
34 | }; |
35 | ||
36 | my $fh = IO::File->new ($fn, "r"); | |
37 | return $res if !$fh; | |
38 | ||
ddcdc5ee | 39 | my $cpuid = 0; |
4235c2f3 | 40 | my $idhash = {}; |
e143e9d8 DM |
41 | my $count = 0; |
42 | while (defined(my $line = <$fh>)) { | |
43 | if ($line =~ m/^processor\s*:\s*\d+\s*$/i) { | |
44 | $count++; | |
45 | } elsif ($line =~ m/^model\s+name\s*:\s*(.*)\s*$/i) { | |
46 | $res->{model} = $1 if $res->{model} eq 'unknown'; | |
47 | } elsif ($line =~ m/^cpu\s+MHz\s*:\s*(\d+\.\d+)\s*$/i) { | |
48 | $res->{mhz} = $1 if !$res->{mhz}; | |
568abb4b SR |
49 | } elsif ($line =~ m/^flags\s*:\s*(.*)$/) { |
50 | $res->{flags} = $1 if !length $res->{flags}; | |
66bda4e0 | 51 | } elsif ($line =~ m/^physical id\s*:\s*(\d+)\s*$/i) { |
ddcdc5ee MB |
52 | $cpuid = $1; |
53 | $idhash->{$1} = 1 if not defined($idhash->{$1}); | |
54 | } elsif ($line =~ m/^cpu cores\s*:\s*(\d+)\s*$/i) { | |
55 | $idhash->{$cpuid} = $1 if defined($idhash->{$cpuid}); | |
e143e9d8 DM |
56 | } |
57 | } | |
58 | ||
568abb4b SR |
59 | # Hardware Virtual Machine (Intel VT / AMD-V) |
60 | $res->{hvm} = $res->{flags} =~ m/\s(vmx|svm)\s/; | |
61 | ||
4235c2f3 DM |
62 | $res->{sockets} = scalar(keys %$idhash) || 1; |
63 | ||
ddcdc5ee MB |
64 | $res->{cores} = sum(values %$idhash) || 1; |
65 | ||
e143e9d8 DM |
66 | $res->{cpus} = $count; |
67 | ||
68 | $fh->close; | |
568abb4b | 69 | |
e143e9d8 DM |
70 | $cpuinfo = $res; |
71 | ||
72 | return $res; | |
73 | } | |
74 | ||
75 | sub read_proc_uptime { | |
76 | my $ticks = shift; | |
77 | ||
78 | my $line = PVE::Tools::file_read_firstline("/proc/uptime"); | |
79 | if ($line && $line =~ m|^(\d+\.\d+)\s+(\d+\.\d+)\s*$|) { | |
80 | if ($ticks) { | |
5157a3a2 | 81 | return (int($1*$clock_ticks), int($2*$clock_ticks)); |
e143e9d8 DM |
82 | } else { |
83 | return (int($1), int($2)); | |
84 | } | |
85 | } | |
86 | ||
87 | return (0, 0); | |
88 | } | |
89 | ||
2f98cd72 TL |
90 | sub kernel_version { |
91 | my $line = PVE::Tools::file_read_firstline("/proc/version"); | |
92 | ||
93 | if ($line && $line =~ m|^Linux\sversion\s((\d+(?:\.\d+)+)-?(\S+)?)|) { | |
94 | my ($fullversion, $version_numbers, $extra) = ($1, $2, $3); | |
95 | ||
96 | # variable names are the one from the Linux kernel Makefile | |
97 | my ($version, $patchlevel, $sublevel) = split(/\./, $version_numbers); | |
98 | ||
99 | return wantarray | |
100 | ? (int($version), int($patchlevel), int($sublevel), $extra, $fullversion) | |
101 | : $fullversion; | |
102 | } | |
103 | ||
104 | return (0, 0, 0, '', ''); | |
105 | } | |
106 | ||
1446e6a8 TL |
107 | # Check if the kernel is at least $major.$minor. Return either just a boolean, |
108 | # or a boolean and the kernel version's major.minor string from /proc/version | |
109 | sub check_kernel_release { | |
110 | my ($major, $minor) = @_; | |
111 | ||
112 | my ($k_major, $k_minor) = kernel_version(); | |
113 | ||
114 | my $ok; | |
115 | if (defined($minor)) { | |
116 | $ok = $k_major > $major || ($k_major == $major && $k_minor >= $minor); | |
117 | } else { | |
118 | $ok = $k_major >= $major; | |
119 | } | |
120 | ||
121 | return wantarray ? ($ok, "$k_major.$k_minor") : $ok; | |
122 | } | |
123 | ||
e143e9d8 DM |
124 | sub read_loadavg { |
125 | ||
126 | my $line = PVE::Tools::file_read_firstline('/proc/loadavg'); | |
127 | ||
128 | if ($line =~ m|^(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+\d+/\d+\s+\d+\s*$|) { | |
129 | return wantarray ? ($1, $2, $3) : $1; | |
130 | } | |
131 | ||
132 | return wantarray ? (0, 0, 0) : 0; | |
133 | } | |
134 | ||
eadfaabd TL |
135 | sub parse_pressure { |
136 | my ($path) = @_; | |
83874202 AD |
137 | |
138 | my $res = {}; | |
eadfaabd TL |
139 | my $v = qr/\d+\.\d+/; |
140 | my $fh = IO::File->new($path, "r") or return undef; | |
141 | while (defined (my $line = <$fh>)) { | |
142 | if ($line =~ /^(some|full)\s+avg10\=($v)\s+avg60\=($v)\s+avg300\=($v)\s+total\=(\d+)/) { | |
143 | $res->{$1}->{avg10} = $2; | |
144 | $res->{$1}->{avg60} = $3; | |
145 | $res->{$1}->{avg300} = $4; | |
146 | $res->{$1}->{total} = $4; | |
83874202 AD |
147 | } |
148 | } | |
eadfaabd TL |
149 | $fh->close; |
150 | return $res; | |
151 | } | |
152 | ||
153 | sub read_pressure { | |
154 | my $res = {}; | |
155 | foreach my $type (qw(cpu memory io)) { | |
156 | my $stats = parse_pressure("/proc/pressure/$type"); | |
157 | $res->{$type} = $stats if $stats; | |
158 | } | |
83874202 AD |
159 | return $res; |
160 | } | |
161 | ||
e143e9d8 DM |
162 | my $last_proc_stat; |
163 | ||
164 | sub read_proc_stat { | |
165 | my $res = { user => 0, nice => 0, system => 0, idle => 0 , sum => 0}; | |
166 | ||
167 | my $cpucount = 0; | |
168 | ||
169 | if (my $fh = IO::File->new ("/proc/stat", "r")) { | |
170 | while (defined (my $line = <$fh>)) { | |
171 | if ($line =~ m|^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s|) { | |
172 | $res->{user} = $1; | |
173 | $res->{nice} = $2; | |
174 | $res->{system} = $3; | |
175 | $res->{idle} = $4; | |
176 | $res->{used} = $1+$2+$3; | |
177 | $res->{iowait} = $5; | |
178 | } elsif ($line =~ m|^cpu\d+\s|) { | |
179 | $cpucount++; | |
180 | } | |
181 | } | |
182 | $fh->close; | |
183 | } | |
184 | ||
185 | $cpucount = 1 if !$cpucount; | |
186 | ||
187 | my $ctime = gettimeofday; # floating point time in seconds | |
188 | ||
189 | $res->{ctime} = $ctime; | |
190 | $res->{cpu} = 0; | |
191 | $res->{wait} = 0; | |
192 | ||
193 | $last_proc_stat = $res if !$last_proc_stat; | |
194 | ||
195 | my $diff = ($ctime - $last_proc_stat->{ctime}) * $clock_ticks * $cpucount; | |
196 | ||
197 | if ($diff > 1000) { # don't update too often | |
198 | my $useddiff = $res->{used} - $last_proc_stat->{used}; | |
199 | $useddiff = $diff if $useddiff > $diff; | |
200 | $res->{cpu} = $useddiff/$diff; | |
f27d5e6b | 201 | |
e143e9d8 DM |
202 | my $waitdiff = $res->{iowait} - $last_proc_stat->{iowait}; |
203 | $waitdiff = $diff if $waitdiff > $diff; | |
204 | $res->{wait} = $waitdiff/$diff; | |
f27d5e6b | 205 | |
e143e9d8 DM |
206 | $last_proc_stat = $res; |
207 | } else { | |
208 | $res->{cpu} = $last_proc_stat->{cpu}; | |
209 | $res->{wait} = $last_proc_stat->{wait}; | |
210 | } | |
211 | ||
212 | return $res; | |
213 | } | |
214 | ||
5157a3a2 | 215 | sub read_proc_pid_stat { |
e143e9d8 DM |
216 | my $pid = shift; |
217 | ||
218 | my $statstr = PVE::Tools::file_read_firstline("/proc/$pid/stat"); | |
219 | ||
5157a3a2 DM |
220 | if ($statstr && $statstr =~ m/^$pid \(.*\) (\S) (-?\d+) -?\d+ -?\d+ -?\d+ -?\d+ \d+ \d+ \d+ \d+ \d+ (\d+) (\d+) (-?\d+) (-?\d+) -?\d+ -?\d+ -?\d+ 0 (\d+) (\d+) (-?\d+) \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ -?\d+ -?\d+ \d+ \d+ \d+/) { |
221 | return { | |
222 | status => $1, | |
34fe9392 | 223 | ppid => $2, |
5157a3a2 DM |
224 | utime => $3, |
225 | stime => $4, | |
226 | starttime => $7, | |
227 | vsize => $8, | |
228 | rss => $9 * 4096, | |
229 | }; | |
e143e9d8 DM |
230 | } |
231 | ||
5157a3a2 DM |
232 | return undef; |
233 | } | |
234 | ||
235 | sub check_process_running { | |
236 | my ($pid, $pstart) = @_; | |
237 | ||
238 | # note: waitpid only work for child processes, but not | |
239 | # for processes spanned by other processes. | |
240 | # kill(0, pid) return succes for zombies. | |
241 | # So we read the status form /proc/$pid/stat instead | |
9bbc4e17 | 242 | |
5157a3a2 | 243 | my $info = read_proc_pid_stat($pid); |
9bbc4e17 | 244 | |
5399d422 | 245 | return $info && (!$pstart || ($info->{starttime} eq $pstart)) && ($info->{status} ne 'Z') ? $info : undef; |
5157a3a2 DM |
246 | } |
247 | ||
248 | sub read_proc_starttime { | |
249 | my $pid = shift; | |
250 | ||
251 | my $info = read_proc_pid_stat($pid); | |
252 | return $info ? $info->{starttime} : 0; | |
e143e9d8 DM |
253 | } |
254 | ||
255 | sub read_meminfo { | |
256 | ||
257 | my $res = { | |
258 | memtotal => 0, | |
259 | memfree => 0, | |
260 | memused => 0, | |
a01c5465 | 261 | memshared => 0, |
e143e9d8 DM |
262 | swaptotal => 0, |
263 | swapfree => 0, | |
264 | swapused => 0, | |
265 | }; | |
266 | ||
267 | my $fh = IO::File->new ("/proc/meminfo", "r"); | |
268 | return $res if !$fh; | |
269 | ||
270 | my $d = {}; | |
271 | while (my $line = <$fh>) { | |
272 | if ($line =~ m/^(\S+):\s+(\d+)\s*kB/i) { | |
273 | $d->{lc ($1)} = $2 * 1024; | |
9bbc4e17 | 274 | } |
e143e9d8 DM |
275 | } |
276 | close($fh); | |
277 | ||
278 | $res->{memtotal} = $d->{memtotal}; | |
279 | $res->{memfree} = $d->{memfree} + $d->{buffers} + $d->{cached}; | |
280 | $res->{memused} = $res->{memtotal} - $res->{memfree}; | |
281 | ||
282 | $res->{swaptotal} = $d->{swaptotal}; | |
283 | $res->{swapfree} = $d->{swapfree}; | |
284 | $res->{swapused} = $res->{swaptotal} - $res->{swapfree}; | |
285 | ||
9c1ccaf9 | 286 | my $spages = PVE::Tools::file_read_firstline("/sys/kernel/mm/ksm/pages_sharing") // 0 ; |
a01c5465 DM |
287 | $res->{memshared} = int($spages) * 4096; |
288 | ||
e143e9d8 DM |
289 | return $res; |
290 | } | |
291 | ||
292 | # memory usage of current process | |
293 | sub read_memory_usage { | |
294 | ||
295 | my $res = { size => 0, resident => 0, shared => 0 }; | |
296 | ||
297 | my $ps = 4096; | |
298 | ||
299 | my $line = PVE::Tools::file_read_firstline("/proc/$$/statm"); | |
300 | ||
845f01b6 | 301 | if ($line =~ m/^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*/) { |
e143e9d8 DM |
302 | $res->{size} = $1*$ps; |
303 | $res->{resident} = $2*$ps; | |
304 | $res->{shared} = $3*$ps; | |
305 | } | |
306 | ||
307 | return $res; | |
308 | } | |
309 | ||
310 | sub read_proc_net_dev { | |
311 | ||
312 | my $res = {}; | |
313 | ||
314 | my $fh = IO::File->new ("/proc/net/dev", "r"); | |
315 | return $res if !$fh; | |
316 | ||
317 | while (defined (my $line = <$fh>)) { | |
318 | if ($line =~ m/^\s*(.*):\s*(\d+)\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)\s+/) { | |
319 | $res->{$1} = { | |
320 | receive => $2, | |
321 | transmit => $3, | |
322 | }; | |
323 | } | |
324 | } | |
325 | ||
326 | close($fh); | |
327 | ||
328 | return $res; | |
329 | } | |
330 | ||
0758d092 DM |
331 | sub write_proc_entry { |
332 | my ($filename, $data) = @_;# | |
333 | ||
334 | my $fh = IO::File->new($filename, O_WRONLY); | |
335 | die "unable to open file '$filename' - $!\n" if !$fh; | |
336 | die "unable to write '$filename' - $!\n" unless print $fh $data; | |
337 | die "closing file '$filename' failed - $!\n" unless close $fh; | |
338 | $fh->close(); | |
339 | } | |
340 | ||
a993e470 DM |
341 | sub read_proc_net_route { |
342 | my $filename = "/proc/net/route"; | |
343 | ||
344 | my $res = []; | |
345 | ||
346 | my $fh = IO::File->new ($filename, "r"); | |
347 | return $res if !$fh; | |
348 | ||
349 | my $int_to_quad = sub { | |
350 | return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0); | |
351 | }; | |
352 | ||
353 | while (defined(my $line = <$fh>)) { | |
354 | next if $line =~/^Iface\s+Destination/; # skip head | |
355 | my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8]; | |
356 | push @$res, { | |
357 | dest => &$int_to_quad(hex($dest)), | |
358 | gateway => &$int_to_quad(hex($gateway)), | |
359 | mask => &$int_to_quad(hex($mask)), | |
360 | metric => $metric, | |
361 | mtu => $mtu, | |
362 | iface => $iface, | |
363 | }; | |
364 | } | |
365 | ||
366 | return $res; | |
367 | } | |
368 | ||
ad571b75 | 369 | sub read_proc_mounts { |
ee834936 | 370 | return PVE::Tools::file_get_contents("/proc/mounts", 512*1024); |
ad571b75 DM |
371 | } |
372 | ||
072dfa92 WB |
373 | # mounts encode spaces (\040), tabs (\011), newlines (\012), backslashes (\\ or \134) |
374 | sub decode_mount { | |
375 | my ($str) = @_; | |
376 | return $str =~ s/\\(?:040|01[12]|134|\\)/"\"$&\""/geer; | |
377 | } | |
378 | ||
379 | sub parse_mounts { | |
380 | my ($mounts) = @_; | |
dc00c052 | 381 | |
072dfa92 WB |
382 | my $mntent = []; |
383 | while ($mounts =~ /^\s*([^#].*)$/gm) { | |
384 | # lines from the file are encoded so we can just split at spaces | |
385 | my ($what, $dir, $fstype, $opts) = split(/[ \t]/, $1, 4); | |
386 | my ($freq, $passno) = (0, 0); | |
387 | # in glibc's parser frequency and pass seem to be optional | |
388 | $freq = $1 if $opts =~ s/\s+(\d+)$//; | |
389 | $passno = $1 if $opts =~ s/\s+(\d+)$//; | |
dc00c052 TL |
390 | push @$mntent, [ |
391 | decode_mount($what), | |
392 | decode_mount($dir), | |
393 | decode_mount($fstype), | |
394 | decode_mount($opts), | |
395 | $freq, | |
396 | $passno, | |
397 | ]; | |
072dfa92 WB |
398 | } |
399 | return $mntent; | |
400 | } | |
401 | ||
402 | sub parse_proc_mounts { | |
403 | return parse_mounts(read_proc_mounts()); | |
404 | } | |
405 | ||
115c8383 WL |
406 | sub is_mounted { |
407 | my ($mountpoint) = @_; | |
408 | ||
11ade4ad WB |
409 | $mountpoint = Cwd::realpath($mountpoint); |
410 | ||
5f808914 FG |
411 | return 0 if !defined($mountpoint); # path does not exist |
412 | ||
072dfa92 WB |
413 | my $mounts = parse_proc_mounts(); |
414 | return (grep { $_->[1] eq $mountpoint } @$mounts) ? 1 : 0; | |
115c8383 WL |
415 | } |
416 | ||
27107d5b WB |
417 | sub read_proc_net_ipv6_route { |
418 | my $filename = "/proc/net/ipv6_route"; | |
419 | ||
420 | my $res = []; | |
421 | ||
422 | my $fh = IO::File->new ($filename, "r"); | |
423 | return $res if !$fh; | |
424 | ||
ef67212c | 425 | my $read_v6addr = sub { $_[0] =~ s/....(?!$)/$&:/gr }; |
27107d5b WB |
426 | |
427 | # ipv6_route has no header | |
428 | while (defined(my $line = <$fh>)) { | |
429 | my ($dest, $prefix, $nexthop, $metric, $iface) = (split(/\s+/, $line))[0,1,4,5,9]; | |
430 | push @$res, { | |
431 | dest => &$read_v6addr($dest), | |
ef67212c | 432 | prefix => hex("$prefix"), |
27107d5b | 433 | gateway => &$read_v6addr($nexthop), |
ef67212c | 434 | metric => hex("$metric"), |
27107d5b WB |
435 | iface => $iface |
436 | }; | |
437 | } | |
438 | ||
439 | return $res; | |
440 | } | |
441 | ||
c1073fdc TL |
442 | sub upid_wait { |
443 | my ($upid, $waitfunc, $sleep_intervall) = @_; | |
444 | ||
445 | my $task = PVE::Tools::upid_decode($upid); | |
446 | ||
447 | $sleep_intervall = $sleep_intervall ? $sleep_intervall : 1; | |
448 | ||
449 | my $next_time = time + $sleep_intervall; | |
450 | ||
451 | while (check_process_running($task->{pid}, $task->{pstart})) { | |
452 | ||
453 | if (time >= $next_time && $waitfunc && ref($waitfunc) eq 'CODE'){ | |
454 | &$waitfunc($task); | |
455 | $next_time = time + $sleep_intervall; | |
456 | } | |
457 | ||
458 | CORE::sleep(1); | |
459 | } | |
460 | } | |
461 | ||
f0d1b04f WB |
462 | # struct ifreq { // FOR SIOCGIFFLAGS: |
463 | # char ifrn_name[IFNAMSIZ] | |
464 | # short ifru_flags | |
465 | # }; | |
466 | my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ . 's1'; | |
467 | sub get_active_network_interfaces { | |
468 | # Use the interface name list from /proc/net/dev | |
469 | open my $fh, '<', '/proc/net/dev' | |
470 | or die "failed to open /proc/net/dev: $!\n"; | |
471 | # And filter by IFF_UP flag fetched via a PF_INET6 socket ioctl: | |
472 | my $sock; | |
473 | socket($sock, PF_INET6, SOCK_DGRAM, &IPPROTO_IP) | |
474 | or socket($sock, PF_INET, SOCK_DGRAM, &IPPROTO_IP) | |
475 | or return []; | |
476 | ||
477 | my $ifaces = []; | |
478 | while(defined(my $line = <$fh>)) { | |
479 | next if $line !~ /^\s*([^:\s]+):/; | |
480 | my $ifname = $1; | |
481 | my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0); | |
482 | if (!defined(ioctl($sock, SIOCGIFFLAGS, $ifreq))) { | |
483 | warn "failed to get interface flags for: $ifname\n"; | |
484 | next; | |
485 | } | |
486 | my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq); | |
487 | push @$ifaces, $ifname if ($flags & IFF_UP); | |
488 | } | |
489 | close $fh; | |
490 | close $sock; | |
491 | return $ifaces; | |
492 | } | |
493 | ||
e143e9d8 | 494 | 1; |