| « Changing your account name on Hotmail | Tips and tricks for searching Google » |
Plesk's Awstats show Select Period in wrong order
December 2nd, 2008Plesk generates web statistics using awstats. The “Select Period” drop down box is created with all the month dates in a random order which is not very helpful! It makes selecting the month you want tricky.
The following code checks which domains are using awstats and will re-jig their nav.html files to put them all in the correct order. Place the following program into your cron to run after awstats has completed.
The simplest way to find out when awstats completes, is to check the creation time of the nav.html file and add 30 minutes to be safe.
plesk-awstats-nav-fixer.sh:
Code:
#!/bin/bash | |
# | |
# This code was written by BigSoft Limited. | |
# Please include this message when you use the code in any form. | |
# Code comes as is and without warrenty | |
# | |
# David Newcomb (c) BigSoft Limited 2008 | |
# | |
| |
# Get MySQL's admin password | |
PSA_PW=`cat /etc/psa/.psa.shadow` | |
PSA_USER=admin | |
| |
# Location of webstats folders | |
VHOST=/var/www/vhosts | |
STATS=statistics/webstat | |
| |
# Find enabled domain names that use awstats statistics. | |
# When a client is deactivated the status of a domain | |
# becomes non-zero, so there is no need to join on the | |
# client's table. | |
| |
SQL=" SELECT d.name" | |
SQL="$SQL FROM domains d, hosting h" | |
SQL="$SQL WHERE d.id = h.dom_id" | |
SQL="$SQL AND d.status = 0" | |
SQL="$SQL AND h.webstat = 'awstats'" | |
| |
# Run SQL | |
DOMAINS=`mysql --skip-column-names -u$PSA_USER -p$PSA_PW -e"$SQL" psa` | |
| |
for DOMAIN in $DOMAINS | |
do | |
| |
AWSTATS_DIR=$VHOST/$DOMAIN/$STATS | |
| |
echo "Checking $AWSTATS_DIR" | |
if [ ! -d "$AWSTATS_DIR" ] | |
then | |
echo "Sorry awstats folder does not exist: $AWSTATS_DIR" | |
continue | |
fi | |
| |
cd $AWSTATS_DIR | |
| |
grep '^<option value="' nav.html | sort -r > nav_options.html | |
grep -v '<option value="' nav.html > nav_nooptions.html | |
| |
ed nav_nooptions.html << EOF | |
/^<\/select> | |
-1r nav_options.html | |
wq | |
EOF | |
| |
rm -f nav_options.html | |
rm -f nav.html | |
mv nav_nooptions.html nav.html | |
| |
done |
You have to love ed! It is (as far as I know) the only editor that is described as WYGIWYG (What You Get Is What You Get - wikipedia deleted my entry because no one else seems to have heard of this expression apart from me!).
This is the second problem with Plesk and awstats. I have documented Plesk Awstat forgetting about the monthly statistics here.
13 comments
./plesk-awstats-nav-fixer.sh: line 49: ed: command not found
Can you recommend any fixes?
Many thanks in advance
http://www.gnu.org/software/ed/
./plesk-awstats-nav-fixer.sh: line 60: syntax error: unexpected end of file
Any ideas?
I suspect this is related somehow in EOF marks on the file itself, as the Notepad++ seems to color the rest of the file from the first EOF line to blue.
I have tried Notepad++ as well as pico when editing, but same error occurs when trying to execute the script...
Try cut and pasting it into something like notepad. Yeh - I know, the side effect of all its faults is that it does strip away anything that is not ASCII. Then save as plain text, and you should be fine.
When you did so the editor added some indentation. In doing so it would have indented the EOF marker from the end of the "ed" script. With no marker the file terminates at the end of the file with the error.
To correct the problem move the EOF marker so that it is the only text on the line and it starts at the beginning of the line.
I have uploaded the file to here:
http://www.bigsoft.co.uk/projects/sample-code/plesk-awstats-nav-fixer.sh
You will see that the following "ed" line finishes with EOF:
ed nav_nooptions.html << EOF and all the lines to the corresponding
EOF marker are how they would appear in the file that would have been piped in.You'll have to read all about awstats yourself. Besides if Plesk is messing up the ordering already and you try to sort Jan, Feb, ... into alphabetical order they will be even more messed up!
As far as I know the month thing is not fixed in Plesk either but my solution still works as is.
I'm moving to a new server, and I'm new to Plesk. I just figured out how to migrate years of Awstats data files and was irritated by the random menu order. It's especially an issue if there are more than a couple of months. A Google search brought me here. And your script is just what I needed.
However, I too would like to have text months rather than numbers, so I set out to tweak your script. Although I do have a programming background, this is my first attempt at shell programming. There's probably a better way to do this, but here is what I came up with.
Add the following line near the top where the other variables are set. Use full months instead of abbreviated ones if you prefer.
MONTHS=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
Add the following 4 lines at line 48.
for MONTH in 1 2 3 4 5 6 7 8 9 10 11 12
do
sed -i "s/-`printf '%.2d' $MONTH`</ ${MONTHS[MONTH-1]}</g" nav_options.html
done
The above changes only the display months and not the values so it doesn't affect function at all. I haven't tested the original or edited script via cron yet, but it works from the command line.
Hope it helps someone else.

