#!/usr/bin/env bash
set -euo pipefail
PATTERN="${1:-}"
if [ -z "$PATTERN" ]; then
echo "사용법: $0 <pod-name-pattern>"
echo "예시: $0 starrocks"
exit 1
fi
echo "검색 패턴: '$PATTERN'"
echo "클러스터 파드 정보 수집 및 분석 중..."
# 1. 파드별 CPU Req/Lim (mcore 단위) 추출 및 Lim 그룹 키 생성
kubectl get pods -A \
--field-selector=status.phase!=Succeeded,status.phase!=Failed \
-o json | jq -r --arg pat "$PATTERN" '
def parse_cpu:
if . == null then 0
elif endswith("m") then (rtrimstr("m") | tonumber)
elif endswith("n") then (rtrimstr("n") | tonumber / 1000000)
else (tonumber * 1000)
end;
.items[]
| select(.metadata.name | test($pat))
| {
name: .metadata.name,
req: ([.spec.containers[].resources.requests.cpu? // null | parse_cpu] | add // 0),
lim: ([.spec.containers[].resources.limits.cpu? // null | parse_cpu] | add // 0)
}
| if .lim == 0 and .req == 0 then "NO_SPEC\tNO_SPEC"
elif .lim == 0 then "NO_LIMIT\tNO_LIMIT"
else
# Limit 코어 크기 라벨 (예: 250m, 1, 2, 4, 8 등 Core 단위 환산)
(
if (.lim % 1000 == 0) then "\((.lim / 1000 | tostring)) Core"
else "\((.lim / 1000 | tostring)) Core"
end
) + "\t" + ((.req / .lim) * 100 | tostring)
end
' | awk -F'\t' '
BEGIN {
# 구간 인덱스 초기화
bin_names[0] = "0~10%"
bin_names[1] = "10~20%"
bin_names[2] = "20~30%"
bin_names[3] = "30~40%"
bin_names[4] = "40~50%"
bin_names[5] = "50~60%"
bin_names[6] = "60~70%"
bin_names[7] = "70~80%"
bin_names[8] = "80~90%"
bin_names[9] = "90~100%"
bin_names[10] = ">100%"
}
{
lim_group = $1
val = $2
if (!seen_group[lim_group]++) {
group_list[++group_cnt] = lim_group
}
total_by_group[lim_group]++
grand_total++
if (val == "NO_LIMIT" || val == "NO_SPEC") {
# 예외 그룹
special_cnt[lim_group]++
} else {
ratio = val + 0
if (ratio >= 0 && ratio < 10) b = 0
else if (ratio >= 10 && ratio < 20) b = 1
else if (ratio >= 20 && ratio < 30) b = 2
else if (ratio >= 30 && ratio < 40) b = 3
else if (ratio >= 40 && ratio < 50) b = 4
else if (ratio >= 50 && ratio < 60) b = 5
else if (ratio >= 60 && ratio < 70) b = 6
else if (ratio >= 70 && ratio < 80) b = 7
else if (ratio >= 80 && ratio < 90) b = 8
else if (ratio >= 90 && ratio <= 100) b = 9
else b = 10
matrix[lim_group, b]++
}
}
END {
if (grand_total == 0) {
print "조건에 매칭되는 파드가 없습니다."
exit 0
}
printf "\n=== CPU LIMIT 크기별 REQ/LIM 비율 분포 ===\n\n"
# 헤더 출력
printf "%-12s | %-5s |", "CPU LIMIT", "TOTAL"
for (i = 0; i < 10; i++) {
printf " %-7s", bin_names[i]
}
printf " | %-6s\n", ">100%"
print "-------------+-------+---------------------------------------------------------------------------------+--------"
for (g = 1; g <= group_cnt; g++) {
grp = group_list[g]
if (grp == "NO_LIMIT" || grp == "NO_SPEC") continue
printf "%-12s | %-5d |", grp, total_by_group[grp]
for (i = 0; i < 10; i++) {
c = matrix[grp, i] + 0
if (c > 0) printf " %-7d", c
else printf " %-7s", "-"
}
over = matrix[grp, 10] + 0
if (over > 0) printf " | %-6d\n", over
else printf " | %-6s\n", "-"
}
print "-------------+-------+---------------------------------------------------------------------------------+--------"
# 예외 케이스 표기
if (special_cnt["NO_LIMIT"] > 0) {
printf "%-12s | %-5d | (Limit 미설정 파드)\n", "No Limit", special_cnt["NO_LIMIT"]
}
if (special_cnt["NO_SPEC"] > 0) {
printf "%-12s | %-5d | (Req/Limit 모두 미설정)\n", "No Spec", special_cnt["NO_SPEC"]
}
printf "\n총 분석 대상 파드: %d개\n", grand_total
}
'