webalizer/generate_webstats.sh

210 lines
4.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# - Script generates webalizer webstatistics
# -
# - usage: generate_webstats.sh [<site1> [<site2> ... ]]
# -
# - Webstatistics for the given sites will be created, where 'site<n>' 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=($(</proc/$PPID/stat))
_PPPID="${_stat[3]}"
_COMMAND="$(ps -o cmd= $_PPPID)"
if [[ "$_COMMAND" =~ /usr/sbin/logrotate ]]; then
invoked_from_logrotate=true
else
invoked_from_logrotate=false
fi
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
if [[ "${_websites_log_file##*.}" = "log" ]]; then
warn "${_site}: Maybe you are going to inspect an empty - because just even rotated - logfile."
fi
fi
echononl " Generate statistics for site '${_site}'.."
rm -f "$_CACHE_FILE"
# - Take care log file is not empty
# -
if [[ ! -s "${_websites_log_file}" ]] ; then
echo_skipped
if ! $terminal ; then
warn "No web-statistics for site '${_site}' generated!
LogFile '${_websites_log_file}' not found or empty."
fi
continue
fi
$WEBALSIZER_BINARY -Q -p -M4 -N20 -D "$_CACHE_FILE" -c "${_conf_file}" > $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