root/lib/Resmon/Module/TCPSERVICE.pm

Revision 3596d9c8a2b13b9f524e13fd871eb6400764b1d7, 1.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

rename Resmon::Modules as Resmon::Module

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

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