Files
ansible-lemp-wordpress/templates/performance-monitor.sh.j2
Sebastian Palencsár 573224a36b 🚀 Initial release: Complete Ansible LEMP WordPress automation
 Features:
- Complete LEMP stack automation
- WordPress with WP-CLI
- Redis Object Cache (50% performance boost)
- Multi-OS support
- SSL/Let's Encrypt integration
- Security hardening
- Enterprise features
- Docker testing environment
- GitHub Actions CI/CD
- Comprehensive documentation

🧪 Fully tested and production-ready

Copyright (c) 2025 Sebastian Palencsár
2025-06-18 18:24:55 +02:00

242 lines
7.8 KiB
Django/Jinja

#!/bin/bash
# WordPress Performance Monitoring Script
# Generated by Ansible
set -e
# Configuration
WORDPRESS_PATH="{{ wordpress_path }}"
LOG_FILE="/var/log/wordpress-performance.log"
ALERT_EMAIL="{{ performance_alert_email | default('admin@localhost') }}"
THRESHOLDS_FILE="/etc/wordpress-performance-thresholds.conf"
# Default thresholds
CPU_THRESHOLD={{ cpu_threshold | default('80') }}
MEMORY_THRESHOLD={{ memory_threshold | default('85') }}
DISK_THRESHOLD={{ disk_threshold | default('90') }}
LOAD_THRESHOLD={{ load_threshold | default('5.0') }}
RESPONSE_TIME_THRESHOLD={{ response_time_threshold | default('2.0') }}
# Load custom thresholds if available
if [[ -f "$THRESHOLDS_FILE" ]]; then
source "$THRESHOLDS_FILE"
fi
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Functions
log_info() {
echo -e "${GREEN}[INFO]${NC} $1" | tee -a "$LOG_FILE"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1" | tee -a "$LOG_FILE"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1" | tee -a "$LOG_FILE"
}
send_alert() {
local subject="$1"
local message="$2"
if command -v mail >/dev/null 2>&1; then
echo "$message" | mail -s "$subject" "$ALERT_EMAIL"
fi
# Log the alert
log_error "ALERT: $subject - $message"
}
check_system_resources() {
log_info "Checking system resources..."
# CPU usage
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | awk -F'%' '{print $1}')
if (( $(echo "$CPU_USAGE > $CPU_THRESHOLD" | bc -l) )); then
send_alert "High CPU Usage" "CPU usage is ${CPU_USAGE}% (threshold: ${CPU_THRESHOLD}%)"
fi
# Memory usage
MEMORY_USAGE=$(free | grep Mem | awk '{printf("%.1f", ($3/$2) * 100.0)}')
if (( $(echo "$MEMORY_USAGE > $MEMORY_THRESHOLD" | bc -l) )); then
send_alert "High Memory Usage" "Memory usage is ${MEMORY_USAGE}% (threshold: ${MEMORY_THRESHOLD}%)"
fi
# Disk usage
DISK_USAGE=$(df {{ wordpress_path }} | tail -1 | awk '{print $5}' | sed 's/%//')
if (( DISK_USAGE > DISK_THRESHOLD )); then
send_alert "High Disk Usage" "Disk usage is ${DISK_USAGE}% (threshold: ${DISK_THRESHOLD}%)"
fi
# Load average
LOAD_AVG=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | sed 's/,//')
if (( $(echo "$LOAD_AVG > $LOAD_THRESHOLD" | bc -l) )); then
send_alert "High Load Average" "Load average is ${LOAD_AVG} (threshold: ${LOAD_THRESHOLD})"
fi
log_info "System resources: CPU: ${CPU_USAGE}%, Memory: ${MEMORY_USAGE}%, Disk: ${DISK_USAGE}%, Load: ${LOAD_AVG}"
}
check_services() {
log_info "Checking services status..."
# Check Nginx
if ! systemctl is-active --quiet nginx; then
send_alert "Nginx Service Down" "Nginx service is not running"
fi
# Check PHP-FPM
if ! systemctl is-active --quiet php{{ php_version }}-fpm; then
send_alert "PHP-FPM Service Down" "PHP-FPM service is not running"
fi
# Check MySQL/MariaDB
if ! systemctl is-active --quiet mysql && ! systemctl is-active --quiet mariadb; then
send_alert "Database Service Down" "MySQL/MariaDB service is not running"
fi
# Check Redis (if enabled)
{% if enable_redis is defined and enable_redis %}
if ! systemctl is-active --quiet redis-server; then
log_warn "Redis service is not running"
fi
{% endif %}
log_info "All critical services are running"
}
check_website_performance() {
log_info "Checking website performance..."
# Test response time
RESPONSE_TIME=$(curl -o /dev/null -s -w '%{time_total}\n' http://localhost/ || echo "999")
if (( $(echo "$RESPONSE_TIME > $RESPONSE_TIME_THRESHOLD" | bc -l) )); then
send_alert "Slow Website Response" "Website response time is ${RESPONSE_TIME}s (threshold: ${RESPONSE_TIME_THRESHOLD}s)"
fi
# Test HTTP status
HTTP_STATUS=$(curl -o /dev/null -s -w '%{http_code}\n' http://localhost/ || echo "000")
if [[ "$HTTP_STATUS" != "200" ]]; then
send_alert "Website HTTP Error" "Website returned HTTP status $HTTP_STATUS"
fi
log_info "Website performance: Response time: ${RESPONSE_TIME}s, HTTP status: $HTTP_STATUS"
}
check_database_performance() {
log_info "Checking database performance..."
# Check MySQL connections
if command -v mysql >/dev/null 2>&1; then
MYSQL_CONNECTIONS=$(mysql -e "SHOW STATUS LIKE 'Threads_connected';" | tail -1 | awk '{print $2}' 2>/dev/null || echo "0")
MYSQL_MAX_CONNECTIONS=$(mysql -e "SHOW VARIABLES LIKE 'max_connections';" | tail -1 | awk '{print $2}' 2>/dev/null || echo "100")
CONNECTION_PERCENTAGE=$(( (MYSQL_CONNECTIONS * 100) / MYSQL_MAX_CONNECTIONS ))
if (( CONNECTION_PERCENTAGE > 80 )); then
send_alert "High Database Connections" "MySQL connections: ${MYSQL_CONNECTIONS}/${MYSQL_MAX_CONNECTIONS} (${CONNECTION_PERCENTAGE}%)"
fi
log_info "Database connections: ${MYSQL_CONNECTIONS}/${MYSQL_MAX_CONNECTIONS} (${CONNECTION_PERCENTAGE}%)"
fi
}
check_log_errors() {
log_info "Checking for recent errors..."
# Check Nginx error log
NGINX_ERRORS=$(tail -n 100 /var/log/nginx/error.log | grep -c "$(date '+%Y/%m/%d')" || echo "0")
if (( NGINX_ERRORS > 10 )); then
log_warn "Found $NGINX_ERRORS Nginx errors today"
fi
# Check PHP error log
if [[ -f /var/log/php{{ php_version }}-fpm.log ]]; then
PHP_ERRORS=$(tail -n 100 /var/log/php{{ php_version }}-fpm.log | grep -c "$(date '+%d-%b-%Y')" || echo "0")
if (( PHP_ERRORS > 10 )); then
log_warn "Found $PHP_ERRORS PHP-FPM errors today"
fi
fi
# Check WordPress debug log
if [[ -f "$WORDPRESS_PATH/wp-content/debug.log" ]]; then
WP_ERRORS=$(tail -n 100 "$WORDPRESS_PATH/wp-content/debug.log" | grep -c "$(date '+%d-%b-%Y')" || echo "0")
if (( WP_ERRORS > 5 )); then
log_warn "Found $WP_ERRORS WordPress errors today"
fi
fi
}
generate_performance_report() {
log_info "Generating performance report..."
cat > /tmp/wordpress-performance-report.txt <<EOF
WordPress Performance Report - $(date)
========================================
System Resources:
- CPU Usage: ${CPU_USAGE}%
- Memory Usage: ${MEMORY_USAGE}%
- Disk Usage: ${DISK_USAGE}%
- Load Average: ${LOAD_AVG}
Website Performance:
- Response Time: ${RESPONSE_TIME}s
- HTTP Status: ${HTTP_STATUS}
Database:
- Connections: ${MYSQL_CONNECTIONS}/${MYSQL_MAX_CONNECTIONS} (${CONNECTION_PERCENTAGE}%)
Error Counts (Today):
- Nginx Errors: ${NGINX_ERRORS}
- PHP Errors: ${PHP_ERRORS}
- WordPress Errors: ${WP_ERRORS}
Services Status:
- Nginx: $(systemctl is-active nginx)
- PHP-FPM: $(systemctl is-active php{{ php_version }}-fpm)
- MySQL/MariaDB: $(systemctl is-active mysql || systemctl is-active mariadb)
{% if enable_redis is defined and enable_redis %}
- Redis: $(systemctl is-active redis-server)
{% endif %}
Cache Status:
{% if nginx_fastcgi_cache_enabled | default(true) %}
- FastCGI Cache Size: $(du -sh /var/cache/nginx 2>/dev/null | awk '{print $1}' || echo "N/A")
{% endif %}
{% if enable_redis is defined and enable_redis %}
- Redis Memory Usage: $(redis-cli info memory | grep used_memory_human | cut -d: -f2 | tr -d '\r' || echo "N/A")
{% endif %}
EOF
# Optionally email the report
if [[ "${SEND_DAILY_REPORT:-false}" == "true" ]]; then
mail -s "WordPress Performance Report - $(date +%Y-%m-%d)" "$ALERT_EMAIL" < /tmp/wordpress-performance-report.txt
fi
}
main() {
log_info "Starting WordPress performance monitoring..."
check_system_resources
check_services
check_website_performance
check_database_performance
check_log_errors
generate_performance_report
log_info "Performance monitoring completed"
}
# Run the monitoring
main "$@"