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

Contents of /trunk/plugins/check_ajp13.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 156 - (show annotations) (download)
Fri Mar 20 14:13:48 2015 UTC (5 years, 8 months ago) by racvision
File MIME type: text/plain
File size: 5938 byte(s)
Add check_ajp13.pl check_compellent.pl and check_livestatus.pl
1 #!/usr/bin/perl -w
2 #
3 # History:
4 # 2010-11-05 Michel Rode <rmichel@devnu11.net> - First release (v1)
5 # 2014-06-12 St├ęphane Urbanovski <s.urbanovski@ac-nancy-metz.fr> - Use Nagios::Plugin
6 #
7 # Comment: Please send bug fixes and enhancements to <rmichel@devnu11.net>
8 #
9 # check_ajp - nagios plugin for tomcat AJP13 protocol monitoring
10 # Copyright (C) 2010 Michel Rode <rmichel@devnu11.net>
11 #
12 # This program is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #
25 #
26 # Usage: ./check_ajp13.pl --H ip.of.the.app [-p 8009 --warn 1 --crit 2 ]
27 #
28
29 use warnings;
30 use strict;
31
32 use Time::HiRes 'time';
33 use POSIX qw(setlocale);
34 use Locale::gettext;
35
36
37 use Socket;
38 use Nagios::Plugin ;
39
40 my $VERSION = '1.1';
41 my $TIMEOUT = 10;
42 my $DEBUG = 0;
43
44 my $np = Nagios::Plugin->new(
45 version => $VERSION,
46 blurb => _gt('Plugin that ping an AJP13 server'),
47 usage => "Usage: %s [ -v|--verbose ] [-t <timeout>] [ -c|--critical=<threshold> ] [ -w|--warning=<threshold> ]",
48 timeout => $TIMEOUT+1
49 );
50 $np->add_arg (
51 spec => 'debug|d',
52 help => _gt('Debug level'),
53 default => 0,
54 );
55 $np->add_arg (
56 spec => 'w=f',
57 help => _gt('Warning request time threshold (in seconds)'),
58 default => 2,
59 label => 'FLOAT'
60 );
61 $np->add_arg (
62 spec => 'c=f',
63 help => _gt('Critical request time threshold (in seconds)'),
64 default => 4,
65 label => 'FLOAT'
66 );
67
68 $np->add_arg (
69 spec => 'hostname|H=s',
70 help => _gt('Hostname'),
71 required => 1,
72 );
73 $np->add_arg (
74 spec => 'port|p=i',
75 help => _gt('Tomcat listen port'),
76 default => 8009,
77 );
78
79
80 $np->getopts;
81
82 $DEBUG = $np->opts->get('debug');
83 my $warntime = $np->opts->get('w');
84 my $crittime = $np->opts->get('c');
85 my $host = $np->opts->get('hostname');
86 my $port = $np->opts->get('port');
87
88
89
90 # GetOptions("app=s" => \$host, "port=s" => \$port, "warn=f" => \$warntime, "crit=f" => \$crittime, "timeout=f" => \$timeout);
91
92 my $ping = pack 'C5' # Format template.
93 , 0x12, 0x34 # Magic number for server->container packets.
94 , 0x00, 0x01 # 2 byte int length of payload.
95 , 0x0A # Type of packet. 10 = CPing.
96 ;
97 my $expected = pack 'C5' # Format template.
98 , 0x41, 0x42 # Magic number for container->server packets.
99 , 0x00, 0x01 # 2 byte int length of payload.
100 , 0x09 # Type of packet. 9 = CPong reply.
101 ;
102
103 my $recv = '';
104
105 my $iaddr = inet_aton($host) || $np->nagios_die(_gt('Unknown host !'));
106 my $paddr = sockaddr_in($port, $iaddr) || $np->nagios_die(_gt('Wrong port !'));
107 my $proto = getprotobyname('tcp');
108
109
110
111
112
113
114
115
116 ############
117
118
119 # socket(my $sock, PF_INET, SOCK_STREAM, $proto) || xdie "Unable to create a socket.";
120 # setsockopt($sock, SOL_SOCKET, SO_SNDTIMEO, pack('l!l!', $timo, 0)) or die $!;
121 # setsockopt($sock, SOL_SOCKET, SO_RCVTIMEO, pack('l!l!', $timo, 0)) or die $!;
122 # connect($sock, $paddr) || xdie ("Unable to connect to server");
123 # syswrite $sock, "\x12\x34\x00\x01\x0a";
124 # sysread $sock, my $recv, 5 || xdie("read: $!, stopped");
125 # my @vals = unpack 'C5', $recv;
126 # my @acks = qw (65 66 0 1 9);
127 # my %vals = map {$_, 1} @vals;
128 # my @diff = grep {!$vals {$_}} @acks;
129 #
130 # if (@diff == 0) {
131 # my $res = (time() - $start) + 0.005;
132 # my $len = (length("@vals") - $#vals);
133 # printf ("AJP OK - %d bytes in %3.3f seconds |time=%3.3fs;;;0.000000 size=%dB;;;0\n", $len, $res, $res, $len);
134 # $code = $ERRORS{'OK'};
135 # } else {
136 # print "AJP CRITICAL - Unable to verify AJP host $host\n";
137 # $code = $ERRORS{'CRITICAL'};
138 # }
139 # close($sock);
140
141 #################
142
143
144
145
146 my $time1 = time();
147 my $time2 = 0;
148 my $conState = 'None';
149 eval {
150 local $SIG{ALRM} = sub { die "alarm\n" };
151 alarm($TIMEOUT);
152
153 $conState = 'Socket';
154 socket (my $sock, PF_INET, SOCK_STREAM, $proto) || $np->nagios_die('Unable to create a socket !');
155
156 $conState = 'Connect';
157 debug('Connecting ...');
158 connect($sock, $paddr) || $np->nagios_exit(CRITICAL,sprintf(_gt('Unable to connect to %s on port %d !'),$host,$port));
159
160 $conState = 'Syswrite';
161 debug('Sending PING');
162 syswrite($sock, $ping) || $np->nagios_exit(CRITICAL,_gt('Unable to send AJP PING !'));
163
164 $conState = 'Sysread';
165 debug('Waiting for reply');
166 sysread($sock, $recv, 5) || $np->nagios_exit(CRITICAL,'Sysread failed !');
167
168 alarm(0);
169 };
170 $time2 = (time() - $time1);
171
172 if ($@) {
173 die unless $@ eq "alarm\n";
174 $np->nagios_exit(CRITICAL, sprintf(_gt('Timeout after %1.0fs (%s)'),$time2,$conState));
175
176 } else {
177
178 if ($DEBUG) {
179 my @vals = unpack 'C5', $recv;
180 debug('Received : '.join(',',@vals));
181 }
182 $np->add_perfdata(
183 'label' => 't',
184 'value' => sprintf('%.6f',$time2),
185 'min' => 0,
186 'max' => $TIMEOUT,
187 'uom' => 's',
188 'threshold' => $np->threshold()
189 );
190
191 if ($recv eq $expected) {
192 debug('Pong received !');
193 if ($time2 >= $crittime) {
194 $np->nagios_exit(CRITICAL, sprintf(_gt('%3.5fs response time (critical at %3.2fs) !'),$time2,$crittime));
195 } elsif ($time2 >= $warntime) {
196 $np->nagios_exit(WARNING, sprintf('%3.5fs response time (warning at %3.2fs) !',$time2,$warntime));
197 } else {
198 $np->nagios_exit(OK, sprintf('%3.5f seconds response time',$time2,$warntime));
199 }
200 } else {
201 $np->nagios_exit(CRITICAL, 'No PONG received !');
202 }
203 }
204
205 # Print debug information if $DEBUG > 0
206 sub debug {
207 print STDERR "[DEBUG] ".$_[0]."\n" if $DEBUG;
208 }
209 # Gettext wrapper
210 sub _gt {
211 return gettext($_[0]);
212 }
213
214 sub usage {
215 printf STDERR "Usage: check_ajp --H ip.of.the.app [--port 8009 --warn 1 --crit 2 --timeout 5]\n\n";
216 exit 3;
217 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8