[Nagiosplug-devel] patch: contrib/check_mem.pl using incorrect command

Nick Burch nick at torchbox.com
Wed Aug 18 03:16:03 CEST 2004


On Tue, 17 Aug 2004, Cook, Garry wrote:
> I can verify that neither of them works properly on my system (RH9). The
> '-f' option to get free memory does not work at all. However, with the
> -u option, it does appear to work as you have said in the original
> version, but the patched version does not. Observe:
> 
> /usr/local/src/nagios/nagios-plugins-HEAD-200408121647/contrib: top
> 
>  07:15:14  up 180 days, 18:56,  3 users,  load average: 1.05, 0.73, 0.66
> 120 processes: 113 sleeping, 1 running, 6 zombie, 0 stopped
> CPU0 states:  14.0% user   2.0% system    0.0% nice   0.0% iowait  82.0%
> idle
> CPU1 states:   1.0% user   1.0% system    0.0% nice   0.0% iowait  96.0%
> idle
> Mem:  1030344k av,  971092k used,   59252k free,       0k shrd,  182032k
> buff
>                     649668k actv,    4996k in_d,   18540k in_c
> Swap: 2040212k av,  143864k used, 1896348k free                  519776k
> cached

Instead of using top, using "free" is normally better, as it just reports 
the memory usage.

>From the above, we can see you have 1030344k memory in total. At that 
point, including disk buffers and caching, 971092k was used. You have 
182032k used for disk buffering and 519776k used for caching, both of 
which are handled by the kernel, and shoved out of the way when memory is 
required.

Based on this, you have 971092-182032-519776 = 269284k of memory in use by 
programs, and hence 1030344-269284 = 761060k free. (The free command does 
these calculation for you)
 
> /usr/local/src/nagios/nagios-plugins-HEAD-200408121647/contrib: sudo
> perl check_mem.pl -u -w 80 -c 90     
> Memory OK - 26.1% (269200 kB) used

This matches the above calculation

> /usr/local/src/nagios/nagios-plugins-HEAD-200408121647/contrib: sudo
> perl check_mem.orig.pl -u -w 80 -c 90
> Memory OK - 70.2% (143852 kB) used

This is your "swap used" value. the old version used vmstat to get its 
info, and the column if though was "memory used" is now "swap used"

> The patched version (check_mem.pl above) shows used memory when using
> the -u option. But I'm not sure where the figure (269200 kB) is coming
> from. Perhaps I'm not reading the output of top correctly, and this is
> correct? If so, please explain.

With linux, you have two pools of memory - real and swap. Swap memory is 
used for program requested storage that hasn't been used in "a bit" (how 
to define "a bit" is the source of much discussion, and is what memory 
managers are all about). Real memory is used for currently and recently 
used program storage, and data the OS has decided to cache for you. On a 
normal linux system, you'd expect used memory (including buffers) to be 
near 100% - this shows the scheduler and cacher are doing their job. What 
is normally of interest is how much memory is in use / free when you 
ignore the cached data (since the disk cache data may be thrown away by 
the memory manager if more space is required)
 
> Instead of fixing it the way that you have, why not add a third option
> to check the swap?

I've enclosed a new patch, which adds the "-s" option. When given, instead 
of reporting on real memory, it reports on swap instead

> Also, since you are taking the time to get in there and fix the code,
> why not add Performance Data output as well? 

I'm using "free" not "top", so all I have to hand is real memory useage, 
swap useage and buffering/caching. It wouldn't be too hard to report 
these, if people think they might be useful

Nick
-------------- next part --------------
--- check_mem.pl.old	2004-08-16 16:30:45.000000000 +0100
+++ check_mem.pl	2004-08-18 10:56:09.000000000 +0100
@@ -22,7 +22,7 @@
 use strict;
 use Getopt::Std;
 
