]> git.proxmox.com Git - pve-common.git/commitdiff
proc fs tools: handle proc/stat without guest values
authorFabian Ebner <f.ebner@proxmox.com>
Fri, 22 Apr 2022 07:17:48 +0000 (09:17 +0200)
committerFabian Ebner <f.ebner@proxmox.com>
Tue, 14 Jun 2022 08:25:24 +0000 (10:25 +0200)
PMG is often run as a container, and in certain environments (like
Virtuozzo 7), the last two values (guest and guest_nice) might not be
present. This led to a division by zero, because the total value was
never updated.

Reported in the community forum:
https://forum.proxmox.com/threads/106896/

Acked-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
src/PVE/ProcFSTools.pm

index 88df5d2b28df1cabde93685ed4a9c9dab11fe582..d636427688d2554254505012e71e7b8160b204aa 100644 (file)
@@ -168,9 +168,9 @@ sub read_proc_stat {
 
     if (my $fh = IO::File->new ("/proc/stat", "r")) {
        while (defined (my $line = <$fh>)) {
-           if ($line =~ m|^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)|) {
-               $res->{user} = $1 - $9;
-               $res->{nice} = $2 - $10;
+           if ($line =~ m|^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)(?:\s+(\d+)\s+(\d+))?|) {
+               $res->{user} = $1 - ($9 // 0);
+               $res->{nice} = $2 - ($10 // 0);
                $res->{system} = $3;
                $res->{idle} = $4;
                $res->{used} = $1+$2+$3+$6+$7+$8;
@@ -178,8 +178,8 @@ sub read_proc_stat {
                $res->{irq} = $6;
                $res->{softirq} = $7;
                $res->{steal} = $8;
-               $res->{guest} = $9;
-               $res->{guest_nice} = $10;
+               $res->{guest} = $9 // 0;
+               $res->{guest_nice} = $10 // 0;
            } elsif ($line =~ m|^cpu\d+\s|) {
                $cpucount++;
            }