diff options
author | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 +0000 |
---|---|---|
committer | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 +0000 |
commit | 44a321cb8a42d6c0ea2d96a1086a17f2134c89cc (patch) | |
tree | a1a4d9f7b92412a17ab08f34f04eec45433048b7 /contrib/check_mem.pl | |
parent | 54fd5d7022ff2d6a59bc52b8869182f3fc77a058 (diff) | |
download | monitoring-plugins-44a321cb8a42d6c0ea2d96a1086a17f2134c89cc.tar.gz |
Initial revision
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@2 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'contrib/check_mem.pl')
-rw-r--r-- | contrib/check_mem.pl | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/contrib/check_mem.pl b/contrib/check_mem.pl new file mode 100644 index 00000000..f0c82129 --- /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 | ||
22 | use strict; | ||
23 | use Getopt::Std; | ||
24 | |||
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); | ||
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 | |||
44 | chomp $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 | ||
53 | if ($#ARGV le 0) | ||
54 | { | ||
55 | &usage; | ||
56 | } | ||
57 | else | ||
58 | { | ||
59 | getopts('c:fuw:'); | ||
60 | } | ||
61 | |||
62 | # Shortcircuit the switches | ||
63 | if (!$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 | } | ||
68 | elsif (!$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 | ||
75 | if ($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 | } | ||
80 | elsif ($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 | |||
89 | if ($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 | } | ||
109 | elsif ($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 | ||
131 | sub 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 | } | ||