Monitoring and Performance Testing

Monitoring and Performance Testing

Implement comprehensive monitoring:

#!/bin/bash
# performance-monitor.sh

# Apache status
if command -v apache2ctl &> /dev/null; then
    echo "=== Apache Performance Status ==="
    apache2ctl -S
    curl -s http://localhost/server-status?auto | grep -E "^(Total accesses|Total kBytes|Uptime|ReqPerSec|BytesPerSec|BusyWorkers|IdleWorkers)"
fi

# Nginx status
if command -v nginx &> /dev/null; then
    echo -e "\n=== Nginx Performance Status ==="
    nginx -V 2>&1 | grep -o "with-http_stub_status_module" && \
    curl -s http://localhost/nginx_status
fi

# System resources
echo -e "\n=== System Resources ==="
free -h
df -h /var/cache/nginx /var/cache/apache2
ps aux | grep -E "(apache2|nginx|php)" | awk '{sum+=$6} END {print "Web server memory usage: " sum/1024 " MB"}'

# Connection statistics
echo -e "\n=== Connection Statistics ==="
ss -tan | awk '{print $1}' | sort | uniq -c
netstat -an | grep :443 | wc -l | xargs echo "HTTPS connections:"
netstat -an | grep :80 | wc -l | xargs echo "HTTP connections:"

# Cache hit rates
echo -e "\n=== Cache Performance ==="
if [ -f /var/log/nginx/access.log ]; then
    awk '{print $upstream_cache_status}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
fi