[Nagiosplug-devel] patch: contrib/check_mem.pl using incorrectcommand
Nick Burch
nick at torchbox.com
Wed Aug 18 10:02:05 CEST 2004
On Wed, 18 Aug 2004, C. Bensend wrote:
> > Almost certainly. Anyone got any other platforms to hand who could let
> > me know the format (if any) of their "free" and "vmstat" commands?
>
> OpenBSD 3.5-STABLE vmstat:
Based on that, I looked for other machines. A few hours of fiddling with
the Sourceforge compile farm and the HP Testdrive later, I think the
script should now be there.
For linux, cygwin and solaris/sunos, it will report the free/used physical
memory. With the -s switch, it will report swap useage
For FreeBSD, NetBSD, OpenBSD, HP-UX and some dec thing, it reports overall
(physical + swap combined) useage, and will barf if asked about swap.
I can also confirm that all the system's I've seen report meaningful
numbers in columns 4 and 5 of vmstat. It's just a pity they can't agree on
what the hell they actually are, or mean....
Do people want to try the attached version?
Nick
-------------- next part --------------
#!/usr/bin/perl -w
# $Id: check_mem.pl,v 1.1.1.1 2002/02/28 06:42:54 egalstad Exp $
# check_mem.pl Copyright (C) 2000 Dan Larsson <dl at tyfon.net>,
# 2004 Nick Burch <nick at torchbox.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# you should have received a copy of the GNU General Public License
# along with this program (or with Nagios); if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA
# Tell Perl what we need to use
use strict;
use Getopt::Std;
use Config;
use vars qw($opt_c $opt_f $opt_u $opt_w $opt_s
$free_memory $used_memory $total_memory
$crit_level $warn_level $os
%exit_codes @memlist
$percent $fmt_pct
$verb_err $command_line);
# Predefined exit codes for Nagios
%exit_codes = ('UNKNOWN' ,-1,
'OK' , 0,
'WARNING' , 1,
'CRITICAL', 2,);
# Turn this to 1 to see reason for parameter errors (if any)
$verb_err = 0;
# What OS are we running on?
$os = $Config{'osname'};
# Get the options
if ($#ARGV le 0)
{
&usage;
}
else
{
getopts('c:fusw:');
}
# Shortcircuit the switches
if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0)
{
print "*** You must define WARN and CRITICAL levels!" if ($verb_err);
&usage;
}
elsif (!$opt_f and !$opt_u)
{
print "*** You must select to monitor either USED or FREE memory!" if ($verb_err);
&usage;
}
# Check if levels are sane
if ($opt_w <= $opt_c and $opt_f)
{
print "*** WARN level must not be less than CRITICAL when checking FREE memory!" if ($verb_err);
&usage;
}
elsif ($opt_w >= $opt_c and $opt_u)
{
print "*** WARN level must not be greater than CRITICAL when checking USED memory!" if ($verb_err);
&usage;
}
# Check if we can report on swap for this platform
if($opt_s && !($os eq "linux" || $os eq "cygwin" || $os eq "solaris")) {
# Can't get swap info from vmstat, only from free
print "Checking swap useage not possible on your platform\n";
exit $exit_codes{'UNKNOWN'};
}
$warn_level = $opt_w;
$crit_level = $opt_c;
# What to report when displaying the output
# Which line of "free" contains our data of interest
my $what_rep = "Memory";
if($opt_s) {
$what_rep = "Swap";
}
# Now go and get the details of the free and used memory
# As different Unix (and unix-esque) flavours report things differently,
# we need to use one of a few commands to check
if($os eq "freebsd" || $os eq "netbsd" || $os eq "openbsd" || $os eq "dec_os" || $os eq "hpux") {
# Ask vmstat. 4th param is used memory, 5th is free
$command_line = `vmstat | tail -1 | awk '{print \$4,\$5}'`;
} elsif($os eq "linux" || $os eq "cygwin") {
# Use the command "free" to get the required data
my $mem_line = 3;
if($opt_s) { $mem_line = 4; }
$command_line = `free | head -$mem_line | tail -1 | awk '{print \$3,\$4}'`;
} elsif($os eq "solaris") {
if($opt_s) {
# Go ask "swap -s"
$command_line = `swap -s | awk '{print \$9,\$11}' | sed s/k//g`;
} else {
# Ask both vmstat col 5 and prtconf line 2
my $free_cmd = `vmstat | tail -1 | awk '{print \$5}'`;
my $tot_cmd = `prtconf -p | head -2 | tail -1 | awk '{print \$3}'`;
chomp $free_cmd;
chomp $tot_cmd;
$command_line = (($tot_cmd*1024)-$free_cmd)." ".$free_cmd."\n";
}
} else {
print "I'm sorry, I don't know how to check memory on your system.\nPlease send the output of the following commands to\nnagiosplug-devel\@lists.sourceforge.net with a subject\n'check_mem.pl information for new platform' :\n\nfree\nvmstat\nman vmstat\n";
exit $exit_codes{'UNKNOWN'};
}
chomp $command_line;
@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 "$what_rep CRITICAL - $fmt_pct% ($free_memory kB) free, $total_memory kB total\n";
exit $exit_codes{'CRITICAL'};
}
elsif ($percent <= $warn_level)
{
print "$what_rep WARNING - $fmt_pct% ($free_memory kB) free, $total_memory kB total\n";
exit $exit_codes{'WARNING'};
}
else
{
print "$what_rep OK - $fmt_pct% ($free_memory kB) free, $total_memory kB total\n";
exit $exit_codes{'OK'};
}
}
elsif ($opt_u)
{
$percent = $used_memory / $total_memory * 100;
$fmt_pct = sprintf "%.1f", $percent;
if ($percent >= $crit_level)
{
print "$what_rep CRITICAL - $fmt_pct% ($used_memory kB) used, $total_memory kB total\n";
exit $exit_codes{'CRITICAL'};
}
elsif ($percent >= $warn_level)
{
print "$what_rep WARNING - $fmt_pct% ($used_memory kB) used, $total_memory kB total\n";
exit $exit_codes{'WARNING'};
}
else
{
print "$what_rep OK - $fmt_pct% ($used_memory kB) used, $total_memory kB total\n";
exit $exit_codes{'OK'};
}
}
# Show usage
sub usage()
{
print "\ncheck_mem.pl v1.0 - Nagios Plugin\n\n";
print "usage:\n";
print " check_mem.pl -<f|u> -w <warnlevel> -c <critlevel>\n\n";
print "options:\n";
print " -f Check FREE memory\n";
print " -u Check USED memory\n";
print " -s Check swap instead of regular memory. Only on some systems\n";
print " -w PERCENT Percent free/used when to warn\n";
print " -c PERCENT Percent free/used when critical\n";
print "\nCopyright 2000 Dan Larsson <dl\@tyfon.net>, 2004 Nick Burch <nick\@torchbox.com>\n";
print "check_mem.pl comes with absolutely NO WARRANTY either implied or explicit\n";
print "This program is licensed under the terms of the\n";
print "GNU General Public License (check source code for details)\n";
exit $exit_codes{'UNKNOWN'};
}
More information about the Devel
mailing list