-# $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;
# 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 {
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:
&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);
&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);