File: //scripts.20110531.215904.25158/cpanpingtest
#!/usr/bin/perl
# cpanel - cpanpingtest Copyright(c) 2010 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 Cpanel::PingTest ();
use Cpanel::Config::LoadConfig ();
my ( $system, $release, $machine ) = ( Cpanel::OSSys::uname() )[ 0, 2, 4 ];
my $basedir = $> == 0 ? '/home' : ( getpwuid($>) )[7];
my $cpcpandir = "$basedir/.cpcpan/";
my $my_mtime = (stat('/scripts/cpanpingtest'))[9];
$| = 1;
if ( !-e $cpcpandir . 'pingtimes' ) {
mkdir $cpcpandir, 0700;
mkdir $cpcpandir . 'pingtimes', 0700;
}
my %URLS;
Cpanel::Config::LoadConfig::loadConfig( $cpcpandir . 'mirrorurls', \%URLS );
my $now = time();
my $ping_file_ttl = ( 86400 * 20 );
my @HOST_LIST;
foreach my $host ( keys %URLS ) {
my $ping_file_mtime = ( stat( $cpcpandir . 'pingtimes/' . $host ) )[9];
if ( $ping_file_mtime && $ping_file_mtime > $my_mtime && ( $ping_file_mtime + $ping_file_ttl ) > $now ) {
next;
}
push @HOST_LIST, $host;
}
if (@HOST_LIST) {
my $rPINGTIMES = Cpanel::PingTest::pinghosts( \@HOST_LIST, 1 );
foreach my $host ( keys %URLS ) {
if ( !exists $rPINGTIMES->{$host} ) { next; }
if ( open my $pingtime_fh, '>', $cpcpandir . 'pingtimes/' . $host ) {
print {$pingtime_fh} $rPINGTIMES->{$host};
close $pingtime_fh;
}
else {
warn "Unable to write ${cpcpandir}pingtimes/$host: $!";
}
}
}