Tutorial

Nuevas actualizaciones y mejoras para Roundfleet.

Aviso importante

Los ejemplos de código y scripts proporcionados en estos tutoriales son solo para propósitos educativos. Roundfleet no es responsable de ningún problema, daño o vulnerabilidad de seguridad que pueda surgir del uso, modificación o implementación de estos ejemplos. Siempre revisa y prueba el código en un entorno seguro antes de usarlo en sistemas de producción.

Storage Management on macOS

Monitor and optimize storage across your Roundfleet devices using advanced disk usage analysis, automated cleanup policies, and comprehensive storage management. This tutorial provides enterprise-grade tools for implementing organizational storage policies and fleet-wide storage optimization.

Understanding macOS Storage Management

macOS provides several command-line tools for storage management:

  • du - Directory usage analysis and space consumption
  • df - Disk free space and filesystem information
  • diskutil - Disk utility for advanced disk operations
  • tmutil - Time Machine utility for backup storage
  • Storage Management - GUI storage optimization interface

Basic Storage Operations

Check User Storage Usage

#!/bin/bash

# Check disk usage for all users
echo "=== User Storage Usage ==="
sudo du -hxd1 /Users

echo "Storage usage check completed"

Check Filesystem Usage

#!/bin/bash

# Check overall filesystem usage
echo "=== Filesystem Usage ==="
df -h

# Check specific filesystem
# df -h /dev/disk1s1

echo "Filesystem usage check completed"

Check Current Directory Usage

#!/bin/bash

# Check current directory usage
echo "=== Current Directory Usage ==="
du -sh *

echo "Directory usage check completed"

Enterprise Storage Management System

#!/bin/bash

# Roundfleet Enterprise Storage Management System
# Comprehensive storage monitoring, optimization, and policy management

# Configuration
ROUNDFLEET_DIR="/etc/roundfleet"
STORAGE_DIR="$ROUNDFLEET_DIR/storage_management"
REPORTS_DIR="$ROUNDFLEET_DIR/reports"
COMPLIANCE_DIR="$ROUNDFLEET_DIR/compliance"
AUDIT_DIR="$ROUNDFLEET_DIR/audit"
LOG_FILE="/var/log/roundfleet_storage_management.log"
POLICIES_DIR="$ROUNDFLEET_DIR/storage_policies"
CLEANUP_DIR="$ROUNDFLEET_DIR/cleanup_logs"

# Create directory structure
create_directories() {
    local dirs=("$ROUNDFLEET_DIR" "$STORAGE_DIR" "$REPORTS_DIR" "$COMPLIANCE_DIR" "$AUDIT_DIR" "$POLICIES_DIR" "$CLEANUP_DIR")
    for dir in "${dirs[@]}"; do
        [[ ! -d "$dir" ]] && mkdir -p "$dir"
    done
}

# Logging function
log_action() {
    local message="$1"
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    echo "[$timestamp] $message" | tee -a "$LOG_FILE"
}

# Storage Categories for Enterprise Management
declare -A STORAGE_CATEGORIES=(
    ["system_critical"]="macOS,system_apps,kernel_extensions,system_logs"
    ["user_data"]="documents,downloads,desktop,pictures,movies"
    ["application_data"]="app_support,caches,preferences,saved_states"
    ["development"]="xcode,simulators,build_artifacts,source_code"
    ["media_content"]="photos,videos,music,podcasts"
    ["temporary_files"]="caches,logs,trash,temp_downloads"
    ["backup_data"]="time_machine,local_snapshots,manual_backups"
)

# Storage Policies for Different Environments
declare -A STORAGE_POLICIES=(
    ["aggressive_cleanup"]="clear_caches,remove_logs_7days,empty_trash,remove_downloads_30days"
    ["conservative_cleanup"]="clear_caches,remove_logs_30days,archive_old_files"
    ["development_optimized"]="preserve_builds,clear_simulator_data,optimize_xcode_cache"
    ["media_optimized"]="compress_videos,optimize_photos,remove_duplicates"
    ["security_focused"]="secure_delete,encrypt_backups,audit_sensitive_files"
    ["compliance_strict"]="retain_all,encrypt_storage,detailed_logging,no_auto_delete"
)

