diff --git a/.gitignore b/.gitignore index 7dd497e..d24ba49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,9 @@ -/BAK/* +# - common +*.log *.swp +conf/*.conf + +/BAK/* php-* log_* !php-5.6-libssl-1.1* diff --git a/conf/organize_php_error_logs.conf.sample b/conf/organize_php_error_logs.conf.sample new file mode 100644 index 0000000..359e608 --- /dev/null +++ b/conf/organize_php_error_logs.conf.sample @@ -0,0 +1,24 @@ +# ---------- +# - Parameter settings for script organize_php_error_logs.sh +# ---------- + +# - VERSIONS +# - +# - PHP Versions that will be checked +# - +#VERSIONS="5.4 5.6 7.0 7.1 7.2 7.3 7.4 8.0" + +# - HTTPD_USER +# - +# - If ommitted, script tries to determine the user under which user the webserver +# - is running. If that fails, parameter defaults to 'www-data' +# - +#HTTPD_USER="" + +# - HTTPD_GROUP +# - +# - If ommitted, script tries to determine the user under which group the webserver +# - is running. If that fails, parameter defaults to 'www-data' +# - +#HTTPD_GROUP="" + diff --git a/organize_php_error_logs.sh b/organize_php_error_logs.sh new file mode 100755 index 0000000..79c0ac4 --- /dev/null +++ b/organize_php_error_logs.sh @@ -0,0 +1,503 @@ +#!/usr/bin/env bash + +script_name="$(basename $(realpath $0))" +working_dir="$(dirname $(realpath $0))" + +conf_file="${working_dir}/conf/${script_name%%.*}.conf" + +LOCK_DIR="/tmp/$(basename $0).$$.LOCK" +log_file="${LOCK_DIR}/${script_name%%.*}.log" + +backup_date="$(date +%Y-%m-%d-%H%M)" + + +# ---------- +# Base Function(s) +# ---------- + +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + blank_line + 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 +} +fatal(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ] $*" + else + echo -e " [ Fatal ] $*" + fi + echo "" + if $terminal ; then + echo -e " \033[1mScript terminated\033[m.." + else + echo -e " Script terminated.." + fi + echo "" + rm -rf $LOCK_DIR + exit 1 +} +error (){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ] $*" + else + echo " [ Error ] $*" + fi + echo "" +} + +warn (){ + echo "" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarning\033[m ] $*" + else + echo " [ Warning ] $*" + fi + echo "" +} + +info (){ + if $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mInfo\033[m ] $*" + else + echo " [ Info ] $*" + fi + echo "" + fi +} + + +echo_ok() { + if $terminal ; then + echo -e "\033[85G[ \033[32mok\033[m ]" + fi +} +echo_failed(){ + if $terminal ; then + echo -e "\033[85G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal ; then + echo -e "\033[85G[ \033[33m\033[1mskipped\033[m ]" + fi +} +echo_wait(){ + if $terminal ; then + echo -en "\033[85G[ \033[5m\033[1m..\033[m ]" + fi +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +blank_line() { + if $terminal ; then + echo "" + fi +} + +detect_os () { + + if $(which lsb_release > /dev/null 2>&1) ; then + + DIST="$(lsb_release -i | awk '{print tolower($3)}')" + DIST_VERSION="$(lsb_release -r | awk '{print tolower($2)}')" + DIST_CODENAME="$(lsb_release -c | awk '{print tolower($2)}')" + + if [[ "$DIST" = "debian" ]]; then + if $(echo "$DIST_VERSION" | grep -q '\.') ; then + DIST_VERSION=$(echo "$DIST_VERSION" | cut --delimiter='.' -f1) + fi + fi + + elif [[ -e "/etc/os-release" ]]; then + + . /etc/os-release + + DIST=$ID + DIST_VERSION=${VERSION_ID} + + fi + + # remove whitespace from DIST and DIST_VERSION + DIST="${DIST// /}" + DIST_VERSION="${DIST_VERSION// /}" + +} + + +# ---------- +# - Jobhandling +# ---------- + +# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM +# - +trap clean_up SIGHUP SIGINT SIGTERM + +# - Create lock directory '$LOCK_DIR" +# +mkdir "$LOCK_DIR" + + +# ---------- +# - Some checks .. +# ---------- + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true +else + fatal "Script must run in a terminal." +fi + + +# ========== +# - Begin Main Script +# ========== + +# ---------- +# - Headline +# ---------- + +if $terminal ; then + echo "" + echo -e "\033[1m----------\033[m" + echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m" + echo -e "\033[1m----------\033[m" +fi + + +# ---------- +# Read Configurations from $conf_file +# ---------- + +# - Give your default values here +# - +DEFAULT_VERSIONS="5.4 5.6 7.0 7.1 7.2 7.3 7.4" +DEFAULT_HTTPD_USER=www-data +DEFAULT_HTTPD_GROUP=www-data + +blank_line +echononl "Loading configuration settings from $(basename ${conf_file}).." +if [[ -f "$conf_file" ]]; then + source "$conf_file" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fatal "$(cat $tmp_log_file)" + fi +else + echo_skipped + warn "No Configuration File found. Loading defaults.." +fi + +[[ -n "$VERSIONS" ]] || VERSIONS="$DEFAULT_VERSIONS" +[[ -n "$HTTPD_USER" ]] && DEFAULT_HTTPD_USER="$HTTPD_USER" +[[ -n "$HTTPD_GROUP" ]] && DEFAULT_HTTPD_GROUP="$HTTPD_GROUP" + +if [[ -z "$HTTPD_USER" ]] || [[ -z "$HTTPD_GROUP" ]] ; then + + ## - Determin httpd binary + ## - + _httpd_binary="`which httpd`" + if [ -z "$_httpd_binary" ]; then + _httpd_binary="$(ps -axu | grep httpd | grep -e "^root" | grep -v grep | awk '{print$11}')" + if [ -z "$_httpd_binary" ]; then + if [ -x "/usr/local/apache2/bin/httpd" ]; then + _httpd_binary="/usr/local/apache2/bin/httpd" + fi + fi + fi + + if [ -x "$_httpd_binary" ];then + ## - Determin websever user + ## - + _pass_web_user=false + web_user="`$_httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^User" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" + web_group="`$_httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^Group" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" + if [[ -n "$web_user" ]] ; then + DEFAULT_HTTPD_USER=$web_user + fi + if [[ -n "$web_group" ]]; then + DEFAULT_HTTPD_GROUP=$web_group + fi + fi +fi + + +echo "" +echo "" +echo -e "\033[32m--\033[m" +echo "" +echo "Insert User/Group for PHP FPM daemon." +echo "" +echo "" + +HTTPD_USER= +while [ "X$HTTPD_USER" = "X" ] +do + echononl "PHP FPM user [${DEFAULT_HTTPD_USER}]: " + read HTTPD_USER + if [ "X$HTTPD_USER" = "X" ]; then + HTTPD_USER=$DEFAULT_HTTPD_USER + fi +done + +if [ -z "$_HTTPD_GROUP" ]; then + if [ "$HTTPD_USER" = "nobody" ]; then + DEFAULT_HTTPD_GROUP="nogroup" + else + DEFAULT_HTTPD_GROUP=$HTTPD_USER + fi +fi +HTTPD_GROUP= +while [ "X$HTTPD_GROUP" = "X" ] +do + echononl "PHP FPM group [$DEFAULT_HTTPD_GROUP]: " + read HTTPD_GROUP + if [ "X$HTTPD_GROUP" = "X" ]; then + HTTPD_GROUP=$DEFAULT_HTTPD_GROUP + fi +done + +blank_line +blank_line +echo -e "\033[m--\033[m" +echo -e "\033[32mStart Script with the following Parameters \033[m" +echo -e "\033[m--\033[m" +echo "" +echo " PHP FPM User.....................: $HTTPD_USER" +echo " PHP FPM GROUP....................: $HTTPD_GROUP" +echo "" +echo " PHP versions to check............: $VERSIONS" + +blank_line +echononl "Continue with this parameters? [\033[1myes/no\033[m]: " +read OK +while [[ "${OK,,}" != "yes" ]] && [[ "${OK,,}" != "no" ]] ; do + echononl "\033[33mWrong entry!\033[m [\033[1myes/no\033[m]: " + read OK +done +[[ "${OK,,}" = "yes" ]] || fatal "Canceled by user input." +blank_line + + +# log file location +# +for _version in $VERSIONS ; do + if [[ ! -d "$(realpath "/usr/local/php-${_version}")" ]]; then + + if $terminal ; then + echo "" + echo -e "\033[1m----------\033[m" + echo -e "\033[33mNo PHP version $_version present\033[m" + echo -e "\033[1m----------\033[m" + fi + + # delete PHP FPM log directories from old non-existent installations + # + if [[ -d "/var/log/php-${_version}-fpm" ]] ; then + + echononl "Remove directory /var/log/php-${_version}-fpm .." + rm -rf "/var/log/php-${_version}-fpm" > "$log_file" 2>&1 + if [[ $? -ne 0 ]]; then + echo_failed + error "$(cat "$log_file")" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Stopped by user" + else + echo_ok + fi + fi + + if [[ -f "/var/log/apache2/php_errors.log" ]]; then + + echononl "Remove old log-files '/var/log/apache2/php_errors.log*'.." + rm -f "/var/log/apache2/php_errors.log*" "$log_file" 2>&1 + + if [[ $? -ne 0 ]]; then + echo_failed + error "$(cat "$log_file")" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Stopped by user" + else + echo_ok + fi + fi + + # delete logrotation definitions from old non-existent installations + # + if [[ -f "/etc/logrotate.d/php-${_version}-fpm" ]] ; then + + echononl "Remove logrotition definition 'php-${_version}-fpm'.." + rm "/etc/logrotate.d/php-${_version}-fpm" > "$log_file" 2>&1 + + if [[ $? -ne 0 ]]; then + echo_failed + error "$(cat "$log_file")" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Stopped by user" + else + echo_ok + fi + fi + + continue + fi + + if $terminal ; then + echo "" + echo -e "\033[1m----------\033[m" + echo -e "\033[32mFound installed PHP version $_version \033[m" + echo -e "\033[1m----------\033[m" + fi + + echononl "Create new log-file '/var/log/php-${_version}-fpm/php_errors.log'.." + touch "/var/log/php-${_version}-fpm/php_errors.log" > "$log_file" 2>&1 + if [[ $? -ne 0 ]]; then + echo_failed + error "$(cat "$log_file")" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Stopped by user" + else + echo_ok + fi + + echononl "Set Owner Permissions to new log-file (${HTTPD_USER}:${HTTPD_GROUP}).." + chown $HTTPD_USER:$HTTPD_GROUP /var/log/php-${_version}-fpm/php_errors.log > "$log_file" 2>&1 + if [[ $? -ne 0 ]]; then + echo_failed + error "$(cat "$log_file")" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Stopped by user" + else + echo_ok + fi + + echononl "Set parameter 'error_log' at PHP ini file.." + perl -i -n -p -e "s#^error_log\s*=.*#error_log = \"/var/log/php-${_version}-fpm/php_errors.log\"#" \ + /usr/local/php-${_version}/etc/php.ini > "$log_file" 2>&1 + if [[ $? -ne 0 ]]; then + echo_failed + error "$(cat "$log_file")" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Stopped by user" + else + echo_ok + fi + + echononl "Create configuration file for logrotation.." + cat << EOF > /etc/logrotate.d/php-${_version}-fpm +/var/log/php-${_version}-fpm/*log { + daily + rotate 7 + start 0 + compress + delaycompress + missingok + notifempty + sharedscripts + postrotate + if [[ -f " /var/log/php-${_version}-fpm/php_errors.log" ]] ; then + chown ${HTTPD_USER}:$HTTPD_GROUP /var/log/php-${_version}-fpm/php_errors.log + fi + [ ! -f /run/php-${_version}-fpm.pid ]] || kill -USR1 \$(cat /run/php-${_version}-fpm.pid) + endscript +} +EOF + if [[ $? -ne 0 ]]; then + echo_failed + error "$(cat "$log_file")" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Stopped by user" + else + echo_ok + fi + + echononl "Restart PHP FPM ${_version} process.." + systemctl restart php-${_version}-fpm > "$log_file" 2>&1 + if [[ $? -ne 0 ]]; then + echo_failed + error "$(cat "$log_file")" + else + echo_ok + fi + +done + +blank_line +clean_up 0 +