Motivation
Recently we were moving a portion of a site from one domain to another, and since the statistic were not going to have continuity, as far as domain name and paths go, we decided to archive the AWStats information into static files. AWStats comes with a utility called awstats_buildstaticpages.pl
to generate static pages, but it only does the most recent month’s data by default. We wanted to generate pages for all months we had history. Thus, this script. It started with some ideas from this post and I also gleaned some useful ideas from this help item .
#!/bin/bash
POSITIONAL =()
AWSTATS_PROG = $( which awstats.pl 2 > /dev/null)
BUILD_STATIC_PROG = $( which awstats_buildstaticpages.pl 2 > /dev/null)
OUTPUT_DIR = "./"
while [[ $# -gt 0 ]] ; do
key = " $1 "
case $key in
-a| --awstats-prog)
AWSTATS_PROG = " $2 "
shift
shift
;;
-b| --build-static-prog)
BUILD_STATIC_PROG = " $2 "
shift
shift
;;
-o| --output-dir)
OUTPUT_DIR = " $2 "
shift
shift
;;
*)
POSITIONAL +=( " $1 " ) # save it in an array for later
shift
;;
esac
done
set -- " ${ POSITIONAL [@] } " # restore positional parameters
if [[ -z $AWSTATS_PROG ]] ; then
echo "** awstats.pl not in \$PATH and no --awstats-prog argument provided"
exit 1
fi
if [[ -z $BUILD_STATIC_PROG ]] ; then
echo "** awstats_buildstaticpages.pl not in \$PATH and no --build-static-prog argument provided"
exit 1
fi
if [ " $# " -lt 2 ] ; then
echo "Usage:" ;
echo " $0 [--awstats-prog /path/to/awstats.pl] [--build-static-prog /path/to/awstats_buildstaticpages.pl] --output-dir /path/to/output/dir/ domain path_to_config_dir" ;
exit 1 ;
fi
DOMAIN = $1
CONFIG_PATH = $2
DATA_PATH = $( grep '^DirData' ${ CONFIG_PATH } /awstats.${ DOMAIN } .conf | cut -f 2 -d '=' | tr -d '"' | tr -d "'" )
cat <<EOF > $OUTPUT_DIR/index.html
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Statistics for $DOMAIN</title>
</head>
<body>
EOF
for full_path in $( ls -1tr $DATA_PATH /awstats*.$DOMAIN .txt) ; do
f = $( basename $full_path )
DATE = ${ f : 7 : 6 }
MONTH = ${ DATE : 0 : 2 }
YEAR = ${ DATE : 2 : 4 }
echo "Building ${ YEAR }${ MONTH } "
$BUILD_STATIC_PROG \
-configdir= $CONFIG_PATH \
-config= $DOMAIN \
-awstatsprog= $AWSTATS_PROG \
-dir= $OUTPUT_DIR \
-builddate= ${ YEAR }${ MONTH } \
-year= $YEAR -month= $MONTH
echo "<a href=\"awstats. $DOMAIN . ${ YEAR }${ MONTH } .html\">Stats for ${ YEAR } - ${ MONTH } </a></br>" >> $OUTPUT_DIR /index.html
done
cat <<EOF >> $OUTPUT_DIR/index.html
</body>
</html>
EOF
Comments
comments powered by Disqus