| 1 | package App::MadEye::Plugin::Agent::FTP; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use App::MadEye::Plugin::Agent::Base; | ||
| 5 | |||
| 6 | use Socket qw(IPPROTO_TCP TCP_NODELAY SOL_SOCKET SOCK_STREAM); | ||
| 7 | use IO::Socket::INET; | ||
| 8 | use IO::Select; | ||
| 9 | |||
| 10 | sub is_dead { | ||
| 11 | my ($self, $host) = @_; | ||
| 12 | |||
| 13 | my $conf = $self->config->{config}; | ||
| 14 | my $message = $conf->{message} or die "missing message"; | ||
| 15 | my $port = $conf->{port} || 21; | ||
| 16 | my $connect_timeout = $conf->{connect_timeout} || 10; | ||
| 17 | my $select_timeout = $conf->{select_timeout} || 10; | ||
| 18 | |||
| 19 | my $sock = IO::Socket::INET->new( | ||
| 20 | PeerAddr => $host, | ||
| 21 | PeerPort => $port, | ||
| 22 | Timeout => $connect_timeout, | ||
| 23 | ); | ||
| 24 | unless ($sock) { | ||
| 25 | return "Cannot connect Socket to $host:$port : $!"; | ||
| 26 | } | ||
| 27 | $sock->blocking(0); | ||
| 28 | setsockopt($sock, IPPROTO_TCP, TCP_NODELAY, pack("l", 1)) or die; | ||
| 29 | $sock->autoflush(1); | ||
| 30 | |||
| 31 | my $select = IO::Select->new; | ||
| 32 | $select->add($sock); | ||
| 33 | |||
| 34 | my ($can_read,) = $select->can_read($select_timeout); | ||
| 35 | if ($can_read) { | ||
| 36 | my $line = <$sock>; | ||
| 37 | $sock->close; | ||
| 38 | |||
| 39 | if ($line =~ /$message/) { | ||
| 40 | return; # This server is still alive! | ||
| 41 | } else { | ||
| 42 | return "this ftp server is dead!! : $line"; | ||
| 43 | } | ||
| 44 | } else { | ||
| 45 | $sock->close; | ||
| 46 | return "pure-ftpd connection timeout : $host"; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | 1; | ||
| 51 | __END__ |