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

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

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