Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > ISPConfig 3 > Developers' Forum

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 29th July 2011, 16:46
erosbk erosbk is offline
Senior Member
 
Join Date: Mar 2011
Posts: 337
Thanks: 49
Thanked 36 Times in 30 Posts
Default AWStats - Fix and Improvement (working)

This will fix AWStats "last day" and will improve AWstats to keep track of every calculated month.

1) New file index.php default for every site with AWStats
Filename: /usr/local/ispconfig/server/conf/awstats/index.php.awstats
Description: This file will create a dynamic "dropdown list" with year-month format, that you can select prev stats to see them. All directories in /stats/ are readed for generate the dropdown content, and current/prev stats are loaded in a frame. Will be copied to every vhost when user/admin select AWStats as default stat program.
Advise:
$aw['aw_jump_text'] = 'Jump to previous stats: '; <--------- This text can be changed to your language... could be improved in a future using ISPConfig Lang files structures...
$aw['aw_renamed_index'] = 'fixedindex.html'; <--------- If you change this filename, remember to change it in "/usr/local/ispconfig/server/cron_daily.php" too, or this fix will not work.

Content:

Code:
<script>
function load_content(url)
{
    var iframe = document.getElementById("content");
    iframe.src = url;
}
</script>

<?php
$aw['aw_jump_text'] = 'Jump to previous stats: ';
$aw['aw_renamed_index'] = 'fixedindex.html';

if ($handle = opendir('.'))
{
        while(false !== ($file = readdir($handle)))
        {
                if (substr($file,0,1) != "." && is_dir($file))
                {
                        $orderkey = substr($file,0,4).substr($file,5,2);
                        if (substr($file,5,2) < 10 )
                        {
                                $orderkey = substr($file,0,4)."0".substr($file,5,2);
                        }
                        $awprev[$orderkey] = $file;
                }
        }

        $month = date("n");
        $year = date("Y");

        if (date("d") == 1)
        {
                $month = date("m")-1;
                if (date("m") == 1)
                {
                        $year = date("Y")-1;
                        $month = "12";
                }
        }

        $current = $year.$month;
        $awprev[$current] = $year."-".$month;

        closedir($handle);
}

echo '<div style="width: 97%; margin-left: 4px; height: 20px; background-color: #FFFFFF; position: fixed; padding: 7px; border: 2px solid #cccccc;><div align="left"><font color="#000000" size="2" face="Verdana, Arial, Helvetica,  sans-serif">' .$aw["aw_jump_text"]. '</font </div>';

echo "<select name='awdate' onchange=\"load_content(this.value)\">";
krsort($awprev);

foreach ($awprev as $key => $value)
{
        if($key == $current)
        {
                echo "<option selected=\"selected\" value=\"".$aw['aw_renamed_index']."\"> $value</option>";
        }
        else
        {
                echo "<option value='$value/".$aw['aw_renamed_index']."'> $value</option>";
        }
}

echo '</select></div><iframe src="'.$aw['aw_renamed_index'].'" frameborder="0" scrolling="Yes" width="100%" height="100%" style="margin-top:25px" id="content"></iframe>';

?>

2) Code for AWStats must be changed for this to work (and for fixing "Last month day problem")
Filename: /usr/local/ispconfig/server/cron_daily.php
Description: this will force AWStats to calculated every time for a specific month/year, and not "current month/year". Default html file of awstats will be renamed as fixedindex.html instead index.html.

Content (only AWStats part must be changed):

Code:
$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');
        $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');

// BOC 1: We need to know which month/year must be used to calculate statistics.

$awmonth = date("m"+1-1);
$awyear = date("Y");

if (date("d") == 1)
{
        $awmonth = date("m")-1;
        if (date("m") == 1)
        {
                $awyear = date("Y")-1;
                $awmonth = "12";
        }
}

// EOC 1: We have month and year vars to calculate stats at this point, to fix the last day problem

        // awstats_buildstaticpages.pl -update -config=mydomain.com -lang=es -dir=/var/www/domain.com/web/stats -awstatsprog=/path/to/awstats.pl
        // $command = "$awstats_buildstaticpages_pl -update -config='$domain' -lang=es -dir='$statsdir' -awstatsprog='$awstats_pl'";

