From b9e8c6da2ff5f8ce4002fff50b0b110c8342eaed Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Fri, 29 Nov 2024 18:19:26 -0500 Subject: [PATCH] test network fix --- server/server.js | 65 +++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/server/server.js b/server/server.js index 1e0d274..b9269dd 100644 --- a/server/server.js +++ b/server/server.js @@ -412,43 +412,56 @@ function handleKillTerminal(containerId, peer) { function streamContainerStats(containerInfo) { const container = docker.getContainer(containerInfo.Id); - container.stats({ stream: true }, (err, stream) => { - if (err) { - console.error(`[ERROR] Failed to get stats for container ${containerInfo.Id}: ${err.message}`); + // First, retrieve the container's IP address using inspect + container.inspect((inspectErr, data) => { + if (inspectErr) { + console.error(`[ERROR] Failed to inspect container ${containerInfo.Id}: ${inspectErr.message}`); return; } - stream.on('data', (data) => { - try { - const stats = JSON.parse(data.toString()); - const cpuUsage = calculateCPUPercent(stats); - const memoryUsage = stats.memory_stats.usage; - const networks = stats.networks; - const ipAddress = networks ? Object.values(networks)[0].IPAddress : '-'; + // Retrieve the IP address from the NetworkSettings + const ipAddress = data.NetworkSettings.Networks + ? Object.values(data.NetworkSettings.Networks)[0].IPAddress + : '-'; - const statsData = { - id: containerInfo.Id, - cpu: cpuUsage, - memory: memoryUsage, - ip: ipAddress, - }; - - // Broadcast stats to all connected peers - for (const peer of connectedPeers) { - peer.write(JSON.stringify({ type: 'stats', data: statsData })); - } - } catch (err) { - console.error(`[ERROR] Failed to parse stats for container ${containerInfo.Id}: ${err.message}`); + // Start streaming container stats + container.stats({ stream: true }, (statsErr, stream) => { + if (statsErr) { + console.error(`[ERROR] Failed to get stats for container ${containerInfo.Id}: ${statsErr.message}`); + return; } - }); - stream.on('error', (err) => { - console.error(`[ERROR] Stats stream error for container ${containerInfo.Id}: ${err.message}`); + stream.on('data', (data) => { + try { + const stats = JSON.parse(data.toString()); + const cpuUsage = calculateCPUPercent(stats); + const memoryUsage = stats.memory_stats.usage; + + const statsData = { + id: containerInfo.Id, + cpu: cpuUsage, + memory: memoryUsage, + ip: ipAddress, // Use the inspected IP address + }; + + // Broadcast stats to all connected peers + for (const peer of connectedPeers) { + peer.write(JSON.stringify({ type: 'stats', data: statsData })); + } + } catch (parseErr) { + console.error(`[ERROR] Failed to parse stats for container ${containerInfo.Id}: ${parseErr.message}`); + } + }); + + stream.on('error', (streamErr) => { + console.error(`[ERROR] Stats stream error for container ${containerInfo.Id}: ${streamErr.message}`); + }); }); }); } + // Handle process termination process.on('SIGINT', () => { console.log('[INFO] Server shutting down');