From: Jan Willamowius Date: Fri, 30 Sep 2005 09:00:10 +0000 (+0000) Subject: tweaks for FreeBSD top output format (patch by Alan) X-Git-Tag: spong-2_8_0-beta3~16 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=81d071a91347c4696416b9376661b3cda3b7b0bd;p=spong.git tweaks for FreeBSD top output format (patch by Alan) --- diff --git a/src/lib/Spong/Client/plugins/check_memory b/src/lib/Spong/Client/plugins/check_memory index cb22193..97a43c8 100755 --- a/src/lib/Spong/Client/plugins/check_memory +++ b/src/lib/Spong/Client/plugins/check_memory @@ -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 () { /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; - $_ = ; - $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; + $_ = ; + $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);