Create stat parsing script to review website visits

In this article I’m going to show you how to create a stat parsing script, which will allow you to get a quick historical view of your website’s daily visitor statistics.

Sometimes it’s nice to be able to see at a glance the overall level of traffic that your website receives. This can help you see if an average increase in your level of traffic might be the cause of recent resource usage problems you’ve been having.

Create bash stat parsing script

Using the steps below I’ll show you how to create a bash stat parsing script that will go through the Webalizer logs stored on your account and put all of the information into a single HTML report for you.

  1. Login to your cPanel.
  2. Under the Files section, click on File Manager, then select Home Directory and click on Go.
  3. At the top-left, click on + New File, name the file genSTATS and click on Create New File.
  4. Now right-click on the genSTATS file and click on Edit.

    Now enter in the following code, making sure to edit user=userna5 with your own cPanel username:

    #!/bin/bash

    user=userna5
    logPath=/home/$user/tmp/webalizer/

    rm -rf /home/$user/STATS.htm

    for log in `find $logPath -type f -name “usage*” -exec grep “daily_usage_” {} ; | sed ‘s#_# #g’ | awk ‘{print substr($4,1,6)}’ | sort -n`; do awk /”Daily usage for “/,/\/TABLE/ $logPath”usage_”$log”.html” | grep -v “IMG SRC” >> /home/$user/STATS.htm; done

    Then click on Save Changes at the top-right.

Create PHP script to run stat script

Now that you’ve already created a bash script to compile your stats into a single HTML report, we can also create a PHP script that will allow you to run the bash script after providing a username and password, and view the compiled HTML report.

  1. Login to your cPanel.
  2. Under the Files section, click on File Manager, then select Web Root and click on Go.
  3. At the top-left, click on + New File, name the file genSTATS.php and click on Create New File.
  4. Now right-click on the genSTATS.php file and click on Edit.

    Enter in the following code, making sure to edit the /home/userna5/genSTATS section with your own username.

    <?php
    $user = $_POST[‘user’];
    $pass = $_POST[‘pass’];

    if($user == “admin”
    && $pass == “pass”)
    {
    exec (“/bin/bash /home/userna5/genSTATS”);
    include(“../STATS.htm”);

    }
    if(isset($_POST))
    {?>

    <form method=”POST” action=”genSTATS.php”>
    User <input type=”TEXT” name=”user”></input>
    Pass <input type=”TEXT” name=”pass”></input>
    <input type=”submit” name=”submit”></input>
    </form>
    <?}
    ?>

  5. Now when you access the PHP script on your website http://example.com/genSTATS.php, you’ll be prompted with a username and password prompt, these need to match what is set in the PHP script for $user == “admin”, and $pass = “pass”. After filling in these credentials, click on Submit and you should then see the full stats report.
    gen stats fill in credentials click on submit

    gen stats view report

You should now know how to create a stat parsing script on your account, so that you can get a quick glance of your daily visits very easily.

Leave a Reply