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

Contents of /trunk/plugins/check_nsd_disk.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: 6697 byte(s)
convert source structure from autotools to rpm-build.sh
1 #!/usr/bin/perl -w
2 #
3 # Copyright (c) 2002-2009 Stéphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
4 #
5 # Thanks to Charlie Cook & Nick Reinking
6 # Copyright (c) 1999 Charlie Cook & Nick Reinking
7 #
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty
16 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # you should have received a copy of the GNU General Public License
20 # along with this program (or with Netsaint); if not, write to the
21 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 # Boston, MA 02111-1307, USA
23 #
24 # $Id: check_nsd_disk.pl,v 1.9 2009-07-15 14:00:14 uid5000 Exp $
25
26 use strict; # should never be differently :-)
27 use warnings;
28
29 use File::Basename; # get dirname()
30 use lib dirname($0); # to get local libs
31 use libplugins qw(%EXIT_CODES &showUsage &showVersion &getBestUnit);
32 use NSD;
33
34
35 # Default values :
36 my %DEFVALUES = (
37 'DISK_WARNING' => {
38 'v' => 90,
39 'desc' => _("Disk fill warning level"),
40 },
41 'DISK_CRITICAL' => {
42 'v' => 95,
43 'desc' => _("Disk fill critical level"),
44 },
45 'TIMEOUT' => {
46 'v' => $NSD::NSD_TIMEOUT,
47 'desc' => _("Nagiosstatd connection timeout"),
48 },
49 'VERSION' => {
50 'v' => "1.2",
51 'desc' => _("Plugin Version"),
52 },
53 );
54
55
56 my $DEBUG = 0;
57
58 # Get the options :
59 use vars qw($opt_V $opt_h $opt_m $opt_perf $opt_H $opt_p );
60 use vars qw($opt_fs $opt_w $opt_c $opt_x );
61
62 Getopt::Long::GetOptions (
63 "V" => \$opt_V, "version" => \$opt_V,
64 "h|?" => \$opt_h, "help" => \$opt_m, "man" => \$opt_m,
65 "perf" => \$opt_perf,
66
67 "d" => \$DEBUG, "debug=i" => \$DEBUG,
68 "H=s" => \$opt_H, "host=s" => \$opt_H,
69 "p=i" => \$opt_p, "port=i" => \$opt_p,
70 "w=i" => \$opt_w, "warning=i" => \$opt_w,
71 "c=i" => \$opt_c, "critical=i" => \$opt_c,
72
73 "f=s" => \$opt_fs, "filesystems=s" => \$opt_fs,
74 "x=s" => \$opt_x, "excludes=s" => \$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 if ( (!defined ($opt_fs)) || ($opt_fs eq "")) {
89 print _("Missing 'filesystems' parameter !");
90 &showUsage();
91 }
92
93 # Set default values :
94 $opt_w = $opt_w || $DEFVALUES{DISK_WARNING}{v};
95 $opt_c = $opt_c || $DEFVALUES{DISK_CRITICAL}{v};
96
97
98 my %excludefs = ();
99 if ( (defined ($opt_x)) && ($opt_x ne "")) {
100 foreach my $exfs ( split(/,/,$opt_x) ) {
101 $excludefs{$exfs}=1;
102 }
103 }
104
105
106 # Connect to remote Nagiosstatd:
107 my $service="disk $opt_fs";
108 my @fs = ();
109 if ( ($opt_fs eq "all") ) {
110 $service="alldisks";
111 } elsif ( $opt_fs =~ /\,/ ) {
112 $service="alldisks";
113 @fs = split(/,/,$opt_fs);
114 }
115 my $o_nsd = new NSD ( address => $opt_H , port => $opt_p, debug => $DEBUG);
116
117 if ( $o_nsd->connect() ) {
118 print $o_nsd->getErrorMsg()."\n";
119 exit $EXIT_CODES{'UNKNOWN'};
120 }
121 if ( $o_nsd->ask($service) ) {
122 $o_nsd->close();
123 print $o_nsd->getErrorMsg()."\n";
124 exit $EXIT_CODES{'UNKNOWN'};
125 }
126 $o_nsd->close();
127 my $servanswer = $o_nsd->getResponse();
128
129
130 my $answer = '';
131 my $perf = '';
132 my $state = "OK";
133
134 if ( $service eq "alldisks" ) {
135 my $nbfs = 0 ;
136 my @allfs=();
137 while ( $servanswer =~ /\(([\/\w\-]+)\,(\d+)\)/g ) {
138 my $mountpt = $1;
139 my $pu = $2; # used %
140
141 if ( $opt_fs ne "all" ) {
142 my $found=0;
143 foreach my $fs (@fs) {
144 if ($fs eq $mountpt) {
145 $found=1;
146 last;
147 }
148 }
149 if ( !$found ) {
150 next;
151 }
152 }
153 if ( exists($excludefs{$mountpt}) ) {
154 next;
155 }
156
157 my $pf = 100 - $pu; # free %
158
159 $allfs[$nbfs]=$mountpt;
160 $nbfs++;
161
162
163 if ( $pu > $opt_w ) {
164
165 if ( $answer ) {
166 $answer.="<br />";
167 }
168 if ( $state eq "OK" ) {
169 $state = "WARNING";
170 }
171
172 if ( $pu > $opt_c ) {
173 $state = "CRITICAL";
174 $answer.= sprintf(_("Only %.1f%% free on %s !"),$pf,$mountpt);
175 } else {
176 $answer.= sprintf(_("Only %.1f%% free on %s."),$pf,$mountpt);
177 }
178 }
179 }
180 if ( $nbfs == 0 ) {
181 $state = "UNKNOWN";
182 $answer = sprintf(_("Invalid data (%s) !"),$servanswer);
183 } elsif ( $state eq "OK" ) {
184 $answer = sprintf(_("All disks (%s) below %.1f%% thresholds."),join(",",@allfs),$opt_w);
185 }
186
187 } elsif ($servanswer =~ /^[\w\/\d:\.\-]+ (\d+) (\d+) ([\w\/]+)/) {
188 my ($avail) = getBestUnit($1);
189 my ($capper) = $2;
190 my ($mountpt) = $3;
191
192 $perf = "DiskUse=$1KB DiskPercent=$capper%";
193
194 if ((100 - $capper) < $opt_w) {
195 $answer = sprintf(_("Disk ok - %s (%.1f%%) free on %s"),$avail,$capper,$mountpt);
196 } elsif ((100 - $capper) < $opt_c) {
197 $state = "WARNING";
198 $answer = sprintf(_("Only %s (%.1f%%) free on %s"),$avail,$capper,$mountpt);
199 } else {
200 $state = "CRITICAL";
201 $answer = sprintf(_("Only %s (%.1f%%) free on %s !"),$avail,$capper,$mountpt);
202 }
203 if ($opt_perf) {
204 $answer .= "|$perf";
205 }
206
207 } else {
208 $state = "UNKNOWN";
209 $answer = sprintf(_("Invalid data (%s) !"),$servanswer);
210 }
211
212 print $answer."\n";
213 exit $EXIT_CODES{$state};
214
215
216
217 __END__
218
219 =head1 NAME
220
221 check_nsd_disk.pl - Nagios plugins.
222
223 =head1 SYNOPSIS
224
225 B<check_nsd_disk.pl> S<-H I<remote host>> S<[-p I<nsd port>]> S<-f I<filesystems>> S<[-w I<warn>]> S<[-c I<crit>]> S<[-x I<excludes>]>
226
227 =head1 DESCRIPTION
228
229 Nagios plugins to check remote unix disk space using Nagiosstatd.
230
231 =head1 OPTIONS
232
233 =over 4
234
235 =item B<-H> I<remote host>
236
237 Host running Nagiosstatd to check.
238
239 =item B<-p> I<nsd port>
240
241 Nagiosstatd port.
242
243 =item B<-f> I<filesystems>
244
245 A comma separated list of filesystems to check or "all".
246
247 =item B<-w> I<warn>
248
249 Percent of used space at which a warning message will be generated.
250
251 =item B<-c> I<crit>
252
253 Percent of used space at which a critical message will be generated.
254
255 =item B<-x> I<excludes>
256
257 A comma separated list of filesystem that will not be checked (with -d all).
258
259 =item B<-h>
260
261 A short help. Also give default values.
262
263 =item B<--help> | B<--man>
264
265 The full man page.
266
267 =item B<--perf>
268
269 Add Nagios performances datas.
270 Available : DiskUse(KB) DiskPercent(%)
271
272 =back
273
274 =head1 NAGIOS CONGIGURATIONS
275
276 In F<checkcommands.cfg> you have to add :
277
278 # 'check_nsd_disk' command definition
279 define command {
280 command_name check_nsd_disk
281 command_line $USER1$/check_nsd_disk.pl -H $HOSTADDRESS$ -f $ARG1$ -w $ARG2$ -c $ARG3$ -x $ARG4$
282 }
283
284 =head1 AUTHOR
285
286 St�hane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
287
288 Thanks to Charlie Cook & Nick Reinking and many others for writing modules that have been used as example for this one.
289
290 =cut

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8