| 1 | #!/usr/bin/env perl -w | ||
| 2 | # $Id: check_mem.pl 2 2002-02-28 06:42:51Z egalstad $ | ||
| 3 | |||
| 4 | # check_mem.pl Copyright (C) 2000 Dan Larsson <dl@tyfon.net> | ||
| 5 | # | ||
| 6 | # This program is free software; you can redistribute it and/or | ||
| 7 | # modify it under the terms of the GNU General Public License | ||
| 8 | # as published by the Free Software Foundation; either version 2 | ||
| 9 | # of the License, or (at your option) any later version. | ||
| 10 | # | ||
| 11 | # This program is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty | ||
| 13 | # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | # GNU General Public License for more details. | ||
| 15 | # | ||
| 16 | # you should have received a copy of the GNU General Public License | ||
| 17 | # along with this program (or with Nagios); if not, write to the | ||
| 18 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
| 19 | # Boston, MA 02111-1307, USA | ||
| 20 | |||
| 21 | ## no critic # added by M.S. | ||
| 22 | |||
| 23 | # Tell Perl what we need to use | ||
| 24 | use strict; | ||
| 25 | use Getopt::Std; | ||
| 26 | |||
| 27 | use vars qw($opt_c $opt_f $opt_u $opt_w | ||
| 28 | $free_memory $used_memory $total_memory | ||
| 29 | $crit_level $warn_level | ||
| 30 | %exit_codes @memlist | ||
| 31 | $percent $fmt_pct | ||
| 32 | $verb_err $command_line); | ||
| 33 | |||
| 34 | # Predefined exit codes for Nagios | ||
| 35 | %exit_codes = ('UNKNOWN' ,-1, | ||
| 36 | 'OK' , 0, | ||
| 37 | 'WARNING' , 1, | ||
| 38 | 'CRITICAL', 2,); | ||
| 39 | |||
| 40 | # Turn this to 1 to see reason for parameter errors (if any) | ||
| 41 | $verb_err = 0; | ||
| 42 | |||
| 43 | # This the unix command string that brings Perl the data | ||
| 44 | $command_line = `vmstat | tail -1 | awk '{print \$4,\$5}'`; | ||
| 45 | |||
| 46 | chomp $command_line; | ||
| 47 | @memlist = split(/ /, $command_line); | ||
| 48 | |||
| 49 | # Define the calculating scalars | ||
| 50 | $used_memory = $memlist[0]; | ||
| 51 | $free_memory = $memlist[1]; | ||
| 52 | $total_memory = $used_memory + $free_memory; | ||
| 53 | |||
| 54 | # Get the options | ||
| 55 | if ($#ARGV le 0) | ||
| 56 | { | ||
| 57 | &usage; | ||
| 58 | } | ||
| 59 | else | ||
| 60 | { | ||
| 61 | getopts('c:fuw:'); | ||
| 62 | } | ||
| 63 | |||
| 64 | # Shortcircuit the switches | ||
| 65 | if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0) | ||
| 66 | { | ||
| 67 | print "*** You must define WARN and CRITICAL levels!" if ($verb_err); | ||
| 68 | &usage; | ||
| 69 | } | ||
| 70 | elsif (!$opt_f and !$opt_u) | ||
| 71 | { | ||
| 72 | print "*** You must select to monitor either USED or FREE memory!" if ($verb_err); | ||
| 73 | &usage; | ||
| 74 | } | ||
| 75 | |||
| 76 | # Check if levels are sane | ||
| 77 | if ($opt_w <= $opt_c and $opt_f) | ||
| 78 | { | ||
| 79 | print "*** WARN level must not be less than CRITICAL when checking FREE memory!" if ($verb_err); | ||
| 80 | &usage; | ||
| 81 | } | ||
| 82 | elsif ($opt_w >= $opt_c and $opt_u) | ||
| 83 | { | ||
| 84 | print "*** WARN level must not be greater than CRITICAL when checking USED memory!" if ($verb_err); | ||
| 85 | &usage; | ||
| 86 | } | ||
| 87 | |||
| 88 | $warn_level = $opt_w; | ||
| 89 | $crit_level = $opt_c; | ||
| 90 | |||
| 91 | if ($opt_f) | ||
| 92 | { | ||
| 93 | $percent = $free_memory / $total_memory * 100; | ||
| 94 | $fmt_pct = sprintf "%.1f", $percent; | ||
| 95 | if ($percent <= $crit_level) | ||
| 96 | { | ||
| 97 | print "Memory CRITICAL - $fmt_pct% ($free_memory kB) free\n"; | ||
| 98 | exit $exit_codes{'CRITICAL'}; | ||
| 99 | } | ||
| 100 | elsif ($percent <= $warn_level) | ||
| 101 | { | ||
| 102 | print "Memory WARNING - $fmt_pct% ($free_memory kB) free\n"; | ||
| 103 | exit $exit_codes{'WARNING'}; | ||
| 104 | } | ||
| 105 | else | ||
| 106 | { | ||
| 107 | print "Memory OK - $fmt_pct% ($free_memory kB) free\n"; | ||
| 108 | exit $exit_codes{'OK'}; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | elsif ($opt_u) | ||
| 112 | { | ||
| 113 | $percent = $used_memory / $total_memory * 100; | ||
| 114 | $fmt_pct = sprintf "%.1f", $percent; | ||
| 115 | if ($percent >= $crit_level) | ||
| 116 | { | ||
| 117 | print "Memory CRITICAL - $fmt_pct% ($used_memory kB) used\n"; | ||
| 118 | exit $exit_codes{'CRITICAL'}; | ||
| 119 | } | ||
| 120 | elsif ($percent >= $warn_level) | ||
| 121 | { | ||
| 122 | print "Memory WARNING - $fmt_pct% ($used_memory kB) used\n"; | ||
| 123 | exit $exit_codes{'WARNING'}; | ||
| 124 | } | ||
| 125 | else | ||
| 126 | { | ||
| 127 | print "Memory OK - $fmt_pct% ($used_memory kB) used\n"; | ||
| 128 | exit $exit_codes{'OK'}; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | # Show usage | ||
| 133 | sub usage() | ||
| 134 | { | ||
| 135 | print "\ncheck_mem.pl v1.0 - Nagios Plugin\n\n"; | ||
| 136 | print "usage:\n"; | ||
| 137 | print " check_mem.pl -<f|u> -w <warnlevel> -c <critlevel>\n\n"; | ||
| 138 | print "options:\n"; | ||
| 139 | print " -f Check FREE memory\n"; | ||
| 140 | print " -u Check USED memory\n"; | ||
| 141 | print " -w PERCENT Percent free/used when to warn\n"; | ||
| 142 | print " -c PERCENT Percent free/used when critical\n"; | ||
| 143 | print "\nCopyright (C) 2000 Dan Larsson <dl\@tyfon.net>\n"; | ||
| 144 | print "check_mem.pl comes with absolutely NO WARRANTY either implied or explicit\n"; | ||
| 145 | print "This program is licensed under the terms of the\n"; | ||
| 146 | print "GNU General Public License (check source code for details)\n"; | ||
| 147 | exit $exit_codes{'UNKNOWN'}; | ||
| 148 | } |