View Single Post
  #2  
Old 20th July 2011, 14:55
erosbk erosbk is offline
Senior Member
 
Join Date: Mar 2011
Posts: 337
Thanks: 49
Thanked 36 Times in 30 Posts
Default

As I could see, reinstalling everything in a VM, ISPConfig is not calculating last day of previous month, directly calculating the new month showing all values in zero...

I will follow in this post what I wrote here:
http://www.howtoforge.com/forums/showthread.php?t=53282

I added this code to cron_daily, in a VM using log files and previous stats from a production server but... when I calculate last day for a previous month, all previous data is removed...

I will test this in 10 days in my production server to see if it works as it should... but I think that I will have the same results... so, please help, I think that this could be great for clients to see preivous month statistics and compare between months.

--- with this script right now, every first day of month, stats will be calculated twice, one for current month, one for previous month (to complete last day). This can be modified to always calculate one time only, and day 2 copy files to "previous month folder" BEFORE running stats calculation... ---

Code:
#######################################################################################################
// Create awstats statistics
#######################################################################################################

$sql = "SELECT domain_id, domain, document_root FROM web_domain WHERE stats_type = 'awstats' AND server_id = ".$conf['server_id'];
$records = $app->db->queryAllRecords($sql);

$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');

foreach($records as $rec) {
	//$yesterday = date('Ymd',time() - 86400);
	$yesterday = date('Ymd',strtotime("-1 day", time()));
	$logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday.'-access.log');
	if(!@is_file($logfile)) {
		$logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday.'-access.log.gz');
		if(!@is_file($logfile)) {
			continue;
		}
	}

	$domain = escapeshellcmd($rec['domain']);
	$statsdir = escapeshellcmd($rec['document_root'].'/web/stats');
// BOC 1: Copy previous month statistics to dir /[year][month]/*
        if (date("d") == 1)
        {
                $statsprev = '/'.date("Y").(date("m")-1);
                if (date("m") == 1)
                {
                        $statsprev = '/'.(date("Y")-1).'12';
                }
        }
	$statsdirold = $statsdir.$statsprev."/";
	mkdir($statsdirold);
	$files = scandir($statsdir);
	foreach ($files as $file)
	{
              if (substr($file,0,1) != "." && substr($file,0,1) != "2" ) copy("$statsdir"."/"."$file", "$statsdirold"."$file");
	}
// EOC 1:


	$awstats_pl = $web_config['awstats_pl'];
	$awstats_buildstaticpages_pl = $web_config['awstats_buildstaticpages_pl'];
	$awstats_conf_dir = $web_config['awstats_conf_dir'];
	$awstats_website_conf_file = $web_config['awstats_conf_dir'].'/awstats.'.$domain.'.conf';
	
	if(is_file($awstats_website_conf_file)) unlink($awstats_website_conf_file);
	
	if(!is_file($awstats_website_conf_file)) {
		$awstats_conf_file_content = 'Include "'.$awstats_conf_dir.'/awstats.conf"
LogFile="/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log"
SiteDomain="'.$domain.'"
HostAliases="www.'.$domain.' localhost 127.0.0.1"';
		file_put_contents($awstats_website_conf_file,$awstats_conf_file_content);
	}
	
	if(!@is_dir($statsdir)) mkdir($statsdir);
	if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
	symlink($logfile,'/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
	
	// awstats_buildstaticpages.pl -update -config=mydomain.com -lang=en -dir=/var/www/domain.com/web/stats -awstatsprog=/path/to/awstats.pl
	$command = "$awstats_buildstaticpages_pl -update -config='$domain' -lang=en -dir='$statsdir' -awstatsprog='$awstats_pl'";
	
	if($awstats_pl != '' && $awstats_buildstaticpages_pl != '' && fileowner($awstats_pl) == 0 && fileowner($awstats_buildstaticpages_pl) == 0) {
		exec($command);
		rename($rec['document_root'].'/web/stats/awstats.'.$domain.'.html',$rec['document_root'].'/web/stats/index.html');
		$app->log('Created awstats statistics with command: '.$command,LOGLEVEL_DEBUG);

// BOC 2: Calculate last day for previous month. -month and -year must be variables, right now are fixed values for testing purpose
	        if (date("d") == 1)
	        {
		        $command = "$awstats_buildstaticpages_pl -month=06 -year=2011 -update -config='$domain' -lang=en -dir='$statsdirold' -awstatsprog='$awstats_pl'";
        	        exec($command);
                	rename($rec['document_root'].'/web/stats'.$statsprev.'/'.'awstats.'.$domain.'.html',$rec['document_root'].'/web/stats/'.$statsprev.'/'.'index.html');
	        }
// EOC 2:

// BOC 3: Add code here to create a index.html with dropdown list to select year/month to view, and point selection to directory. This can be done for ispconfig when AWStats are selected as web stats type, and create one time as static index.html, creating drop down reading directories dinamically...
// EOC 3:

	} else {
		$app->log("No awstats statistics created. Either $awstats_pl or $awstats_buildstaticpages_pl is not owned by root user.",LOGLEVEL_WARN);
	}
	
}

Last edited by erosbk; 20th July 2011 at 14:59.
Reply With Quote