From 85be34df8469237d3cb9a52ef8b5e47551b4d407 Mon Sep 17 00:00:00 2001 From: Christoph Date: Sat, 18 Nov 2017 22:33:12 +0100 Subject: [PATCH] generate_webstats.sh: supports generating statistics only for given site(s). --- generate_webstats.sh | 61 +++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/generate_webstats.sh b/generate_webstats.sh index d0882c6..e158784 100755 --- a/generate_webstats.sh +++ b/generate_webstats.sh @@ -1,5 +1,16 @@ #!/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" @@ -23,6 +34,14 @@ clean_up() { 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$$ @@ -37,25 +56,27 @@ echononl(){ info (){ if $terminal ; then echo "" - echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*" + echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" echo "" fi } warn (){ if $terminal ; then echo "" - echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" echo "" else - echo "Warning: $*" + echo "" + echo "[ Warning ]: $*" + echo "" fi } error(){ echo "" if $terminal ; then - echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*" + echo -e " [ \033[31m\033[1mFehler\033[m ]: $*" else - echo "Error: $*" + echo "[ Error ]: $*" fi echo "" } @@ -92,6 +113,17 @@ 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 # ------------- @@ -112,13 +144,6 @@ fi _CACHE_FILE="${tmp_dir}/dns_cache.db" -#/bin/rm -f ${TMP_DIR}/$CACHE_FILE -# -#for i in /var/log/apache/*.log; do -# -# /usr/local/webalizer/bin/webazolver -Q -o ${TMP_DIR} -N20 -D $CACHE_FILE $i -# -#done # - Check if script was invoked from logrotation script # - @@ -133,13 +158,19 @@ else invoked_from_logrotate=false fi -info "Generate web-statistics at host '$(hostname -f)'" +info "Generate web-statistics at host \033[1m$(hostname -f)\033[m" while IFS='' read -r -d '' _conf_file ; do _websites_log_file="$(grep -E "^\s*LogFile" $_conf_file | awk '{print$2}')" _site="$(grep -e "^\s*HostName" $_conf_file | awk '{print$2}')" + + if [[ ${#given_site_arr[@]} -gt 0 ]] ; then + containsElement "$_site" "${given_site_arr[@]}" || continue + fi + + # - Warn if script was called from logrotation script # - if $invoked_from_logrotate ; then @@ -156,8 +187,8 @@ while IFS='' read -r -d '' _conf_file ; do if [[ ! -s "${_websites_log_file}" ]] ; then echo_skipped if ! $terminal ; then - echo "[ Warning ]: No web-statistics for site '${_site}' generated!" - echo " LogFile "${_websites_log_file}" not found or empty." + warn "No web-statistics for site '${_site}' generated! + LogFile '${_websites_log_file}' not found or empty." fi continue fi