File: //proc/self/root/scripts.20110531.215904.25158/apachelimits
#!/usr/bin/perl
# cpanel - apachelimits 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 Cpanel::EditHttpdconf ();
use Cpanel::DataStore ();
use Cpanel::OSSys ();
use Cpanel::SafeFile;
#this is under the artistic lics
print "Apache Limiter by cPanel, Inc. <copyright\@cpanel.net>\n";
print "Checking for BSD Accounting .... \n";
print "Calculating Memory Limit .....";
$size = 0;
if ( open my $meminfo_fh, '<', '/proc/meminfo' ) {
while (<$meminfo_fh>) {
tr/[A-Z]/[a-z]/;
if (/^(\S+):\s*(\S+)\s(\S+)$/) {
if ( $3 eq "kb" ) {
if ( $1 eq "swaptotal" ) {
print "..$2..";
$size += int( $2 / 3 );
print "$size";
}
if ( $1 eq "memtotal" ) {
print "..$2..";
$size += int( $2 / 2 );
print "$size";
}
}
}
}
close $meminfo_fh;
}
if ( !$size && Cpanel::OSSys::get_system() eq 'freebsd' ) {
system '/scripts/ensurepkg', 'muse';
}
if ( -x '/usr/local/bin/muse' ) {
foreach ( split( /\n/, `/usr/local/bin/muse` ) ) {
if (/^(total|inactive):[\t|\s]*(\d+)/i) { $size += int( $2 / 1024 / 2 ); }
}
}
$size = int( $size / 2 );
$size = int( $size * 1024 );
if ( !$size ) {
print "Unable to determine appropriate memory limit.\n";
}
else {
print "..$size..\n";
$size = int( $size / 3 );
print "..$size..\n";
Cpanel::EditHttpdconf::edit_httpdconf(
sub {
my ( $rw_fh, $safe_replace_content_coderef ) = @_;
my @new_content = ( "RLimitMEM $size\n", "RLimitCPU 240\n" );
while ( my $line = readline($rw_fh) ) {
next if $line =~ m{RLimitMEM} || $line =~ m{RLimitCPU};
push @new_content, $line;
}
return "Edited by $0 " if $safe_replace_content_coderef->( $rw_fh, \@new_content );
return;
}
);
# Update data stores
my $ap_conf = Cpanel::DataStore::fetch_ref('/var/cpanel/conf/apache/main');
if ( !exists $ap_conf->{'main'}{'rlimitcpu'}{'item'}{'softrlimitcpu'} ) { # Just distill it
system '/usr/local/cpanel/bin/apache_conf_distiller', '--update', '--main';
}
else {
$ap_conf->{'main'}{'rlimitcpu'}{'item'}{'softrlimitcpu'} = '240';
$ap_conf->{'main'}{'rlimitmem'}{'item'}{'softrlimitmem'} = $size;
Cpanel::DataStore::store_ref( '/var/cpanel/conf/apache/main', $ap_conf );
}
$meg = int( $size / ( 1024 * 1024 ) );
print "Largest Webserver child cgi/ssi/php is now limited to $meg meg\n";
}