File: //proc/self/root/scripts.20110531.215904.25158/mysqlrpmpingtest
#!/usr/bin/perl
# cpanel10 - mysqlrpmpingtest Copyright(c) 1997-2006 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
BEGIN {
unshift @INC,'/usr/local/cpanel';
}
use strict;
use IPC::Open3 ();
use Cpanel::OSSys ();
my ( $system, $release, $machine ) = ( Cpanel::OSSys::uname() )[ 0, 2, 4 ];
$| = 1;
if ( ! -e '/root/.cpmysqlrpm/pingtimes' ) {
mkdir( '/root/.cpmysqlrpm', 0700 );
mkdir( '/root/.cpmysqlrpm/pingtimes', 0700 );
}
my @HOSTS;
if ( open my $mirrors_fh, '<', '/root/.cpmysqlrpm/mirrors.lst' ) {
while ( my $mirror = readline $mirrors_fh ) {
chomp $mirror;
my ( $pro, $host, $url ) = $mirror =~ /([^\:]+):\/\/([^\/]+)(\S+)/;
push @HOSTS, $host;
}
close $mirrors_fh;
}
else {
die "Missing MySQL mirror list: $!";
}
my %current_pingtime_hosts;
if (opendir my $pingtime_dh, '/root/.cpmysqlrpm/pingtimes') {
%current_pingtime_hosts = map { $_ => 1 } grep { ! /^[.]/ } readdir $pingtime_dh;
closedir $pingtime_dh;
}
my $now = time();
my $child = 0;
my $pinging = 0;
foreach my $host (@HOSTS) {
if ( ( ( stat( '/root/.cpmysqlrpm/pingtimes/' . $host ) )[9] + ( 86400 * 15 ) ) > $now ) {
delete $current_pingtime_hosts{ $host };
next;
}
if ( $pinging == 0 ) {
print "Testing connection speed...(this could take a while)....";
$pinging = 1;
}
if ( $child <= 15 ) {
if ( my $pid = fork() ) {
$child++;
print '.';
}
else {
my $pingtime = pinghost($host);
open( PT, '>', '/root/.cpmysqlrpm/pingtimes/' . $host );
print PT $pingtime;
close(PT);
delete $current_pingtime_hosts{ $host };
exit;
}
}
else {
sleep(1);
}
while ( waitpid( -1, 1 ) > 0 ) {
$child--;
}
}
while ( waitpid( -1, 0 ) > 0 ) {
$child--;
}
if ( $pinging == 1 ) {
print "Done\n";
}
foreach my $host (keys %current_pingtime_hosts) {
print "Removing dead host $host\n";
unlink '/root/.cpmysqlrpm/pingtimes/' . $host;
}
sub pinghost {
my ($host) = @_;
my ($pingtime);
open( WNULL, '>/dev/null' );
open( RNULL, '</dev/null' );
my $pingflag = '-w';
if ( $system =~ /freebsd/i ) { $pingflag = '-t'; }
my $pid = IPC::Open3::open3( '<&RNULL', \*PING, '>&WNULL', 'ping', $pingflag, '3', '-c', '2', $host );
while (<PING>) {
if (/[\d\.]+\/([\d\.]+)/) {
$pingtime = $1;
}
}
close(PING);
if ( ! $pingtime ) { $pingtime = 1000; }
close(RNULL);
close(WNULL);
waitpid($pid,0);
return $pingtime;
}