// BOC 2: In day 2, we need to keep track of full stats for prev month, accesible for users when they need them.

        $command = "$awstats_buildstaticpages_pl -month='$awmonth' -year='$awyear' -update -config='$domain' -lang=es -dir='$statsdir' -awstatsprog='$awstats_pl'";

        if (date("d") == 2)
        {
                $awmonth = date("m")-1;
                if (date("m") == 1)
                {
                        $awyear = date("Y")-1;
                        $awmonth = "12";
                }

                $statsdirold = $statsdir."/".$awyear."-".$awmonth."/";
                mkdir($statsdirold);
                $files = scandir($statsdir);
                foreach ($files as $file)
                {
                      if (substr($file,0,1) != "." && !is_dir($file) && substr($file,0,1) != "w" && substr($file,0,1) != "i") copy("$statsdir"."/"."$file","$statsdirold"."$file");
                }
        }

// EOC 2: At this point, current month will overwrite older stats, but a copy will be kept in a separate directory, accesible through index.php

        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/fixedindex.html');
                $app->log('Created awstats statistics with command: '.$command,LOGLEVEL_DEBUG);
        } else {
                $app->log("No awstats statistics created. Either $awstats_pl or $awstats_buildstaticpages_pl is not owned by root user.",LOGLEVEL_WARN);
        }

}
3) Every time AWStats is modified index.php must be copied to vhost /stats folder.
Filename: /usr/local/ispconfig/server/plugins-available/apache2_plugin.inc.php
Description: Everytime AWStats is selected for a vhost, or password for admin is changed (for vhosts stat program), this file must be copied to vhost path. Till recommended trigger this from this file (thanks xD)

Content (At line 895, add line between "BOC 1 blocks" to "if" body)

Code:
                if($data["new"]["stats_type"] == 'awstats' && $data["new"]["type"] == "vhost") {
                        $this->awstats_update($data,$web_config);

// BOC 1: If awstats, copy file index.php.awstats to stats folder
                        unlink($data['new']['document_root']."/web/stats/index.html");
                        copy("/usr/local/ispconfig/server/conf/awstats/index.php.awstats",$data['new']['document_root']."/web/stats/index.php");
// BOC 1: If awstats, copy file index.php.awstats to stats folder

                }
4) When webalizer is set, index.php of awstats must be deleted, so, at line 134 you have to add the "unlink" command between code (in cron_daily.php):

Code:
...

        $domain = escapeshellcmd($rec['domain']);
        $statsdir = escapeshellcmd($rec['document_root'].'/web/stats');
        $webalizer = '/usr/bin/webalizer';
        $webalizer_conf_main = '/etc/webalizer/webalizer.conf';
        $webalizer_conf = escapeshellcmd($rec['document_root'].'/log/webalizer.conf');

// XXXXXXXXXXXXXXXX BOC 3: Delete index.php if awstats was working before
        unlink($statsdir.'/index.php');
// XXXXXXXXXXXXXXXX BOC 3: Delete index.php if awstats was working before

        if(!@is_file($webalizer_conf)) {
                copy($webalizer_conf_main,$webalizer_conf);
        }

...
Tested and working in production server. No problems right now!!!

If you have some errors, please post here (I am tired, maybe I forget to copy some chars... large lines are a problem with nano xD)

Use this at your own risk! :P

Best regards

Last edited by erosbk; 3rd October 2011 at 08:27. Reason: Final fix, including "index.php" copy to vhosts with AWStats!
Reply With Quote
The Following 3 Users Say Thank You to erosbk For This Useful Post:
pititis (31st July 2011), till (29th July 2011), zbuzanic (30th September 2011)
Sponsored Links
 

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ISPconfig 3 - awstats Mouton Tips/Tricks/Mods 99 27th January 2011 11:31


All times are GMT +2. The time now is 23:58.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.