File: //proc/self/root/scripts.20110531.215904.25158/initbyteslog
#!/usr/bin/perl
# cpanel - initbyteslog 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::RcsRecord ();
use Cpanel::SafeFile ();
use Cpanel::FtpUtils ();
use Cpanel::EditHttpdconf ();
use Cpanel::HttpUtils::Version ();
use Cpanel::Logger ();
die if !-e '/usr/local/apache/conf/httpd.conf';
my $logger = Cpanel::Logger->new();
my $apv = Cpanel::HttpUtils::Version::get_current_apache_version_key();
die 'Could not determine apache verison' if !$apv;
Cpanel::EditHttpdconf::edit_httpdconf(
sub {
my ( $rw_fh, $safe_replace_content_coderef ) = @_;
my @new_contents;
my $in_vhost = 0;
LINE:
while ( my $line = readline($rw_fh) ) {
if ( !/^\s*#/ ) {
if ( $line =~ /<virtualhost/i ) {
$in_vhost = 1;
}
if ( $line =~ /<\/virtualhost/i ) {
$in_vhost = 0;
}
if ($in_vhost) {
next LINE if $line =~ m{bytes_log\s*}i; # strip 1.3 and 2.x style bytes log entry
if ( $line =~ /ServerName (\S+)/i ) {
$sn = $1;
$sn =~ s/^www\.//g;
my $byteslog_entry = $apv eq '1' ? qq{BytesLog domlogs/$sn-bytes_log} : "CustomLog domlogs/$sn-bytes_log" . q( "%{%s}t %I .\n%{%s}t %O .");
push @new_contents, $line, " $byteslog_entry\n";
next LINE;
}
}
}
push @new_contents, $line;
} # end while
return "Edited by $0" if $safe_replace_content_coderef->( $rw_fh, \@new_contents );
return;
},
);
my $proftpdconf = Cpanel::FtpUtils::find_proftpconf();
exit if !-e $proftpdconf;
Cpanel::RcsRecord::rcsrecord($proftpdconf);
my @HFILE;
my $conflock = Cpanel::SafeFile::safeopen( \*HC, '+<', $proftpdconf );
if ( !$conflock ) {
$logger->die("Could not edit $proftpdconf");
}
$lf = 1;
while (<HC>) {
push( @HFILE, $_ );
}
seek( HC, 0, 0 );
foreach (@HFILE) {
if ( !/^#/ ) {
if (/logformat byteslog/i) {
$lf = 0;
}
if (/^servertype/i) {
if ($lf) {
print HC "LogFormat BytesLog \"%b\"\n";
}
}
if (/<virtualhost/i) {
$bh = 1;
$ivh = 1;
}
if (/<\/virtualhost/i) {
$ivh = 0;
}
if ($ivh) {
if (/extendedlog /i) {
$bh = 0;
}
if (/ServerName (\S+)/i) {
$sn = $1;
$sn =~ s/^www\.//g;
if ($bh) {
print HC "ExtendedLog /etc/httpd/domlogs/$sn-ftpbytes_log READ BytesLog\n";
}
}
}
}
print HC $_;
}
truncate( HC, tell(HC) );
Cpanel::SafeFile::safeclose( \*HC, $conflock );
undef @HFILE;
my $conflock = Cpanel::SafeFile::safeopen( \*HC, '+<', $proftpdconf );
if ( !$conflock ) {
$logger->die("Could not edit $proftpdconf");
}
$lf = 1;
while (<HC>) {
push( @HFILE, $_ );
}
seek( HC, 0, 0 );
foreach (@HFILE) {
if ( !/^#/ ) {
if (/<virtualhost/i) {
$bh = 1;
$ivh = 1;
}
if (/<\/virtualhost/i) {
$ivh = 0;
}
if ($ivh) {
if (/transferlog /i) {
$bh = 0;
}
if (/ServerName (\S+)/i) {
$sn = $1;
$sn =~ s/^www\.//g;
if ($bh) {
print HC "TransferLog /etc/httpd/domlogs/$sn-ftp_log\n";
}
}
}
}
print HC $_;
}
truncate( HC, tell(HC) );
Cpanel::SafeFile::safeclose( \*HC, $conflock );
Cpanel::RcsRecord::rcsrecord($proftpdconf);