4 # check_mem.pl Copyright (C) 2000 Dan Larsson <dl@tyfon.net>
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.
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.
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
21 # Tell Perl what we need to use
25 use vars
qw($opt_c $opt_f $opt_u $opt_w
26 $free_memory $used_memory $total_memory
27 $crit_level $warn_level
30 $verb_err $command_line);
32 # Predefined exit codes for Nagios
33 %exit_codes = ('UNKNOWN' ,-1,
38 # Turn this to 1 to see reason for parameter errors (if any)
41 # This the unix command string that brings Perl the data
42 $command_line = `vmstat | tail -1 | awk '{print \$4,\$5}'`;
45 @memlist = split(/ /, $command_line);
47 # Define the calculating scalars
48 $used_memory = $memlist[0];
49 $free_memory = $memlist[1];
50 $total_memory = $used_memory + $free_memory;
62 # Shortcircuit the switches
63 if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0)
65 print "*** You must define WARN and CRITICAL levels!" if ($verb_err);
68 elsif (!$opt_f and !$opt_u)
70 print "*** You must select to monitor either USED or FREE memory!" if ($verb_err);
74 # Check if levels are sane
75 if ($opt_w <= $opt_c and $opt_f)
77 print "*** WARN level must not be less than CRITICAL when checking FREE memory!" if ($verb_err);
80 elsif ($opt_w >= $opt_c and $opt_u)
82 print "*** WARN level must not be greater than CRITICAL when checking USED memory!" if ($verb_err);
91 $percent = $free_memory / $total_memory * 100;
92 $fmt_pct = sprintf "%.1f", $percent;
93 if ($percent <= $crit_level)
95 print "Memory CRITICAL - $fmt_pct% ($free_memory kB) free\n";
96 exit $exit_codes{'CRITICAL'};
98 elsif ($percent <= $warn_level)
100 print "Memory WARNING - $fmt_pct% ($free_memory kB) free\n";
101 exit $exit_codes{'WARNING'};
105 print "Memory OK - $fmt_pct% ($free_memory kB) free\n";
106 exit $exit_codes{'OK'};
111 $percent = $used_memory / $total_memory * 100;
112 $fmt_pct = sprintf "%.1f", $percent;
113 if ($percent >= $crit_level)
115 print "Memory CRITICAL - $fmt_pct% ($used_memory kB) used\n";
116 exit $exit_codes{'CRITICAL'};
118 elsif ($percent >= $warn_level)
120 print "Memory WARNING - $fmt_pct% ($used_memory kB) used\n";
121 exit $exit_codes{'WARNING'};
125 print "Memory OK - $fmt_pct% ($used_memory kB) used\n";
126 exit $exit_codes{'OK'};
133 print "\ncheck_mem.pl v1.0 - Nagios Plugin\n\n";
135 print " check_mem.pl -<f|u> -w <warnlevel> -c <critlevel>\n\n";
137 print " -f Check FREE memory\n";
138 print " -u Check USED memory\n";
139 print " -w PERCENT Percent free/used when to warn\n";
140 print " -c PERCENT Percent free/used when critical\n";
141 print "\nCopyright (C) 2000 Dan Larsson <dl\@tyfon.net>\n";
142 print "check_mem.pl comes with absolutely NO WARRANTY either implied or explicit\n";
143 print "This program is licensed under the terms of the\n";
144 print "GNU General Public License (check source code for details)\n";
145 exit $exit_codes{'UNKNOWN'};