#!/usr/bin/perl -w
 
###############################################################################
#                                                                             #
# Filename: rotate_web_logs                                                   #
#                                                                             #
###############################################################################
###############################################################################
#                                                                             #
#  Copyright  2000 - 2001 Intel Corporation.                                 #
#  Intel Corporation All Rights Reserved.                                     #
#                                                                             #
#  The source code contained or described herein and all documents related to #
#  the source code ("Material") are owned by Intel Corporation or its         #
#  suppliers or licensors.  Title to the Material remains with Intel          #
#  Corporation or its suppliers and licensors.  The Material contains trade   #
#  secrets and proprietary and confidential information of Intel or its       #
#  suppliers and licensors.  The Material is protected by worldwide copyright #
#  and trade secret laws and treaty provisions.  No part of the Material may  #
#  be used, copied, reproduced, modified, published, uploaded, posted,        #
#  transmitted, distributed, or disclosed in any way without Intel's prior    #
#  express written permission.                                                #
#                                                                             #
#  No license under any patent, copyright, trade secret or other intellectual #
#  property right is granted to or conferred upon you by disclosure or        #
#  delivery of the Materials, either expressly, by implication, inducement,   #
#  estoppel or otherwise. Any license under such intellectual property rights #
#  must be express and approved by Intel in writing.                          #
#                                                                             #
###############################################################################
 
use lib '/usr/ins/intel/gui/perllib';
use VirtualDomain;
 
my @domains = VirtualDomain::list_vws();
my $log_dir;

# Iterate through all existing domains and rotate their logs 
foreach (@domains) 
{
  $log_dir = "/home/domain/$_->{vdn}/logs";
  unless (-e "$log_dir/logrotate.conf") 
  {
	eval { create_logrotate_conf($log_dir) };
	if ($@) { print "Error Occured! \nGenerated by /etc/cron.hourly/rotate_web_logs. Error Details:\n$@"; }
  }
  system("/usr/sbin/logrotate $log_dir/logrotate.conf");
}

# Now rotate logs at the default location
$log_dir = "/var/log/httpd"; 
unless (-e "$log_dir/logrotate.conf")
{
  create_logrotate_conf($log_dir);
}
system("/usr/sbin/logrotate $log_dir/logrotate.conf");

# Now rotate logs at the default location
$log_dir = "/usr/ins/intel/apache/logs"; 
unless (-e "$log_dir/logrotate.conf")
{
  create_logrotate_conf($log_dir);
}
system("/usr/sbin/logrotate $log_dir/logrotate.conf");


# Subroutine to create a logrotate config file
sub create_logrotate_conf {

my $log_dir = shift;

open(LOG_CONF, "> $log_dir/logrotate.conf") 
 or die "Cannot open $log_dir/logrotate.conf for writing. $!";
flock (LOG_CONF, 2) or die "Cannot lock $log_dir/logrotate.conf for writing. $!";
print LOG_CONF <<HERE;
# send errors to root
errors root

# create new (empty) log files after rotating old ones
create

compress

$log_dir/error.log {
	missingok
	rotate 5
	size=30M
}

$log_dir/access_log {
	missingok
	rotate 5
	size=30M
	postrotate
		/usr/bin/killall -HUP libhttpd.ep 2> /dev/null || true
	endscript
}

$log_dir/error_log {
	missingok
	rotate 5
	size=5M
	postrotate
		/usr/bin/killall -HUP libhttpd.ep 2> /dev/null || true
	endscript
}

$log_dir/agent_log {
	missingok
	rotate 5
	size=5M
	postrotate
		/usr/bin/killall -HUP libhttpd.ep 2> /dev/null || true
	endscript
}

$log_dir/referer_log {
	missingok
	rotate 5
	size=5M
	postrotate
		/usr/bin/killall -HUP libhttpd.ep 2> /dev/null || true
	endscript
}
HERE

close(LOG_CONF);
}
