| 1 | #!/usr/bin/env perl -w | ||
| 2 | |||
| 3 | # check_mem2.pl Copyright (C) 2012 Gisbert W. Selke <gws@cpan.org> | ||
| 4 | # Modelled after | ||
| 5 | # check_mem.pl Copyright (C) 2000 Dan Larsson <dl@tyfon.net> | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the terms of the GNU General Public License | ||
| 9 | # as published by the Free Software Foundation; either version 2 | ||
| 10 | # of the License, or (at your option) any later version. | ||
| 11 | # | ||
| 12 | # This program is distributed in the hope that it will be useful, | ||
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty | ||
| 14 | # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | # GNU General Public License for more details. | ||
| 16 | # | ||
| 17 | # You should have received a copy of the GNU General Public License | ||
| 18 | # along with this program (or with Nagios); if not, write to the | ||
| 19 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
| 20 | # Boston, MA 02111-1307, USA | ||
| 21 | |||
| 22 | ## no critic # added by M.S. | ||
| 23 | |||
| 24 | # Tell Perl what we need to use | ||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | use Getopt::Long qw(:config bundling); | ||
| 28 | |||
| 29 | BEGIN { eval('require Win32::SystemInfo') if ($^O =~ /Win/i); } | ||
| 30 | |||
| 31 | my $PROGNAME = 'check_mem2.pl'; | ||
| 32 | my $VERSION = '1.1'; | ||
| 33 | |||
| 34 | my ($check_free, $check_used, $level_warn, $level_crit, $verbose); | ||
| 35 | |||
| 36 | # Predefined exit codes for Nagios | ||
| 37 | my %exit_codes = ('UNKNOWN' => -1, | ||
| 38 | 'OK' => 0, | ||
| 39 | 'WARNING' => 1, | ||
| 40 | 'CRITICAL' => 2, | ||
| 41 | ); | ||
| 42 | |||
| 43 | # Get the options | ||
| 44 | GetOptions('critical|c=f' => \$level_crit, | ||
| 45 | 'warning|w=f' => \$level_warn, | ||
| 46 | 'free|f!' => \$check_free, | ||
| 47 | 'used|u!' => \$check_used, | ||
| 48 | 'verbose|v!' => \$verbose, | ||
| 49 | ); | ||
| 50 | |||
| 51 | my $opt_count = 0; | ||
| 52 | $opt_count++ if $check_free; | ||
| 53 | $opt_count++ if $check_used; | ||
| 54 | if ($opt_count != 1) { | ||
| 55 | print '*** You must select to monitor either USED or FREE memory!' if $verbose; | ||
| 56 | usage(); | ||
| 57 | } | ||
| 58 | |||
| 59 | # Check the levels | ||
| 60 | if (($level_warn <= 0) || ($level_crit <= 0)) { | ||
| 61 | print '*** You must define positive WARN and CRITICAL levels!' if $verbose; | ||
| 62 | usage(); | ||
| 63 | } | ||
| 64 | |||
| 65 | # Check if levels are sane | ||
| 66 | if ($check_free && ($level_warn <= $level_crit)) { | ||
| 67 | print '*** WARN level must not be less than or equal to CRITICAL when checking FREE memory!' if $verbose; | ||
| 68 | usage(); | ||
| 69 | } | ||
| 70 | |||
| 71 | if ($check_used && ($level_warn >= $level_crit)) { | ||
| 72 | print '*** WARN level must not be greater than or equal to CRITICAL when checking USED memory or PROCESSOR load!' if $verbose; | ||
| 73 | usage(); | ||
| 74 | } | ||
| 75 | |||
| 76 | my($memory, $percent); | ||
| 77 | if ($^O !~ /Win/i) { | ||
| 78 | # This the unix command string that brings Perl the data | ||
| 79 | my $command_line = `vmstat | tail -1`; | ||
| 80 | if (!$command_line) { | ||
| 81 | print "*** System command vmstat not found or yielded no result!"; | ||
| 82 | exit $exit_codes{'UNKNOWN'}; | ||
| 83 | } | ||
| 84 | my @memlist = split(/\s+/, $command_line); | ||
| 85 | my $total_memory = $memlist[3] + $memlist[4]; | ||
| 86 | $memory = $memlist[$check_used ? 3 : 4]; | ||
| 87 | $percent = $memory/$total_memory * 100; | ||
| 88 | } else { | ||
| 89 | my %info; | ||
| 90 | Win32::SystemInfo::MemoryStatus(%info); | ||
| 91 | my $total_memory = $info{'TotalPhys'}; # Or TotalVirtual?? | ||
| 92 | $percent = $check_used ? $info{'MemLoad'} : (100 - $info{'MemLoad'}); | ||
| 93 | $memory = sprintf '%d', $total_memory*$percent*0.01; | ||
| 94 | } | ||
| 95 | |||
| 96 | my $state = 'OK'; | ||
| 97 | if ($check_used) { | ||
| 98 | $state = 'WARNING' if ($percent >= $level_warn); | ||
| 99 | $state = 'CRITICAL' if ($percent >= $level_crit); | ||
| 100 | } else { | ||
| 101 | $state = 'WARNING' if ($percent <= $level_warn); | ||
| 102 | $state = 'CRITICAL' if ($percent <= $level_crit); | ||
| 103 | } | ||
| 104 | printf "Memory %s - %1.1f%% (%d kB) %s\n", $state, $percent, $memory, ($check_used ? 'used' : 'free'); | ||
| 105 | exit $exit_codes{$state}; | ||
| 106 | |||
| 107 | # Show usage | ||
| 108 | sub usage { | ||
| 109 | print "\n$PROGNAME $VERSION - Nagios Plugin\n\n"; | ||
| 110 | print "usage:\n"; | ||
| 111 | print " $PROGNAME -<f|u> -w <warnlevel> -c <critlevel> [-v]\n\n"; | ||
| 112 | print "options:\n"; | ||
| 113 | print " --free Check FREE memory\n"; | ||
| 114 | print " --used Check USED memory\n"; | ||
| 115 | print " --warning PERCENT Percent free/used/load when to warn\n"; | ||
| 116 | print " --critical PERCENT Percent free/used/load when critical\n"; | ||
| 117 | print " --verbose Show verbose error messages\n"; | ||
| 118 | print "All options may be abbreviated.\n"; | ||
| 119 | print "\nCopyright (C) 2012 Dan Larsson <dl\@tyfon.net> / Gisbert W. Selke <gws\@cpan.org)\n"; | ||
| 120 | print "$PROGNAME comes with absolutely NO WARRANTY either implied or explicit\n"; | ||
| 121 | print "This program is licensed under the terms of the\n"; | ||
| 122 | print "GNU General Public License (check source code for details)\n"; | ||
| 123 | exit $exit_codes{'UNKNOWN'}; | ||
| 124 | } |