summaryrefslogtreecommitdiffstats
path: root/contrib/check_mem.pl
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/check_mem.pl')
-rw-r--r--contrib/check_mem.pl146
1 files changed, 146 insertions, 0 deletions
diff --git a/contrib/check_mem.pl b/contrib/check_mem.pl
new file mode 100644
index 0000000..f0c8212
--- /dev/null
+++ b/contrib/check_mem.pl
@@ -0,0 +1,146 @@
1#!/usr/bin/perl -w
2# $Id$
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# Tell Perl what we need to use
22use strict;
23use Getopt::Std;
24
25use 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);
31
32# Predefined exit codes for Nagios
33%exit_codes = ('UNKNOWN' ,-1,
34 'OK' , 0,
35 'WARNING' , 1,
36 'CRITICAL', 2,);
37
38# Turn this to 1 to see reason for parameter errors (if any)
39$verb_err = 0;
40
41# This the unix command string that brings Perl the data
42$command_line = `vmstat | tail -1 | awk '{print \$4,\$5}'`;
43
44chomp $command_line;
45@memlist = split(/ /, $command_line);
46
47# Define the calculating scalars
48$used_memory = $memlist[0];
49$free_memory = $memlist[1];
50$total_memory = $used_memory + $free_memory;
51
52# Get the options
53if ($#ARGV le 0)
54{
55 &usage;
56}
57else
58{
59 getopts('c:fuw:');
60}
61
62# Shortcircuit the switches
63if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0)
64{
65 print "*** You must define WARN and CRITICAL levels!" if ($verb_err);
66 &usage;
67}
68elsif (!$opt_f and !$opt_u)
69{
70 print "*** You must select to monitor either USED or FREE memory!" if ($verb_err);
71 &usage;
72}
73
74# Check if levels are sane
75if ($opt_w <= $opt_c and $opt_f)
76{
77 print "*** WARN level must not be less than CRITICAL when checking FREE memory!" if ($verb_err);
78 &usage;
79}
80elsif ($opt_w >= $opt_c and $opt_u)
81{
82 print "*** WARN level must not be greater than CRITICAL when checking USED memory!" if ($verb_err);
83 &usage;
84}
85
86$warn_level = $opt_w;
87$crit_level = $opt_c;
88
89if ($opt_f)
90{
91 $percent = $free_memory / $total_memory * 100;
92 $fmt_pct = sprintf "%.1f", $percent;
93 if ($percent <= $crit_level)
94 {
95 print "Memory CRITICAL - $fmt_pct% ($free_memory kB) free\n";
96 exit $exit_codes{'CRITICAL'};
97 }
98 elsif ($percent <= $warn_level)
99 {
100 print "Memory WARNING - $fmt_pct% ($free_memory kB) free\n";
101 exit $exit_codes{'WARNING'};
102 }
103 else
104 {
105 print "Memory OK - $fmt_pct% ($free_memory kB) free\n";
106 exit $exit_codes{'OK'};
107 }
108}
109elsif ($opt_u)
110{
111 $percent = $used_memory / $total_memory * 100;
112 $fmt_pct = sprintf "%.1f", $percent;
113 if ($percent >= $crit_level)
114 {
115 print "Memory CRITICAL - $fmt_pct% ($used_memory kB) used\n";
116 exit $exit_codes{'CRITICAL'};
117 }
118 elsif ($percent >= $warn_level)
119 {
120 print "Memory WARNING - $fmt_pct% ($used_memory kB) used\n";
121 exit $exit_codes{'WARNING'};
122 }
123 else
124 {
125 print "Memory OK - $fmt_pct% ($used_memory kB) used\n";
126 exit $exit_codes{'OK'};
127 }
128}
129
130# Show usage
131sub usage()
132{
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'};
146}