215 lines
No EOL
6.8 KiB
Bash
215 lines
No EOL
6.8 KiB
Bash
#!/bin/bash
|
|
|
|
# Enable debug mode to see what's happening
|
|
set -x
|
|
|
|
# Function to check if a command exists
|
|
command_exists() {
|
|
command -v "$1" >/dev/null 2>&1
|
|
}
|
|
|
|
# Function to find user directories in /home with exclusions
|
|
find_user_dirs() {
|
|
find /home -maxdepth 1 -mindepth 1 -type d | grep -v -E '^/home/(lost\+found|mysql|homelab)$'
|
|
}
|
|
|
|
# Function to setup GoAccess if not already installed
|
|
setup_goaccess() {
|
|
if ! command_exists goaccess; then
|
|
echo "Installing GoAccess..."
|
|
echo "deb https://deb.goaccess.io/ $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list.d/goaccess.list
|
|
wget -O - https://deb.goaccess.io/gnugpg.key | sudo apt-key add -
|
|
sudo apt-get update
|
|
sudo apt-get install -y goaccess
|
|
else
|
|
echo "GoAccess is already installed."
|
|
fi
|
|
}
|
|
|
|
# Function to update logrotate configuration while preserving existing configs
|
|
update_logrotate() {
|
|
local user_dirs=($(find_user_dirs))
|
|
local existing_config=""
|
|
local new_config=""
|
|
|
|
# Read existing configuration if it exists
|
|
if [ -f "/etc/logrotate.d/nginx" ]; then
|
|
existing_config=$(cat /etc/logrotate.d/nginx)
|
|
fi
|
|
|
|
for user_dir in "${user_dirs[@]}"; do
|
|
username=$(basename "$user_dir")
|
|
if [ -d "$user_dir/logs/nginx" ]; then
|
|
# Check if this user's configuration already exists
|
|
if ! echo "$existing_config" | grep -q "$user_dir/logs/nginx/\*\.log"; then
|
|
echo "Adding new nginx logs directory for user: $username"
|
|
new_config+="
|
|
$user_dir/logs/nginx/*.log {
|
|
yearly
|
|
maxsize 1000000000
|
|
missingok
|
|
rotate 4
|
|
compress
|
|
delaycompress
|
|
notifempty
|
|
create 0640 $username adm
|
|
sharedscripts
|
|
prerotate
|
|
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
|
|
run-parts /etc/logrotate.d/httpd-prerotate; \
|
|
fi \
|
|
endscript
|
|
postrotate
|
|
invoke-rc.d nginx rotate >/dev/null 2>&1
|
|
endscript
|
|
}
|
|
"
|
|
else
|
|
echo "Configuration already exists for user: $username"
|
|
fi
|
|
fi
|
|
done
|
|
|
|
# Only update if there are new configurations
|
|
if [ ! -z "$new_config" ]; then
|
|
echo "${existing_config}${new_config}" | sudo tee /etc/logrotate.d/nginx
|
|
echo "Logrotate configuration updated with new entries."
|
|
else
|
|
echo "No new configurations to add to logrotate."
|
|
fi
|
|
}
|
|
|
|
# Function to create or update GoAccess update script
|
|
create_goaccess_script() {
|
|
# Check if script already exists and is different
|
|
local script_path="/usr/bin/update_goaccess.sh"
|
|
local script_content='#!/bin/bash
|
|
|
|
# Enable error handling
|
|
set -e
|
|
|
|
# Enable debug output
|
|
set -x
|
|
|
|
process_logs() {
|
|
# Find all user directories in /home, excluding mysql and homelab
|
|
for user_dir in /home/*/; do
|
|
user_dir=${user_dir%/}
|
|
username=$(basename "$user_dir")
|
|
|
|
# Skip excluded directories
|
|
case "$username" in
|
|
"lost+found"|"mysql"|"homelab")
|
|
continue
|
|
;;
|
|
esac
|
|
|
|
echo "Processing user directory: $user_dir"
|
|
|
|
# Check for nginx logs directory and access.log
|
|
log_file="$user_dir/logs/nginx/access.log"
|
|
if [ -f "$log_file" ]; then
|
|
echo "Found nginx access log: $log_file"
|
|
|
|
# Check for htdocs directory
|
|
if [ -d "$user_dir/htdocs" ]; then
|
|
echo "Found htdocs directory"
|
|
|
|
# Process each domain directory
|
|
for domain_dir in "$user_dir/htdocs"/*/ ; do
|
|
if [ -d "$domain_dir" ]; then
|
|
domain=$(basename "$domain_dir")
|
|
echo "Processing domain: $domain"
|
|
|
|
# Create insights directory with proper permissions
|
|
insights_dir="$domain_dir/insights"
|
|
if [ ! -d "$insights_dir" ]; then
|
|
echo "Creating insights directory: $insights_dir"
|
|
mkdir -p "$insights_dir"
|
|
fi
|
|
|
|
# Generate GoAccess report
|
|
goaccess "$log_file" \
|
|
--log-format=COMBINED \
|
|
--date-format="%d/%b/%Y" \
|
|
--time-format="%H:%M:%S" \
|
|
--double-decode \
|
|
--anonymize-ip \
|
|
-a \
|
|
-o "$insights_dir/goaccess.html"
|
|
|
|
# Set proper ownership
|
|
chown -R "$username:$username" "$insights_dir"
|
|
chmod -R 755 "$insights_dir"
|
|
|
|
echo "Successfully generated report for $domain"
|
|
fi
|
|
done
|
|
else
|
|
echo "Warning: No htdocs directory found in $user_dir"
|
|
fi
|
|
else
|
|
echo "Warning: No access.log found at $log_file"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Main execution
|
|
echo "Starting GoAccess report generation..."
|
|
process_logs
|
|
echo "Finished GoAccess report generation"'
|
|
|
|
if [ -f "$script_path" ]; then
|
|
local existing_content=$(cat "$script_path")
|
|
if [ "$existing_content" != "$script_content" ]; then
|
|
echo "Updating existing GoAccess script with improvements..."
|
|
echo "$script_content" | sudo tee "$script_path"
|
|
else
|
|
echo "GoAccess script is already up to date."
|
|
fi
|
|
else
|
|
echo "Creating new GoAccess script..."
|
|
echo "$script_content" | sudo tee "$script_path"
|
|
fi
|
|
|
|
sudo chmod +x "$script_path"
|
|
}
|
|
|
|
# Function to setup or update cron job
|
|
setup_cron() {
|
|
# Check if cron job already exists
|
|
if ! crontab -l 2>/dev/null | grep -q "update_goaccess.sh"; then
|
|
# Add new cron job only if it doesn't exist
|
|
(crontab -l 2>/dev/null; echo "*/5 * * * * /usr/bin/update_goaccess.sh > /var/log/goaccess_cron.log 2>&1") | crontab -
|
|
echo "Added new cron job for GoAccess updates."
|
|
else
|
|
echo "Cron job already exists."
|
|
fi
|
|
}
|
|
|
|
# Initial run of the update script
|
|
initial_run() {
|
|
echo "Running update_goaccess.sh to process new folders..."
|
|
sudo /usr/bin/update_goaccess.sh
|
|
}
|
|
|
|
# Main execution
|
|
echo "Starting setup..."
|
|
setup_goaccess
|
|
update_logrotate
|
|
create_goaccess_script
|
|
setup_cron
|
|
initial_run
|
|
|
|
# Only restart nginx if logrotate configuration was updated
|
|
if [ ! -z "$new_config" ]; then
|
|
sudo systemctl restart nginx
|
|
echo "Nginx restarted due to configuration changes."
|
|
else
|
|
echo "No nginx restart needed."
|
|
fi
|
|
|
|
echo "Setup completed!"
|
|
echo "GoAccess reports will be updated every 5 minutes"
|
|
echo "You can check the cron job logs at /var/log/goaccess_cron.log"
|
|
echo "Reports should be available at: /home/<username>/htdocs/<domain>/insights/goaccess.html" |