webinoly/plugins/log
Cristhian Martínez Ochoa 4364ed70c4 logs improvements
Custom tail support from command line.
Error message if access log is disabled.
2019-12-19 19:47:30 -07:00

164 lines
5.5 KiB
Bash

#!/bin/bash
# View logs in real time Plugins
# Syntax: log <domain> <option>
# Options: -wp, -error, -mail, -php o -fpm, -mysql, -only-error
# Arguments: -lines
# 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
}
# 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 $mail || -n $email ]]; then
if [[ -a /var/log/mail.log && -a /var/log/mail.err ]]; then
sudo tail -f --lines=$clines /var/log/mail.log /var/log/mail.err
elif [[ -a /var/log/mail.log ]]; then
sudo tail -f --lines=$clines /var/log/mail.log
elif [[ -a /var/log/mail.err ]]; then
sudo tail -f --lines=$clines /var/log/mail.err
else
error
fi
elif [[ -n $fpm || -n $php ]]; then
check_for_php
if [[ -a /var/log/php/$(conf_read php-ver)/fpm.log ]]; then
sudo tail -f --lines=$clines /var/log/php/$(conf_read php-ver)/*.log
else
error
fi
elif [[ -n $mysql ]]; then
check_for_mysql
if [[ -a /var/log/mysql/error.log ]]; then
sudo tail -f --lines=$clines /var/log/mysql/*.log
else
error
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
[[ $(conf_read nginx) == "true" ]] && islog=$( grep -F "access_log off;" /etc/nginx/nginx.conf )
if [[ $only_error == "on" ]]; then
api-events_update lg1
if [[ -z $islog ]]; then
[[ $(conf_read nginx) == "true" ]] && sudo sed -i '/access_log/c \ access_log off;' /etc/nginx/nginx.conf
conf_write global-access-log-off true
sudo service nginx reload
echo "${gre}Only-Error Log was successfully enabled (global)!${end}"
else
echo "${gre}Only-Error Log is already enabled (global)!${end}"
fi
elif [[ $only_error == "off" ]]; then
api-events_update lg2
if [[ -n $islog || ( -z $islog && $(conf_read nginx) != "true" ) ]]; then
[[ $(conf_read nginx) == "true" ]] && sudo sed -i "/access_log/c \ access_log \/var\/log\/nginx\/access.log;" /etc/nginx/nginx.conf
conf_write global-access-log-off purged
sudo service nginx reload
echo "${gre}Only-Error Log was successfully disabled (global)!${end}"
else
echo "${gre}Only-Error Log is already disabled (global)!${end}"
fi
fi
fi
# Per site
if [[ -n $domain && -a /etc/nginx/sites-available/$domain ]]; then
check_for_nginx
islog=$( grep -F "access_log off;" /etc/nginx/sites-available/$domain )
if [[ $domain == "default" || $domain == $(conf_read tools-port) ]]; 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
if [[ -z $islog ]]; then
sudo sed -i '/access_log/c \ access_log off;' /etc/nginx/sites-available/$domain
sudo service nginx reload
echo "${gre}Only-Error Log was successfully enabled for ${blu}${domain} ${gre}site!${end}"
else
echo "${gre}Access Log is already enabled for ${blu}${domain} ${gre}site!${end}"
fi
elif [[ $only_error == "off" ]]; then
api-events_update lg4
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
sudo service nginx reload
echo "${gre}Only-Error Log was successfully disabled for ${blu}${domain} ${gre}site!${end}"
else
echo "${gre}Access Log is already disabled for ${blu}${domain} ${gre}site!${end}"
fi
fi
elif [[ -n $domain && ! -a /etc/nginx/sites-available/$domain ]]; then
echo "${red}[ERROR] Domain not found!${end}"
exit 1
fi
# Site log files
elif [[ -n $domain && -a /etc/nginx/sites-available/$domain ]]; then
# Check if Access Log is enabled
islog=$( grep -F "access_log off;" /etc/nginx/sites-available/$domain )
if [[ -z $error && -z $wp && -n $islog ]]; then
echo "${red}[ERROR] Access Log for ${blu}- $domain -${red} is disabled!${end}"
exit 1
fi
if [[ -n $error && -a /var/log/nginx/$domain.error.log ]]; then
sudo tail -f --lines=$clines /var/log/nginx/$domain.error.log
elif [[ -n $wp && -a /var/www/$domain/htdocs/wp-content/debug.log ]]; then
sudo tail -f --lines=$clines /var/www/$domain/htdocs/wp-content/debug.log
elif [[ -a /var/log/nginx/$domain.access.log && -z $error && -z $wp ]]; then
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 && ! -a /etc/nginx/sites-available/$domain ]]; then
echo "${red}[ERROR] Domain not found!${end}"
exit 1
elif [[ -z $domain ]]; 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 && -a /var/log/nginx/error.log ]]; then
sudo tail -f --lines=$clines /var/log/nginx/*error.log
elif [[ -a /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
else
echo "${red}[ERROR] Please, enter a valid option! ${end}"
exit 1
fi
api-events_update lge