webinoly/usr/log
Cristhian Martínez Ochoa 5f9ebaecc1 subfolders
Improved experience for subfolders.
2023-09-26 16:28:40 -07:00

378 lines
13 KiB
Bash

#!/bin/bash
# View logs in real time Plugins
# Syntax: log <domain> <option>
# Options: -wp, -error, -mail, -php o -fpm, -mysql, -only-error, -le, -ssh, -syslog
# Arguments: -lines, -display, -enable, -disable
# Notes: If no domain or option is entered, access logs are displayed.
source /opt/webinoly/lib/general
check_for_parameters $@
api-events_update lgs
error() {
echo "${red}[ERROR] Log file not found! ${end}"
exit 1
}
waiting_for_log() {
echo "${blu}[Log File is Empty] ${dim}Waiting to receive some data, you will see it in real-time here...${end}"
}
# Only these commands are supported with subfolders
if [[ -n $subfolder && -z $wp ]]; then
echo "${red}[ERROR] Subfolder option not supported!${end}"
exit 1
fi
# Check for custom "lines" value
[[ -n $(conf_read log-lines) && $(conf_read log-lines) =~ ^[0-9]+$ && $(conf_read log-lines) -gt 0 ]] && clines=$(conf_read log-lines) || clines=10
[[ -n $lines && $lines =~ ^[0-9]+$ && $lines -gt 0 ]] && clines=$lines
if [[ -n $purge ]]; then
if [[ $purge =~ ^(nginx|letsencrypt|mysql|redis|force)$ ]]; then
answer="Y"
if [[ $purge =~ ^(nginx|letsencrypt|mysql|redis)$ ]]; then
fol="/$purge"
mes="($purge) "
else
fol=""
fi
elif [[ $purge =~ ^(all|true)$ ]]; then
echo "${gre}"
echo "All your logs GZ files will be removed from your server!"
echo "${blu}Are you sure [y/N]? "
while read -r -n 1 -s answer; do
answer=${answer:-n}
echo ""
[[ $answer = [YyNn] ]] && break
done
else
echo "${red}[ERROR] Invalid log value!${end}"
exit 1
fi
if [[ ! -d /var/log$fol ]]; then
echo "${red}[ERROR] Log folder not found!${end}"
exit 1
elif [[ $answer == [Yy] ]]; then
sudo find /var/log$fol -name "*.gz" -type f -delete
echo "${gre}All your logs ${mes}GZ files has been successfully removed from your server!${end}"
else
echo "${red}Action aborted!${end}"
exit 1
fi
elif [[ -n $mail || -n $email ]]; then
if [[ -f /var/log/mail.log && -f /var/log/mail.err ]]; then
[[ ! -s /var/log/mail.log && ! -s /var/log/mail.err ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/mail.log /var/log/mail.err
elif [[ -f /var/log/mail.log ]]; then
[[ ! -s /var/log/mail.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/mail.log
elif [[ -f /var/log/mail.err ]]; then
[[ ! -s /var/log/mail.err ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/mail.err
else
error
fi
elif [[ -n $fpm || -n $php ]]; then
check_for_php
# REMOVE: Temporal check because we change log path in v1.11.0
php_log_path=$(grep -w "^error_log = .*" /etc/php/$(conf_read php-ver)/fpm/php-fpm.conf | cut -f 3 -d ' ')
if [[ -f $php_log_path ]]; then
[[ ! -s $php_log_path ]] && waiting_for_log
sudo tail -f --lines=$clines $php_log_path
else
error
fi
elif [[ -n $le ]]; then
if [[ -f /var/log/letsencrypt/letsencrypt.log ]]; then
[[ ! -s /var/log/letsencrypt/letsencrypt.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/letsencrypt/letsencrypt.log
elif [[ ! -L /usr/bin/letsencrypt ]]; then
echo "${red}[ERROR] Seems like Let's Encrypt is not installed!${end}"
exit 1
else
error
fi
elif [[ -n $ssh ]]; then
if [[ -f /var/log/auth.log ]]; then
[[ ! -s /var/log/auth.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/auth.log
else
error
fi
# MySQL Logs
elif [[ -n $mysql ]]; then
check_for_mysql
# General Log
if [[ $mysql == "general" ]]; then
if [[ -n $enable ]]; then
cnf_write general_log 1
cnf_write general_log_file /var/log/mysql/mysql.log
conf_write mysql-log-general true
sudo systemctl restart mysql
echo "${gre}MySQL/MariaDB General log was successfully enabled!${end}"
elif [[ -n $disable ]]; then
cnf_write general_log 0
conf_write mysql-log-general false
sudo systemctl restart mysql
echo "${gre}MySQL/MariaDB General log was successfully disabled!${end}"
elif [[ -f /var/log/mysql/mysql.log && $(cnf_read general_log) == 1 ]]; then
[[ ! -s /var/log/mysql/mysql.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/mysql/mysql.log
elif [[ $(cnf_read general_log) != 1 ]]; then
echo "${red}[ERROR] MySQL/MariaDB General log is not enabled!${end}"
exit 1
else
echo "${red}[ERROR] MySQL/MariaDB General log file not found or still empty!${end}"
exit 1
fi
# Slow Query Log
elif [[ $mysql == "slow" ]]; then
if [[ -n $enable ]]; then
# https://mariadb.com/kb/en/slow-query-log-overview/
if [[ -n $long_query_time && $long_query_time =~ ^[0-9]+$ && $long_query_time -gt 1 ]]; then
cnf_write long_query_time $long_query_time
echo "${gre}${dim}MySQL/MariaDB long query time set to ${long_query_time}s ${end}"
elif [[ -n $long_query_time ]]; then
echo "${red}[ERROR] Invalid value for long query time! ${dim}(Should be a number greater than zero)${end}"
exit 1
fi
cnf_write slow_query_log 1
conf_write mysql-log-slow true
sudo systemctl restart mysql
echo "${gre}MySQL/MariaDB Slow Query log was successfully enabled!${end}"
elif [[ -n $disable ]]; then
# Don't remove long_query_time variable to preserve the value
cnf_write slow_query_log 0
conf_write mysql-log-slow false
sudo systemctl restart mysql
echo "${gre}MySQL/MariaDB Slow Query log was successfully disabled!${end}"
elif [[ -f /var/log/mysql/mariadb-slow.log && $(cnf_read slow_query_log) == 1 ]]; then
[[ ! -s /var/log/mysql/mariadb-slow.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/mysql/mariadb-slow.log
elif [[ $(cnf_read slow_query_log) != 1 ]]; then
echo "${red}[ERROR] MySQL/MariaDB Slow Query log is not enabled!${end}"
exit 1
else
echo "${red}[ERROR] MySQL/MariaDB Slow Query log file not found or still empty!${end}"
exit 1
fi
# Binary Log
elif [[ $mysql == "binary" ]]; then
if [[ -n $enable ]]; then
cnf_delete skip-log-bin
cnf_write log_bin /var/log/mysql/mariadb-bin
cnf_write log_bin_index /var/log/mysql/mariadb-bin.index
conf_write mysql-log-binary true
sudo systemctl restart mysql
echo "${gre}MySQL/MariaDB Binary log was successfully enabled!${end}"
elif [[ -n $disable ]]; then
cnf_delete log_bin
cnf_delete log_bin_index
cnf_write skip-log-bin
conf_write mysql-log-binary false
sudo systemctl restart mysql
echo "${gre}MySQL/MariaDB Binary log was successfully disabled!${end}"
elif [[ -f /var/log/mysql/mariadb-bin && -n $(cnf_read log_bin) ]]; then
[[ ! -s /var/log/mysql/mariadb-bin ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/mysql/mariadb-bin
elif [[ -z $(cnf_read log_bin) ]]; then
echo "${red}[ERROR] MySQL/MariaDB Binary log is not enabled!${end}"
exit 1
else
echo "${red}[ERROR] MySQL/MariaDB Binary log file not found or still empty!${end}"
exit 1
fi
# Error Log
elif [[ $mysql =~ ^(error|true)$ ]]; then
if [[ -f /var/log/mysql/error.log ]]; then
[[ ! -s /var/log/mysql/error.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/mysql/error.log
else
echo "${red}[ERROR] MySQL/MariaDB Error log file not found or still empty! ${dim}(Older versions sent the error log to syslog, check it!)${end}"
exit 1
fi
else
echo "${red}[ERROR] Invalid value for MySQL/MariaDB Log!${end}"
exit 1
fi
# Syslog
elif [[ -n $syslog ]]; then
if [[ -s /var/log/syslog ]]; then
[[ ! -s /var/log/syslog ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/syslog
else
echo "${red}[ERROR] MySQL/MariaDB Error log (syslog) file not found or still empty!${end}"
exit 1
fi
# Turn On/Off Access Logs
elif [[ -n $only_error ]]; then
if ! [[ $only_error =~ ^(on|off)$ ]]; then
echo "${red}[ERROR] Empty or not valid parameter for Only-Error!${end}"
exit 1
fi
# Global-Conf
if [[ -z $domain ]]; then
if [[ $only_error == "on" ]]; then
api-events_update lg1
[[ $(conf_read nginx) == "true" ]] && sudo sed -i '/access_log /c \ access_log off;' /etc/nginx/nginx.conf
conf_write global-access-log-off true
echo "${gre}Only-Error Log was successfully enabled (global)!${end}"
elif [[ $only_error == "off" ]]; then
api-events_update lg2
[[ $(conf_read nginx) == "true" ]] && sudo sed -i "/access_log /c \ access_log \/var\/log\/nginx\/access.log $(check_var nginx-log-format);" /etc/nginx/nginx.conf
conf_write global-access-log-off false
echo "${gre}Only-Error Log was successfully disabled (global)!${end}"
fi
[[ $(check_var nginx-error-log-level) != "false" ]] && sudo sed -i "/error_log /c \ error_log \/var\/log\/nginx\/error.log $(check_var nginx-error-log-level);" /etc/nginx/nginx.conf
sudo systemctl reload nginx
fi
# Per site
if [[ -n $domain && -f /etc/nginx/sites-available/$domain ]]; then
check_for_nginx
if [[ $domain == "default" || $domain == $ADMIN_TOOLS_SITE ]]; then
echo "${red}[ERROR] Only-Error is not allowed to be used in Tools-Port or Default Nginx site! ${end}"
exit 1
fi
if [[ $only_error == "on" ]]; then
api-events_update lg3
sudo sed -i '/access_log /c \ access_log off;' /etc/nginx/sites-available/$domain
echo "${gre}Only-Error Log was successfully enabled for ${blu}${domain} ${gre}site!${end}"
elif [[ $only_error == "off" ]]; then
api-events_update lg4
sudo sed -i "/access_log /c \ access_log \/var\/log\/nginx\/${domain}.access.log $(check_var nginx-log-format);" /etc/nginx/sites-available/$domain
echo "${gre}Only-Error Log was successfully disabled for ${blu}${domain} ${gre}site!${end}"
fi
[[ $(check_var nginx-error-log-level) != "false" ]] && sudo sed -i "/error_log /c \ error_log \/var\/log\/nginx\/${domain}.error.log $(check_var nginx-error-log-level);" /etc/nginx/sites-available/$domain
sudo systemctl reload nginx
elif [[ -n $domain && ! -f /etc/nginx/sites-available/$domain ]]; then
echo "${red}[ERROR] Domain not found!${end}"
exit 1
fi
# Site log files
elif [[ -n $domain && -f /etc/nginx/sites-available/$domain ]]; then
# Check if Access Log is enabled
if [[ -z $error && -z $wp && $(is_log $domain) != "true" ]]; then
echo "${red}[ERROR] Access Log for${blu} $domain ${red}is disabled!${end}"
exit 1
fi
# WordPress Logs
[[ -n $wp && $(is_parked $domain) == "true" ]] && echo "${dim}[WARNING] WP Debug in a parked site will have effect over all your multisite network!${end}"
if [[ -n $wp && $(is_wp $domain $subfolder) == "true" ]]; then
iswpdeb=$( is_wp_debug $domain $subfolder )
if [[ $wp == "on" ]]; then
if [[ $iswpdeb == "false" ]]; then
[[ $display == "off" ]] && dis="false" || dis="true"
wp_config_write $domain WP_DEBUG true $subfolder
wp_config_write $domain WP_DEBUG_DISPLAY $dis $subfolder
wp_config_write $domain WP_DEBUG_LOG true $subfolder
[[ -n $env ]] && sudo site $domain -env=$env -skip-debug
echo "${gre}WordPress Debug mode for${blu} $domain$subfolder ${gre}has been successfully enabled!${end}"
else
echo "${gre}WordPress Debug mode for${blu} $domain$subfolder ${gre}is already enabled!${end}"
fi
elif [[ $wp == "off" ]]; then
if [[ $iswpdeb == "true" ]]; then
wp_config_write $domain WP_DEBUG false $subfolder
wp_config_delete $domain WP_DEBUG_DISPLAY $subfolder
wp_config_delete $domain WP_DEBUG_LOG $subfolder
[[ -n $env ]] && sudo site $domain -env=$env -skip-debug
echo "${gre}WordPress Debug mode for${blu} $domain$subfolder ${gre}has been successfully disabled!${end}"
else
echo "${gre}WordPress Debug mode for${blu} $domain$subfolder ${gre}is already disabled!${end}"
fi
elif [[ $wp != "true" ]]; then
echo "${red}[ERROR] Invalid value for WP parameter!${end}"
exit 1
elif [[ -f /var/www/$domain/htdocs$subfolder/wp-content/debug.log && $iswpdeb == "true" ]]; then
[[ ! -s /var/www/$domain/htdocs$subfolder/wp-content/debug.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/www/$domain/htdocs$subfolder/wp-content/debug.log
elif [[ $iswpdeb != "true" ]]; then
echo "${red}[ERROR] WordPress Debug Mode is not enabled!${end}"
exit 1
else
echo "${red}[ERROR] WordPress log file not found or still empty!${end}"
exit 1
fi
elif [[ -n $wp ]]; then
echo "${red}[ERROR] Please, enter a valid WP site! ${dim}(${domain}${subfolder})${end}"
exit 1
# Access and error logs
elif [[ -n $error && -f /var/log/nginx/$domain.error.log ]]; then
[[ ! -s /var/log/nginx/$domain.error.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/nginx/$domain.error.log
elif [[ -f /var/log/nginx/$domain.access.log && -z $error && -z $wp ]]; then
[[ ! -s /var/log/nginx/$domain.access.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/nginx/$domain.access.log
else
echo "${red}[ERROR] Log file not found!${end}"
exit 1
fi
elif [[ -n $domain && ! -f /etc/nginx/sites-available/$domain ]]; then
echo "${red}[ERROR] Domain not found!${end}"
exit 1
elif [[ -z $domain && ( -n $error || $empty_param == "true" ) ]]; then
# Check if Access Log is enabled
check_for_nginx
islog=$( grep -F "access_log off;" /etc/nginx/nginx.conf )
if [[ -z $error && -z $1 && -n $islog ]]; then
echo "${red}[ERROR] Access Log (global) is disabled!${end}"
exit 1
fi
if [[ -n $error && -f /var/log/nginx/error.log ]]; then
sudo tail -f --lines=$clines /var/log/nginx/*error.log
elif [[ -f /var/log/nginx/access.log && -z $1 ]]; then
sudo tail -f --lines=$clines /var/log/nginx/*access.log
else
echo "${red}[ERROR] Please, enter a valid option! ${end}"
exit 1
fi
elif [[ -n $help || -n $h || -n $H || $empty_param == "true" ]]; then
help_message
else
echo "${red}[ERROR] Please, enter a valid option! ${end}"
exit 1
fi
ads_donate
api-events_update lge