#!/bin/bash
#
# Targeted Performance Discovery Script
# Fast collection of performance metrics and resource usage
#

set -euo pipefail

TIMESTAMP=$(date +%Y%m%d_%H%M%S)
HOSTNAME=$(hostname -f)
OUTPUT_DIR="/tmp/performance_discovery_${HOSTNAME}_${TIMESTAMP}"
mkdir -p "$OUTPUT_DIR"
LOG_FILE="${OUTPUT_DIR}/performance.log"

exec > >(tee -a "$LOG_FILE") 2>&1
echo "Starting Performance Discovery on ${HOSTNAME} at $(date)"
echo "Output: $OUTPUT_DIR"
echo "=========================================="

# System load and uptime
echo "1. System Load & Uptime"
uptime > "$OUTPUT_DIR/uptime.txt"
cat /proc/loadavg > "$OUTPUT_DIR/load_average.txt"
w > "$OUTPUT_DIR/who_load.txt"

# CPU information and usage
echo "2. CPU Information & Usage"
lscpu > "$OUTPUT_DIR/cpu_info.txt"
cat /proc/cpuinfo | grep -E "(processor|model name|cpu MHz|cache size)" > "$OUTPUT_DIR/cpu_details.txt"
top -b -n1 | head -20 > "$OUTPUT_DIR/cpu_top.txt"

# Memory usage
echo "3. Memory Usage"
free -h > "$OUTPUT_DIR/memory_free.txt"
cat /proc/meminfo > "$OUTPUT_DIR/memory_detailed.txt"
ps aux --sort=-%mem | head -20 > "$OUTPUT_DIR/memory_top_processes.txt"

# Disk I/O and usage
echo "4. Disk I/O & Usage"
if command -v iostat >/dev/null 2>&1; then
    iostat -x 1 3 > "$OUTPUT_DIR/iostat.txt" 2>/dev/null || echo "iostat failed"
else
    echo "iostat not available" > "$OUTPUT_DIR/iostat.txt"
fi
df -h > "$OUTPUT_DIR/disk_usage.txt"
df -i > "$OUTPUT_DIR/inode_usage.txt"

# Network performance
echo "5. Network Performance"
if command -v ss >/dev/null 2>&1; then
    ss -s > "$OUTPUT_DIR/network_summary.txt"
    ss -tuln > "$OUTPUT_DIR/network_listening.txt"
else
    netstat -s > "$OUTPUT_DIR/network_summary.txt" 2>/dev/null || echo "netstat not available"
    netstat -tuln > "$OUTPUT_DIR/network_listening.txt" 2>/dev/null || echo "netstat not available"
fi

# Network interface statistics
cat /proc/net/dev > "$OUTPUT_DIR/network_interfaces.txt"
ip -s link > "$OUTPUT_DIR/interface_stats.txt" 2>/dev/null || ifconfig -a > "$OUTPUT_DIR/interface_stats.txt" 2>/dev/null

# Process information
echo "6. Process Information"
ps aux --sort=-%cpu | head -30 > "$OUTPUT_DIR/processes_by_cpu.txt"
ps aux --sort=-%mem | head -30 > "$OUTPUT_DIR/processes_by_memory.txt"
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -30 > "$OUTPUT_DIR/processes_detailed.txt"

# System services performance
echo "7. System Services"
systemctl list-units --type=service --state=running --no-pager > "$OUTPUT_DIR/running_services.txt"
systemctl list-units --failed --no-pager > "$OUTPUT_DIR/failed_services.txt"

# Docker performance (if available)
echo "8. Container Performance"
if command -v docker >/dev/null 2>&1; then
    docker system df > "$OUTPUT_DIR/docker_storage_usage.txt" 2>/dev/null || echo "Docker system df failed"
    docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}" > "$OUTPUT_DIR/docker_stats.txt" 2>/dev/null || echo "Docker stats failed"
    docker system events --since "1h" --until "now" > "$OUTPUT_DIR/docker_events.txt" 2>/dev/null || echo "No recent docker events"
else
    echo "Docker not available" > "$OUTPUT_DIR/docker_status.txt"
fi

# Kernel and system information
echo "9. Kernel & System Info"
uname -a > "$OUTPUT_DIR/kernel_info.txt"
cat /proc/version > "$OUTPUT_DIR/kernel_version.txt"
dmesg | tail -50 > "$OUTPUT_DIR/dmesg_recent.txt" 2>/dev/null || echo "dmesg not accessible"

# Resource limits
echo "10. Resource Limits"
ulimit -a > "$OUTPUT_DIR/ulimits.txt"
cat /proc/sys/fs/file-max > "$OUTPUT_DIR/file_max.txt" 2>/dev/null || echo "file-max not readable"
cat /proc/sys/fs/file-nr > "$OUTPUT_DIR/file_nr.txt" 2>/dev/null || echo "file-nr not readable"

# Temperature and hardware sensors (if available)
echo "11. Hardware Sensors"
if command -v sensors >/dev/null 2>&1; then
    sensors > "$OUTPUT_DIR/temperature_sensors.txt" 2>/dev/null || echo "sensors failed"
else
    echo "lm-sensors not available" > "$OUTPUT_DIR/temperature_sensors.txt"
fi

# Storage device performance
echo "12. Storage Performance"
if command -v smartctl >/dev/null 2>&1; then
    # Check primary storage device
    primary_disk=$(lsblk -d -o NAME,TYPE | grep disk | head -1 | awk '{print $1}')
    if [ ! -z "$primary_disk" ]; then
        smartctl -a "/dev/$primary_disk" > "$OUTPUT_DIR/smart_${primary_disk}.txt" 2>/dev/null || echo "SMART data not available for $primary_disk"
    fi
else
    echo "smartmontools not available" > "$OUTPUT_DIR/smart_status.txt"
fi

# System performance over time (brief sample)
echo "13. Performance Sampling"
echo "Sampling system performance for 30 seconds..."
{
    echo "=== CPU Usage Sample ==="
    sar 5 6 2>/dev/null || vmstat 5 6 2>/dev/null || echo "No sar/vmstat available"
    echo "=== Load Average Sample ==="
    for i in {1..6}; do
        echo "$(date): $(cat /proc/loadavg)"
        sleep 5
    done
} > "$OUTPUT_DIR/performance_sample.txt" &

# Wait for sampling to complete
echo "Performance sampling running in background..."
wait

echo "Performance discovery completed at $(date)"
echo "Results in: $OUTPUT_DIR"
ls -la "$OUTPUT_DIR"