Commit c381946e6a58db3e5f378463ca50ac6319357d8c
0 parents
Initial checkin of standard check_memory script
Showing
1 changed file
with
154 additions
and
0 deletions
check_memory
0 → 100755
1 | +++ a/check_memory | ||
1 | +#!/usr/bin/perl | ||
2 | +# | ||
3 | +# check_memory - Check free(1) data against given tresholds | ||
4 | +# | ||
5 | +# Copyright (C) 2007 Thomas Guyot-Sionnest <tguyot@gmail.com> | ||
6 | +# | ||
7 | +# This program is free software; you can redistribute it and/or | ||
8 | +# modify it under the terms of the GNU General Public License | ||
9 | +# as published by the Free Software Foundation; either version 2 | ||
10 | +# of the License, or (at your option) any later version. | ||
11 | +# | ||
12 | +# This program is distributed in the hope that it will be useful, | ||
13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | +# GNU General Public License for more details. | ||
16 | +# | ||
17 | +# You should have received a copy of the GNU General Public License | ||
18 | +# along with this program; if not, write to the Free Software | ||
19 | +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
20 | +# | ||
21 | + | ||
22 | + | ||
23 | +use strict; | ||
24 | +use warnings; | ||
25 | +use vars qw($PROGNAME $VERSION $FREECMD $UNIT); | ||
26 | +use Nagios::Plugin; | ||
27 | + | ||
28 | +$PROGNAME = "check_memory"; | ||
29 | +$VERSION = '1.0.1'; | ||
30 | +$FREECMD = '/usr/bin/free'; | ||
31 | +$UNIT = 'M'; | ||
32 | + | ||
33 | +my $np = Nagios::Plugin->new( | ||
34 | + usage => "Usage: %s [ -w <warning_threshold> ] [ -c <critical_threshold> ]\n" | ||
35 | + . ' [ -u <unit> ]', | ||
36 | + version => $VERSION, | ||
37 | + plugin => $PROGNAME, | ||
38 | + blurb => 'Check free(1) data against given tresholds', | ||
39 | + timeout => 30, | ||
40 | +); | ||
41 | + | ||
42 | +$np->add_arg( | ||
43 | + spec => 'warning|w=s', | ||
44 | + help => "-w, --warning=THRESHOLD[%]\n" | ||
45 | + . " Warning threshold (in bytes or percent) for free memory. See\n" | ||
46 | + . " http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT\n" | ||
47 | + . " for the threshold format. Alternatively this can be defined as a percentage\n" | ||
48 | + . ' of minimum free memory (warning and critical must be in the same format).', | ||
49 | + required => 0, | ||
50 | +); | ||
51 | + | ||
52 | +$np->add_arg( | ||
53 | + spec => 'critical|c=s', | ||
54 | + help => "-c, --critical=THRESHOLD[%]\n" | ||
55 | + . " Critical threshold (in bytes or percent) for free memory. See\n" | ||
56 | + . " http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT\n" | ||
57 | + . " for the threshold format. Alternatively this can be defined as a percentage\n" | ||
58 | + . ' of minimum free memory (warning and critical must be in the same format).', | ||
59 | + required => 0, | ||
60 | +); | ||
61 | + | ||
62 | +$np->add_arg( | ||
63 | + spec => 'unit|u=s', | ||
64 | + help => "-u, --unit=UNIT\n" | ||
65 | + . " Unit to use for human-redeable output. Can be 'b', 'K' 'M' or 'G' for\n" | ||
66 | + . " bytes, KiB, MiB or GiB respectively (default: '$UNIT').", | ||
67 | + default => $UNIT, | ||
68 | + required => 0, | ||
69 | +); | ||
70 | + | ||
71 | +$np->getopts; | ||
72 | + | ||
73 | +# Assign, then check args | ||
74 | + | ||
75 | +my $multiple; | ||
76 | +my $unit = $np->opts->unit; | ||
77 | +if ($unit eq 'M') { | ||
78 | + $multiple = 1024 * 1024; | ||
79 | +} elsif ( $unit eq 'K') { | ||
80 | + $multiple = 1024; | ||
81 | +} elsif ( $unit eq 'b') { | ||
82 | + $multiple = 1; | ||
83 | +} elsif ( $unit eq 'G') { | ||
84 | + $multiple = 1024 * 1024 * 1024; | ||
85 | +} else { | ||
86 | + $np->nagios_exit('UNKNOWN', "Unit must be one of 'b', 'K', 'M' or 'G', case-sensitive."); | ||
87 | +} | ||
88 | +my $verbose = $np->opts->verbose; | ||
89 | + | ||
90 | +# Would better fit later but doing it here validates thresholds | ||
91 | +my $warning = $np->opts->warning; | ||
92 | +my $critical = $np->opts->critical; | ||
93 | +$np->set_thresholds( | ||
94 | + warning => ((defined($warning) && $warning !~ /^\d+%$/) ? $warning : undef), | ||
95 | + critical => ((defined($critical) && $critical !~ /^\d+%$/) ? $critical : undef), | ||
96 | +); | ||
97 | + | ||
98 | +# Better safe than sorry | ||
99 | +alarm $np->opts->timeout; | ||
100 | + | ||
101 | +# We always get bytes, then calculate units ourselves | ||
102 | +warn("Running: '$FREECMD -b'\n") if ($verbose); | ||
103 | +open(RESULT, "$FREECMD -b |") | ||
104 | + or $np->nagios_exit('CRITICAL', "Could not run $FREECMD"); | ||
105 | + | ||
106 | +warn("Output from $FREECMD:\n") if ($verbose > 1); | ||
107 | +my ($used, $free); | ||
108 | +while (<RESULT>) { | ||
109 | + warn(" $_") if ($verbose > 1); | ||
110 | + next unless (m#^\-/\+\ buffers/cache:\s*(\d+)\s+(\d+)#); | ||
111 | + $used = $1; | ||
112 | + $free = $2; | ||
113 | +} | ||
114 | + | ||
115 | +close(RESULT); | ||
116 | +alarm(0); | ||
117 | + | ||
118 | +$np->nagios_exit('CRITICAL', "Unable to interpret $FREECMD output") if (!defined($free)); | ||
119 | + | ||
120 | +my $total = $used + $free; | ||
121 | +if (defined($warning) && $warning =~ /^\d+%$/) { | ||
122 | + if ($warning) { | ||
123 | + $warning =~ s/%//; | ||
124 | + $warning = $total / 100 * $warning; | ||
125 | + $warning .= ':'; | ||
126 | + } | ||
127 | + warn("Calculated threshold (from percentage): warn=>$warning\n") if ($verbose); | ||
128 | +} | ||
129 | + | ||
130 | +if (defined($critical) && $critical =~ /^\d+%$/) { | ||
131 | + if ($critical) { | ||
132 | + $critical =~ s/%//; | ||
133 | + $critical = $total / 100 * $critical; | ||
134 | + $critical .= ':'; | ||
135 | + } | ||
136 | + warn("Calculated threshold (from percentage): crit=>$critical\n") if ($verbose); | ||
137 | +} | ||
138 | + | ||
139 | +$np->set_thresholds( | ||
140 | + warning => $warning, | ||
141 | + critical => $critical, | ||
142 | +); | ||
143 | + | ||
144 | +$np->add_perfdata( | ||
145 | + label => "free", | ||
146 | + value => $free, | ||
147 | + uom => 'b', | ||
148 | + threshold => $np->threshold, | ||
149 | +); | ||
150 | + | ||
151 | +my $freeprint = int($free/$multiple); | ||
152 | + | ||
153 | +$np->nagios_exit($np->check_threshold($free), "$freeprint$unit free"); | ||
154 | + |