#!/usr/bin/env bash # - Script generates webalizer webstatistics # - # - usage: generate_webstats.sh [ [ ... ]] # - # - Webstatistics for the given sites will be created, where 'site' must match # - the 'HostName' parameter of the webalizer configuration. # - # - If no commandline is parameter given, statistics for all sites (which has a webalizer # - configuration) will be generated. # - _src_base_dir="$(dirname $(realpath $0))" conf_file="${_src_base_dir}/conf/webalizer.conf" tmp_dir="$(mktemp -d)" log_file="$(mktemp)" # ------------- # - Functions # ------------- clean_up() { # Perform program exit housekeeping rm -f "$log_file" rm -rf "$tmp_dir" if $terminal ; then echo "" fi exit $1 } ## - Check if a given array (parameter 2) contains a given string (parameter 1) ## - containsElement () { local e for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done return 1 } echononl(){ if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n "$*\\c" 1>&2 else echo -e -n "$*" 1>&2 fi rm /tmp/shprompt$$ fi } info (){ if $terminal ; then echo "" echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" echo "" fi } warn (){ if $terminal ; then echo "" echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" echo "" else echo "" echo "[ Warning ]: $*" echo "" fi } error(){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mFehler\033[m ]: $*" else echo "[ Error ]: $*" fi echo "" } echo_done() { if $terminal ; then echo -e "\033[75G[ \033[32mdone\033[m ]" fi } echo_failed(){ if $terminal ; then echo -e "\033[75G[ \033[1;31mfailed\033[m ]" fi } echo_skipped() { if $terminal ; then echo -e "\033[75G[ \033[37mskipped\033[m ]" fi } # ------------- # --- Check some prerequisites # ------------- # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true else terminal=false fi declare -a given_site_arr # - Is a site given on command line? # - if [[ $# -gt 0 ]]; then for _site in "$@" ; do given_site_arr+=("$_site") done fi # ------------- # --- Default values # ------------- DEFAULT_WEBALSIZER_BINARY="/usr/local/webalizer/bin/webalizer" DEFAULT_WEBALSIZER_CONF_DIR="/usr/local/webalizer/etc" # ------------- # --- Read Configurations from $conf_file # ------------- if [[ -f "$conf_file" ]]; then source "$conf_file" fi [[ -n "$WEBALSIZER_BINARY" ]] || WEBALSIZER_BINARY="$DEFAULT_WEBALSIZER_BINARY" [[ -n "$WEBALSIZER_CONF_DIR" ]] || WEBALSIZER_CONF_DIR="$DEFAULT_WEBALSIZER_CONF_DIR" _CACHE_FILE="${tmp_dir}/dns_cache.db" # - Check if script was invoked from logrotation script # - # - Note: Its the parent of the parent # - _stat=($( $log_file 2>&1 if [[ $? -ne 0 ]]; then if ! $terminal ; then echo "[ Error ]: Generatin webstatistics for site '${_site}' failed!" fi echo_failed error "$(cat "$log_file")" else echo_done fi done < <(find "${WEBALSIZER_CONF_DIR}" -maxdepth 1 -type f -name "*.conf" -print0) clean_up 0