root/TCPSERVICE.pl

Revision 37cbe43bf668696d2f26324ca62752e777b57d2b, 1.3 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 13 years ago)

Initial revision

git-svn-id: https://labs.omniti.com/resmon/trunk@2 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   fcntl($handle, Fcntl::F_SETFL, Fcntl::O_NONBLOCK) ||
22     (close($handle) && return "BAD(fcntl error)");
23   my $sin = Socket::sockaddr_in($port, Socket::inet_aton($host));
24   connect($handle, $sin);
25   $con->add($handle);
26   my ($fd) = $con->can_write($timeout);
27   if($fd == $handle) {
28     my $error = unpack("s", getsockopt($handle, Socket::SOL_SOCKET,
29                                         Socket::SO_ERROR));
30     if($error != 0) {
31       close($handle);
32       return "BAD(connect failed)";
33     }
34     ($fd) = $con->can_read($timeout);
35     if($fd == $handle) {
36       my $banner;
37       chomp($banner = <$handle>);
38       print $handle $arg->{post} if ($arg->{post});
39       close($handle);
40       return "BAD($banner)"
41         if($arg->{match} && ($banner =! /$arg->{match}/));
42       return "OK($banner)";
43     }
44   }
45   close($handle);
46   return "BAD(timeout)";
47 });
48
49 1;
Note: See TracBrowser for help on using the browser.