]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/ProcFSTools.pm
new helper read_proc_net_route
[pve-common.git] / data / PVE / ProcFSTools.pm
index 723fd422e19347c1fb49f20821983f2527a1b27a..8bb0d729b50b8ab974cdbc5d3b2ac57d5fe24e70 100644 (file)
@@ -1,6 +1,7 @@
 package PVE::ProcFSTools;
 
 use strict;
+use warnings;
 use POSIX;
 use Time::HiRes qw (gettimeofday);
 use IO::File;
@@ -20,11 +21,13 @@ sub read_cpuinfo {
        model => 'unknown',
        mhz => 0,
        cpus => 1,
+       sockets => 1,
     };
 
     my $fh = IO::File->new ($fn, "r");
     return $res if !$fh;
 
+    my $idhash = {};
     my $count = 0;
     while (defined(my $line = <$fh>)) {
        if ($line =~ m/^processor\s*:\s*\d+\s*$/i) {
@@ -35,9 +38,13 @@ sub read_cpuinfo {
            $res->{mhz} = $1 if !$res->{mhz};
        } elsif ($line =~ m/^flags\s*:.*(vmx|svm)/) {
            $res->{hvm} = 1; # Hardware Virtual Machine (Intel VT / AMD-V)
+       } elsif ($line =~ m/^physical id\s*:\s*(\d+)\s*$/i) {
+           $idhash->{$1} = 1;
        }
     }
 
+    $res->{sockets} = scalar(keys %$idhash) || 1;
+
     $res->{cpus} = $count;
 
     $fh->close;
@@ -169,6 +176,7 @@ sub read_meminfo {
        memtotal => 0,
        memfree => 0,
        memused => 0,
+       memshared => 0,
        swaptotal => 0,
        swapfree => 0,
        swapused => 0,
@@ -193,6 +201,9 @@ sub read_meminfo {
     $res->{swapfree} = $d->{swapfree};
     $res->{swapused} = $res->{swaptotal} - $res->{swapfree};
 
+    my $spages = PVE::Tools::file_read_firstline("/sys/kernel/mm/ksm/pages_sharing");
+    $res->{memshared} = int($spages) * 4096;
+
     return $res;
 }
 
@@ -205,7 +216,7 @@ sub read_memory_usage {
 
     my $line = PVE::Tools::file_read_firstline("/proc/$$/statm");
 
-    if ($line =~ m/^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+/) {
+    if ($line =~ m/^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*/) {
        $res->{size} = $1*$ps;
        $res->{resident} = $2*$ps;
        $res->{shared} = $3*$ps;
@@ -235,4 +246,42 @@ sub read_proc_net_dev {
     return $res;
 }
 
+sub write_proc_entry {
+    my ($filename, $data) = @_;#
+
+    my $fh = IO::File->new($filename,  O_WRONLY);
+    die "unable to open file '$filename' - $!\n" if !$fh;
+    die "unable to write '$filename' - $!\n" unless print $fh $data;
+    die "closing file '$filename' failed - $!\n" unless close $fh;
+    $fh->close();
+}
+
+sub read_proc_net_route {
+    my $filename = "/proc/net/route";
+
+    my $res = [];
+
+    my $fh = IO::File->new ($filename, "r");
+    return $res if !$fh;
+
+    my $int_to_quad = sub {
+       return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } (3, 2, 1, 0);
+    };
+
+    while (defined(my $line = <$fh>)) {
+       next if $line =~/^Iface\s+Destination/; # skip head
+       my ($iface, $dest, $gateway, $metric, $mask, $mtu) = (split(/\s+/, $line))[0,1,2,6,7,8];
+       push @$res, {
+           dest => &$int_to_quad(hex($dest)),
+           gateway => &$int_to_quad(hex($gateway)),
+           mask => &$int_to_quad(hex($mask)),
+           metric => $metric,
+           mtu => $mtu,
+          iface => $iface,
+       };
+    }
+
+    return $res;
+}
+
 1;