File: //proc/self/root/proc/self/root/scripts.20110531.215904.25158/cPanelMYSQLRPM.pm
package cPanelMYSQLRPM;
# cpanel - cPanelMYSQLRPM.pm 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
use Carp;
use Cpanel::HttpTimer ();
use Socket;
use strict;
use Cwd 'chdir';
use IPC::Open3 ();
use Cpanel::OSSys ();
my ( $system, $release, $machine ) = ( Cpanel::OSSys::uname() )[ 0, 2, 4 ];
my $hassigint = 0;
$SIG{'INT'} = sub {
$hassigint = 1;
print "SIGINT received. Cleaning up and halting operations.\n\n";
};
sub checkedbsdpkgs {
mkdir( '/root/.cpmysqlrpm', 0700 ) if !-e '/root/.cpmysqlrpm';
if ( ( ( stat('/root/.cpmysqlrpm/modulecheck') )[9] + 86400 ) < time() ) {
open( MC, '>/root/.cpmysqlrpm/modulecheck' );
close(MC);
return 0;
}
return 1;
}
sub httpreq {
my $page;
my ( $host, $url, $outfile ) = @_;
if ($outfile) {
open( HTTPREQF, '>', $outfile );
}
eval {
$SIG{'PIPE'} = $SIG{'ALRM'} = sub {
print "Unable to retrieve file\n";
die;
};
alarm(30);
my $proto = getprotobyname('tcp');
socket( Socket_Handle, AF_INET, SOCK_STREAM, $proto );
my $iaddr = inet_aton("$host");
my $port = getservbyname( 'http', 'tcp' );
my $sin = sockaddr_in( $port, $iaddr );
connect( Socket_Handle, $sin );
send Socket_Handle, "GET $url HTTP/1.0\r\nHost: $host\r\n\r\n", 0;
my $inheader = 1;
while (<Socket_Handle>) {
alarm(20);
if ( !$inheader ) {
if ( $outfile ne "" ) { print HTTPREQF; }
else { $page .= $_; }
}
if ( $inheader && (/^HTTP\/\d+\.\d+ (\d+)/) ) {
if ( $1 eq "404" || $1 eq "500" || $1 eq "301" ) { print "Error $1 while fetching url http://$host/$url\n"; return (); }
}
if ( $inheader && ( /^\n$/ || /^\r\n$/ || /^$/ ) ) { $inheader = 0; }
}
alarm(0);
};
if ($outfile) {
open(HTTPREQF);
return ();
}
return $page;
}
sub getmirrorlist {
my $now = time();
my ( @GOODURLS, @BADURLS );
my (%URLS);
$release =~ /(\d+\.\d+)/;
$release = $1;
my $pkgdir = 'packages-' . $release . '-release';
if ( !-e '/root/.cpmysqlrpm' ) {
mkdir( '/root/.cpmysqlrpm', 0700 );
}
if ( !-e '/root/.cpmysqlrpm/mirrors.lst' || ( ( ( stat('/root/.cpmysqlrpm/mirrors.lst') )[9] + ( 86400 * 15 ) ) < $now ) ) {
print 'Fetching mysql mirrors...';
httpreq( 'httpupdate.cpanel.net', '/pub/mysqlmirror/mirrors.lst', '/root/.cpmysqlrpm/mirrors.lst' );
die 'Cannot fetch mirror list' if ( !-e '/root/.cpmysqlrpm/mirrors.lst' );
print "Done\n";
}
open( BADMIRRORS, '/root/.cpmysqlrpm/badmirrors.lst' );
while (<BADMIRRORS>) {
chomp;
push @BADURLS, $_;
}
close(BADMIRRORS);
open( MIRRORS, '/root/.cpmysqlrpm/mirrors.lst' );
while (<MIRRORS>) {
chomp;
my $mirror = $_;
$mirror =~ /([^\:]+):\/\/([^\/]+)(\S+)/;
my $pro = $1;
my $host = $2;
my $url = $3;
if ( !grep( /^\Q${mirror}\E$/, @BADURLS ) ) {
$URLS{$host} = $mirror;
}
}
close(MIRRORS);
system '/scripts/mysqlrpmpingtest';
my (%PINGTIMES);
opendir( PT, '/root/.cpmysqlrpm/pingtimes' );
my @PT = readdir(PT);
closedir(PT);
foreach my $pt (@PT) {
next if ( $pt =~ /^\./ );
open( PINGTIMES, '/root/.cpmysqlrpm/pingtimes/' . $pt );
$PINGTIMES{$pt} = <PINGTIMES>;
chomp $PINGTIMES{$pt};
close(PINGTIMES);
}
undef @PT;
mkdir( '/var/spool/cpmysqlrpm', 0700 ) if ( !-e '/var/spool/cpmysqlrpm' );
chdir '.';
my $cwd = $ENV{'PWD'};
chdir '/var/spool/cpmysqlrpm';
my %MIRRORSPEED;
open( MIRRORSPEEDS, '/root/.cpmysqlrpm/mirrors.speeds' );
while (<MIRRORSPEEDS>) {
chomp;
my ( $mirror, $speed ) = split( /=/, $_ );
$MIRRORSPEED{$mirror} = $speed;
}
close(MIRRORSPEEDS);
my $dc = 0;
foreach my $host ( sort { $PINGTIMES{$a} <=> $PINGTIMES{$b} } keys %PINGTIMES ) {
if ( $dc >= 3 ) {
#print "Four usable mirrors located\n";
last;
}
if ( $MIRRORSPEED{$host} eq '' ) {
print "Ping:$PINGTIMES{$host} ";
( $MIRRORSPEED{$host} ) = testmirrorspeed( $host, $URLS{$host}, $pkgdir );
}
if ( $MIRRORSPEED{$host} > 1 ) { $dc++; }
}
open( MIRRORSPEEDS, '>', '/root/.cpmysqlrpm/mirrors.speeds' );
foreach my $mirror ( keys %MIRRORSPEED ) {
print MIRRORSPEEDS $mirror . '=' . $MIRRORSPEED{$mirror} . "\n";
}
close(MIRRORSPEEDS);
$dc = 0;
foreach my $host ( sort { $MIRRORSPEED{$b} <=> $MIRRORSPEED{$a} } keys %MIRRORSPEED ) {
next if ( $MIRRORSPEED{$host} eq '0' );
push( @GOODURLS, $URLS{$host} );
}
delete @MIRRORSPEED{ keys %MIRRORSPEED };
chdir $cwd;
return @GOODURLS;
}
sub testmirrorspeed {
my ( $host, $url, $pkgdir ) = @_;
my $now = time();
my $speed;
if ( $url =~ /^http/ ) {
print "Testing connection speed to $host using pureperl...";
my $RES = Cpanel::HttpTimer::timedrequest( url => $url . '/index.html' );
$speed = $$RES{'speed'};
}
else {
open( WNULL, '>', '/dev/null' );
open( RNULL, '<', '/dev/null' );
unlink 'MYSQLINDEX';
my $tpid;
if ( -e '/usr/bin/wget' || -e '/usr/local/bin/wget' ) {
print "Testing connection speed to $host using wget...";
$tpid = IPC::Open3::open3( "<&RNULL", ">&WNULL", \*ERRFH, 'wget', '-t', '2', '--passive-ftp', '--timeout', '500', '-O', 'MYSQLINDEX', $url . '/index.html' );
while (<ERRFH>) {
if (/\(([\d\.]+)\s+([^\)]+)/) {
$speed = $1;
if ( $2 =~ /^k/i ) { $speed = ( $speed * 1000 ); }
if ( $2 =~ /^m/i ) { $speed = ( $speed * 1000000 ); }
if ( $2 =~ /^g/i ) { $speed = ( $speed * 1000000000 ); }
}
}
close(ERRFH);
}
elsif ( -e '/usr/bin/curl' || -e '/usr/local/bin/curl' ) {
print "Testing connection speed to $host using curl...";
$tpid = IPC::Open3::open3( '<&RNULL', '>&WNULL', \*ERRFH, 'curl', '-m', '100', '-o', 'MYSQLINDEX', $url . "/index.html" );
while (<ERRFH>) {
# 7 628k 7 46091 0 0 39876
if (/\s*\d+\s+\S+\s+\d+\S+\s+\d+\s+\S+\s+(\d+)(\S+)/) {
$speed = $1;
if ( $2 =~ /k$/i ) { $speed = ( $speed * 1000 ); }
if ( $2 =~ /m$/i ) { $speed = ( $speed * 1000000 ); }
if ( $2 =~ /g$/i ) { $speed = ( $speed * 1000000000 ); }
}
}
close(ERRFH);
}
elsif ( $url =~ /^ftp/ && ( -e "/usr/bin/ncftpget" || -e "/usr/local/bin/ncftpget" ) ) {
print "Testing connection speed to $host using ncftpget...";
open( ERRFH, "-|" ) || exec( "ncftpget", "-z", $url . "/index.html" );
while (<ERRFH>) {
if (/([\d\.]+)\s+(\S+)\s*$/) {
$speed = $1;
if ( $2 =~ /^k/i ) { $speed = ( $speed * 1000 ); }
if ( $2 =~ /^m/i ) { $speed = ( $speed * 1000000 ); }
if ( $2 =~ /^g/i ) { $speed = ( $speed * 1000000000 ); }
}
}
close(ERRFH);
}
else {
Carp::confess 'System is missing wget, curl, ncftpget, and fetch';
}
unlink 'MYSQLINDEX';
close(RNULL);
close(WNULL);
waitpid( $tpid, 0 );
}
if ( !$speed ) {
$speed = 0;
print "test failed...Done\n";
}
else {
print "($speed bytes/s)...Done\n";
}
return $speed;
}
sub removemirrors {
my (@mirrors) = @_;
my $file = "/root/.cpmysqlrpm/mirrors.lst";
open( LIST, "<", $file ) or die "Unable to open mirror file.";
my @list = <LIST>;
close(LIST);
my @badlist = ();
if ( -e "/root/.cpmysqlrpm/badmirrors.lst" ) {
open( BADM, "<", "/root/.cpmysqlrpm/badmirrors.lst" ) or die "Unable to open bad mirror file.";
@badlist = <BADM>;
close(BADM);
}
open( LIST, ">", $file ) or die "Unable to open mirror file.";
foreach my $m (@list) {
chomp($m);
print LIST "${m}\n" unless grep( /^\Q${m}\E$/, @mirrors );
}
close(LIST);
open( BAD, ">", "/root/.cpmysqlrpm/badmirrors.lst" ) or die "Unable to open bad mirror file.";
foreach my $mirror (@mirrors) {
if ( !grep( /^\Q${mirror}\E$/, @badlist ) ) {
print BAD $mirror . "\n";
}
}
print join( "\n", @badlist ) . "\n";
close(BAD);
foreach my $mirror (@mirrors) {
$mirror =~ /([^\:]+):\/\/([^\/]+)(\S+)/;
my ( $pro, $host, $url ) = ( $1, $2, $3 );
unlink("/root/.cpmysqlrpm/pingtimes/${host}");
}
unlink("/root/.cpmysqlrpm/mirrors.speeds");
}
1;