diff --git a/cluster/check_resources.sh b/cluster/check_resources.sh index 7d50c1b..25f0f58 100755 --- a/cluster/check_resources.sh +++ b/cluster/check_resources.sh @@ -25,16 +25,32 @@ echo "------------------------------------------" kubectl top pods -A --sort-by=cpu 2>/dev/null | head -11 || echo "需要 metrics-server" echo "" -echo "🔋 高資源使用 Pod (>80% CPU 或 >1G Memory)" +echo "🔋 資源不足警告 (CPU>80% 或 MEM>1Gi)" echo "------------------------------------------" -kubectl top pods -A 2>/dev/null | awk -v OFS='\t' 'NR>1 && ($3 > 80 || $4 > 1000) {print $1, $2, $3"%", $4"Mi"}' || echo "無法計算" +HIGH_PODS=$(kubectl top pods -A 2>/dev/null | awk -v OFS='\t' 'NR>1 {split($4, v, /([0-9]+)/); mem=int(v[2]); if ($3+0 > 80 || mem > 1024) print $0}') + +if [ -z "$HIGH_PODS" ]; then + echo -e "${GREEN}✓ 無高資源使用 Pod${NC}" +else + echo "$HIGH_PODS" | while IFS=$'\t' read -r ns name cpu mem; do + echo -e "${YELLOW}⚠ $ns/$name${NC}" + echo " CPU: $cpu Memory: $mem" + done +fi echo "" -echo "📊 資源請求 vs 限制" +echo "📊 無資源限制的 Pod" echo "------------------------------------------" -echo "查看 Pod 是否設定了資源限制:" -kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\t"}{.spec.containers[*].resources.requests.cpu}{"\t"}{.spec.containers[*].resources.limits.cpu}{"\n"}{end}' | \ - awk '{if ($3 != "" && $4 == "") print $1"/"$2 " ❌ 只有 request 無 limit"; else if ($3 == "") print $1"/"$2 " ⚠️ 無資料設定"}' | head -10 || echo -e "${GREEN}所有 Pod 都有設定${NC}" +NO_LIMIT=$(kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"/"}{.metadata.name}{"\t"}{.spec.containers[*].resources.requests.cpu}{"\t"}{.spec.containers[*].resources.limits.cpu}{"\n"}{end}' | \ + awk -F'\t' '$3 == "" {print $1}') + +if [ -z "$NO_LIMIT" ]; then + echo -e "${GREEN}✓ 所有 Pod 都有資料設定${NC}" +else + echo "$NO_LIMIT" | while read -r pod; do + echo -e "${YELLOW}⚠ $pod${NC} - 無資源限制" + done | head -10 +fi echo "" echo "=========================================="