#!/bin/sh
# Update all awstats sites there is a config file for
AWSTATS_BIN="/usr/lib/cgi-bin/awstats.pl"
CONF_DIR="/etc/awstats"
CONF_TEMPLATE="awstats.default"
CONF_EXT="conf"
NICENESS=16
MAX_PROCESSES=8
# Wait that many seconds when MAX_PROCESSES are COUNT_PROC
# to retry starting new update processes
RETRY=2
# ----don't edit below this line-----
# default to babbly, -q suppress output
SPEAK=true
if [ "$1" = "-q" ]; then
SPEAK=false
fi
function count_proc() {
# count how many instances are already COUNT_PROC
COUNT_PROC=$(ps -o command -u "$(id -u)" | egrep -cwe '^(/bin/sh|/usr/bin/perl)* *'"$AWSTATS_BIN")
}
function do_update() {
# call awstats
if $SPEAK; then
nice -n "$NICENESS" "$AWSTATS_BIN" -update -config="$site" &
else
# Print errors only
nice -n "$NICENESS" "$AWSTATS_BIN" -update -config="$site" > /dev/null &
fi
}
# call awstats for each configured domain
COUNT_PROC=0
CURRENT_DIR="$(pwd)"
cd "$CONF_DIR"
for conf in *.$CONF_EXT; do
notstarted=true
# control number uf update processes
while [ $notstarted = true ]; do
count_proc
if [ $COUNT_PROC -lt $MAX_PROCESSES ]; then
# exclude the template file
if [ "$conf" != "$CONF_TEMPLATE" ]; then
site="$(echo "$conf" | sed -e "s/^awstats\.\(.*\)\.conf$/\1/")"
$SPEAK && echo "`date "+%Y-%m-%d %H:%M:%S"`: Starting update for $site (already running: $COUNT_PROC)."
do_update
fi
notstarted=false
let "site_count += 1"
break;
fi
sleep "$RETRY"
done
done
cd "$CURRENT_DIR"
# wait for all processes to finish
count_proc
if [ $COUNT_PROC -gt 0 ]; then
$SPEAK && echo "`date "+%Y-%m-%d %H:%M:%S"`: All jobs started - $COUNT_PROC process(es) still runnning. Waiting..."
# wait for processes to finish
while [ $COUNT_PROC -gt 0 ]; do
sleep "$RETRY"
count_proc
done
fi
$SPEAK && echo "`date "+%Y-%m-%d %H:%M:%S"`: Finished. Statistics for $site_count sites updated."
exit 0
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>