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.
Reply
 
Thread Tools Display Modes
  #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
  #2  
Old 29th July 2011, 16:55
till till is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lneburg, Germany
Posts: 36,796
Thanks: 840
Thanked 5,613 Times in 4,424 Posts
Default

Thanks for the patch. Looks like a nice solution for the problem. I've added this to the bugtracker.
__________________
Till Brehm
--
Get ISPConfig support and the ISPConfig 3 manual from ispconfig.org.
Reply With Quote
The Following User Says Thank You to till For This Useful Post:
robertoshulze2257 (15th November 2014)
  #3  
Old 29th July 2011, 17:03
erosbk erosbk is offline
Senior Member
 
Join Date: Mar 2011
Posts: 337
Thanks: 49
Thanked 36 Times in 30 Posts
Default

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
Reply With Quote
  #4  
Old 1st August 2011, 14:52
erosbk erosbk is offline
Senior Member
 
Join Date: Mar 2011
Posts: 337
Thanks: 49
Thanked 36 Times in 30 Posts
Default

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...)
Reply With Quote
  #5  
Old 2nd August 2011, 14:20
erosbk erosbk is offline
Senior Member
 
Join Date: Mar 2011
Posts: 337
Thanks: 49
Thanked 36 Times in 30 Posts
Default

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
Reply With Quote
The Following User Says Thank You to erosbk For This Useful Post:
till (2nd August 2011)
  #6  
Old 2nd August 2011, 14:45
till till is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lneburg, Germany
Posts: 36,796
Thanks: 840
Thanked 5,613 Times in 4,424 Posts
Default

you couls also try this:

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

or

$myvar = intval(date('m');
__________________
Till Brehm
--
Get ISPConfig support and the ISPConfig 3 manual from ispconfig.org.
Reply With Quote
  #7  
Old 2nd August 2011, 16:49
erosbk erosbk is offline
Senior Member
 
Join Date: Mar 2011
Posts: 337
Thanks: 49
Thanked 36 Times in 30 Posts
Default

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
Reply With Quote
  #8  
Old 2nd August 2011, 17:35
till till is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lneburg, Germany
Posts: 36,796
Thanks: 840
Thanked 5,613 Times in 4,424 Posts
Default

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/
__________________
Till Brehm
--
Get ISPConfig support and the ISPConfig 3 manual from ispconfig.org.
Reply With Quote
The Following User Says Thank You to till For This Useful Post:
erosbk (8th August 2011)
  #9  
Old 8th August 2011, 10:17
erosbk erosbk is offline
Senior Member
 
Join Date: Mar 2011
Posts: 337
Thanks: 49
Thanked 36 Times in 30 Posts
Default

Ok, looks like a final solution! be my guess to test it :P
Reply With Quote
  #10  
Old 10th September 2011, 20:24
erosbk erosbk is offline
Senior Member
 
Join Date: Mar 2011
Posts: 337
Thanks: 49
Thanked 36 Times in 30 Posts
 
Default

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
Reply With Quote
The Following 2 Users Say Thank You to erosbk For This Useful Post:
falko (11th September 2011), till (12th September 2011)
Reply

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 08:00.


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