http://www.perlmonks.org?node_id=766945

I was having some rare intermittent problems with Apache httpd talking to Tomcat.

I needed to determining if either the mod_jk in httpd or the Tomcat AJP connector was misbehaving. I did not easily find a script that performed an AJP ping. Therefore, I had to write one.

This is my first working version. The expected scalar is not used at this time. It is mainly there for documentation. Also, I probably won't remove the debug logging until it starts annoying me.

I stuck this on my tech blog, too.
http://it-nonwhizzos.blogspot.com/2009/05/ajp-ping-in-perl.html

Thanks.

#!/usr/bin/perl -w use warnings; use strict; use Socket; my ($remote, $port) = split /:/, shift @ARGV, 2; if (! $remote) { $remote = 'localhost'; } print "remote = $remote\n"; if (! $port) { $port = 8009; } print "port = $port\n"; my ($iaddr, $paddr, $proto); # If the port has anything other than numbers, we're assuming it is an # /etc/services name. if ($port =~ /\D/) { $port = getservbyname $port, 'tcp' ; } die "Bad port, stopped" unless $port; print "port = $port\n"; $iaddr = inet_aton($remote) || die "No host: $remote, stopped"; print "iaddr = $iaddr\n"; $paddr = sockaddr_in($port, $iaddr) || die "sockaddr: $!, stopped"; print "paddr = $paddr\n"; # Grab the number for TCP out of /etc/protocols. $proto = getprotobyname 'tcp' ; print "proto = $proto\n"; my $sock; # PF_INET and SOCK_STREAM are constants imported by the Socket module. + They # are the same as what is defined in sys/socket.h. socket $sock, PF_INET, SOCK_STREAM, $proto || die "socket: $!, stopped +"; print "sock = $sock\n"; print "BEFORE CONNECT\n"; connect $sock, $paddr || die "connect: $!, stopped"; print "AFTER CONNECT\n"; # This is the ping packet. For detailed documentation, see # http://tomcat.apache.org/connectors-doc/ajp/ajpv13a.html # I stole the exact byte sequence from # http://sourceforge.net/project/shownotes.php?group_id=128058&release +_id=438456 # instead of fully understanding the packet structure. my $ping = pack 'C5' # Format template. , 0x12, 0x34 # Magic number for server->container packets. , 0x00, 0x01 # 2 byte int length of payload. , 0x0A # Type of packet. 10 = CPing. ; my @ping_values = unpack 'C5', $ping; print "ping_values = " , join ' ', @ping_values , "\n"; # This is the expected pong packet. That is, this is what Tomcat send +s back # to indicate that it is operating OK. my $expected = pack 'C5' # Format template. , 0x41, 0x42 # Magic number for container->server packe +ts. , 0x00, 0x01 # 2 byte int length of payload. , 0x09 # Type of packet. 9 = CPong reply. ; syswrite $sock, $ping || die "syswrite: $!, stopped"; my $pong; $pong = 'empty'; print "BEFORE READ\n"; sysread $sock, $pong, 5 || die "read: $!, stopped"; print "AFTER READ\n"; my @pong_values = unpack 'C5', $pong; print "pong_values = " , join ' ', @pong_values , "\n"; close $sock || die "close: $!, stopped"; exit 0;

The following is an example of a good ping response.

me@mybox:~/ajp_ping $ time ./ajp_ping.pl localhost:8009 remote = localhost port = 8009 port = 8009 iaddr = paddr = proto = 6 sock = GLOB(0x506290) BEFORE CONNECT AFTER CONNECT ping_values = 18 52 0 1 10 BEFORE READ AFTER READ pong_values = 65 66 0 1 9 real 0m0.028s user 0m0.015s sys 0m0.013s

