Results 1 to 8 of 8

Thread: AW Stats Code

  1. #1
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,252
    Thanks
    173
    Thanked 303 Times in 230 Posts

    AW Stats Code

    Some code to spit out awstats config files from your Apache vhosts file ("hosts.conf") based on a template ("awstats.conf")
    PHP Code:
    <?php
    /* * Generates awstats config files * LogFile="C:/Apache24/logs/mysite-access.log" * SiteDomain="mysite.com" */
    $hosts file_get_contents("hosts.conf");$conf file_get_contents("awstats.conf");
    preg_match_all("@ServerName ([.\-a-z1-9]+)@"$hosts$matches);
    preg_match_all("@CustomLog logs/([-a-z1-9]+)-access.log combined@"$hosts$matches2);
    foreach(
    $matches[1] as $key => $domain){    $content sprintf("LogFile=\"C:/Apache24/logs/%s-access.log\"\nSiteDomain=\"%s\"\n%s"$matches2[1][$key], $domain$conf);    file_put_contents(sprintf("awstats.%s.conf"$domain), $content);}?>

  2. #2
    TopDogger's Avatar
    TopDogger is offline Über Hund
    Join Date
    Jan 2009
    Location
    Hellfire, AZ
    Posts
    3,050
    Thanks
    345
    Thanked 909 Times in 694 Posts
    Where does the PHP code get placed? Is this a stand-alone script that is run with a cron job?

    What exactly does it spit out? Is it raw data?

    I have been looking for a way to automatically e-mail AwStats data one the first of every month.
    "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- Benjamin Franklin


  3. #3
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,252
    Thanks
    173
    Thanked 303 Times in 230 Posts
    This is a script I wrote to generate the log configuration files based on what I had set up in my virtual hosts file (I renamed that file to hosts.conf). So it's a script to run one-time to save a lot of manual text editing.

    To automatically e-mail stats data on the first of every month, you can send the website access log file by email.

    You could set up a cron job to run a PHP script such as this:

    PHP Code:
    $stats file_get_contents("location of the access log file");
    mail('the destination email box''Latest Stats for' $_SERVER['HTTP_HOST'], $stats); 
    And give the PHP script a cryptic name, and locate it above the public HTML directory area for security purposes.

    Another idea would be to capture the HTML output of the AW Stats URL and send this via e-mail to a client. The above script may even work for this too. Otherwise, look at using CURL to capture the data.

    The visual stats URL is something like this:
    Code:
    http://server/cgi-bin/awstats/awstats.pl?config=website.com
    On Apache, the stats packages operate on the server access log files. And the stats script is run by a timer to process the raw log data. Then the user views the stats visually via a URL to a perl script in the cgi-bin.

  4. Thanked by:

    TopDogger (24 April, 2013)

  5. #4
    TopDogger's Avatar
    TopDogger is offline Über Hund
    Join Date
    Jan 2009
    Location
    Hellfire, AZ
    Posts
    3,050
    Thanks
    345
    Thanked 909 Times in 694 Posts
    Interesting. I will have to try this.

    When I did some research on this topic several months ago, numerous articles and forum threads said that AwStats data could not be extracted and e-mailed.
    "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- Benjamin Franklin


  6. #5
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,252
    Thanks
    173
    Thanked 303 Times in 230 Posts
    Another idea is to log into cPanel and find out the URL for the stats that you want to see. Then use the PHP Curl function with login credentials (username + password) to grab the HTML contents of this URL. This should work with shared hosting accounts where you don't get access to the server log files via PHP scripts.

    Maybe of some help?:
    PHP Code:
    class Connector{
        public 
    $cpanelUser;    public $cpanelPassword;    public $domain;    public $bcName;    private $curlDone;
        public function 
    tryCurl($url$pdata null$usecookies false) {        if (strpos($url':2082/') > 0str_replace('/www.''/'$url);        $c curl_init($url);        if (!empty($pdata)) {            foreach ($pdata as $k => $p$pdata[$k] = preg_replace('/^@/''\@'$p);            curl_setopt($cCURLOPT_POST1);            curl_setopt($cCURLOPT_POSTFIELDS$pdata);        }        if ($usecookies) {            curl_setopt($cCURLOPT_COOKIEJAR"cookies.txt");  //initiates cookie file if needed            curl_setopt($c, CURLOPT_COOKIEFILE, "cookies.txt");        }        curl_setopt($c, CURLOPT_USERPWD, $this->cpanelUser . ':' . $this->cpanelPassword);        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);        //curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);        $result = curl_exec($c);        if ($result === false) $result = curl_error($c); else $this->curlDone = true;        curl_close($c);        return $result;    }
        
    public function doCurl($url$pdata null$usecookies false) {    $tries 0;        $this->curlDone false;    do {            $result $this->tryCurl($url$pdata$usecookies);            if ($this->curlDone) break;            sleep(2); // Wait for 2 seconds            $tries++;    } while ($tries < 4);        return $result;    }} 

  7. #6
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,252
    Thanks
    173
    Thanked 303 Times in 230 Posts
    And another way may be to automate the Internet Explorer browser on Windows using AutoIt - AutoItScript i.e. simulate how you would view your web stats and capture the web page source to send via email. And set up the script as a scheduled task.

  8. #7
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,252
    Thanks
    173
    Thanked 303 Times in 230 Posts
    Also, with a GUI automation script, you could e-mail a screen capture image if the HTML doesn't render well for the remote recipient.

    btw. one reason for stats data being given restricted access is to combat referral spam where spammers inject their link to the stats (via automated page requests) that may be accessible by a search engine robot.

    On VPS or dedicated servers, it should be easy to get hold of the stats data to send in an e-mail with a cgi script.

  9. #8
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,252
    Thanks
    173
    Thanked 303 Times in 230 Posts
    AW Stats Download Script

    Here is the workflow for the script development:

    * Login to cPanel using Curl
    * Download the HTML of the AW Stats page
    * Parse the required data
    * Produce the stats file to email to clients or whatever

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •