Fix newly added stratum check (has to be decreased by one when using normal NTP packe...
[monitoring-plugins.git] / contrib / check_mem.pl
blobf0c821293aef4d95c80e9f187895993116b40471
1 #!/usr/bin/perl -w
2 # $Id$
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
22 use strict;
23 use Getopt::Std;
25 use vars qw($opt_c $opt_f $opt_u $opt_w
26 $free_memory $used_memory $total_memory
27 $crit_level $warn_level
28 %exit_codes @memlist
29 $percent $fmt_pct
30 $verb_err $command_line);
32 # Predefined exit codes for Nagios
33 %exit_codes = ('UNKNOWN' ,-1,
34 'OK' , 0,
35 'WARNING' , 1,
36 'CRITICAL', 2,);
38 # Turn this to 1 to see reason for parameter errors (if any)
39 $verb_err = 0;
41 # This the unix command string that brings Perl the data
42 $command_line = `vmstat | tail -1 | awk '{print \$4,\$5}'`;
44 chomp $command_line;
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;
52 # Get the options
53 if ($#ARGV le 0)
55 &usage;
57 else
59 getopts('c:fuw:');
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);
66 &usage;
68 elsif (!$opt_f and !$opt_u)
70 print "*** You must select to monitor either USED or FREE memory!" if ($verb_err);
71 &usage;
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);
78 &usage;
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);
83 &usage;
86 $warn_level = $opt_w;
87 $crit_level = $opt_c;
89 if ($opt_f)
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'};
103 else
105 print "Memory OK - $fmt_pct% ($free_memory kB) free\n";
106 exit $exit_codes{'OK'};
109 elsif ($opt_u)
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'};
123 else
125 print "Memory OK - $fmt_pct% ($used_memory kB) used\n";
126 exit $exit_codes{'OK'};
130 # Show usage
131 sub usage()
133 print "\ncheck_mem.pl v1.0 - Nagios Plugin\n\n";
134 print "usage:\n";
135 print " check_mem.pl -<f|u> -w <warnlevel> -c <critlevel>\n\n";
136 print "options:\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'};