blog:plugin_to_check_free_server_memory_with_nagios
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| blog:plugin_to_check_free_server_memory_with_nagios [2011-08-08 19:46] – brb | blog:plugin_to_check_free_server_memory_with_nagios [2011-08-08 19:47] (current) – brb | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| Great plugin, | Great plugin, | ||
| - | <file check_mem> | + | [[:blogfiles: |
| - | # | + | |
| - | # $Id: check_mem.pl 2 2002-02-28 06:42:51Z egalstad $ | + | |
| - | + | ||
| - | # Original script stolen from: | + | |
| - | # check_mem.pl | + | |
| - | # hacked by | + | |
| - | # Justin Ellison < | + | |
| - | # | + | |
| - | # 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. | + | |
| - | # 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); | + | |
| - | # 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:: | + | |
| - | + | ||
| - | #TODO - Convert to Nagios:: | + | |
| - | #TODO - Use an alarm | + | |
| - | + | ||
| - | # Predefined exit codes for Nagios | + | |
| - | use vars qw($opt_c $opt_f $opt_u $opt_w $opt_C $opt_v %exit_codes); | + | |
| - | %exit_codes | + | |
| - | ' | + | |
| - | ' | + | |
| - | ' | + | |
| - | ); | + | |
| - | + | ||
| - | # Get our variables, do our checking: | + | |
| - | init(); | + | |
| - | + | ||
| - | # Get the numbers: | + | |
| - | my ($free_memory_kb, | + | |
| - | print " | + | |
| - | + | ||
| - | if ($opt_C) { #Do we count caches as free? | + | |
| - | $used_memory_kb -= $caches_kb; | + | |
| - | $free_memory_kb += $caches_kb; | + | |
| - | } | + | |
| - | + | ||
| - | # Round to the nearest KB | + | |
| - | $free_memory_kb = sprintf(' | + | |
| - | $used_memory_kb = sprintf(' | + | |
| - | $caches_kb = sprintf(' | + | |
| - | + | ||
| - | # Tell Nagios what we came up with | + | |
| - | tell_nagios($used_memory_kb, | + | |
| - | + | ||
| - | + | ||
| - | sub tell_nagios { | + | |
| - | my ($used, | + | |
| - | + | ||
| - | # Calculate Total Memory | + | |
| - | my $total = $free + $used; | + | |
| - | print " | + | |
| - | + | ||
| - | my $perfdata = " | + | |
| - | + | ||
| - | if ($opt_f) { | + | |
| - | my $percent | + | |
| - | if ($percent <= $opt_c) { | + | |
| - | finish(" | + | |
| - | } | + | |
| - | elsif ($percent <= $opt_w) { | + | |
| - | finish(" | + | |
| - | } | + | |
| - | else { | + | |
| - | finish(" | + | |
| - | } | + | |
| - | } | + | |
| - | elsif ($opt_u) { | + | |
| - | my $percent | + | |
| - | if ($percent >= $opt_c) { | + | |
| - | finish(" | + | |
| - | } | + | |
| - | elsif ($percent >= $opt_w) { | + | |
| - | finish(" | + | |
| - | } | + | |
| - | else { | + | |
| - | finish(" | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | # Show usage | + | |
| - | sub usage() { | + | |
| - | print " | + | |
| - | print " | + | |
| - | print " check_mem.pl -< | + | |
| - | print " | + | |
| - | print " -f Check FREE memory\n"; | + | |
| - | print " -u Check USED memory\n"; | + | |
| - | print " -C Count OS caches as FREE memory\n"; | + | |
| - | print " -w PERCENT | + | |
| - | print " -c PERCENT | + | |
| - | print " | + | |
| - | print " | + | |
| - | 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{' | + | |
| - | } | + | |
| - | + | ||
| - | sub get_memory_info { | + | |
| - | my $used_memory_kb | + | |
| - | my $free_memory_kb | + | |
| - | my $total_memory_kb = 0; | + | |
| - | my $caches_kb | + | |
| - | + | ||
| - | my $uname; | + | |
| - | if ( -e '/ | + | |
| - | $uname = `/ | + | |
| - | } | + | |
| - | elsif ( -e '/ | + | |
| - | $uname = `/bin/uname -a`; | + | |
| - | } | + | |
| - | else { | + | |
| - | die " | + | |
| - | } | + | |
| - | print "uname returns $uname" | + | |
| - | if ( $uname =~ /Linux/ ) { | + | |
| - | my @meminfo = `/bin/cat / | + | |
| - | foreach (@meminfo) { | + | |
| - | chomp; | + | |
| - | if (/ | + | |
| - | my $counter_name = $1; | + | |
| - | if ($counter_name eq ' | + | |
| - | $free_memory_kb = $2; | + | |
| - | } | + | |
| - | elsif ($counter_name eq ' | + | |
| - | $total_memory_kb = $2; | + | |
| - | } | + | |
| - | } | + | |
| - | elsif (/ | + | |
| - | $caches_kb += $2; | + | |
| - | } | + | |
| - | } | + | |
| - | $used_memory_kb = $total_memory_kb - $free_memory_kb; | + | |
| - | } | + | |
| - | elsif ( $uname =~ /SunOS/ ) { | + | |
| - | eval "use Sun:: | + | |
| - | if ($@) { #Kstat not available | + | |
| - | if ($opt_C) { | + | |
| - | print "You can't report on Solaris caches without Sun:: | + | |
| - | exit $exit_codes{UNKNOWN}; | + | |
| - | } | + | |
| - | my @vmstat = `/ | + | |
| - | my $line; | + | |
| - | foreach (@vmstat) { | + | |
| - | chomp; | + | |
| - | $line = $_; | + | |
| - | } | + | |
| - | $free_memory_kb = (split(/ /,$line))[5] / 1024; | + | |
| - | my @prtconf = `/ | + | |
| - | foreach (@prtconf) { | + | |
| - | if (/^Memory size: (\d+) Megabytes/) { | + | |
| - | $total_memory_kb = $1 * 1024; | + | |
| - | } | + | |
| - | } | + | |
| - | $used_memory_kb = $total_memory_kb - $free_memory_kb; | + | |
| - | + | ||
| - | } | + | |
| - | else { # We have kstat | + | |
| - | my $kstat = Sun:: | + | |
| - | my $phys_pages = ${kstat}-> | + | |
| - | my $free_pages = ${kstat}-> | + | |
| - | # We probably should account for UFS caching here, but it's unclear | + | |
| - | # to me how to determine UFS's cache size. There' | + | |
| - | # and maybe the physmem variable in the system_pages module?? | + | |
| - | # In the real world, it looks to be so small as not to really matter, | + | |
| - | # so we don't grab it. If someone can give me code that does this, | + | |
| - | # I'd be glad to put it in. | + | |
| - | my $arc_size = (exists ${kstat}-> | + | |
| - | | + | |
| - | : 0; | + | |
| - | $caches_kb += $arc_size; | + | |
| - | my $pagesize = `pagesize`; | + | |
| - | + | ||
| - | $total_memory_kb = $phys_pages * $pagesize / 1024; | + | |
| - | $free_memory_kb = $free_pages * $pagesize / 1024; | + | |
| - | $used_memory_kb = $total_memory_kb - $free_memory_kb; | + | |
| - | } | + | |
| - | } | + | |
| - | else { | + | |
| - | if ($opt_C) { | + | |
| - | print "You can't report on $uname caches!\n"; | + | |
| - | exit $exit_codes{UNKNOWN}; | + | |
| - | } | + | |
| - | my $command_line = `vmstat | tail -1 | awk ' | + | |
| - | chomp $command_line; | + | |
| - | my @memlist | + | |
| - | + | ||
| - | # Define the calculating scalars | + | |
| - | $used_memory_kb | + | |
| - | $free_memory_kb = $memlist[1]/ | + | |
| - | $total_memory_kb = $used_memory_kb + $free_memory_kb; | + | |
| - | } | + | |
| - | return ($free_memory_kb, | + | |
| - | } | + | |
| - | + | ||
| - | sub init { | + | |
| - | # Get the options | + | |
| - | if ($#ARGV le 0) { | + | |
| - | & | + | |
| - | } | + | |
| - | else { | + | |
| - | getopts(' | + | |
| - | } | + | |
| - | + | ||
| - | # 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!\n"; | + | |
| - | & | + | |
| - | } | + | |
| - | elsif (!$opt_f and !$opt_u) { | + | |
| - | print "*** You must select to monitor either USED or FREE memory!\n"; | + | |
| - | & | + | |
| - | } | + | |
| - | + | ||
| - | # 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!\n"; | + | |
| - | & | + | |
| - | } | + | |
| - | elsif ($opt_w >= $opt_c and $opt_u) { | + | |
| - | print "*** WARN level must not be greater than CRITICAL when checking USED memory!\n"; | + | |
| - | & | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | sub finish { | + | |
| - | my ($msg, | + | |
| - | print " | + | |
| - | exit $state; | + | |
| - | } | + | |
| - | </ | + | |
| Ref: http:// | Ref: http:// | ||
blog/plugin_to_check_free_server_memory_with_nagios.1312825571.txt.gz · Last modified: 2011-08-08 19:46 by brb