-use vars qw($opt_c $opt_f $opt_u $opt_w
+use vars qw($opt_c $opt_f $opt_u $opt_w $opt_s
 	    $free_memory $used_memory $total_memory
 	    $crit_level $warn_level
             %exit_codes @memlist
@@ -38,17 +38,6 @@
 # Turn this to 1 to see reason for parameter errors (if any)
 $verb_err     = 0;
 
-# This the unix command string that brings Perl the data
-$command_line = `vmstat | tail -1 | awk '{print \$4,\$5}'`;
-
-chomp $command_line;
- at memlist      = split(/ /, $command_line);
-
-# Define the calculating scalars
-$used_memory  = $memlist[0];
-$free_memory  = $memlist[1];
-$total_memory = $used_memory + $free_memory;
-
 # Get the options
 if ($#ARGV le 0)
 {
@@ -56,7 +45,7 @@
 }
 else
 {
-  getopts('c:fuw:');
+  getopts('c:fusw:');
 }
 
 # Shortcircuit the switches
@@ -86,23 +75,46 @@
 $warn_level   = $opt_w;
 $crit_level   = $opt_c;
 
+# Which line of "free" contains our data of interest
+my $mem_line = 3;
+my $what_rep = "Memory";
+if($opt_s) { 
+	$mem_line = 4; 
+	$what_rep = "Swap";
+}
+
+# Now go and get the details of the free and used memory
+
+# This the unix command string that brings Perl the data
+$command_line = `free | head -$mem_line | tail -1 | awk '{print \$3,\$4}'`;
+
+chomp $command_line;
+ at memlist      = split(/ /, $command_line);
+
+# Define the calculating scalars
+$used_memory  = $memlist[0];
+$free_memory  = $memlist[1];
+$total_memory = $used_memory + $free_memory;
+
+
+# Now do our reporting bits
 if ($opt_f)
 {
   $percent    = $free_memory / $total_memory * 100;
   $fmt_pct    = sprintf "%.1f", $percent;
   if ($percent <= $crit_level)
   {
-    print "Memory CRITICAL - $fmt_pct% ($free_memory kB) free\n";
+    print "$what_rep CRITICAL - $fmt_pct% ($free_memory kB) free, $total_memory kB total\n";
     exit $exit_codes{'CRITICAL'};
   }
   elsif ($percent <= $warn_level)
   {
-    print "Memory WARNING - $fmt_pct% ($free_memory kB) free\n";
+    print "$what_rep WARNING - $fmt_pct% ($free_memory kB) free, $total_memory kB total\n";
     exit $exit_codes{'WARNING'};
   }
   else
   {
-    print "Memory OK - $fmt_pct% ($free_memory kB) free\n";
+    print "$what_rep OK - $fmt_pct% ($free_memory kB) free, $total_memory kB total\n";
     exit $exit_codes{'OK'};
   }
 }
@@ -112,17 +124,17 @@
   $fmt_pct    = sprintf "%.1f", $percent;
   if ($percent >= $crit_level)
   {
-    print "Memory CRITICAL - $fmt_pct% ($used_memory kB) used\n";
+    print "$what_rep CRITICAL - $fmt_pct% ($used_memory kB) used, $total_memory kB total\n";
     exit $exit_codes{'CRITICAL'};
   }
   elsif ($percent >= $warn_level)
   {
-    print "Memory WARNING - $fmt_pct% ($used_memory kB) used\n";
+    print "$what_rep WARNING - $fmt_pct% ($used_memory kB) used, $total_memory kB total\n";
     exit $exit_codes{'WARNING'};
   }
   else
   {
-    print "Memory OK - $fmt_pct% ($used_memory kB) used\n";
+    print "$what_rep OK - $fmt_pct% ($used_memory kB) used, $total_memory kB total\n";
     exit $exit_codes{'OK'};
   }
 }
@@ -136,6 +148,7 @@
   print "options:\n";
   print " -f           Check FREE memory\n";
   print " -u           Check USED memory\n";
+  print " -s           Instead of checking regular memory, check swap\n";
   print " -w PERCENT   Percent free/used when to warn\n";
   print " -c PERCENT   Percent free/used when critical\n";
   print "\nCopyright (C) 2000 Dan Larsson <dl\@tyfon.net>\n";


More information about the Devel mailing list