File: //proc/self/root/proc/self/root/scripts.20110531.215904.25158/vps_optimizer
#!/usr/bin/perl
# cpanel - vps_optimizer 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::FileUtils::TouchFile ();
use Cpanel::LoadFile ();
use Cpanel::Version ();
use Cpanel::OSSys ();
use Cpanel::Config::Services ();
use Cpanel::Config::LoadCpConf ();
my @VER;
my @OPTS;
foreach my $arg (@ARGV) {
if ( $arg =~ /^-/ ) {
push @OPTS, $arg;
}
else {
push @VER, $arg;
}
}
my $system = Cpanel::OSSys::get_system();
my $pver = $VER[0] || '588';
my $tree = Cpanel::Version::gettree();
##
## Do not change this as we do not want to deply CPAN::SQLLite until it is more stable
##
my $version = ( $tree =~ /^(?:beta|edge)/i ? '2.0' : '1.8' );
exit 1 if !-e '/usr/local/cpanel/cpkeyclt';
if ( !-e '/var/cpanel/envtype' ) {
system '/usr/local/cpanel/cpkeyclt';
if ( !-e '/var/cpanel/envtype' ) {
die "Problem verifying license information";
}
}
my $envtype = Cpanel::LoadFile::loadfile('/var/cpanel/envtype');
exit if ( $envtype && $envtype eq 'standard' );
exit if ( -e '/var/cpanel/vps_optimized/' . $version && !grep( /force/, @OPTS ) );
print "Enabling conserve_memory options...";
Cpanel::FileUtils::TouchFile::touchfile('/var/cpanel/conserve_memory');
print "Done\n";
my $cpconf = Cpanel::Config::LoadCpConf::loadcpconf();
if ( $tree =~ m/^(?:beta|edge)/i ) {
print "Enabling CPAN::SQLite...";
$cpconf->{'enable_cpansqlite'} = 1;
Cpanel::FileUtils::TouchFile::touchfile('/var/cpanel/enable_cpansqlite');
require Cpanel::Config;
Cpanel::Config::savecpconf($cpconf);
print "Done\n";
}
require Cpanel::MysqlUtils;
my $mysql_host = Cpanel::MysqlUtils::getmydbhost() || 'localhost';
if ( Cpanel::Config::Services::service_enabled('mysql') && $mysql_host eq 'localhost' ) {
print "Disabling MySQL InnoDB engine if possible....";
my $hasinnodb = 0;
my $dbdir = Cpanel::MysqlUtils::getmysqldir();
my $dbcheckok = -d $dbdir . '/mysql' ? 1 : 0;
if ($dbcheckok) {
opendir( my $sql_dh, $dbdir );
while ( my $db = readdir($sql_dh) ) {
next if ( $db =~ m/^\.+$/ );
next if $db eq 'mysql'; # mysql db should never have InnoDB on
next if $db eq 'horde'; # Horde session table is the only one that uses InnoDB, so this is not a problem
next if ( !-d $dbdir . '/' . $db );
my $ms = sqlcmd("show table status from `$db`;");
if ( !$ms ) {
$dbcheckok = 0;
last;
}
elsif ( $ms =~ m/\s+InnoDB\s+/m ) {
$hasinnodb = 1;
last;
}
}
closedir($sql_dh);
}
if ( $dbcheckok && !$hasinnodb ) {
print "not needed...disabled\n";
if ( !-e '/etc/my.cnf' ) { Cpanel::FileUtils::TouchFile::touchfile('/etc/my.cnf'); }
require Cpanel::MysqlUtils::InnoDB;
Cpanel::MysqlUtils::InnoDB::disable(); #causes a restart
}
else {
print "in use...cannot disable\n";
}
}
unless ( $system =~ m/freebsd/i ) {
my $recomp_perl = 0;
print "Checking perl config....";
require Cpanel::SafeRun::Simple;
my $perlinfo = Cpanel::SafeRun::Simple::saferun( '/usr/bin/perl', '-V' );
if ( $perlinfo !~ m/useshrplib=true/i || $perlinfo =~ m/USE_ITHREADS/ || $perlinfo !~ m/usemymalloc=\'?y/i || $perlinfo !~ m/\-Os/ ) {
print "Needs recompile\n";
$recomp_perl = 1;
}
else {
print "OK\n";
}
if ($recomp_perl) {
require Cpanel::HttpRequest;
my $httpClient = Cpanel::HttpRequest->new();
$httpClient->download( 'http://httpupdate.cpanel.net/perl' . $pver . 'installer.tar.gz', 'perl' . $pver . 'installer.tar.gz' );
if ( -e 'perl' . $pver . 'installer.tar.gz' ) {
system 'tar', 'xfvz', 'perl' . $pver . 'installer.tar.gz';
if ( chdir( 'perl' . $pver . 'installer' ) ) {
system './install', '-optimize-memory', '-upgrade';
}
else {
print "FAILED TO EXTRACT PERL INSTALLER!!!\n";
}
}
else {
print "FAILED TO DOWNLOAD PERL INSTALLER!!!!\n";
}
system '/usr/local/cpanel/bin/low_mem_mods';
}
}
if ( $cpconf->{'maildir'} && Cpanel::Config::Services::service_enabled('imap') ) {
if ( eval 'require Cpanel::CourierConfig;' ) {
print "Switching to 2 auth daemons for courier....";
Cpanel::CourierConfig::set_auth_config_value( 'daemons', 2 );
print "Done\n";
}
else {
print "Failed to load Cpanel::CourierConfig: $@\n";
}
}
if ( $cpconf->{'dovecot'} ) {
if ( eval 'require Cpanel::AdvConfig::dovecot::utils;' ) {
#FIXME: dovecot support
}
else {
print "Failed to load Cpanel::AdvConfig::dovecot::utils: $@\n";
}
}
if ( Cpanel::Config::Services::service_enabled('spamd') ) {
require Cpanel::StringFunc;
Cpanel::FileUtils::TouchFile::touchfile('/etc/cpspamd.conf');
print "Switching spamassassin max spare to 1....";
Cpanel::StringFunc::remlinefile( '/etc/cpspamd.conf', 'maxspare=1' );
Cpanel::StringFunc::addlinefile( '/etc/cpspamd.conf', 'maxspare=1' );
print "Done\n";
print "Switching spamassassin max children to 3....";
Cpanel::StringFunc::remlinefile( '/etc/cpspamd.conf', 'maxchildren=3' );
Cpanel::StringFunc::addlinefile( '/etc/cpspamd.conf', 'maxchildren=3' );
print "Done\n";
print "Restarting spamassassin....";
if ( my $pid = fork() ) {
waitpid( $pid, 0 );
}
else {
require Cpanel::CleanupStub;
chdir '/';
Cpanel::OSSys::setsid();
Cpanel::CleanupStub::daemonclosefds();
exec '/scripts/restartsrv_spamd';
die "Failed to restart spamd."; # in case the exec fails
}
print "Done\n";
}
if ( !grep( /skipstartup/, @OPTS ) ) {
print "Restarting Services....";
if ( my $pid = fork() ) {
waitpid( $pid, 0 );
}
else {
require Cpanel::CleanupStub;
chdir '/';
Cpanel::OSSys::setsid();
Cpanel::CleanupStub::daemonclosefds();
system '/scripts/restartsrv_exim';
system '/scripts/restartsrv_imap';
system '/scripts/restartsrv_chkservd';
exit; # no need to restart cpanel here -- could be a problem with upcp in whm anyways
#
#
#exec '/usr/local/cpanel/startup';
die "Failed to restart cPanel services."; # in case the exec fails
}
print "Done\n";
}
if ( !-e '/var/cpanel/vps_optimized' ) {
mkdir( '/var/cpanel/vps_optimized', 0700 );
}
Cpanel::FileUtils::TouchFile::touchfile("/var/cpanel/vps_optimized/$version");
print "Optimizations Complete!\n";
sub sqlcmd {
my ($cmd) = @_;
my $result;
require Cpanel::MysqlUtils;
require IPC::Open3;
my $mysql = Cpanel::MysqlUtils::find_mysql();
my $pid = IPC::Open3::open3( \*WTRFH, \*RDRFH, ">&STDERR", $mysql, '-N' );
print WTRFH "show status like 'uptime'; $cmd\n"; #make sure we already get something back so we know mysql is up
close(WTRFH);
while (<RDRFH>) {
$result .= $_;
}
close(RDRFH);
waitpid( $pid, 0 );
return $result;
}