commit 59447488ccc547bee14a3e5fc431b03fc6d8b9aa
parent 91b4bd74ebb21bbdff001d139e58b53638bf6d96
Author: Oscar Benedito <oscar@oscarbenedito.com>
Date:   Fri,  4 Sep 2020 19:41:05 +0200

Configure GoAccess script with env variables

Diffstat:
Mgoaccess-run-daily.sh | 80+++++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 42 insertions(+), 38 deletions(-)

diff --git a/goaccess-run-daily.sh b/goaccess-run-daily.sh @@ -16,9 +16,13 @@ # Runs daily to generate stats with GoAccess. -OUT_DIR="/srv/stats" -DB_DIR="/srv/goaccess-db" -LOGS_PREFIX="/var/log/apache2/a-website.log" +# directory for html output +[ -z "$OUT_DIR" ] && echo "Error: variable OUT_DIR not set." && exit 1 +# directory with all the GoAccess databases +[ -z "$DB_DIR" ] && echo "Error: variable DB_DIR not set." && exit 1 +# name of log (the script appends strings ".1", ".2.gz" assuming that logrotate +# is used with delaycompress) +[ -z "$LOGS_PREFIX" ] && echo "Error: variable LOGS_PREFIX not set." && exit 1 # Shortcut to run GoAccess. # Arguments: @@ -57,27 +61,27 @@ run_goaccess() { # Runs GoAccess with a filtered accesses file and without a filter. run_goaccess_time_interval() { if [ "$#" -eq 2 ]; then - DB="$DB_DIR/$1" - FILTERED_FILE=$(mktemp) - filter_file "$LOGS_PREFIX.1" > "$FILTERED_FILE" - if [ -d "$DB" ]; then + db="$DB_DIR/$1" + filtered=$(mktemp) + filter_file "$LOGS_PREFIX.1" > "$filtered" + if [ -d "$db" ]; then mkdir -p "$OUT_DIR/$1" - run_goaccess "$FILTERED_FILE" "$OUT_DIR/$1/index.html" "$DB" 1 1 + run_goaccess "$filtered" "$OUT_DIR/$1/index.html" "$db" 1 1 else - mkdir -p "$DB" + mkdir -p "$db" mkdir -p "$OUT_DIR/$1" - run_goaccess "$FILTERED_FILE" "$OUT_DIR/$1/index.html" "$DB" 0 1 + run_goaccess "$filtered" "$OUT_DIR/$1/index.html" "$db" 0 1 fi if [ "$1" != "$2" ]; then - DB="$DB_DIR/$2" - if [ -d "$DB" ]; then + db="$DB_DIR/$2" + if [ -d "$db" ]; then mkdir -p "$OUT_DIR/$2" - run_goaccess "$FILTERED_FILE" "$OUT_DIR/$2/index.html" "$DB" 1 0 - rm -rf "$DB" + run_goaccess "$filtered" "$OUT_DIR/$2/index.html" "$db" 1 0 + rm -rf "$db" fi fi - rm "$FILTERED_FILE" + rm "$filtered" else exit 1 fi @@ -88,36 +92,36 @@ filter_file() { } # Day -TMP_FILE=$(mktemp) -TMP_FILE2=$(mktemp) -LOGS_2=$(mktemp) -OUT_DIR_TODAY="$OUT_DIR/$(date --date="yesterday" +"d/%Y/%m/%d")" +tmp=$(mktemp) +tmp2=$(mktemp) +log2=$(mktemp) +out_today="$OUT_DIR/$(date --date="yesterday" +"d/%Y/%m/%d")" -cp "$LOGS_PREFIX.2.gz" "$LOGS_2.gz" -gunzip -f "$LOGS_2.gz" +cp "$LOGS_PREFIX.2.gz" "$log2.gz" +gunzip -f "$log2.gz" -mkdir -p "$OUT_DIR_TODAY" -cat "$LOGS_2" "$LOGS_PREFIX.1" > "$TMP_FILE" -filter_file "$TMP_FILE" > "$TMP_FILE2" -run_goaccess "$TMP_FILE2" "$OUT_DIR_TODAY/index.html" -rm "$TMP_FILE" "$TMP_FILE2" "$LOGS_2" +mkdir -p "$out_today" +cat "$log2" "$LOGS_PREFIX.1" > "$tmp" +filter_file "$tmp" > "$tmp2" +run_goaccess "$tmp2" "$out_today/index.html" +rm "$tmp" "$tmp2" "$log2" # Week -TODAY="$(date +"w/%G/%V")" -YESTERDAY="$(date --date="yesterday" +"w/%G/%V")" -run_goaccess_time_interval "$TODAY" "$YESTERDAY" +today="$(date +"w/%G/%V")" +yesterday="$(date --date="yesterday" +"w/%G/%V")" +run_goaccess_time_interval "$today" "$yesterday" # Month -TODAY="$(date +"m/%Y/%m")" -YESTERDAY="$(date --date="yesterday" +"m/%Y/%m")" -run_goaccess_time_interval "$TODAY" "$YESTERDAY" +today="$(date +"m/%Y/%m")" +yesterday="$(date --date="yesterday" +"m/%Y/%m")" +run_goaccess_time_interval "$today" "$yesterday" # Year -TODAY="$(date +"y/%Y")" -YESTERDAY="$(date --date="yesterday" +"y/%Y")" -run_goaccess_time_interval "$TODAY" "$YESTERDAY" +today="$(date +"y/%Y")" +yesterday="$(date --date="yesterday" +"y/%Y")" +run_goaccess_time_interval "$today" "$yesterday" # All time -TODAY="all" -YESTERDAY="all" -run_goaccess_time_interval "$TODAY" "$YESTERDAY" +today="all" +yesterday="all" +run_goaccess_time_interval "$today" "$yesterday"