OSSP CVS Repository

ossp - Difference in ossp-pkg/snmpdx/snmpdx.d/sysVM.pm versions 1.8 and 1.9
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/snmpdx/snmpdx.d/sysVM.pm 1.8 -> 1.9

--- sysVM.pm     2003/09/20 10:11:22     1.8
+++ sysVM.pm     2003/09/20 10:33:17     1.9
@@ -40,9 +40,9 @@
         ##
         ##  FreeBSD 4/5
         ##
-        ##  sysVMRamMax   "/sbin/sysctl -n hw.physmem", convert bytes to MB
+        ##  sysVMRamMax   "/sbin/sysctl -a", "hw.physmem", convert bytes to MB
         ##  sysVMRamUsed  sysVMRamMax - sysVMRamFree
-        ##  sysVMRamFree  "/usr/bin/vmstat", avm, convert KB to MB
+        ##  sysVMRamFree  "/sbin/sysctl -a", "vm.stats.vm.*", convert pages to MB
         ##  sysVMSwapMax  "/usr/sbin/swapinfo -k", 1K-blocks, last line, convert KB to MB
         ##  sysVMSwapUsed "/usr/sbin/swapinfo -k", Used, last line, convert KB to MB
         ##  sysVMSwapFree sysVMSwapMax - sysVMSwapUsed
@@ -53,28 +53,26 @@
             my $sysVMRamUsed = undef;
             my $sysVMRamFree = undef;
 
-            #   determine sysVMRamMax
-            my $out = $self->{-ctx}->{-sys}->run("/sbin/sysctl -n hw.physmem", "forever");
-            my $raw = $out->{-stdout};
-            if ($raw =~ m/^(\d+)$/) {
-                $sysVMRamMax = int($1 / (1024*1024));
+            #   query system for sysctl(8) variables
+            my $out = $self->{-ctx}->{-sys}->run("/sbin/sysctl -a", "1m");
+            my $sysctl = {};
+            foreach my $line (split(/\n/, $out->{-stdout})) {
+                $sysctl->{$1} = $2 if ($line =~ m/^([^:]+):\s+(.+)\s*$/s);
             }
 
+            #   determine sysVMRamMax
+            $sysVMRamMax = int(&mem_rounded($sysctl->{"hw.physmem"}) / (1024*1024));
+
             #   determine sysVMRamFree
-            $out = $self->{-ctx}->{-sys}->run("/usr/bin/vmstat", "1m");
-            $raw = $out->{-stdout};
-            my $n2i = {};
-            if ($raw =~ m/^[^\n]*\n *([^\n]*\bavm\b[^\n]*)\n *([^\n]*)\n$/s) {
-                $n2i = &mkn2i(split(/\s+/, $1));
-            }
-            if (defined($n2i->{avm})) {
-                my @dat = split(/\s+/, $2);
-                $sysVMRamFree = int(@dat[$n2i->{avm}] / 1024);
-            }
+            my $sysVMRamFree =
+                int((  $sysctl->{"hw.pagesize"}
+                     * (  $sysctl->{"vm.stats.vm.v_inactive_count"} 
+                        + $sysctl->{"vm.stats.vm.v_cache_count"}
+                        + $sysctl->{"vm.stats.vm.v_free_count"}    )) / (1024*1024));
 
             #   determine sysVMRamUsed
             if (defined($sysVMRamMax) and defined($sysVMRamFree)) {
-                $sysVMRamUsed = $sysVMRamMax - $sysVMRamFree;
+                $sysVMRamUsed = ($sysVMRamMax - $sysVMRamFree);
             }
 
             #   provide result
@@ -352,5 +350,22 @@
     return $rc;
 }
 
+#   round the physical memory size to the next power of two which is
+#   reasonable for memory cards. We do this by first determining the
+#   guessed memory card size under the assumption that usual computer
+#   hardware has an average of a maximally eight memory cards installed
+#   and those are usually of equal size.
+sub mem_rounded {
+    my ($mem_size) = @_;
+    my $chip_size  = 1;
+    my $chip_guess = ($mem_size / 8) - 1;
+    while ($chip_guess != 0) {
+        $chip_guess >>= 1;
+        $chip_size  <<= 1;
+    }
+    my $mem_round = (int($mem_size / $chip_size) + 1) * $chip_size;
+    return $mem_round;
+}
+
 1;
 

CVSTrac 2.0.1