/[nagios-plugins-perl]/trunk/plugins/check_nsd_swap.pl
ViewVC logotype

Contents of /trunk/plugins/check_nsd_swap.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (show annotations) (download)
Thu Nov 12 13:29:04 2009 UTC (11 years ago) by racvision
File MIME type: text/plain
File size: 6799 byte(s)
convert source structure from autotools to rpm-build.sh
1 #!/usr/bin/perl -w
2 #
3 # Copyright (C) 2001 Eirik Robertstad (erobertstad@users.sourceforge.net) (check_swap.pl)
4 # Code taken from check_mem.pl by Dan Larsson <dl@tyfon.net>
5 #
6 # (c) 2002-2004 Stéphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty
15 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # you should have received a copy of the GNU General Public License
19 # along with this program (or with Netsaint); if not, write to the
20 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 # Boston, MA 02111-1307, USA
22 #
23 #
24 # $Id: check_nsd_swap.pl,v 1.4 2004-06-08 07:49:26 urba Exp $
25
26 use strict; # should never be differently :-)
27 use warnings;
28
29 use File::Basename; # get basename()
30 use lib dirname($0); # to get local libs
31 use libplugins qw(%EXIT_CODES &showUsage);
32 use NSD;
33
34
35 # Default values :
36 my %DEFVALUES = (
37 'SWAP_WARNING' => {
38 'v' => 60,
39 'desc' => _("Swap fill warning level (%)"),
40 },
41 'SWAP_CRITICAL' => {
42 'v' => 80,
43 'desc' => _("Swap fill critical level (%)"),
44 },
45 'TIMEOUT' => {
46 'v' => $NSD::NSD_TIMEOUT,
47 'desc' => _("Nagiosstatd connection timeout"),
48 },
49 'VERSION' => {
50 'v' => "1.1",
51 'desc' => _("Plugin Version"),
52 },
53 );
54
55 my $DEBUG = 0;
56
57 # Get the options :
58 use vars qw($opt_V $opt_h $opt_m $opt_perf $opt_H $opt_p);
59 use vars qw( $opt_w $opt_c $opt_f $opt_u $opt_x );
60
61 Getopt::Long::GetOptions (
62 "V" => \$opt_V, "version" => \$opt_V,
63 "h|?" => \$opt_h, "help" => \$opt_m, "man" => \$opt_m,
64 "perf" => \$opt_perf,
65
66 "d" => \$DEBUG, "debug=i" => \$DEBUG,
67 "H=s" => \$opt_H, "host=s" => \$opt_H,
68 "p=i" => \$opt_p, "port=i" => \$opt_p,
69 "w=i" => \$opt_w, "warning=i" => \$opt_w,
70 "c=i" => \$opt_c, "critical=i" => \$opt_c,
71
72 "f" => \$opt_f, "free" => \$opt_f,
73 "u" => \$opt_u, "used" => \$opt_u,
74 "e" => \$opt_x, "extended" => \$opt_x,
75 );
76
77
78 if ( defined ($opt_h) || defined ($opt_m) ) {
79 &showUsage($opt_m,\%DEFVALUES);
80 } elsif (defined ($opt_V)) {
81 &showVersion($DEFVALUES{VERSION}{v});
82 }
83
84 if ( (!defined ($opt_H)) || ($opt_H eq "")) {
85 print _("Missing remote hostname parameter !");
86 &showUsage();
87 }
88
89 if (defined ($opt_f) && $opt_f == 1) {
90 # set default values :
91 if (!$opt_w or $opt_w == 0) {
92 $opt_w = 100 - $DEFVALUES{SWAP_WARNING}{v};
93 }
94 if (!$opt_c or $opt_c == 0) {
95 $opt_c = $opt_w - 10;
96 }
97
98 # Check if levels are sane
99 if ($opt_w <= $opt_c ) {
100 # Swap critical and warning level:
101 ($opt_w,$opt_c)=($opt_c,$opt_w);
102 }
103
104 } elsif (defined ($opt_u) && $opt_u == 1) {
105 # set default values :
106 if (!$opt_w or $opt_w == 0 ) {
107 $opt_w = $DEFVALUES{SWAP_WARNING}{v};
108 }
109 if (!$opt_c or $opt_c == 0 ) {
110 $opt_c = $opt_w + 10;
111 }
112
113 # Check if levels are sane
114 if ($opt_w >= $opt_c ) {
115 # Swap critical and warning level:
116 ($opt_w,$opt_c)=($opt_c,$opt_w);
117 }
118
119 } else {
120 print _("You must select to monitor either USED (-u) or FREE (-f) memory!");
121 &showUsage();
122 }
123
124 my $warn_level = $opt_w;
125 my $crit_level = $opt_c;
126
127
128 # Check 'Swap' or 'Extended swap' :
129 my $service="swap";
130 if ( defined ($opt_x) ) {
131 $service="swape";
132 }
133
134
135 # Connect to remote Nagiosstatd:
136 my $o_nsd = new NSD ( address => $opt_H , port => $opt_p, debug => $DEBUG);
137
138 if ( $o_nsd->connect() ) {
139 print $o_nsd->getErrorMsg()."\n";
140 exit $EXIT_CODES{'UNKNOWN'};
141 }
142 if ( $o_nsd->ask($service) ) {
143 $o_nsd->close();
144 print $o_nsd->getErrorMsg()."\n";
145 exit $EXIT_CODES{'UNKNOWN'};
146 }
147 $o_nsd->close();
148 my $servanswer = $o_nsd->getResponse();
149
150 my $answer = '';
151 my $perf = '';
152 my $state = "OK";
153
154 if ( $servanswer =~ /^\s*Used: (.*)\s* Free: (.*)/) {
155
156 my $used_memory = $1;
157 $used_memory =~ tr/,/./;
158 my $choped = chop($used_memory);
159 my $free_memory = $2;
160 chop($free_memory);
161 $free_memory =~ tr/,/./;
162 my $total_memory = $used_memory + $free_memory;
163
164 my $byte_type=uc($choped)._("B");
165
166 my $fullservice = ($service eq "swap")?_("Swap"):_("Extended swap");
167
168 if ($opt_f) {
169 my $percent = $free_memory / $total_memory * 100;
170 my $fmt_pct = sprintf "%.1f", $percent;
171
172 if ($percent <= $crit_level) {
173 $state = "CRITICAL";
174 } elsif ($percent <= $warn_level) {
175 $state = "WARNING";
176 }
177 $answer = "$service $state - ".sprintf(_("%s free:"),$fullservice)." $fmt_pct% ($free_memory $byte_type / $total_memory $byte_type)";
178 $perf = "Swap=$fmt_pct%";
179
180 } elsif ($opt_u) {
181 my $percent = $used_memory / $total_memory * 100;
182 my $fmt_pct = sprintf ("%.1f", $percent);
183
184 if ($percent >= $crit_level) {
185 $state = "CRITICAL";
186 } elsif ($percent >= $warn_level) {
187 $state = "WARNING";
188 }
189 $answer = "$service $state - ".sprintf(_("%s used:"),$fullservice)." $fmt_pct% ($used_memory $byte_type / $total_memory $byte_type)";
190 $perf = "Swap=$fmt_pct%";
191 }
192 if ($opt_perf) {
193 $answer .= "|$perf";
194 }
195
196 } else {
197 $state = "UNKNOWN";
198 $answer = sprintf(_("Invalid data (%s) !"),$servanswer);
199 }
200
201 print $answer."\n";
202 exit $EXIT_CODES{$state};
203
204
205 __END__
206
207 =head1 NAME
208
209 check_nsd_swap.pl - Nagios plugins.
210
211 =head1 SYNOPSIS
212
213 B<check_nsd_swap.pl> S<-H I<remote host>> [S<-p I<nsd port>>] [S<-f>|S<-u>] [S<-e>] [S<-w I<warn>>] [S<-c I<crit>>]
214
215 =head1 DESCRIPTION
216
217 Nagios plugins to check remote swap usage using Nagiosstatd.
218
219 =head1 OPTIONS
220
221 =over 4
222
223 =item B<-H> I<remote host>
224
225 Host running Nagiosstatd to check.
226
227 =item B<-p> I<nsd port>
228
229 Nagiosstatd port.
230
231 =item B<-f>
232
233 Check free swap space.
234
235 =item B<-u>
236
237 Check used swap space.
238
239 =item B<-x>
240
241 Check extended swap.
242
243 =item B<-w> I<warn>
244
245 Percent of used/free swap at which a warning message will be generated.
246
247 =item B<-c> I<crit>
248
249 Percent of used/free swap at which a critical message will be generated.
250
251 =item B<-h>
252
253 A short help. Also give default values.
254
255 =item B<--help> | B<--man>
256
257 The full man page.
258
259 =item B<--perf>
260
261 Add Nagios performances datas.
262 Available : Swap
263
264 =back
265
266 =head1 NAGIOS CONGIGURATIONS
267
268 In F<checkcommands.cfg> you have to add :
269
270 # 'check_nsd_swap'
271 # Check remote swap.
272 define command {
273 command_name check_nsd_swap
274 command_line $USER1$/check_nsd_disk.pl -H $HOSTADDRESS$ -u $ARG1$ -w $ARG2$ -c $ARG3$
275 }
276 # 'check_nsd_swape'
277 # Check remote extended swap.
278 define command {
279 command_name check_nsd_swape
280 command_line $USER1$/check_nsd_disk.pl -H $HOSTADDRESS$ -u $ARG1$ -w $ARG2$ -c $ARG3$ -x
281 }
282
283 =head1 AUTHOR
284
285 Stéphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
286
287 Thanks to Charlie Cook & Nick Reinking and many others for writing modules that have been used as example for this one.
288
289 =cut

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8