# Disk Space Thresholds for Different Alert Levels
declare -A ALERT_THRESHOLDS=(
    ["critical"]=5     # Less than 5GB free
    ["warning"]=10     # Less than 10GB free
    ["low"]=20         # Less than 20GB free
    ["optimal"]=50     # Less than 50GB free
)

# Comprehensive storage analysis
analyze_storage_comprehensive() {
    local analysis_level="$1"
    local output_file="$STORAGE_DIR/storage_analysis_$(date +%Y%m%d_%H%M%S).json"
    
    log_action "Starting comprehensive storage analysis: $analysis_level"
    
    # Get basic filesystem information
    local total_space=$(df -h / | awk 'NR==2 {print $2}')
    local used_space=$(df -h / | awk 'NR==2 {print $3}')
    local available_space=$(df -h / | awk 'NR==2 {print $4}')
    local usage_percentage=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
    
    # Convert to bytes for calculations
    local available_gb=$(df -g / | awk 'NR==2 {print $4}')
    
    # Determine alert level
    local alert_level="optimal"
    if [[ $available_gb -lt ${ALERT_THRESHOLDS["critical"]} ]]; then
        alert_level="critical"
    elif [[ $available_gb -lt ${ALERT_THRESHOLDS["warning"]} ]]; then
        alert_level="warning"
    elif [[ $available_gb -lt ${ALERT_THRESHOLDS["low"]} ]]; then
        alert_level="low"
    fi
    
    # Analyze user directories
    local user_data=""
    while IFS= read -r user_dir; do
        if [[ -d "$user_dir" && ! "$user_dir" =~ (Shared|.localized)$ ]]; then
            local user_name=$(basename "$user_dir")
            local user_size=$(du -sg "$user_dir" 2>/dev/null | awk '{print $1}' || echo "0")
            user_data="$user_data{\"user\":\"$user_name\",\"size_gb\":$user_size},"
        fi
    done <<< "$(find /Users -maxdepth 1 -type d)"
    
    # Remove trailing comma
    user_data="${user_data%,}"
    
    # Analyze large directories
    local large_dirs=""
    if [[ "$analysis_level" == "detailed" || "$analysis_level" == "full" ]]; then
        while IFS= read -r dir_info; do
            local size=$(echo "$dir_info" | awk '{print $1}')
            local path=$(echo "$dir_info" | awk '{$1=""; print substr($0,2)}')
            [[ $size -gt 1 ]] && large_dirs="$large_dirs{\"path\":\"$path\",\"size_gb\":$size},"
        done <<< "$(sudo du -sg /Applications /Library /System /Users 2>/dev/null | sort -nr)"
        large_dirs="${large_dirs%,}"
    fi
    
    # Analyze system caches and temporary files
    local temp_data=""
    local cache_size=$(du -sg ~/Library/Caches 2>/dev/null | awk '{print $1}' || echo "0")
    local log_size=$(du -sg /var/log 2>/dev/null | awk '{print $1}' || echo "0")
    local trash_size=$(du -sg ~/.Trash 2>/dev/null | awk '{print $1}' || echo "0")
    
    temp_data="{\"caches_gb\":$cache_size,\"logs_gb\":$log_size,\"trash_gb\":$trash_size}"
    
    # Create comprehensive report
    cat > "$output_file" << EOF
{
  "storage_analysis": {
    "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
    "analysis_level": "$analysis_level",
    "alert_level": "$alert_level",
    "filesystem_info": {
      "total_space": "$total_space",
      "used_space": "$used_space",
      "available_space": "$available_space",
      "usage_percentage": $usage_percentage,
      "available_gb": $available_gb
    },
    "user_breakdown": [$user_data],
    "large_directories": [$large_dirs],
    "temporary_files": $temp_data
  },
  "recommendations": [
    $([ $alert_level == "critical" ] && echo "\"Immediate cleanup required - critical storage level\",")
    $([ $alert_level == "warning" ] && echo "\"Storage cleanup recommended\",")
    $([ $cache_size -gt 5 ] && echo "\"Clear application caches ($cache_size GB)\",")
    $([ $trash_size -gt 1 ] && echo "\"Empty trash ($trash_size GB)\",")
    $([ $log_size -gt 2 ] && echo "\"Archive old log files ($log_size GB)\",")
    "\"Regular storage monitoring recommended\""
  ]
}
EOF
    
    log_action "Storage analysis completed. Report: $output_file"
    
    # Alert if critical
    if [[ "$alert_level" == "critical" ]]; then
        log_action "CRITICAL ALERT: Storage space critically low ($available_gb GB remaining)"
        # Send alert notification
        osascript -e "display notification \"Critical storage space: $available_gb GB remaining\" with title \"Roundfleet Storage Alert\""
    fi
    
    echo "$output_file"
}

