#!/bin/bash

# ceph_compression_stats.sh - Display formatted Ceph OSD compression statistics
# Usage: ceph_compression_stats.sh --osd <osd_number>

set -euo pipefail

# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color

# Default values
OSD_ID=""
SHOW_HELP=0

# Function to display usage
usage() {
    echo "Usage: $0 --osd <osd_number>"
    echo ""
    echo "Options:"
    echo "  --osd <number>    Specify OSD ID to check"
    echo "  -h, --help        Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 --osd 249"
    echo "  $0 --osd 15"
}

# Function to format bytes to human readable
format_bytes() {
    local bytes=$1
    if (( bytes >= 1073741824 )); then
        printf "%.2f GB" $(echo "scale=2; $bytes / 1073741824" | bc -l)
    elif (( bytes >= 1048576 )); then
        printf "%.2f MB" $(echo "scale=2; $bytes / 1048576" | bc -l)
    elif (( bytes >= 1024 )); then
        printf "%.2f KB" $(echo "scale=2; $bytes / 1024" | bc -l)
    else
        printf "%d B" $bytes
    fi
}

# Function to calculate percentage
calc_percentage() {
    local part=$1
    local total=$2
    if (( total > 0 )); then
        printf "%.1f%%" $(echo "scale=1; $part * 100 / $total" | bc -l)
    else
        printf "0.0%%"
    fi
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --osd)
            OSD_ID="$2"
            shift 2
            ;;
        -h|--help)
            SHOW_HELP=1
            shift
            ;;
        *)
            echo -e "${RED}Error: Unknown option $1${NC}" >&2
            usage
            exit 1
            ;;
    esac
done

# Show help if requested
if [[ $SHOW_HELP -eq 1 ]]; then
    usage
    exit 0
fi

# Validate OSD ID
if [[ -z "$OSD_ID" ]]; then
    echo -e "${RED}Error: OSD ID is required${NC}" >&2
    usage
    exit 1
fi

# Check if OSD ID is numeric
if ! [[ "$OSD_ID" =~ ^[0-9]+$ ]]; then
    echo -e "${RED}Error: OSD ID must be numeric${NC}" >&2
    exit 1
fi

# Check if we can run ceph commands
if ! command -v ceph &> /dev/null; then
    echo -e "${RED}Error: ceph command not found${NC}" >&2
    exit 1
fi

echo -e "${BOLD}${CYAN}=== Ceph OSD Compression Statistics ===${NC}"
echo -e "${BOLD}OSD ID:${NC} ${GREEN}$OSD_ID${NC}"
echo ""

# Get the performance dump and extract compression stats
echo -e "${YELLOW}Fetching compression statistics...${NC}"

# Create temporary file for the perf dump
TEMP_FILE=$(mktemp)
trap "rm -f $TEMP_FILE" EXIT

# Get perf dump and filter compression stats
if ! sudo ceph daemon osd.$OSD_ID perf dump | grep -E "(compress|bluestore_compressed)" > "$TEMP_FILE" 2>/dev/null; then
    echo -e "${RED}Error: Could not retrieve statistics for OSD $OSD_ID${NC}" >&2
    echo "Please check that:"
    echo "  - OSD $OSD_ID exists and is running"
    echo "  - You have sudo privileges"
    echo "  - The ceph daemon is accessible"
    exit 1
fi

# Check if we got any compression data
if [[ ! -s "$TEMP_FILE" ]]; then
    echo -e "${YELLOW}Warning: No compression statistics found for OSD $OSD_ID${NC}"
    echo "This OSD may not have compression enabled or may not have processed any compressed data yet."
    exit 0
fi

# Parse the statistics
compress_success=$(grep '"compress_success_count"' "$TEMP_FILE" | sed 's/.*: \([0-9]*\),*/\1/' || echo "0")
compress_rejected=$(grep '"compress_rejected_count"' "$TEMP_FILE" | sed 's/.*: \([0-9]*\),*/\1/' || echo "0")
compressed_bytes=$(grep '"bluestore_compressed"' "$TEMP_FILE" | sed 's/.*: \([0-9]*\),*/\1/' || echo "0")
allocated_bytes=$(grep '"bluestore_compressed_allocated"' "$TEMP_FILE" | sed 's/.*: \([0-9]*\),*/\1/' || echo "0")
original_bytes=$(grep '"bluestore_compressed_original"' "$TEMP_FILE" | sed 's/.*: \([0-9]*\),*/\1/' || echo "0")
extent_compress=$(grep '"bluestore_extent_compress"' "$TEMP_FILE" | sed 's/.*: \([0-9]*\),*/\1/' || echo "0")

