| 1 |
# Connect to a given server and read initial data, simulating |
| 2 |
# load balancer connecting to MySQL to check server availability. |
| 3 |
# Following variables should be set: |
| 4 |
# $SRV_HOST Server hostname to connect to |
| 5 |
# $SRV_PORT Server port |
| 6 |
|
| 7 |
--perl |
| 8 |
use Socket; |
| 9 |
|
| 10 |
my $host = $ENV{'SRV_HOST'}; |
| 11 |
my $port = $ENV{'SRV_PORT'}; |
| 12 |
my $iaddr = inet_aton($host) || die "no host: $host\n"; |
| 13 |
my $paddr = sockaddr_in($port, $iaddr); |
| 14 |
my $proto = getprotobyname("tcp"); |
| 15 |
|
| 16 |
socket(my $sock, PF_INET, SOCK_STREAM, $proto) || die "socket failed\n"; |
| 17 |
connect($sock, $paddr) || die "connect failed to $host:$port\n"; |
| 18 |
|
| 19 |
my $read = ''; |
| 20 |
vec($read, fileno($sock), 1) = 1; # Set the appropriate bit |
| 21 |
my $found = select($read, undef, undef, undef); |
| 22 |
|
| 23 |
# Does socket have data waiting? |
| 24 |
if (vec($read,fileno($sock), 1)) |
| 25 |
{ |
| 26 |
sysread($sock,my $line,1024); |
| 27 |
} |
| 28 |
|
| 29 |
print "closing connection\n"; |
| 30 |
close($sock); |
| 31 |
EOF |