Monday, January 11, 2010

awstats php script

# Use this LogFormat for limited IIS log (default log format from IIS 6)
LogFormat="date time s-sitename s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) sc-status sc-substatus sc-bytes"

Don't put line break in.

A format like this works well for IIS logs:
LogFile="C:/WINNT/system32/LogFiles/W3SVC3/ex%YY-24%MM-24%DD-24.log"

When you run awstats, you can also specify which file you want it to run on (as long as it is where you specified for the LogFile location above):

perl c:\awstats-6.5\wwwroot\cgi-bin\awstats.pl -config=mymodel -LogFile="C:/WINNT/system32/LogFiles/W3SVC3/ex061123.log" -update

Again, no line breaks.

Sample list.txt
ex090120.log
ex090121.log
ex090122.log

Run awstats.pl
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

ini_set('max_execution_time', 0); 

echo '[START] ' . date('Y-m-d H:i:s') . PHP_EOL;


$domain_name = 'www.example.com';
$log_path = 'G:/LogFiles/W3SVC2113097918';


$lines = file('list.txt');
$lineCount = count($lines);

for ($i = 0; $i < $lineCount; $i++) {
  $line = rtrim($lines[$i]);

  $cmd = 'perl D:/www/Apache2.2/cgi-bin/awstats-6.95/wwwroot/cgi-bin/awstats.pl -config=' . $domain_name . ' -LogFile="' . $log_path . '/' . $line . '" -update';
  exec($cmd);

  echo $line . PHP_EOL;
}

echo '[END] ' . date('Y-m-d H:i:s') . PHP_EOL;

echo "done";
?>

Generate Output
<?php
$domain_name = 'www.example.com';
$year = '2009';
$log_file = 'G:/LogFiles/W3SVC2113097918/ex%YY-24%MM-24%DD-24.log';


for ($i = 1; $i<=12; $i++) {
  $month = sprintf("%02d", $i);

  $cmd = 'perl D:/www/Apache2.2/cgi-bin/awstats-6.95/wwwroot/cgi-bin/awstats.pl -config=' . $domain_name . ' -LogFile="' . $log_file . '" -month=' . $month . ' -year=' . $year . ' -output -staticlinks > ' . $domain_name . '_' . $year . '-' . $month . '.html';
  exec($cmd);

  echo $month . PHP_EOL;
}
?>

No comments: