HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Developers' Forum (http://www.howtoforge.com/forums/forumdisplay.php?f=33)
-   -   AWStats - Fix and Improvement (working) (http://www.howtoforge.com/forums/showthread.php?t=53578)

erosbk 29th July 2011 15:46

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

till 29th July 2011 15:55

Thanks for the patch. Looks like a nice solution for the problem. I've added this to the bugtracker.

erosbk 29th July 2011 16:03

Glad to help :P

You can see a testsite here to see it working (site with practacally no traffic, stats working, will test "last day" on day 2.

http://www.tecatest.com.ar/stats/

user: admin
pass: a

I will change pass tomorrow xD

erosbk 1st August 2011 13:52

Last day fix was tested and is working perfectly.

In cron_daily.php, code must be change in segment "BOC/EOC 2".

Code edited in post. (if day = 2...)

erosbk 2nd August 2011 13:20

2 Ago 2011: Modifications (code in first post has been modified)

cron_daily.php. Added check for not copy index.php, because is not needed.

in index.php, month has now +1-1. If you use date("m") it will return 08, if you REST 1 to that date, it will cut off the zero and you will have 7... I am a little ignorant about how to resolve this, so... if sum/rest 1 to moth to everytime get month with one char and not with zero :P. This was making problems with drop down list.

Please, be my guess to improve this code xD

till 2nd August 2011 13:45

you couls also try this:

$myvar = (int)date('m');

or

$myvar = intval(date('m');

erosbk 2nd August 2011 15:49

I will try with your suggestions, but I have to think a little more about because the order in the drop down list... I want order desc for the list, if I use month of only 1 char, I will get a disorder with months 1,11,12,2,3,4,5,6...x

Sorry, I have limited english xD

Please help me with this... I need that, creating or modifying a client stats program, is AWStats is selected, file ".../ispconfig/server/conf/index.php.awstats" must be copied to "...clients/.../web/stats/".

I can't find where is the function thats insert/update stat type in DB xD

Thanks in advance

till 2nd August 2011 16:35

The apache 2 plugin is executed when the stats type is changed. So the esaiest way is to add your code into the apache plugin in /usr/local/ispconfig/server/plugins-available/

erosbk 8th August 2011 09:17

Ok, looks like a final solution! be my guess to test it :P

erosbk 10th September 2011 19:24

The was a bug in the code, where if month one, it will be calculating all the time stats for month 12... code has been modified to fix this, just replace from BOC 1 to EOC 1

This is the bug code:
Code:

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

$awmonth = date("m");

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

$awyear = date("Y");

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


Replace with this code:
Code:

// 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

Original post has been updated with this changes.

Regards


All times are GMT +2. The time now is 21:56.

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