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

Contents of /trunk/plugins/check_squid.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 204 - (show annotations) (download)
Fri Oct 5 14:59:46 2018 UTC (2 years, 1 month ago) by racvision
File MIME type: text/plain
File size: 7352 byte(s)
Fusion des modifs sur supervision01/02 et racvision3. Pas fini.
1 #!/usr/bin/perl -w
2 #
3 # (c) 2003 Stéphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
4 # DSI - Académie de Nancy-Metz
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 #
22
23 use strict; # should never be differently :-)
24 use warnings;
25
26 use File::Basename; # get basename()
27 use lib dirname($0); # to get local libs
28 use libplugins qw(%EXIT_CODES %ERR_LEVELS &showUsage &showVersion);
29
30 use Socket; # we need tcp connection
31
32 my %DEFVALUES = (
33 'FD_WARNING' => {
34 'v' => 200,
35 'desc' => _("File descriptor warning level"),
36 },
37 'FD_CRITICAL' => {
38 'v' => 50,
39 'desc' => _("File descriptor critical level"),
40 },
41 'SQUID_PORT' => {
42 'v' => 3128,
43 'desc' => _("Squid listen port"),
44 },
45 'TIMEOUT' => {
46 'v' => 180,
47 'desc' => _("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 # $opt_H = remote host
59 # $opt_P = [port] $DEFAULT_PORT
60 # $opt_p = cache manager password
61 # $opt_w = [warn level] (unused yet)
62 # $opt_c = [critical level] (unused yet)
63
64 use vars qw( $opt_H $opt_P $opt_w $opt_c $opt_p);
65
66 # Set default value:
67 $opt_P = $DEFVALUES{SQUID_PORT}{v};
68 $opt_w = $DEFVALUES{FD_WARNING}{v};
69 $opt_c = $DEFVALUES{FD_CRITICAL}{v};
70 $opt_p = "";
71
72 my ($opt_V, $opt_h, $opt_m, $opt_perf);
73
74 Getopt::Long::GetOptions (
75 "V" => \$opt_V, "version" => \$opt_V,
76 "h|?" => \$opt_h, "help" => \$opt_m,
77 "man" => \$opt_m,
78 "perf" => \$opt_perf,
79
80 "d" => \$DEBUG, "debug=i" => \$DEBUG,
81 "w=i" => \$opt_w, "warning=i" => \$opt_w,
82 "c=i" => \$opt_c, "critical=i" => \$opt_c,
83
84 "H=s" => \$opt_H, "hostname=s" => \$opt_H,
85 "p=s" => \$opt_p, "password=s" => \$opt_p,
86 "P=i" => \$opt_P, "port=i" => \$opt_P,
87 );
88
89 if ( defined ($opt_h) || defined ($opt_m) ) {
90 &showUsage($opt_m,\%DEFVALUES);
91 } elsif (defined ($opt_V)) {
92 &showVersion($DEFVALUES{VERSION}{v});
93 }
94
95 if ( (!defined ($opt_H)) || ($opt_H eq "")) {
96 print _("Missing remote hostname parameter !");
97 &showUsage();
98 }
99
100 if ( defined ($opt_p) ) {
101 $opt_p = "\@$opt_p";
102 }
103
104
105 my $sendMsg = "GET cache_object://$opt_H/info".$opt_p." HTTP/1.1\n\n";
106
107 if ($DEBUG) { print "Sended [ ".$sendMsg."]"; }
108
109 # Connecting to Squid :
110 my $remoteaddr ;
111 my $proto = getprotobyname('tcp');
112 my $paddr;
113
114 if ( ! ($remoteaddr = inet_aton("$opt_H")) ) {
115 printf (_("Unkown host: %s %s"),$opt_H,$!);
116 exit $EXIT_CODES{'UNKNOWN'};
117 } elsif ( ! ($paddr = sockaddr_in($opt_P, $remoteaddr)) ) {
118 printf (_("Can't create info for connection: %s"),$!);
119 exit $EXIT_CODES{'UNKNOWN'};
120 } elsif ( !socket(SERVER, PF_INET, SOCK_STREAM, $proto) ) {
121 printf (_("Can't create socket: %s"),$!);
122 exit $EXIT_CODES{'UNKNOWN'};
123 }
124
125 setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1);
126
127 if (!connect(SERVER, $paddr) ) {
128 printf (_("Can't connect to Squid at '%s:%s' : %s"),$opt_H,$opt_P,$!);
129 exit $EXIT_CODES{'UNKNOWN'};
130 }
131
132 # Just in case of problems, let's not hang Nagios
133 $SIG{'ALRM'} = sub {
134 close(SERVER);
135 print _("No Answer from Squid !");
136 exit $EXIT_CODES{'UNKNOWN'};
137 };
138 alarm($DEFVALUES{TIMEOUT}{v});
139
140 select(SERVER);
141 $| = 1;
142 select(STDOUT);
143
144 print SERVER $sendMsg;
145 my @servanswer = <SERVER>;
146 alarm(0);
147 close(SERVER);
148
149 if ($DEBUG) { print "Received [ ".join("\n",@servanswer)."]\n"; }
150
151 $servanswer[0] =~ s/\r\n//;
152
153 my $state = "UNKNOWN";
154 my $answer = sprintf(_("Unknown answer (%s)"),$servanswer[0]);
155 my $perf = "";
156
157 if ($servanswer[0] =~ /^HTTP\/1.\d 200 OK/) {
158 $state = "OK";
159 $answer = "Squid cache ";
160 my @infos;
161 my $errmsg;
162 foreach my $l (@servanswer) {
163 if ($l =~ /HTTP requests per minute:\s+([\d\.\-]+)/) {
164 push(@infos, $1." http/mn");
165 $perf .= "HttpRequests=".$1."http/min "
166 }
167 if ($l =~ /Available number of file descriptors:\s+(\d+)/) {
168 push(@infos, $1." FreeFileDesc");
169 if ($1 < $opt_c) {
170 $state = "CRITICAL";
171 $errmsg = _("Low available file descriptors !");
172 } elsif ($1 < $opt_w) {
173 $state = "WARNING";
174 $errmsg = _("Low available file descriptors !");
175 }
176 $perf .= "FreeFileDesc=".$1.":$opt_w:$opt_c "
177 }
178 }
179 $answer .= $state;
180 if ($errmsg) {
181 $answer .= " : ".$errmsg;
182 }
183 if (@infos) {
184 $answer .= " (".join(", ",@infos).")";
185 }
186 if ($opt_perf) {
187 $answer .= "|$perf";
188 }
189
190 }
191
192
193 print $answer."\n";
194 exit $EXIT_CODES{$state};
195
196
197 __END__
198
199 =head1 NAME
200
201 Nagios plugins to check Squid cache
202
203 =head1 SYNOPSIS
204
205 B<check_squid.pl> S<-H I<remote host>> S<[-p I<password>]> S<[-P I<port>]> S<[-w I<warn>]> S<[-c I<crit>]>
206
207 =head1 OPTIONS
208
209 =over 4
210
211 =item B<-H> I<remote host>
212
213 Host running Squid.
214
215 =item B<-p> I<password>
216
217 Cache manager password (see Squid configuration).
218
219 =item B<-P> I<port>
220
221 Port that Squid is listenning on (http)
222
223 =item B<-w> I<warn>
224
225 Minimum available number of file descriptors to trigger a WARNING level.
226
227 =item B<-c> I<crit>
228
229 Minimum available number of file descriptors to trigger a CRITICAL level.
230
231 =item B<-h>
232
233 A short help. Also give default values.
234
235 =item B<--help> | B<--man>
236
237 The full man page.
238
239 =item B<--perf>
240
241 Add Nagios performances datas.
242
243 =back
244
245 =head1 NAGIOS CONGIGURATIONS
246
247 In F<checkcommands.cfg> you have to add :
248
249 # 'check_squid'
250 # Check squid number of filehandler using manager access.
251 # @param $ARG1$ Port number.
252 # @param $ARG2$ Manager password.
253 define command {
254 command_name check_squid
255 command_line $USER1$/check_squid.pl -H $HOSTADDRESS$ -P $ARG1$ -p $ARG2$
256 }
257
258 or
259
260 # 'check_squid'
261 # Check squid number of filehandler using manager access.
262 # @param $ARG1$ Port number.
263 # @param $ARG2$ Manager password.
264 # @param $ARG3$ Warning fd level.
265 # @param $ARG4$ Critial fd level.
266 define command {
267 command_name check_squid
268 command_line $USER1$/check_squid.pl -H $HOSTADDRESS$ -P $ARG1$ -p $ARG2$ -w $ARG3$ -c $ARG4$
269 }
270
271 if you want to be able to set warning and critical levels.
272
273 In F<services.cfg> you just have to add something like :
274
275 define service {
276 name host-squid-service
277 host_name cache.exemple.org
278 normal_check_interval 10
279 retry_check_interval 5
280 contact_groups linux-admins
281 service_description Squid
282 check_command check_squid!3128!squidPassw0rd
283 }
284
285 =head1 SQUID CONGIGURATIONS
286
287 This plugins is using Squid's cache_object protocol to get some informations from the Squid info page.
288
289 Here is a F<squid.conf> example :
290
291 acl manager proto cache_object
292 # Nagios host :
293 acl racvision src 172.29.1.1/255.255.255.255
294
295 # Deny access to everyone exept Nagios host :
296 http_access deny manager !racvision
297
298 # The cache manager password
299 cachemgr_passwd squidPassw0rd info
300
301 =head1 AUTHOR
302
303 Stéphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
304
305 Thanks to Charlie Cook & Nick Reinking and many others for writing modules that have been used as example for this one.
306
307 =cut

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8