# Advanced storage cleanup with policies
perform_storage_cleanup() {
    local cleanup_policy="$1"
    local dry_run="${2:-false}"
    local cleanup_report="$CLEANUP_DIR/cleanup_$(date +%Y%m%d_%H%M%S).json"
    
    log_action "Starting storage cleanup with policy: $cleanup_policy (dry_run: $dry_run)"
    
    local total_freed=0
    local operations=""
    
    case "$cleanup_policy" in
        "aggressive_cleanup")
            # Clear application caches
            if [[ "$dry_run" == "false" ]]; then
                local cache_size_before=$(du -sg ~/Library/Caches 2>/dev/null | awk '{print $1}' || echo "0")
                find ~/Library/Caches -type f -mtime +1 -delete 2>/dev/null
                local cache_size_after=$(du -sg ~/Library/Caches 2>/dev/null | awk '{print $1}' || echo "0")
                local cache_freed=$((cache_size_before - cache_size_after))
                total_freed=$((total_freed + cache_freed))
                operations="$operations{\"operation\":\"clear_caches\",\"freed_gb\":$cache_freed},"
            fi
            
            # Remove old logs (7 days)
            if [[ "$dry_run" == "false" ]]; then
                local log_size_before=$(du -sg ~/Library/Logs 2>/dev/null | awk '{print $1}' || echo "0")
                find ~/Library/Logs -type f -mtime +7 -delete 2>/dev/null
                local log_size_after=$(du -sg ~/Library/Logs 2>/dev/null | awk '{print $1}' || echo "0")
                local log_freed=$((log_size_before - log_size_after))
                total_freed=$((total_freed + log_freed))
                operations="$operations{\"operation\":\"remove_old_logs\",\"freed_gb\":$log_freed},"
            fi
            
            # Empty trash
            if [[ "$dry_run" == "false" ]]; then
                local trash_size=$(du -sg ~/.Trash 2>/dev/null | awk '{print $1}' || echo "0")
                rm -rf ~/.Trash/*
                total_freed=$((total_freed + trash_size))
                operations="$operations{\"operation\":\"empty_trash\",\"freed_gb\":$trash_size},"
            fi
            
            # Remove old downloads (30 days)
            if [[ "$dry_run" == "false" ]]; then
                local downloads_size_before=$(du -sg ~/Downloads 2>/dev/null | awk '{print $1}' || echo "0")
                find ~/Downloads -type f -mtime +30 -delete 2>/dev/null
                local downloads_size_after=$(du -sg ~/Downloads 2>/dev/null | awk '{print $1}' || echo "0")
                local downloads_freed=$((downloads_size_before - downloads_size_after))
                total_freed=$((total_freed + downloads_freed))
                operations="$operations{\"operation\":\"remove_old_downloads\",\"freed_gb\":$downloads_freed},"
            fi
            ;;
            
        "development_optimized")
            # Clear Xcode derived data
            if [[ -d ~/Library/Developer/Xcode/DerivedData && "$dry_run" == "false" ]]; then
                local derived_size=$(du -sg ~/Library/Developer/Xcode/DerivedData 2>/dev/null | awk '{print $1}' || echo "0")
                rm -rf ~/Library/Developer/Xcode/DerivedData/*
                total_freed=$((total_freed + derived_size))
                operations="$operations{\"operation\":\"clear_xcode_derived_data\",\"freed_gb\":$derived_size},"
            fi
            
            # Clear iOS Simulator data
            if [[ -d ~/Library/Developer/CoreSimulator && "$dry_run" == "false" ]]; then
                local sim_size_before=$(du -sg ~/Library/Developer/CoreSimulator 2>/dev/null | awk '{print $1}' || echo "0")
                xcrun simctl delete unavailable
                local sim_size_after=$(du -sg ~/Library/Developer/CoreSimulator 2>/dev/null | awk '{print $1}' || echo "0")
                local sim_freed=$((sim_size_before - sim_size_after))
                total_freed=$((total_freed + sim_freed))
                operations="$operations{\"operation\":\"cleanup_simulator_data\",\"freed_gb\":$sim_freed},"
            fi
            ;;
            
        "media_optimized")
            # Find and report duplicate files
            if [[ "$dry_run" == "false" ]]; then
                log_action "Scanning for duplicate media files..."
                # This is a placeholder for duplicate detection logic
                operations="$operations{\"operation\":\"duplicate_scan\",\"freed_gb\":0},"
            fi
            
            # Optimize photo library (placeholder)
            log_action "Photo library optimization available through Photos app"
            operations="$operations{\"operation\":\"photo_optimization_recommended\",\"freed_gb\":0},"
            ;;
    esac
    
    # Remove trailing comma
    operations="${operations%,}"
    
    # Create cleanup report
    cat > "$cleanup_report" << EOF
{
  "cleanup_report": {
    "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
    "policy": "$cleanup_policy",
    "dry_run": "$dry_run",
    "total_freed_gb": $total_freed,
    "operations": [$operations]
  },
  "storage_after_cleanup": {
    "available_space": "$(df -h / | awk 'NR==2 {print $4}')",
    "usage_percentage": "$(df -h / | awk 'NR==2 {print $5}')"
  }
}
EOF
    
    if [[ "$dry_run" == "false" ]]; then
        log_action "Cleanup completed. Total space freed: ${total_freed}GB"
    else
        log_action "Dry run completed. Would free approximately: ${total_freed}GB"
    fi
    
    echo "$cleanup_report"
}

# Storage monitoring and alerting
monitor_storage_continuously() {
    local monitoring_interval="${1:-300}"  # 5 minutes default
    local alert_threshold="${2:-10}"       # 10GB default
    
    log_action "Starting continuous storage monitoring (interval: ${monitoring_interval}s, threshold: ${alert_threshold}GB)"
    
    while true; do
        local available_gb=$(df -g / | awk 'NR==2 {print $4}')
        local usage_percent=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
        
        if [[ $available_gb -lt $alert_threshold ]]; then
            log_action "STORAGE ALERT: Only ${available_gb}GB remaining (${usage_percent}% used)"
            
            # Send system notification
            osascript -e "display notification \"Low storage: ${available_gb}GB remaining\" with title \"Roundfleet Storage Monitor\""
            
            # Optionally trigger automatic cleanup
            if [[ $available_gb -lt 5 ]]; then
                log_action "Triggering emergency cleanup..."
                perform_storage_cleanup "aggressive_cleanup" "false"
            fi
        fi
        
        # Log current status
        log_action "Storage status: ${available_gb}GB available (${usage_percent}% used)"
        
        sleep "$monitoring_interval"
    done
}

# Enterprise storage reporting
generate_storage_report() {
    local report_type="$1"
    local report_file="$REPORTS_DIR/storage_enterprise_$(date +%Y%m%d_%H%M%S).json"
    
    log_action "Generating enterprise storage report: $report_type"
    
    # Collect comprehensive storage data
    local filesystem_data=$(df -h | grep -v "tmpfs\|devfs" | awk 'NR>1 {print "{\"filesystem\":\""$1"\",\"size\":\""$2"\",\"used\":\""$3"\",\"available\":\""$4"\",\"usage_percent\":\""$5"\"}"}' | tr '\n' ',' | sed 's/,$//')
    
    # Analyze user storage patterns
    local user_analysis=""
    local total_users=0
    local total_user_storage=0
    
    while IFS= read -r user_dir; do
        if [[ -d "$user_dir" && ! "$user_dir" =~ (Shared|.localized)$ ]]; then
            local user_name=$(basename "$user_dir")
            local user_size_gb=$(du -sg "$user_dir" 2>/dev/null | awk '{print $1}' || echo "0")
            ((total_users++))
            total_user_storage=$((total_user_storage + user_size_gb))
            user_analysis="$user_analysis{\"user\":\"$user_name\",\"storage_gb\":$user_size_gb},"
        fi
    done <<< "$(find /Users -maxdepth 1 -type d)"
    
    user_analysis="${user_analysis%,}"
    local avg_user_storage=$((total_user_storage / total_users))
    
    # Application storage analysis
    local app_storage=""
    if [[ -d /Applications ]]; then
        while IFS= read -r app_info; do
            local size=$(echo "$app_info" | awk '{print $1}')
            local app_path=$(echo "$app_info" | awk '{$1=""; print substr($0,2)}')
            local app_name=$(basename "$app_path")
            [[ $size -gt 0 ]] && app_storage="$app_storage{\"application\":\"$app_name\",\"size_gb\":$size},"
        done <<< "$(du -sg /Applications/*.app 2>/dev/null | sort -nr | head -20)"
        app_storage="${app_storage%,}"
    fi
    
    # System health indicators
    local health_indicators=""
    local available_gb=$(df -g / | awk 'NR==2 {print $4}')
    local health_status="healthy"
    
    if [[ $available_gb -lt 5 ]]; then
        health_status="critical"
    elif [[ $available_gb -lt 10 ]]; then
        health_status="warning"
    elif [[ $available_gb -lt 20 ]]; then
        health_status="low"
    fi
    
    health_indicators="{\"status\":\"$health_status\",\"available_gb\":$available_gb,\"recommendation\":\"$([ "$health_status" != "healthy" ] && echo "Cleanup required" || echo "Storage optimal")\"}"
    
    # Create comprehensive enterprise report
    cat > "$report_file" << EOF
{
  "enterprise_storage_report": {
    "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
    "report_type": "$report_type",
    "device_info": {
      "hostname": "$(hostname)",
      "mac_address": "$(ifconfig en0 | grep ether | awk '{print $2}')",
      "serial_number": "$(system_profiler SPHardwareDataType | grep 'Serial Number' | awk '{print $4}')"
    }
  },
  "storage_overview": {
    "filesystems": [$filesystem_data],
    "health_status": $health_indicators
  },
  "user_analysis": {
    "total_users": $total_users,
    "total_user_storage_gb": $total_user_storage,
    "average_user_storage_gb": $avg_user_storage,
    "user_breakdown": [$user_analysis]
  },
  "application_analysis": {
    "top_applications": [$app_storage]
  },
  "compliance_info": {
    "data_retention_policy": "enterprise_standard",
    "encryption_status": "$(fdesetup status | grep -q "On" && echo "enabled" || echo "disabled")",
    "backup_status": "$(tmutil status | grep -q "Running" && echo "active" || echo "inactive")"
  },
  "recommendations": [
    $([ "$health_status" == "critical" ] && echo "\"Immediate storage cleanup required\",")
    $([ $total_user_storage -gt 100 ] && echo "\"Consider user storage policies\",")
    $([ $available_gb -lt 20 ] && echo "\"Implement automated cleanup policies\",")
    "\"Regular storage monitoring recommended\""
  ]
}
EOF
    
    log_action "storage report generated: $report_file"
    echo "$report_file"
}

# Storage policy enforcement
enforce_storage_policies() {
    local policy_level="$1"
    
    log_action "Enforcing storage policies: $policy_level"
    
    case "$policy_level" in
        "enterprise_standard")
            # Standard enterprise storage policies
            log_action "Applying enterprise standard storage policies"
            
            # Set up automatic cleanup schedules
            cat > /tmp/roundfleet_storage_cleanup.plist << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.roundfleet.storage.cleanup</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>-c</string>
        <string>$(realpath "$0") perform_storage_cleanup conservative_cleanup false</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>2</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
    <key>StandardOutPath</key>
    <string>/var/log/roundfleet_storage_cleanup.log</string>
    <key>StandardErrorPath</key>
    <string>/var/log/roundfleet_storage_cleanup.log</string>
</dict>
</plist>
EOF
            sudo mv /tmp/roundfleet_storage_cleanup.plist /Library/LaunchDaemons/
            sudo launchctl load /Library/LaunchDaemons/com.roundfleet.storage.cleanup.plist
            ;;
            
        "development_team")
            # Policies optimized for development teams
            log_action "Applying development team storage policies"
            perform_storage_cleanup "development_optimized" "false"
            ;;
            
        "media_production")
            # Policies for media production environments
            log_action "Applying media production storage policies"
            perform_storage_cleanup "media_optimized" "false"
            ;;
    esac
    
    log_action "Storage policies enforcement completed: $policy_level"
}

# Storage optimization recommendations
generate_optimization_recommendations() {
    local analysis_file="$1"
    local recommendations_file="$REPORTS_DIR/storage_recommendations_$(date +%Y%m%d_%H%M%S).json"
    
    log_action "Generating storage optimization recommendations"
    
    # Analyze current storage state
    local available_gb=$(df -g / | awk 'NR==2 {print $4}')
    local usage_percent=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
    
    # Check for common storage issues
    local cache_size=$(du -sg ~/Library/Caches 2>/dev/null | awk '{print $1}' || echo "0")
    local downloads_size=$(du -sg ~/Downloads 2>/dev/null | awk '{print $1}' || echo "0")
    local trash_size=$(du -sg ~/.Trash 2>/dev/null | awk '{print $1}' || echo "0")
    local log_size=$(sudo du -sg /var/log 2>/dev/null | awk '{print $1}' || echo "0")
    
    # Generate recommendations based on analysis
    local recommendations=""
    local priority="medium"
    
    if [[ $available_gb -lt 10 ]]; then
        priority="high"
        recommendations="$recommendations\"Immediate cleanup required - storage critically low\","
    fi
    
    if [[ $cache_size -gt 5 ]]; then
        recommendations="$recommendations\"Clear application caches (${cache_size}GB potential savings)\","
    fi
    
    if [[ $downloads_size -gt 10 ]]; then
        recommendations="$recommendations\"Review Downloads folder (${downloads_size}GB)\","
    fi
    
    if [[ $trash_size -gt 1 ]]; then
        recommendations="$recommendations\"Empty Trash (${trash_size}GB immediate savings)\","
    fi
    
    if [[ $log_size -gt 5 ]]; then
        recommendations="$recommendations\"Archive old log files (${log_size}GB)\","
    fi
    
    if [[ $usage_percent -gt 80 ]]; then
        recommendations="$recommendations\"Consider external storage or cloud archiving\","
    fi
    
    # Remove trailing comma
    recommendations="${recommendations%,}"
    
    cat > "$recommendations_file" << EOF
{
  "optimization_recommendations": {
    "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
    "priority_level": "$priority",
    "current_status": {
      "available_gb": $available_gb,
      "usage_percent": $usage_percent,
      "storage_health": "$([ $available_gb -gt 20 ] && echo "healthy" || echo "attention_required")"
    },
    "immediate_actions": [$recommendations],
    "long_term_strategies": [
      "Implement automated cleanup policies",
      "Set up storage monitoring alerts",
      "Consider cloud storage integration",
      "Regular storage health assessments"
    ],
    "potential_savings": {
      "caches_gb": $cache_size,
      "downloads_gb": $downloads_size,
      "trash_gb": $trash_size,
      "logs_gb": $log_size,
      "total_potential_gb": $((cache_size + downloads_size + trash_size + log_size))
    }
  }
}
EOF
    
    log_action "Storage optimization recommendations generated: $recommendations_file"
    echo "$recommendations_file"
}

# Fleet storage management
deploy_storage_management_to_fleet() {
    local fleet_file="$1"
    local management_policy="$2"
    
    if [[ ! -f "$fleet_file" ]]; then
        log_action "ERROR: Fleet file not found: $fleet_file"
        return 1
    fi
    
    log_action "Deploying storage management to fleet with policy: $management_policy"
    
    while IFS= read -r host; do
        [[ -z "$host" || "$host" =~ ^#.*$ ]] && continue
        
        echo "Deploying storage management to: $host"
        
        # Deploy storage management script to remote host
        ssh "$host" "bash -s" << EOF
#!/bin/bash
# Remote deployment of storage management: $management_policy

# Create Roundfleet directories
mkdir -p /etc/roundfleet/{storage_management,reports,compliance,audit,storage_policies,cleanup_logs}

# Set up basic storage monitoring
echo "Setting up storage monitoring for policy: $management_policy"

case "$management_policy" in
    "enterprise_standard")
        # Set up enterprise-grade monitoring
        echo "Configuring enterprise storage monitoring"
        ;;
    "development_optimized")
        # Set up development-focused monitoring
        echo "Configuring development storage optimization"
        ;;
    "media_production")
        # Set up media production monitoring
        echo "Configuring media production storage management"
        ;;
esac

echo "Storage management deployment completed on \$(hostname)"
EOF
        
        if [[ $? -eq 0 ]]; then
            log_action "Successfully deployed storage management to: $host"
        else
            log_action "Failed to deploy storage management to: $host"
        fi
        
    done < "$fleet_file"
    
    log_action "Fleet storage management deployment completed"
}

# Health check and system validation
perform_storage_health_check() {
    echo "=== Roundfleet Storage Management Health Check ==="
    
    # Check disk space
    local available_gb=$(df -g / | awk 'NR==2 {print $4}')
    local usage_percent=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
    
    if [[ $available_gb -gt 20 ]]; then
        echo "✓ Disk space: Healthy (${available_gb}GB available)"
    elif [[ $available_gb -gt 10 ]]; then
        echo "⚠️  Disk space: Low (${available_gb}GB available)"
    else
        echo "🔴 Disk space: Critical (${available_gb}GB available)"
    fi
    
    # Check for large cache files
    local cache_size=$(du -sg ~/Library/Caches 2>/dev/null | awk '{print $1}' || echo "0")
    if [[ $cache_size -gt 5 ]]; then
        echo "⚠️  Large caches detected: ${cache_size}GB"
    else
        echo "✓ Cache size: Normal (${cache_size}GB)"
    fi
    
    # Check trash
    local trash_size=$(du -sg ~/.Trash 2>/dev/null | awk '{print $1}' || echo "0")
    if [[ $trash_size -gt 1 ]]; then
        echo "○ Trash contains: ${trash_size}GB (can be emptied)"
    else
        echo "✓ Trash: Empty"
    fi
    
    # Check FileVault encryption
    if fdesetup status | grep -q "On"; then
        echo "✓ FileVault: Enabled"
    else
        echo "○ FileVault: Disabled"
    fi
    
    # Check Time Machine
    if tmutil status | grep -q "Running"; then
        echo "✓ Time Machine: Active"
    else
        echo "○ Time Machine: Inactive"
    fi
    
    # Check APFS snapshots
    local snapshots=$(tmutil listlocalsnapshots / | wc -l)
    if [[ $snapshots -gt 10 ]]; then
        echo "⚠️  Many local snapshots: $snapshots (consider cleanup)"
    else
        echo "✓ Local snapshots: $snapshots"
    fi
}

# Main execution function
main() {
    create_directories
    
    case "${1:-}" in
        "analyze")
            analyze_storage_comprehensive "${2:-standard}"
            ;;
        "cleanup")
            perform_storage_cleanup "${2:-conservative_cleanup}" "${3:-false}"
            ;;
        "monitor")
            monitor_storage_continuously "${2:-300}" "${3:-10}"
            ;;
        "report")
            generate_storage_report "${2:-comprehensive}"
            ;;
        "enforce_policies")
            enforce_storage_policies "${2:-enterprise_standard}"
            ;;
        "recommendations")
            generate_optimization_recommendations "$2"
            ;;
        "deploy_fleet")
            deploy_storage_management_to_fleet "$2" "${3:-enterprise_standard}"
            ;;
        "health_check")
            perform_storage_health_check
            ;;
        "help"|*)
            echo "Roundfleet Enterprise Storage Management System"
            echo ""
            echo "Usage: $0 <command> [options]"
            echo ""
            echo "Commands:"
            echo "  analyze [level]                     - Analyze storage (basic|standard|detailed|full)"
            echo "  cleanup <policy> [dry_run]          - Perform cleanup (aggressive_cleanup|conservative_cleanup|development_optimized|media_optimized) [true|false]"
            echo "  monitor [interval] [threshold]      - Continuous monitoring (interval in seconds, threshold in GB)"
            echo "  report [type]                       - Generate enterprise report (comprehensive|summary|compliance)"
            echo "  enforce_policies [policy]           - Enforce storage policies (enterprise_standard|development_team|media_production)"
            echo "  recommendations [analysis_file]     - Generate optimization recommendations"
            echo "  deploy_fleet <fleet_file> [policy]  - Deploy to fleet"
            echo "  health_check                        - Perform system health check"
            echo ""
            echo "Examples:"
            echo "  $0 analyze detailed"
            echo "  $0 cleanup aggressive_cleanup false"
            echo "  $0 monitor 600 15"
            echo "  $0 report comprehensive"
            echo "  $0 health_check"
            ;;
    esac
}

# Execute main function
main "$@"

Storage Analysis and Monitoring

The enterprise system provides comprehensive storage analysis:

Analysis LevelScopeDetailsUse Case
BasicOverall disk usageQuick space checkDaily monitoring
StandardUser and system breakdownDetailed usage patternsWeekly reviews
DetailedLarge directories analysisComprehensive inventoryMonthly audits
FullComplete system analysisEnterprise reportingQuarterly assessments

Storage Cleanup Policies

Aggressive Cleanup Policy

# Perform aggressive cleanup to reclaim maximum space
./storage_manager.sh cleanup aggressive_cleanup false

# Dry run to see what would be cleaned
./storage_manager.sh cleanup aggressive_cleanup true

Development-Optimized Cleanup

# Clean development-specific cache and build data
./storage_manager.sh cleanup development_optimized false

Media Production Cleanup

# Optimize storage for media production workflows
./storage_manager.sh cleanup media_optimized false

Enterprise Storage Monitoring

Continuous Monitoring

# Monitor storage every 5 minutes with 10GB threshold
./storage_manager.sh monitor 300 10

# Monitor storage every hour with 20GB threshold
./storage_manager.sh monitor 3600 20

Automated Alerts

The system provides intelligent alerting:

  • Critical Alert - Less than 5GB remaining
  • Warning Alert - Less than 10GB remaining
  • Low Storage - Less than 20GB remaining
  • Optimal - More than 50GB remaining

Storage Policy Management

Enterprise Standard Policy

# Apply enterprise standard storage policies
./storage_manager.sh enforce_policies enterprise_standard

Development Team Policy

# Apply development-focused storage policies
./storage_manager.sh enforce_policies development_team

Media Production Policy

# Apply media production storage policies
./storage_manager.sh enforce_policies media_production

Fleet Deployment Examples

Deploy to Development Fleet

Create a fleet file dev_fleet.txt:

dev-mac-01.company.com
dev-mac-02.company.com
dev-mac-03.company.com
# Deploy development-optimized storage management
./storage_manager.sh deploy_fleet dev_fleet.txt development_team

Deploy to Production Fleet

# Deploy enterprise standard storage management
./storage_manager.sh deploy_fleet prod_fleet.txt enterprise_standard

Advanced Storage Features

Storage Analytics and Reporting

The system provides comprehensive enterprise reporting:

  • Usage patterns across users and applications
  • Cost analysis for storage optimization
  • Compliance reporting for regulatory requirements
  • Trend analysis for capacity planning

Automated Optimization

Advanced optimization features include:

  • Duplicate file detection and removal
  • Cache management with intelligent cleanup
  • Log rotation and archival
  • Backup optimization and verification

Security and Compliance

Enterprise security features:

  • Encrypted storage verification and monitoring
  • Audit trails for all storage operations
  • Data retention policy enforcement
  • Secure deletion for sensitive data

Important Storage Considerations

  • FileVault encryption should be enabled for enterprise security
  • Time Machine backups require adequate external storage
  • APFS snapshots consume storage and should be monitored
  • Application caches can grow large and require regular cleanup
  • User education helps maintain optimal storage usage

Compliance and Governance

The enterprise system supports various compliance frameworks:

  • GDPR Compliance - Data retention and secure deletion
  • HIPAA Requirements - Healthcare data storage security
  • SOX Compliance - Financial data storage controls
  • ISO 27001 - Information security storage management

Performance Optimization

Storage performance optimization includes:

  • SSD health monitoring and optimization
  • APFS optimization for modern file systems
  • Cache management for improved performance
  • Defragmentation recommendations when needed

Testing and Validation

Before enterprise deployment:

  1. Test cleanup policies on non-production systems
  2. Verify monitoring thresholds are appropriate for your environment
  3. Confirm alert mechanisms work correctly
  4. Test recovery procedures for critical storage situations
  5. Validate compliance reporting meets organizational requirements

This comprehensive system transforms basic disk usage commands into an enterprise-grade storage management platform with advanced monitoring, automated optimization, and fleet-wide management capabilities.