Annotation of badi/public_scripts/awstats_update_all/awstats_update_all, revision 1.4

1.1       adi         1: #!/bin/sh
                      2: 
1.3       adi         3: # Update all awstats sites there is a config file for
                      4: # This script runs awstats -update for all sites a config file
                      5: # exists in the awstats config dir. It runs the update process in parallel
                      6: # for several sites at once. Give the command line argument "-q" to suppress
                      7: # all but error output.
1.1       adi         8: 
1.4     ! adi         9: # (c) 2010 under GPL v2 by Adrian Zaugg.
1.1       adi        10: 
                     11: 
1.4     ! adi        12: # awstats main script
1.1       adi        13: AWSTATS_BIN="/usr/lib/cgi-bin/awstats.pl"
1.4     ! adi        14: 
        !            15: # awstats configuration directory
1.1       adi        16: CONF_DIR="/etc/awstats"
1.3       adi        17: 
                     18: # normal awstats config files have this extension
1.1       adi        19: CONF_EXT="conf"
1.3       adi        20: 
1.4     ! adi        21: # File to exclude
        !            22: # Exclude this file, which isn't a config file for awstats, but
        !            23: # also resides in the config directory. Note: Only
        !            24: # files with an extension $CONF_EXT are considered anyway. 
        !            25: CONF_TEMPLATE="awstats.default"
        !            26: 
        !            27: # Run with this priority (0: normal - 19: friendly)
1.1       adi        28: NICENESS=16
1.3       adi        29: 
1.4     ! adi        30: # Maximum awstats update processes to run at once
1.1       adi        31: MAX_PROCESSES=8
1.3       adi        32: 
1.4     ! adi        33: # Wait that many seconds when MAX_PROCESSES are running 
1.1       adi        34: # to retry starting new update processes
                     35: RETRY=2
                     36: 
                     37: 
1.4     ! adi        38: 
1.1       adi        39: # ----don't edit below this line-----
                     40: 
                     41: # default to babbly, -q suppress output
                     42: SPEAK=true
                     43: if [ "$1" = "-q" ]; then
                     44:        SPEAK=false
                     45: fi
                     46: 
                     47: function count_proc() {
1.4     ! adi        48:        # count how many instances are already running
1.1       adi        49:        COUNT_PROC=$(ps -o command -u "$(id -u)" | egrep -cwe '^(/bin/sh|/usr/bin/perl)* *'"$AWSTATS_BIN")
                     50: }
                     51: 
                     52: function do_update() {
                     53:        # call awstats
                     54:        if $SPEAK; then 
                     55:                nice -n "$NICENESS" "$AWSTATS_BIN" -update -config="$site" &
                     56:        else
                     57:                # Print errors only
                     58:                nice -n "$NICENESS" "$AWSTATS_BIN" -update -config="$site" > /dev/null &
                     59:        fi
                     60: }
                     61: 
                     62: 
                     63: # call awstats for each configured domain
                     64: COUNT_PROC=0
                     65: CURRENT_DIR="$(pwd)"
                     66: cd "$CONF_DIR"
                     67: for conf in *.$CONF_EXT; do
                     68: 
1.4     ! adi        69:        notstarted=true
1.1       adi        70:         
                     71:        # control number uf update processes
1.4     ! adi        72:        while [ $notstarted = true ]; do
        !            73:                count_proc
        !            74:                if [ $COUNT_PROC -lt $MAX_PROCESSES ]; then
        !            75:                        # exclude the template file
1.1       adi        76:                        if [ "$conf" != "$CONF_TEMPLATE" ]; then
                     77:                                site="$(echo "$conf" | sed -e "s/^awstats\.\(.*\)\.conf$/\1/")"
                     78:                                $SPEAK && echo "`date "+%Y-%m-%d %H:%M:%S"`: Starting update for $site (already running: $COUNT_PROC)."
                     79:                                do_update
1.4     ! adi        80:                        fi
        !            81:                        notstarted=false
1.1       adi        82:                        let "site_count += 1"
                     83:                        break;
                     84:                fi
                     85: 
                     86:                sleep "$RETRY"
1.4     ! adi        87:        done
1.1       adi        88: 
                     89: done
                     90: cd "$CURRENT_DIR"
                     91: 
1.4     ! adi        92: 
1.1       adi        93: # wait for all processes to finish
                     94: count_proc
                     95: if [ $COUNT_PROC -gt 0 ]; then
1.4     ! adi        96:        $SPEAK && echo "`date "+%Y-%m-%d %H:%M:%S"`: All jobs started - $COUNT_PROC process(es) still runnning. Waiting..." 
1.1       adi        97:         
1.4     ! adi        98:        # wait for processes to finish
        !            99:        while [ $COUNT_PROC -gt 0 ]; do
        !           100:                sleep "$RETRY"
1.1       adi       101:                count_proc
1.4     ! adi       102:        done
1.1       adi       103: fi
                    104: 
                    105: $SPEAK && echo "`date "+%Y-%m-%d %H:%M:%S"`: Finished. Statistics for $site_count sites updated."
                    106: exit 0

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>