mb-admin/scripts/litespeed_metrics.sh

220 lines
6.4 KiB
Bash

#!/bin/bash
# LiteSpeed Cache Critical Metrics Monitor
# Captures the 4 most important performance metrics
# Find WP-CLI location
WP_CLI_PATH=""
if [ -f "/home/litespeed/bin/wp" ]; then
WP_CLI_PATH="/home/litespeed/bin/wp"
elif command -v wp >/dev/null 2>&1; then
WP_CLI_PATH="wp"
else
echo '{"error": "WP-CLI not found. Please ensure WP-CLI is installed and accessible."}'
exit 1
fi
# Function to run WP-CLI commands
run_wp_command() {
$WP_CLI_PATH --path=/var/www/webroot/ROOT "$@" 2>/dev/null
}
# Initialize JSON output
echo "{"
echo ' "timestamp": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'",'
echo ' "litespeed_metrics": {'
# 1. CACHE STATUS & HIT RATE
echo ' "cache_status": {'
cache_enabled=$(run_wp_command litespeed-option get cache)
cache_ttl_pub=$(run_wp_command litespeed-option get cache-ttl_pub)
cache_ttl_priv=$(run_wp_command litespeed-option get cache-ttl_priv)
cache_browser=$(run_wp_command litespeed-option get cache-browser)
echo ' "enabled": "'${cache_enabled:-"unknown"}'",'
echo ' "public_ttl_seconds": "'${cache_ttl_pub:-"0"}'",'
echo ' "private_ttl_seconds": "'${cache_ttl_priv:-"0"}'",'
echo ' "browser_cache": "'${cache_browser:-"unknown"}'",'
# Get cache effectiveness from debug if available
cache_effectiveness="unknown"
if run_wp_command litespeed-option get debug 2>/dev/null | grep -q "1"; then
cache_effectiveness="debug_enabled"
fi
echo ' "effectiveness": "'$cache_effectiveness'"'
echo ' },'
# 2. IMAGE OPTIMIZATION METRICS
echo ' "image_optimization": {'
img_status=$(run_wp_command litespeed-image status 2>/dev/null)
# Parse image optimization status
if [[ -n "$img_status" ]]; then
# Extract key metrics from status output
total_images=$(echo "$img_status" | grep -o "Total.*[0-9]\+" | grep -o "[0-9]\+" | head -1)
optimized_images=$(echo "$img_status" | grep -o "Optimized.*[0-9]\+" | grep -o "[0-9]\+" | head -1)
pending_images=$(echo "$img_status" | grep -o "Pending.*[0-9]\+" | grep -o "[0-9]\+" | head -1)
echo ' "total_images": "'${total_images:-"0"}'",'
echo ' "optimized_images": "'${optimized_images:-"0"}'",'
echo ' "pending_images": "'${pending_images:-"0"}'",'
# Calculate optimization percentage
if [[ -n "$total_images" && "$total_images" -gt 0 && -n "$optimized_images" ]]; then
optimization_percentage=$(awk "BEGIN {printf \"%.1f\", ($optimized_images / $total_images) * 100}")
else
optimization_percentage="0.0"
fi
echo ' "optimization_percentage": "'$optimization_percentage'%",'
echo ' "status": "available"'
else
echo ' "total_images": "0",'
echo ' "optimized_images": "0",'
echo ' "pending_images": "0",'
echo ' "optimization_percentage": "0.0%",'
echo ' "status": "unavailable"'
fi
echo ' },'
# 3. CDN STATUS & PERFORMANCE
echo ' "cdn_performance": {'
cdn_status=$(run_wp_command litespeed-online cdn_status 2>/dev/null)
active_nodes=$(run_wp_command litespeed-online nodes 2>/dev/null)
if [[ -n "$cdn_status" ]]; then
# Parse CDN status
if echo "$cdn_status" | grep -q -i "enabled\|active"; then
cdn_enabled="true"
else
cdn_enabled="false"
fi
# Count active nodes
node_count=$(echo "$active_nodes" | wc -l)
if [[ "$node_count" -le 1 ]]; then
node_count="0"
fi
echo ' "enabled": "'$cdn_enabled'",'
echo ' "active_nodes": "'$node_count'",'
echo ' "status": "connected"'
else
echo ' "enabled": "false",'
echo ' "active_nodes": "0",'
echo ' "status": "disconnected"'
fi
echo ' },'
# 4. QUIC.CLOUD SERVICE USAGE
echo ' "quic_cloud_services": {'
# Sync service data first
run_wp_command litespeed-online sync >/dev/null 2>&1
services_list=$(run_wp_command litespeed-online services 2>/dev/null)
if [[ -n "$services_list" ]]; then
# Parse services - look for common service indicators
img_optm_available="false"
cdn_available="false"
if echo "$services_list" | grep -q -i "image\|img_optm"; then
img_optm_available="true"
fi
if echo "$services_list" | grep -q -i "cdn"; then
cdn_available="true"
fi
# Count total services
service_count=$(echo "$services_list" | grep -c ":" 2>/dev/null || echo "0")
echo ' "image_optimization_available": "'$img_optm_available'",'
echo ' "cdn_available": "'$cdn_available'",'
echo ' "total_services": "'$service_count'",'
echo ' "connection_status": "connected",'
echo ' "last_sync": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"'
else
echo ' "image_optimization_available": "false",'
echo ' "cdn_available": "false",'
echo ' "total_services": "0",'
echo ' "connection_status": "disconnected",'
echo ' "last_sync": "never"'
fi
echo ' }'
echo ' },'
# SUMMARY HEALTH SCORE
echo ' "health_summary": {'
# Calculate overall health score (0-100)
health_score=0
# Cache enabled adds 30 points
if [[ "$cache_enabled" == "1" ]]; then
health_score=$((health_score + 30))
fi
# Image optimization adds 25 points
if [[ "$optimization_percentage" != "0.0%" ]]; then
health_score=$((health_score + 25))
fi
# CDN enabled adds 25 points
if [[ "$cdn_enabled" == "true" ]]; then
health_score=$((health_score + 25))
fi
# QUIC.cloud connected adds 20 points
if [[ "$service_count" -gt 0 ]]; then
health_score=$((health_score + 20))
fi
# Determine health status
if [[ $health_score -ge 80 ]]; then
health_status="excellent"
elif [[ $health_score -ge 60 ]]; then
health_status="good"
elif [[ $health_score -ge 40 ]]; then
health_status="fair"
else
health_status="poor"
fi
echo ' "score": "'$health_score'/100",'
echo ' "status": "'$health_status'",'
# Recommendations
recommendations=()
if [[ "$cache_enabled" != "1" ]]; then
recommendations+=("Enable LiteSpeed Cache")
fi
if [[ "$optimization_percentage" == "0.0%" ]]; then
recommendations+=("Configure Image Optimization")
fi
if [[ "$cdn_enabled" != "true" ]]; then
recommendations+=("Enable CDN")
fi
if [[ "$service_count" -eq 0 ]]; then
recommendations+=("Connect to QUIC.cloud Services")
fi
echo ' "recommendations": ['
for i in "${!recommendations[@]}"; do
echo -n ' "'${recommendations[$i]}'"'
if [[ $i -lt $((${#recommendations[@]} - 1)) ]]; then
echo ","
else
echo ""
fi
done
echo ' ]'
echo ' }'
echo "}"