Replies are listed 'Best First'.
Re: AJP ping
by Anonymous Monk on Jul 07, 2011 at 15:55 UTC
    Thanks for saving me a chunk of time. A few minor tweeks were needed for me to make it work properly, the most critical was swapping out the "||" for an actual "or", the bitwise operator didn't seem to be catching the return values properly. A bit more to add and it'll be a fully functional nagios plugin A

      Actually, || is not a bit-wise operator.

      The reason that my statements like this:

      socket $sock, PF_INET, SOCK_STREAM, $proto || die $!;

      ...aren't dying properly is because , has lower operator precedence than ||. The or operator has the lowest precedence. See the perlop docs.

      This means that statement was evaluated like this:

      socket $sock, PF_INET, SOCK_STREAM, ($proto || die $!);

      ...instead of like this:

      socket ($sock, PF_INET, SOCK_STREAM, $proto) || die $!;

      And this is now making me question the wisdom of PBP's rules of:

      Don't use unnecessary parentheses for builtins and "honorary" builtins.
      Because I was thinking that functions in the Socket module were essentially honorary builtins.

Re: AJP ping
by jffry (Hermit) on Nov 18, 2011 at 22:11 UTC

    Wow. What a terrible script I wrote. It doesn't even compare the pong reply with expected values. I feel so ashamed.

    Anyway, here is an updated version.

    #!/usr/bin/perl -w use warnings; use strict; use Socket; my $host = 'localhost'; my $port = '8009'; # The host and port arguments can be specified by either space or colo +n # separating the values. if (scalar(@ARGV) >= 2) { $host = shift @ARGV; $port = shift @ARGV; } elsif (scalar(@ARGV) == 1) { if ($ARGV[0] =~ /:/) { ($host, $port) = split /:/, shift @ARGV, 2; } else { $host = shift @ARGV; } } # If the port has anything other than numbers, assume it is an /etc/se +rvices # name. if ($port =~ /\D/) { $port = getservbyname $port, 'tcp' ; die "Bad port: $port" unless $port; } my $iaddr = inet_aton($host) or die "No such host: $host"; # Get a printable IP address from the in_addr_t type returned by inet_ +aton(). my $ip = join('.', unpack('C4', $iaddr)); # Now that the port value is a number, and the host (if it was origina +lly a # name) has been resolved to an IP address, then print a status header + like # the real ping does. print "AJP PING $host ($ip) port $port\n"; my $paddr = sockaddr_in($port, $iaddr) or die $!; # Grab the number for TCP out of /etc/protocols. my $proto = getprotobyname 'tcp' ; my $sock; # PF_INET and SOCK_STREAM are constants imported by the Socket module. + They # are the same as what is defined in sys/socket.h. socket $sock, PF_INET, SOCK_STREAM, $proto or die $!; # This is the ping packet. For detailed documentation, see # http://tomcat.apache.org/connectors-doc/ajp/ajpv13a.html # I stole the exact byte sequence from # http://sourceforge.net/project/shownotes.php?group_id=128058&release +_id=438456 # instead of fully understanding the packet structure. my $ping = pack 'C5' # Format template. , 0x12, 0x34 # Magic number for server->container packets. , 0x00, 0x01 # 2 byte int length of payload. , 0x0A # Type of packet. 10 = CPing. ; # If the connection is closed, log a decent message. $SIG{PIPE} = sub { die $!; }; connect $sock, $paddr or die $!; syswrite $sock, $ping or die $!; print 'Sent: '; for my $value (unpack 'C5', $ping) { printf '%02d ', $value; } print "\n"; my $pong; $pong = 0; sysread $sock, $pong, 5 or die $!; print 'Received: '; for my $value (unpack 'C5', $pong) { printf '%02d ', $value; } print "\n"; close $sock or warn $!; # This is the expected pong packet. That is, this is what Tomcat send +s back # to indicate that it is operating OK. my $expected = pack 'C5' # Format template. , 0x41, 0x42 # Magic number for container->server packe +ts. , 0x00, 0x01 # 2 byte int length of payload. , 0x09 # Type of packet. 9 = CPong reply. ; print 'Expected: '; for my $value (unpack 'C5', $expected) { printf '%02d ', $value; } print "\n"; if ($pong eq $expected) { print "Server pong OK.\n"; exit 0; } print "Server pong FAILED.\n"; exit 1;

    I also dropped a copy on my lame blog along with some examples of running it.

      sysread does not guaranteed, that all 5 bytes can be readed in one block. so you should add a loop over the sysread command.
      my $pos = 0; while ( $pos < 5) { my $c = sysread $sock, $pong, 5, $pos or die $!; $pos += $c; }