#!/bin/sh # Awstats Auto Virtual Domain Config # Written by Ugo Viti # ---------------------------------------------------------------------------- # # Copyright (C) 2005 Ugo Viti # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # ---------------------------------------------------------------------------- VERSION="20050708" # Apache Directories APACHE_VIRTUAL_DIR=/var/hosting/www APACHE_LOG_DIR=/var/log/httpd # Awstats Directories AWSTATS_ROOT_DIR=/var/www/awstats AWSTATS_CONF_DIR=/etc/awstats AWSTATS_WEB_INDEX_DIR=/admin/log/http AWSTATS_WEB_CGI_DIR=/admin/log/http # Template: # ========= # SiteDomain=example.com # DirData=/var/www/virtual/example.com/logs # LogFile=/var/log/httpd/example.com-combined.log ############################################################################### create_config() { awstats_create_config() { echo "# AWStats Virtual Domain Config - Automatically created by InitZero awstats-create-config SiteDomain=\"${DOMAIN}\" DirData=\"$AWSTATS_LOG_DIR\" LogFile=\"$APACHE_LOG_DIR/${DOMAIN}-combined.log\" DirIcons=\"$AWSTATS_WEB_INDEX_DIR/icon\" HostAliases=\"www.${DOMAIN} localhost 127.0.0.1\" DirIcons=\"icon\" LogType=W LogFormat=1 CreateDirDataIfNotExists=1 SaveDatabaseFilesWithPermissionsForEveryone=0 BuildHistoryFormat=xml ShowFlagLinks=\"it en fr de es\" DNSLookup=1 ShowAuthenticatedUsers=PHBL LoadPlugin=\"tooltips\" ShowScreenSizeStats=1 ShowMiscStats=1 LoadPlugin="hostinfo" #MiscTrackerUrl="/stats/tracker.js" #LoadPlugin="graphapplet /admin/log/http/classes" $(if [ ! -z "$(find $APACHE_HTDOCS_DIR/admin -type f -name .htaccess)" ] then echo AllowAccessFromWebToAuthenticatedUsersOnly=1 echo AllowAccessFromWebToFollowingAuthenticatedUsers="$(awstats_auth_check)" fi)" } dir_check() { # Return Codes # 0 = Directory exist, it is writable and executable # 1 = Directory doesn't exist or it isn't writable/executable if [[ -d "$1" && -w "$1" && -x "$1" ]] then return 0; else echo "The directory \"$1\" doesn't exist or it isn't writable/executable" return 1; fi } awstats_create_web_files() { USR=$(ls -ld $APACHE_HTDOCS_DIR | awk '{print $3}') GRP=$(ls -ld $APACHE_HTDOCS_DIR | awk '{print $4}') #echo "DOMAIN=$DOMAIN USR=$USR GRP=$GRP" #echo "su $USR -s /bin/bash -c \"mkdir -p $AWSTATS_INDEX_DIR\"" #echo "su $USR -s /bin/bash -c \"awstats_create_web_files > $AWSTATS_INDEX_FILE\"" #su $USR -s /bin/bash -c "mkdir -p $AWSTATS_INDEX_DIR" mkdir -p $AWSTATS_INDEX_DIR chown -R $USR:$GRP $APACHE_HTDOCS_DIR/admin # cgi-bin Management cd $AWSTATS_CGI_DIR ln -sf $AWSTATS_ROOT_DIR/awstats.pl ; for FILE in $(find $AWSTATS_ROOT_DIR/* -maxdepth 0 -type d | grep -v icon) ; do ln -sf $FILE; done # Web Index Dir Management cd $AWSTATS_INDEX_DIR ln -sf $AWSTATS_ROOT_DIR/icon #index.html contents using frame to hide echo "" > $AWSTATS_INDEX_FILE #echo "" > $AWSTATS_INDEX_FILE chown $USR:$GRP $AWSTATS_INDEX_FILE #echo -e "Options +ExecCGI -Indexes\nAddHandler cgi-script .pl\nDirectoryIndex index.html awstats.pl" >$AWSTATS_HTPASSWD_FILE #chown $USR:$GRP $AWSTATS_HTPASSWD_FILE #rm -f $AWSTATS_HTPASSWD_FILE } awstats_auth_check() { for HTACCESS in $(find $APACHE_HTDOCS_DIR/admin -type f -name .htaccess) do echo -n "$(cat $HTACCESS | grep "Require user" | sed 's/Require user//g' | awk '{ for(i=1 ; i<=NF ; i=i+1) printf " "$i}') " done } create_host_config() { APACHE_DOMAIN_DIR=$APACHE_VIRTUAL_DIR/$DOMAIN APACHE_HTDOCS_DIR=$APACHE_DOMAIN_DIR/htdocs AWSTATS_LOG_DIR=$APACHE_DOMAIN_DIR/logs AWSTATS_CONF_FILE=$AWSTATS_CONF_DIR/awstats.${DOMAIN}.conf AWSTATS_CGI_DIR=$APACHE_HTDOCS_DIR/$(echo $AWSTATS_WEB_CGI_DIR | sed 's/^\///') AWSTATS_INDEX_DIR=$APACHE_HTDOCS_DIR/$(echo $AWSTATS_WEB_INDEX_DIR | sed 's/^\///') AWSTATS_INDEX_FILE=$AWSTATS_INDEX_DIR/index.html AWSTATS_HTPASSWD_FILE=$AWSTATS_INDEX_DIR/.htaccess if [[ -f "$AWSTATS_CONF_FILE" && "$1" != "--force" ]] then #echo "Non aggiorno il file $AWSTATS_CONF_FILE" return 0; else #echo "Aggiorno il file $AWSTATS_CONF_FILE" # Sanity Checks dir_check $APACHE_DOMAIN_DIR ; [ $? -eq 1 ] && exit 1; dir_check $APACHE_HTDOCS_DIR ; [ $? -eq 1 ] && exit 1; dir_check $AWSTATS_ROOT_DIR ; [ $? -eq 1 ] && exit 1; dir_check $AWSTATS_LOG_DIR ; [ $? -eq 1 ] && exit 1; #dir_check $AWSTATS_INDEX_DIR ; [ $? -eq 1 ] && exit 1; dir_check $APACHE_HTDOCS_DIR ; [ $? -eq 0 ] && awstats_create_web_files dir_check $AWSTATS_LOG_DIR ; [ $? -eq 0 ] && awstats_create_config > $AWSTATS_CONF_FILE cd $APACHE_VIRTUAL_DIR fi } cd $APACHE_VIRTUAL_DIR if [ -z "$1" ] || [[ "$1" = "--force" && -z "$2" ]] then for DOMAIN in $(find -maxdepth 1 -type d | sed -e 's/.\///' | grep -wv ^"."$ | sort) do if [ -d "$DOMAIN/htdocs" ] then #echo -n "Domain: $DOMAIN" create_host_config $1 fi done else if [ "$1" = "--force" ] then DOMAIN=$2 create_host_config $1 else DOMAIN=$1 create_host_config $2 fi fi } create_config $1 $2