#!/usr/bin/perl # AJPING - pings a servlet engine with AJP protocol # Sends: \x12\x34\x00\x01\x0a # Expects: \0x41\0x42\0x00\0x01\0x09 # use Socket; use Time::HiRes 'time'; use Getopt::Long; use vars qw($opt_V $opt_h $opt_t $opt_p $opt_H); use strict; use vars qw($PROGRAM $VERSION); use lib "/usr/local/zenoss/common/libexec" ; use utils qw($TIMEOUT %ERRORS &print_revision &support &usage); $PROGRAM = "check_ajp"; $VERSION = "1.0.0"; sub xdie; sub print_help; my ($host, $port, $timo, $code); Getopt::Long::Configure('bundling'); GetOptions ( "V" => \$opt_V, "version" => \$opt_V, "h" => \$opt_h, "help" => \$opt_h, "t=i" => \$opt_t, "timeout=i" => \$opt_t, "p=i" => \$opt_p, "port=i" => \$opt_p, "H=s" => \$opt_H, "hostname=s" => \$opt_H ); if ($opt_V) { print_revision($PROGRAM, $VERSION); exit; } if ($opt_h) { print_help(); } ($opt_H) || ($opt_H = shift) || usage("Host name/address not specified\n"); $host = $1 if ($opt_H =~ /([-.A-Za-z0-9]+)/); ($opt_p) || ($opt_p = shift) || ($opt_p = 8009); $port = $1 if ($opt_p =~ /^([0-9]+)$/); ($port) || usage("Invalid port: $opt_p\n"); if (!$timo) { $timo = 5; } my $start = time(); my $rin = ''; my $iaddr = inet_aton($host) || xdie("Unknown host ($host)"); my $paddr = sockaddr_in($port, $iaddr) || xdie ("Unable to establish a socket address"); my $proto = getprotobyname('tcp'); socket(my $sock, PF_INET, SOCK_STREAM, $proto) || xdie "Unable to create a socket."; setsockopt($sock, SOL_SOCKET, SO_SNDTIMEO, pack('l!l!', $timo, 0)) or die $!; setsockopt($sock, SOL_SOCKET, SO_RCVTIMEO, pack('l!l!', $timo, 0)) or die $!; connect($sock, $paddr) || xdie ("Unable to connect to server"); syswrite $sock, "\x12\x34\x00\x01\x0a"; sysread $sock, my $recv, 5 || xdie("read: $!, stopped"); my @vals = unpack 'C5', $recv; my @acks = qw (65 66 0 1 9); my %vals = map {$_, 1} @vals; my @diff = grep {!$vals {$_}} @acks; if (@diff == 0) { my $res = (time() - $start) + 0.005; my $len = (length("@vals") - $#vals); printf ("AJP OK - %d bytes in %3.3f seconds |time=%3.3fs;;;0.000000 size=%dB;;;0\n", $len, $res, $res, $len); $code = $ERRORS{'OK'}; } else { print "AJP CRITICAL - Unable to verify AJP host $host\n"; $code = $ERRORS{'CRITICAL'}; } close($sock); #if (open(FILE, ">> /tmp/haha")) { # print FILE "haha!\n"; #} #close(FILE); exit $code; sub print_help { print "$PROGRAM - AJP protocol check for Zenoss\n"; print "usage: $PROGRAM [options] -H, --hostname=HOST Name or IP address of host to check -p, --port=INTEGER Port on which the server listens for AJP requests (Default: 8009) -t, --timeout=INTEGER Socket time out for tcp connection to (Default: 5 seconds) "; exit $ERRORS{'OK'}; } sub xdie { my $msg = shift; printf "AJP CRITICAL - $msg\n"; exit $ERRORS{'CRITICAL'}; }