stock-bot/scripts/get-redis-connections.sh

112 lines
3.5 KiB
Bash

#!/bin/bash
# Simple script to get Redis/Dragonfly connections using redis-cli
# This provides a quick alternative to the TypeScript version
set -e
# Configuration
REDIS_HOST="${DRAGONFLY_HOST:-localhost}"
REDIS_PORT="${DRAGONFLY_PORT:-6379}"
REDIS_PASSWORD="${DRAGONFLY_PASSWORD:-}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}🐉 Dragonfly/Redis Connection Monitor${NC}"
echo "========================================"
# Check if redis-cli is available
if ! command -v redis-cli &> /dev/null; then
echo -e "${RED}❌ redis-cli not found. Please install redis-tools:${NC}"
echo " Ubuntu/Debian: sudo apt-get install redis-tools"
echo " macOS: brew install redis"
echo " Or use the TypeScript version: bun run scripts/get-redis-connections.ts"
exit 1
fi
# Build redis-cli command
REDIS_CMD="redis-cli -h $REDIS_HOST -p $REDIS_PORT"
if [ -n "$REDIS_PASSWORD" ]; then
REDIS_CMD="$REDIS_CMD -a $REDIS_PASSWORD"
fi
echo -e "${GREEN}📡 Connecting to Dragonfly at $REDIS_HOST:$REDIS_PORT${NC}"
# Test connection
if ! $REDIS_CMD ping > /dev/null 2>&1; then
echo -e "${RED}❌ Cannot connect to Dragonfly/Redis${NC}"
echo " Make sure the Docker container is running:"
echo " docker ps | grep dragonfly"
exit 1
fi
echo -e "${GREEN}✅ Connected successfully${NC}"
echo ""
# Get client list
echo -e "${YELLOW}📋 Active Client Connections:${NC}"
echo "----------------------------------------"
CLIENT_LIST=$($REDIS_CMD CLIENT LIST)
if [ -z "$CLIENT_LIST" ]; then
echo "No active connections found"
exit 0
fi
# Count total connections
TOTAL_CONNECTIONS=$(echo "$CLIENT_LIST" | wc -l)
echo -e "${BLUE}Total Connections: $TOTAL_CONNECTIONS${NC}"
echo ""
# Group by client name (simple grouping)
echo -e "${YELLOW}🔍 Connection Details:${NC}"
echo "$CLIENT_LIST" | while IFS= read -r line; do
if [ -n "$line" ]; then
# Extract key information
ID=$(echo "$line" | grep -o 'id=[0-9]*' | cut -d'=' -f2)
ADDR=$(echo "$line" | grep -o 'addr=[^[:space:]]*' | cut -d'=' -f2)
NAME=$(echo "$line" | grep -o 'name=[^[:space:]]*' | cut -d'=' -f2 || echo "unnamed")
AGE=$(echo "$line" | grep -o 'age=[0-9]*' | cut -d'=' -f2)
IDLE=$(echo "$line" | grep -o 'idle=[0-9]*' | cut -d'=' -f2)
CMD=$(echo "$line" | grep -o 'cmd=[^[:space:]]*' | cut -d'=' -f2 || echo "none")
echo "├─ Client ID: $ID | Name: $NAME | Address: $ADDR"
echo " Age: ${AGE}s | Idle: ${IDLE}s | Last Command: $CMD"
echo ""
fi
done
# Server info
echo -e "${YELLOW}🖥️ Server Information:${NC}"
echo "----------------------------------------"
$REDIS_CMD INFO server | grep -E "(dragonfly_version|redis_version|connected_clients|uptime_in_seconds)" | head -10
echo ""
echo -e "${YELLOW}💾 Memory Usage:${NC}"
echo "----------------------------------------"
$REDIS_CMD INFO memory | grep -E "(used_memory_human|maxmemory_human|used_memory_percentage)" | head -5
echo ""
echo -e "${GREEN}✅ Connection analysis complete${NC}"
# Optional: Monitor mode
if [ "$1" = "--monitor" ] || [ "$1" = "-m" ]; then
INTERVAL=${2:-5}
echo ""
echo -e "${BLUE}🔄 Starting monitoring mode (refresh every ${INTERVAL}s)${NC}"
echo "Press Ctrl+C to stop..."
while true; do
sleep $INTERVAL
clear
echo -e "${BLUE}🕐 Last updated: $(date)${NC}"
echo ""
exec "$0" # Re-run this script
done
fi