# Calculate totals and ratios
total_attempts=$((compress_success + compress_rejected))
space_saved=$((original_bytes - compressed_bytes))
compression_ratio="0.0"
success_rate="0.0"

if (( original_bytes > 0 )); then
    compression_ratio=$(echo "scale=2; $compressed_bytes * 100 / $original_bytes" | bc -l)
fi

if (( total_attempts > 0 )); then
    success_rate=$(echo "scale=1; $compress_success * 100 / $total_attempts" | bc -l)
fi

# Display formatted results
echo -e "${BOLD}${BLUE}Compression Attempts:${NC}"
echo -e "  ${GREEN}Successful:${NC} $(printf "%'d" $compress_success)"
echo -e "  ${RED}Rejected:${NC}   $(printf "%'d" $compress_rejected)"
echo -e "  ${CYAN}Total:${NC}      $(printf "%'d" $total_attempts)"
echo -e "  ${YELLOW}Success Rate:${NC} ${success_rate}%"
echo ""

echo -e "${BOLD}${BLUE}Storage Statistics:${NC}"
echo -e "  ${GREEN}Original Size:${NC}    $(format_bytes $original_bytes) ($(printf "%'d" $original_bytes) bytes)"
echo -e "  ${YELLOW}Compressed Size:${NC}  $(format_bytes $compressed_bytes) ($(printf "%'d" $compressed_bytes) bytes)"
echo -e "  ${CYAN}Allocated Size:${NC}   $(format_bytes $allocated_bytes) ($(printf "%'d" $allocated_bytes) bytes)"
echo -e "  ${GREEN}Space Saved:${NC}      $(format_bytes $space_saved) ($(printf "%'d" $space_saved) bytes)"
echo ""

echo -e "${BOLD}${BLUE}Compression Efficiency:${NC}"
echo -e "  ${GREEN}Compression Ratio:${NC} ${compression_ratio}% (smaller is better)"
if (( original_bytes > 0 )); then
    space_saved_pct=$(calc_percentage $space_saved $original_bytes)
    echo -e "  ${CYAN}Space Savings:${NC}     ${space_saved_pct}"
fi
echo ""

echo -e "${BOLD}${BLUE}Additional Metrics:${NC}"
echo -e "  ${YELLOW}Extent Compressions:${NC} $(printf "%'d" $extent_compress)"
echo ""

# Provide interpretation
echo -e "${BOLD}${CYAN}=== Analysis ===${NC}"

if (( total_attempts == 0 )); then
    echo -e "${YELLOW}No compression attempts recorded yet.${NC}"
elif (( compress_success == 0 )); then
    echo -e "${RED}All compression attempts were rejected.${NC}"
    echo "This might indicate that the data is already compressed or not suitable for compression."
else
    if (( $(echo "$success_rate > 50" | bc -l) )); then
        echo -e "${GREEN}Good compression success rate (${success_rate}%).${NC}"
    else
        echo -e "${YELLOW}Low compression success rate (${success_rate}%).${NC}"
        echo "Consider reviewing compression settings or data types being stored."
    fi
    
    if (( $(echo "$compression_ratio < 70" | bc -l) )); then
        echo -e "${GREEN}Excellent compression efficiency (${compression_ratio}% of original size).${NC}"
    elif (( $(echo "$compression_ratio < 85" | bc -l) )); then
        echo -e "${CYAN}Good compression efficiency (${compression_ratio}% of original size).${NC}"
    else
        echo -e "${YELLOW}Moderate compression efficiency (${compression_ratio}% of original size).${NC}"
    fi
fi

echo ""
echo -e "${BOLD}Timestamp:${NC} $(date)"