메모리 상태 또 변경
This commit is contained in:
@@ -210,17 +210,10 @@ async function getServerDashboard() {
|
||||
let serverLevel = 'offline'
|
||||
let cpuLevel = 'normal', memLevel = 'normal', diskLevel = 'normal'
|
||||
|
||||
// 메모리 퍼센트 계산: (total - free) / total * 100
|
||||
let calculatedMemPercent = 0
|
||||
if (snapshot) {
|
||||
const memTotal = Number(snapshot.memory_total) || 0
|
||||
const memFree = Number(snapshot.memory_free) || 0
|
||||
calculatedMemPercent = memTotal > 0 ? ((memTotal - memFree) / memTotal) * 100 : 0
|
||||
}
|
||||
|
||||
if (!isOffline && snapshot) {
|
||||
cpuLevel = getLevel(Number(snapshot.cpu_percent), thresholds.server?.cpu || { warning: 70, critical: 85, danger: 95 })
|
||||
memLevel = getLevel(calculatedMemPercent, thresholds.server?.memory || { warning: 80, critical: 90, danger: 95 })
|
||||
// 메모리: snapshot.memory_percent 직접 사용
|
||||
memLevel = getLevel(Number(snapshot.memory_percent), thresholds.server?.memory || { warning: 80, critical: 90, danger: 95 })
|
||||
diskLevel = getLevel(Number(diskData?.disk_percent), thresholds.server?.disk || { warning: 80, critical: 90, danger: 95 })
|
||||
serverLevel = getHighestLevel([cpuLevel, memLevel, diskLevel])
|
||||
}
|
||||
@@ -300,7 +293,7 @@ async function getServerDashboard() {
|
||||
level: serverLevel,
|
||||
cpu_percent: snapshot?.cpu_percent ?? null,
|
||||
cpu_level: cpuLevel,
|
||||
memory_percent: calculatedMemPercent,
|
||||
memory_percent: snapshot?.memory_percent ?? null, // snapshot의 memory_percent 직접 사용
|
||||
memory_level: memLevel,
|
||||
memory_total: snapshot?.memory_total ?? null,
|
||||
memory_free: snapshot?.memory_free ?? null,
|
||||
|
||||
@@ -70,7 +70,7 @@ async function detectAnomalies(targetId: number, serverName: string) {
|
||||
const SHORT_TERM_THRESHOLD = 30
|
||||
|
||||
const snapshots = await query<any>(`
|
||||
SELECT cpu_percent, memory_total, memory_free
|
||||
SELECT cpu_percent, memory_percent
|
||||
FROM server_snapshots
|
||||
WHERE target_id = $1 AND is_online = 1
|
||||
ORDER BY collected_at DESC
|
||||
@@ -85,14 +85,9 @@ async function detectAnomalies(targetId: number, serverName: string) {
|
||||
const currCpuAvg = currSnapshots.reduce((sum, s) => sum + (s.cpu_percent || 0), 0) / currSnapshots.length
|
||||
const prevCpuAvg = prevSnapshots.reduce((sum, s) => sum + (s.cpu_percent || 0), 0) / prevSnapshots.length
|
||||
|
||||
// 메모리: (total - free) / total * 100
|
||||
const calcMemPct = (s: any) => {
|
||||
const total = Number(s.memory_total) || 0
|
||||
const free = Number(s.memory_free) || 0
|
||||
return total > 0 ? ((total - free) / total) * 100 : 0
|
||||
}
|
||||
const currMemAvg = currSnapshots.reduce((sum, s) => sum + calcMemPct(s), 0) / currSnapshots.length
|
||||
const prevMemAvg = prevSnapshots.reduce((sum, s) => sum + calcMemPct(s), 0) / prevSnapshots.length
|
||||
// 메모리: memory_percent 직접 사용
|
||||
const currMemAvg = currSnapshots.reduce((sum, s) => sum + (s.memory_percent || 0), 0) / currSnapshots.length
|
||||
const prevMemAvg = prevSnapshots.reduce((sum, s) => sum + (s.memory_percent || 0), 0) / prevSnapshots.length
|
||||
|
||||
const cpuChange = prevCpuAvg > 1 ? ((currCpuAvg - prevCpuAvg) / prevCpuAvg) * 100 : currCpuAvg - prevCpuAvg
|
||||
const memChange = prevMemAvg > 1 ? ((currMemAvg - prevMemAvg) / prevMemAvg) * 100 : currMemAvg - prevMemAvg
|
||||
@@ -149,7 +144,7 @@ async function detectAnomalies(targetId: number, serverName: string) {
|
||||
const DANGER_Z = 3.0
|
||||
|
||||
const hourSnapshots = await query<any>(`
|
||||
SELECT cpu_percent, memory_total, memory_free
|
||||
SELECT cpu_percent, memory_percent
|
||||
FROM server_snapshots
|
||||
WHERE target_id = $1 AND is_online = 1
|
||||
AND collected_at::timestamp >= NOW() - INTERVAL '1 hour'
|
||||
@@ -159,16 +154,10 @@ async function detectAnomalies(targetId: number, serverName: string) {
|
||||
if (hourSnapshots.length >= 10) {
|
||||
const current = hourSnapshots[0]
|
||||
const currCpu = current.cpu_percent ?? 0
|
||||
// 메모리: (total - free) / total * 100
|
||||
const calcMemPctZ = (s: any) => {
|
||||
const total = Number(s.memory_total) || 0
|
||||
const free = Number(s.memory_free) || 0
|
||||
return total > 0 ? ((total - free) / total) * 100 : 0
|
||||
}
|
||||
const currMem = calcMemPctZ(current)
|
||||
const currMem = current.memory_percent ?? 0 // memory_percent 직접 사용
|
||||
|
||||
const cpuValues = hourSnapshots.map(s => s.cpu_percent ?? 0)
|
||||
const memValues = hourSnapshots.map(s => calcMemPctZ(s))
|
||||
const memValues = hourSnapshots.map(s => s.memory_percent ?? 0)
|
||||
|
||||
const cpuAvg = cpuValues.reduce((a, b) => a + b, 0) / cpuValues.length
|
||||
const memAvg = memValues.reduce((a, b) => a + b, 0) / memValues.length
|
||||
@@ -236,7 +225,7 @@ async function detectAnomalies(targetId: number, serverName: string) {
|
||||
const dayType = isWeekend ? 'weekend' : 'weekday'
|
||||
|
||||
const baselineData = await query<any>(`
|
||||
SELECT cpu_percent, memory_total, memory_free
|
||||
SELECT cpu_percent, memory_percent
|
||||
FROM server_snapshots
|
||||
WHERE target_id = $1 AND is_online = 1
|
||||
AND collected_at::timestamp >= NOW() - INTERVAL '14 days'
|
||||
@@ -249,25 +238,18 @@ async function detectAnomalies(targetId: number, serverName: string) {
|
||||
`, [targetId, currentHour, dayType])
|
||||
|
||||
const currentSnapshot = await queryOne<any>(`
|
||||
SELECT cpu_percent, memory_total, memory_free
|
||||
SELECT cpu_percent, memory_percent
|
||||
FROM server_snapshots
|
||||
WHERE target_id = $1 AND is_online = 1
|
||||
ORDER BY collected_at DESC LIMIT 1
|
||||
`, [targetId])
|
||||
|
||||
// 메모리 계산 함수: (total - free) / total * 100
|
||||
const calcMemPctBaseline = (s: any) => {
|
||||
const total = Number(s.memory_total) || 0
|
||||
const free = Number(s.memory_free) || 0
|
||||
return total > 0 ? ((total - free) / total) * 100 : 0
|
||||
}
|
||||
|
||||
if (baselineData.length >= 5 && currentSnapshot) {
|
||||
const currCpu = currentSnapshot.cpu_percent ?? 0
|
||||
const currMem = calcMemPctBaseline(currentSnapshot)
|
||||
const currMem = currentSnapshot.memory_percent ?? 0 // memory_percent 직접 사용
|
||||
|
||||
const cpuValues = baselineData.map(s => s.cpu_percent ?? 0)
|
||||
const memValues = baselineData.map(s => calcMemPctBaseline(s))
|
||||
const memValues = baselineData.map(s => s.memory_percent ?? 0)
|
||||
|
||||
const cpuAvg = cpuValues.reduce((a, b) => a + b, 0) / cpuValues.length
|
||||
const memAvg = memValues.reduce((a, b) => a + b, 0) / memValues.length
|
||||
@@ -506,7 +488,7 @@ async function collectServerData(target: ServerTarget) {
|
||||
quicklook?.cpu_number || quicklook?.cpu_log_core || cpu?.cpucore || null,
|
||||
cpu?.total ?? quicklook?.cpu ?? null,
|
||||
mem?.total || null,
|
||||
mem?.used || null,
|
||||
mem?.total && mem?.percent ? (mem.total * mem.percent / 100) : null, // memory_used = total × percent / 100
|
||||
mem?.free || null,
|
||||
mem?.percent || null,
|
||||
memswap?.total || null,
|
||||
@@ -548,6 +530,26 @@ async function collectServerData(target: ServerTarget) {
|
||||
if (Array.isArray(docker) && docker.length > 0) {
|
||||
console.log(`[${now}] 🐳 [${target.server_name}] container 저장 (${docker.length}개 컨테이너)`)
|
||||
for (const container of docker) {
|
||||
// CPU 값 추출 로직
|
||||
const cpuPercent = container.cpu?.total ?? container.cpu_percent ?? null
|
||||
const memoryUsage = container.memory?.usage || container.memory_usage || null
|
||||
const memoryLimit = container.memory?.limit || container.memory_limit || null
|
||||
const memoryPercent = container.memory?.usage && container.memory?.limit
|
||||
? (container.memory.usage / container.memory.limit * 100)
|
||||
: container.memory_percent ?? null
|
||||
|
||||
// 각 컨테이너별 상세 로그
|
||||
console.log(`[${now}] 🐳 - ${container.name}: CPU=${cpuPercent?.toFixed(2) ?? 'null'}%, MEM=${memoryPercent?.toFixed(2) ?? 'null'}%`)
|
||||
|
||||
// CPU가 null인 경우 상세 디버깅
|
||||
if (cpuPercent === null) {
|
||||
console.warn(`[${now}] ⚠️ - ${container.name}: CPU 값 null! 원본:`, JSON.stringify({
|
||||
'cpu.total': container.cpu?.total,
|
||||
'cpu_percent': container.cpu_percent,
|
||||
'cpu': container.cpu
|
||||
}))
|
||||
}
|
||||
|
||||
await execute(`
|
||||
INSERT INTO server_containers (
|
||||
target_id, docker_id, container_name, container_image,
|
||||
@@ -560,12 +562,10 @@ async function collectServerData(target: ServerTarget) {
|
||||
container.name || null,
|
||||
Array.isArray(container.image) ? container.image.join(', ') : container.image || null,
|
||||
container.status || null,
|
||||
container.cpu?.total ?? container.cpu_percent ?? null,
|
||||
container.memory?.usage || container.memory_usage || null,
|
||||
container.memory?.limit || container.memory_limit || null,
|
||||
container.memory?.usage && container.memory?.limit
|
||||
? (container.memory.usage / container.memory.limit * 100)
|
||||
: container.memory_percent ?? null,
|
||||
cpuPercent,
|
||||
memoryUsage,
|
||||
memoryLimit,
|
||||
memoryPercent,
|
||||
container.uptime || null,
|
||||
container.network?.rx ?? container.network_rx ?? null,
|
||||
container.network?.tx ?? container.network_tx ?? null,
|
||||
|
||||
Reference in New Issue
Block a user