root/TCPSERVICE.pl

Revision 5e140f298dbb2f136691b8623c74af85be4eda8e, 1.4 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 13 years ago)

prepost and non ASCII banners

git-svn-id: https://labs.omniti.com/resmon/trunk@7 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

  • Property mode set to 100755
Line 
1 #!/usr/bin/perl
2
3 use Socket;
4 use Fcntl;
5 use IO::Select;
6 use IO::Handle;
7
8 register_monitor('TCPSERVICE',
9 sub {
10   my $arg = shift;
11   my $os = fresh_status($arg);
12   return $os if $os;
13   my $host = $arg->{host};
14   my $port = $arg->{port};
15   my $timeout = $arg->{timeout} || 5;
16   my $proto = getprotobyname('tcp');
17   my $con = new IO::Select();
18   my $handle = new IO::Handle;
19   socket($handle, Socket::PF_INET, Socket::SOCK_STREAM, $proto) ||
20     return "BAD(socket error)";
21   $handle->autoflush(1);
22   fcntl($handle, Fcntl::F_SETFL, Fcntl::O_NONBLOCK) ||
23     (close($handle) && return "BAD(fcntl error)");
24   my $sin = Socket::sockaddr_in($port, Socket::inet_aton($host));
25   connect($handle, $sin);
26   $con->add($handle);
27   my ($fd) = $con->can_write($timeout);
28   if($fd == $handle) {
29     my $error = unpack("s", getsockopt($handle, Socket::SOL_SOCKET,
30                                         Socket::SO_ERROR));
31     if($error != 0) {
32       close($handle);
33       return "BAD(connect failed)";
34     }
35     print $handle $arg->{prepost}."\r\n" if ($arg->{prepost});
36     ($fd) = $con->can_read($timeout);
37     if($fd == $handle) {
38       my $banner;
39       chomp($banner = <$handle>);
40       print $handle $arg->{post} if ($arg->{post});
41       close($handle);
42       $banner =~ s/([^\s\d\w.,;\/\\])/sprintf "\\%o", $1/eg;
43       return "BAD($banner)"
44         if($arg->{match} && ($banner =! /$arg->{match}/));
45       return "OK($banner)";
46     }
47   }
48   close($handle);
49   return "BAD(timeout)";
50 });
51
52 1;
Note: See TracBrowser for help on using the browser.