134 lines
4 KiB
Bash
134 lines
4 KiB
Bash
#!/bin/bash
|
|
|
|
# View logs in real time Plugins
|
|
# Syntax: log <domain> <option>
|
|
# Options: -wp, -error, -mail, -php o -fpm, -mysql, -only-error
|
|
# Notes: If no domain or option is entered, access logs are displayed.
|
|
|
|
source /opt/webinoly/lib/general
|
|
domain=$1
|
|
opt=$2
|
|
|
|
error() {
|
|
echo ""
|
|
echo "${red} Log file not found!!! ${end}"
|
|
echo ""
|
|
exit 1
|
|
}
|
|
|
|
# Validations
|
|
if [[ $domain =~ ^(-error|-wp)$ || $domain == "-only-error="* ]]; then
|
|
domain=$2
|
|
opt=$1
|
|
fi
|
|
|
|
# Extract value if exist
|
|
if [[ $opt == "-only-error="* ]]; then
|
|
value=$(echo "${opt}" | cut -d'=' -f 2 -s)
|
|
opt=$(echo "${opt}" | cut -d'=' -f 1 -s)
|
|
fi
|
|
|
|
if [[ -n $opt ]] && ! [[ $opt =~ ^(-error|-wp|-only-error)$ ]]; then
|
|
echo "${red}[ERROR] Please, enter a valid option!${end}"
|
|
exit 1
|
|
fi
|
|
|
|
|
|
if [[ -z $opt ]]; then
|
|
case "$domain" in
|
|
"-mail")
|
|
if [[ -a /var/log/mail.log && -a /var/log/mail.err ]]; then
|
|
sudo tail -f /var/log/mail.log /var/log/mail.err
|
|
elif [[ -a /var/log/mail.log ]]; then
|
|
sudo tail -f /var/log/mail.log
|
|
elif [[ -a /var/log/mail.err ]]; then
|
|
sudo tail -f /var/log/mail.err
|
|
else
|
|
error
|
|
fi
|
|
;;
|
|
"-fpm"|"-php")
|
|
if [[ -a /var/log/php/$(conf_read php-ver)/fpm.log ]]; then
|
|
sudo tail -f /var/log/php/$(conf_read php-ver)/*.log
|
|
else
|
|
error
|
|
fi
|
|
;;
|
|
"-mysql")
|
|
if [[ -a /var/log/mysql/error.log ]]; then
|
|
sudo tail -f /var/log/mysql/*.log
|
|
else
|
|
error
|
|
fi
|
|
;;
|
|
esac
|
|
fi
|
|
|
|
|
|
# Turn On/Off Access Logs
|
|
if [[ $opt == "-only-error" ]]; then
|
|
if ! [[ $value =~ ^(on|off)$ ]]; then
|
|
echo "${red}[ERROR] Empty or not valid parameter for Only-Error!${end}"
|
|
exit 1
|
|
fi
|
|
|
|
# Global-Conf
|
|
if [[ -z $domain ]]; then
|
|
islog=$( grep -F "access_log off;" /etc/nginx/nginx.conf )
|
|
if [[ $value == "on" ]]; then
|
|
if [[ -z $islog ]]; then
|
|
sudo sed -i '/access_log/c \ access_log off;' /etc/nginx/nginx.conf
|
|
echo "${gre}Only-Error Log was successfully enabled (global)!${end}"
|
|
else
|
|
echo "${red}Only-Error Log is already enabled (global)!${end}"
|
|
fi
|
|
elif [[ $value == "off" ]]; then
|
|
if [[ -n $islog ]]; then
|
|
sudo sed -i "/access_log/c \ access_log \/var\/log\/nginx\/access.log;" /etc/nginx/nginx.conf
|
|
echo "${gre}Only-Error Log was successfully disabled (global)!${end}"
|
|
else
|
|
echo "${red}Only-Error Log is already disabled (global)!${end}"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
# Per site
|
|
if [[ -n $domain && -a /etc/nginx/sites-available/$domain ]]; then
|
|
islog=$( grep -F "access_log off;" /etc/nginx/sites-available/$domain )
|
|
if [[ $value == "on" ]]; then
|
|
if [[ -z $islog ]]; then
|
|
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}"
|
|
else
|
|
echo "${red}Access Log is already enabled for ${blu}${domain} ${red}site!${end}"
|
|
fi
|
|
elif [[ $value == "off" ]]; then
|
|
if [[ -n $islog ]]; then
|
|
sudo sed -i "/access_log/c \ access_log \/var\/log\/nginx\/${domain}.access.log we_log;" /etc/nginx/sites-available/$domain
|
|
echo "${gre}Only-Error Log was successfully disabled for ${blu}${domain} ${gre}site!${end}"
|
|
else
|
|
echo "${red}Access Log is already disabled for ${blu}${domain} ${red}site!${end}"
|
|
fi
|
|
fi
|
|
elif [[ -n $domain && ! -a /etc/nginx/sites-available/$domain ]]; then
|
|
echo "${red}[ERROR] Domain not found!${end}"
|
|
fi
|
|
exit 0
|
|
fi
|
|
|
|
|
|
# Show the correct log file
|
|
if [[ "$opt" == "-error" && -z "$domain" && -a /var/log/nginx/error.log ]]; then
|
|
sudo tail -f /var/log/nginx/*error.log
|
|
elif [[ "$opt" == "-error" && -n "$domain" && -a /var/log/nginx/$domain.error.log ]]; then
|
|
sudo tail -f /var/log/nginx/$domain.error.log
|
|
elif [[ "$opt" == "-wp" && -n "$domain" && -a /var/www/$domain/htdocs/wp-content/debug.log ]]; then
|
|
sudo tail -f /var/www/$domain/htdocs/wp-content/debug.log
|
|
elif [[ -z "$domain" && -z "$opt" && -a /var/log/nginx/access.log ]]; then
|
|
sudo tail -f /var/log/nginx/*access.log
|
|
elif [[ -n "$domain" && -z "$opt" && -a /var/log/nginx/$domain.access.log ]]; then
|
|
sudo tail -f /var/log/nginx/$domain.access.log
|
|
else
|
|
error
|
|
fi
|