This commit is contained in:
2025-12-28 23:44:40 +09:00
parent 248eb6e6e0
commit 8bcb5c7ff7
2 changed files with 309 additions and 247 deletions

View File

@@ -1,4 +1,5 @@
import { query, queryOne, execute, getPool } from './db'
import { ofetch } from 'ofetch'
interface ServerTarget {
target_id: number
@@ -20,16 +21,20 @@ function timestamp(): string {
return new Date().toLocaleString('sv-SE', { timeZone: 'Asia/Seoul' }).replace('T', ' ')
}
// Glances API 호출 (버전 지정)
// Glances API 호출 (버전 지정) - ofetch 사용
async function fetchGlancesApi(baseUrl: string, endpoint: string, version: string): Promise<any> {
const url = `${baseUrl}/api/${version}/${endpoint}`
try {
const url = `${baseUrl}/api/${version}/${endpoint}`
const response = await fetch(url, {
signal: AbortSignal.timeout(5000)
const data = await ofetch(url, {
timeout: 5000,
retry: 0
})
if (!response.ok) return null
return await response.json()
} catch (err) {
return data
} catch (err: any) {
// 404, timeout 등 에러는 조용히 null 반환
if (err.statusCode !== 404) {
console.error(`[fetchGlancesApi] ${url} - Error: ${err.message}`)
}
return null
}
}
@@ -487,12 +492,12 @@ async function collectServerData(target: ServerTarget) {
quicklook?.cpu_name || null,
quicklook?.cpu_number || quicklook?.cpu_log_core || cpu?.cpucore || null,
cpu?.total ?? quicklook?.cpu ?? null,
mem?.total || null,
mem?.total && mem?.percent ? (mem.total * mem.percent / 100) : null, // memory_used = total × percent / 100
mem?.free || null,
mem?.total ? Math.round(mem.total) : null, // bigint: 정수 변환
mem?.total && mem?.percent ? Math.round(mem.total * mem.percent / 100) : null, // bigint: 정수 변환
mem?.free ? Math.round(mem.free) : null, // bigint: 정수 변환
mem?.percent || null,
memswap?.total || null,
memswap?.used || null,
memswap?.total ? Math.round(memswap.total) : null, // bigint: 정수 변환
memswap?.used ? Math.round(memswap.used) : null, // bigint: 정수 변환
memswap?.percent || null,
isOnline ? 1 : 0,
apiVersion,
@@ -518,8 +523,8 @@ async function collectServerData(target: ServerTarget) {
disk.device_name || null,
disk.mnt_point || null,
disk.fs_type || null,
disk.size || null,
disk.used || null,
disk.size ? Math.round(disk.size) : null, // bigint: 정수 변환
disk.used ? Math.round(disk.used) : null, // bigint: 정수 변환
disk.percent || null,
now
])
@@ -563,8 +568,8 @@ async function collectServerData(target: ServerTarget) {
Array.isArray(container.image) ? container.image.join(', ') : container.image || null,
container.status || null,
cpuPercent,
memoryUsage,
memoryLimit,
memoryUsage ? Math.round(memoryUsage) : null, // bigint: 정수 변환
memoryLimit ? Math.round(memoryLimit) : null, // bigint: 정수 변환
memoryPercent,
container.uptime || null,
container.network?.rx ?? container.network_rx ?? null,
@@ -578,6 +583,11 @@ async function collectServerData(target: ServerTarget) {
if (Array.isArray(network) && network.length > 0) {
console.log(`[${now}] 🌐 [${target.server_name}] network 저장 (${network.length}개 인터페이스)`)
for (const iface of network) {
const bytesRecv = iface.bytes_recv || iface.cumulative_rx || null
const bytesSent = iface.bytes_sent || iface.cumulative_tx || null
const packetsRecv = iface.packets_recv || null
const packetsSent = iface.packets_sent || null
await execute(`
INSERT INTO server_networks (
target_id, interface_name, bytes_recv, bytes_sent,
@@ -587,10 +597,10 @@ async function collectServerData(target: ServerTarget) {
`, [
target.target_id,
iface.interface_name || null,
iface.bytes_recv || iface.cumulative_rx || null,
iface.bytes_sent || iface.cumulative_tx || null,
iface.packets_recv || null,
iface.packets_sent || null,
bytesRecv ? Math.round(bytesRecv) : null, // bigint: 정수 변환
bytesSent ? Math.round(bytesSent) : null, // bigint: 정수 변환
packetsRecv ? Math.round(packetsRecv) : null, // bigint: 정수 변환
packetsSent ? Math.round(packetsSent) : null, // bigint: 정수 변환
iface.bytes_recv_rate_per_sec || iface.rx || iface.bytes_recv_rate || null,
iface.bytes_sent_rate_per_sec || iface.tx || iface.bytes_sent_rate || null,
iface.is_up ? 1 : 0,