B
balu
Guest
So, is this the solution you all are looking for?
(Hm, looks like I can not attach a file here? So here is the complete script
Balu
(Hm, looks like I can not attach a file here? So here is the complete script
Code:
#!/bin/sh
## The following script is meant to allow the viewing of last months awstats
## information on Plesk servers. It creates the statistics for all found
## awstats monthly data and a frameset to allow to choose a month from the
## generated ones.
##
## The main idea behind this file is to avoid any changes to the installed Plesk.
## So I'm misusing e.g. the at_domains_index.html which is first in Apaches
## DirectoryIndex configuration on Plesk servers to display the frameset, e.g.
##
## It was written on a Debian 3.1 (Sarge) and will probably not work on other
## distributions because of different paths, scripts, etc.
##
## BEWARE: This works on my box, but it might crash your system, delete all
## existing files on it and make you loose your job. So
## USE AT YOUR OWN RISK. I'M NOT RESPONSIBLE FOR ANY DAMAGE THIS
## SCRIPT DOES ON YOUR SERVERS. USE IT WITH CARE AND MAKE SURE YOU
## UNDERSTAND WHAT YOU ARE DOING.
##
## The script is fairly new and should be called "alpha" in this state,
## but it seems to works fine here.
##
## Notes:
## - The script only loops through all virtual hosts in /var/www/vhosts/
## and does not check wether awstats is enabled for the host in Plesk
## (it is for all domains on the machine this script was written for)
## - It does not update the awstats data from logfiles, but uses the
## ones that were created by awstats in the daily runs
## - it is probably best used by putting it into /etc/cron.monthly/
## (don't forget to make it executable ;)
## - it is to be run as root, so have a look at the BEWARE above
##
## 2007-04-17 - initial release
## by Thomas "Balu" Walter <[email protected]>
# set some variables
buildStaticPages="/usr/share/doc/awstats/examples/awstats_buildstaticpages.pl"
webstatDir='statistics/webstat'
# if you want to translate the title of the frameset or the text in front
# of the select box, update the following variables which are only used for the
# first creation of the files
tlStatsFor="Statistics for"
tlShowStatsFor="Show statistics for"
tlThisMonth="this month"
# awstats on debian needs this to override configdir
export AWSTATS_ENABLE_CONFIG_DIR=1
# to avoid generating of this months stats which is done by cron.daily
thisMonth=`date +%m`
thisYear=`date +%Y`
# loop through all virtual hosts
# TODO it would probably be better to fetch the awstats enabled hosts directly
# from the database, but what the heck
for vhostDir in /var/www/vhosts/*
do
# get only the virtual hosts name
vhostBaseName=`basename $vhostDir`
# don't create statistics for those
if [[ "$vhostBaseName" == "default" ]]; then
continue
fi
if [[ "$vhostBaseName" == "chroot" ]]; then
continue
fi
# create the frameset file
# I'm misusing at_domains_index.html here which is meant to be used for
# something else, but is first in Apaches DirectoryIndex setting in plesk
framesFile="$vhostDir/$webstatDir/at_domains_index.html"
echo "<html><head><title>$tlStatsFor $vhostBaseName</title>" > $framesFile
echo '</head><frameset rows="38,*">' >> $framesFile
echo '<frame src="monthchooser.html" name="monthchooser" scrolling="no" noresize="noresize" />' >> $framesFile
echo '<frame src="index.html" name="statistics" />' >> $framesFile
echo "</frameset></html>" >> $framesFile
# create the month chooser frame
frameFile="$vhostDir/$webstatDir/monthchooser.html"
echo '<html><head><title>Statistics</title></head><body>' > $frameFile
echo '<body style="font: 11px verdana, arial, helvetica, sans-serif; background: #ececec;">' >> $frameFile
echo '<form style="margin: 0; padding: 0;" action="#">' >> $frameFile
echo $tlShowStatsFor >> $frameFile
echo ' <select onChange="top.statistics.location.href = this.value;">' >> $frameFile
echo '<option value="index.html">'"$tlThisMonth"'</option>' >> $frameFile
echo '<!-- add next month here -->' >> $frameFile
echo '</select></form></body></html>' >> $frameFile
## loop through all last months statistics stored by awstats
for monthFile in $vhostDir/$webstatDir/awstats*.$vhostBaseName-http.txt
do
monthFileBaseName=`basename $monthFile`
# figure out year and month from the filename (might be solved more elegant...)
month=`echo $monthFileBaseName | sed "s%awstats\(..\)\(....\).$vhostBaseName-http.txt%\1%"`
year=`echo $monthFileBaseName | sed "s%awstats\(..\)\(....\).$vhostBaseName-http.txt%\2%"`
if [[ $month == $thisMonth && $year == $thisYear ]]; then
continue
fi
# check if the stats for the month were already generated
if [[ ! -e "$vhostDir/$webstatDir/$year-$month" ]]; then
# if not create the stats directory
mkdir "$vhostDir/$webstatDir/$year-$month"
# uncomment this if you want to see anything
# echo "Building for $vhostBaseName $year-$month"
# and build the statistics for the month
$buildStaticPages \
-config=$vhostBaseName-http \
-staticlinks \
-configdir=/opt/psa/etc/awstats \
-month=$month \
-year=$year \
-awstatsprog=/usr/lib/cgi-bin/awstats.pl \
-dir=$vhostDir/$webstatDir/$year-$month >/dev/null 2>&1
# link index.html to the newly generated stats main page
ln -sf "$vhostDir/$webstatDir/$year-$month/awstats.$vhostBaseName-http.html" \
"$vhostDir/$webstatDir/$year-$month/index.html"
fi
# add the statistics to the monthchooser
sed 's%<!-- add next month here -->%<!-- add next month here -->'"\n"'<option value="'$year'-'$month'/index.html">'$year'-'$month'</option>%' \
"$vhostDir/$webstatDir/monthchooser.html" > "$vhostDir/$webstatDir/monthchooser.html.tmp"
mv "$vhostDir/$webstatDir/monthchooser.html.tmp" "$vhostDir/$webstatDir/monthchooser.html"
done
done
Balu