]> git.etc.gen.nz Git - spong.git/commitdiff
tweaks for FreeBSD top output format (patch by Alan)
authorJan Willamowius <jan@willamowius.de>
Fri, 30 Sep 2005 09:00:10 +0000 (09:00 +0000)
committerJan Willamowius <jan@willamowius.de>
Fri, 30 Sep 2005 09:00:10 +0000 (09:00 +0000)
src/lib/Spong/Client/plugins/check_memory

index cb22193e2e66a5c110c1e8c64ac10dc1020d72f0..97a43c8b4a6ff91ab917b89ee33734fee9987d11 100755 (executable)
@@ -1,4 +1,4 @@
-# $Id: check_memory,v 1.4 2002/07/24 18:30:17 sljohnson Exp $
+# $Id: check_memory,v 1.5 2005/09/30 09:00:10 willamowius Exp $
 # vim:syn=perl:expandtab:ts=3:sw=3:ai:si
 # Register this routine with the plugin registry
 $CHECKFUNCS{'memory'} = \&check_memory;
@@ -7,7 +7,7 @@ $CHECKFUNCS{'memory'} = \&check_memory;
 # nice way of doing things, we grab the output of top or free on linux. It's
 # likely going to have to be configured for each OS it runs on. :-(
 
-$MEMCHECK   = "/usr/bin/top -b -n 1 "; # To use 'top'
+$MEMCHECK   = "/usr/bin/top -b -n 1 "; # To use 'top'
 # $MEMCHECK   = "/usr/bin/free";         # On linux systems
 
 sub check_memory {
@@ -63,11 +63,17 @@ sub check_memory_free {
 
 sub check_memory_top {
    $kmb='\s+(\d+[KM])';
-   @parse_error = ("yellow", "Can't parse memory line", $message);
+   $message = "";
    open CMD, "$MEMCHECK |";
    while (<CMD>) {
       /PID/ && last;
       /^$/ && next;
+
+       # checks for FreeBSD top output format
+      /^last pid:/ && next;
+      /^\d*.*processes/ && next;
+       # end
+
       $message .= $_;
       if (/^Mem/) {
          # top from the procps on Linux has this format:
@@ -81,24 +87,9 @@ sub check_memory_top {
                &to_KB($swaptotal, $swapused, $swapfree, $cached);
             } else {
                close CMD;
-               return @parse_error;
+               return ("yellow", "Can't parse memory line", $message);
             };
             $hardmemused = $memused-$buffers-$cached;
-         } elsif (/^Mem:$kmb Active,$kmb Inact,$kmb Wired,$kmb Cache,$kmb Buf,$kmb Free/) {
-            # the top package supporting FreeBSD has this format
-            ($memactive,$meminact,$memwired,$memcache,$buffers,$memfree) = ($1,$2,$3,$4,$5,$6);
-            &to_KB($memactive,$meminact,$memwired,$cached,$buffers,$memfree);
-            $memtotal = $memactive+$meminact+$memwired+$memcache;
-            $hardmemused = $memtotal-$memfree;
-            $_ = <CMD>;
-            $message .= $_;
-            if (/^Swap:$kmb Total,$kmb Used,$kmb Free/) {
-               ($swaptotal, $swapused, $swapfree) = ($1,$2,$3);
-               &to_KB($swaptotal, $swapused, $swapfree);
-            } else {
-               close CMD;
-               return @parse_error;
-            }
          } elsif (/^Memory:$kmb real,$kmb free,$kmb swap,$kmb free swap/) {
             # the top package supporting various unices has this format
             ($memtotal,$memfree,$swapused,$swapfree) = ($1,$2,$3,$4);
@@ -111,9 +102,24 @@ sub check_memory_top {
             &to_KB($memtotal,$memfree,$swapused,$swapfree);
             $hardmemused = $memtotal-$memfree;
             $swaptotal = $swapused+$swapfree;
+        } elsif (/^Mem:$kmb\sActive,$kmb\sInact,$kmb\sWired,$kmb\sCache,$kmb\sBuf,$kmb\sFree/) {
+               # the top package output format of FreeBSD
+               ($memactive,$meminactive,$memwired,$memcache,$membuf,$memfree) = ($1,$2,$3,$4,$5,$6);
+               &to_KB($memactive,$meminactive,$memwired,$memcache,$membuf,$memfree);
+               $memtotal = $memactive + $meminactive + $memwired + $memcache + $membuf + $memfree;
+               $_ = <CMD>;
+               $message .= $_;
+               if (/^Swap:$kmb Total,$kmb Used,$kmb Free/) {
+                       ($swaptotal,$swapused,$swapfree) = ($1,$2,$3);
+                       &to_KB($swaptotal,$swapused,$swapfree);
+               } else {
+                       close CMD;
+                       return ("yellow", "Can't parse SWAP line", $message);
+               }
+               $hardmemused = $memtotal - $memfree;
          } else {
             close CMD;
-            return @parse_error;
+            return ("yellow", "Can't parse memory line (no match)", $message);
          }
          $physpctused = floor($hardmemused/$memtotal*100);
          $virtpctused = floor(($hardmemused+$swapused)/($memtotal+$swaptotal)*100);