webalizer/generate_webstats.sh
2017-11-17 21:20:54 +01:00

179 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
_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
}
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 "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
fi
}
warn (){
if $terminal ; then
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
else
echo "Warning: $*"
fi
}
error(){
echo ""
if $terminal ; then
echo -e "\t[ \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
# -------------
# --- 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"
#/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
# -
# - 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 '$(hostname -f)'"
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}')"
# - 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
echo "[ Warning ]: No web-statistics for site '${_site}' generated!"
echo " 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