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