test network fix

This commit is contained in:
Raven Scott 2024-11-29 18:19:26 -05:00
parent 0e6f464d16
commit b9e8c6da2f

View File

@ -412,9 +412,22 @@ function handleKillTerminal(containerId, peer) {
function streamContainerStats(containerInfo) { function streamContainerStats(containerInfo) {
const container = docker.getContainer(containerInfo.Id); const container = docker.getContainer(containerInfo.Id);
container.stats({ stream: true }, (err, stream) => { // First, retrieve the container's IP address using inspect
if (err) { container.inspect((inspectErr, data) => {
console.error(`[ERROR] Failed to get stats for container ${containerInfo.Id}: ${err.message}`); if (inspectErr) {
console.error(`[ERROR] Failed to inspect container ${containerInfo.Id}: ${inspectErr.message}`);
return;
}
// Retrieve the IP address from the NetworkSettings
const ipAddress = data.NetworkSettings.Networks
? Object.values(data.NetworkSettings.Networks)[0].IPAddress
: '-';
// 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; return;
} }
@ -423,32 +436,32 @@ function streamContainerStats(containerInfo) {
const stats = JSON.parse(data.toString()); const stats = JSON.parse(data.toString());
const cpuUsage = calculateCPUPercent(stats); const cpuUsage = calculateCPUPercent(stats);
const memoryUsage = stats.memory_stats.usage; const memoryUsage = stats.memory_stats.usage;
const networks = stats.networks;
const ipAddress = networks ? Object.values(networks)[0].IPAddress : '-';
const statsData = { const statsData = {
id: containerInfo.Id, id: containerInfo.Id,
cpu: cpuUsage, cpu: cpuUsage,
memory: memoryUsage, memory: memoryUsage,
ip: ipAddress, ip: ipAddress, // Use the inspected IP address
}; };
// Broadcast stats to all connected peers // Broadcast stats to all connected peers
for (const peer of connectedPeers) { for (const peer of connectedPeers) {
peer.write(JSON.stringify({ type: 'stats', data: statsData })); peer.write(JSON.stringify({ type: 'stats', data: statsData }));
} }
} catch (err) { } catch (parseErr) {
console.error(`[ERROR] Failed to parse stats for container ${containerInfo.Id}: ${err.message}`); console.error(`[ERROR] Failed to parse stats for container ${containerInfo.Id}: ${parseErr.message}`);
} }
}); });
stream.on('error', (err) => { stream.on('error', (streamErr) => {
console.error(`[ERROR] Stats stream error for container ${containerInfo.Id}: ${err.message}`); console.error(`[ERROR] Stats stream error for container ${containerInfo.Id}: ${streamErr.message}`);
});
}); });
}); });
} }
// Handle process termination // Handle process termination
process.on('SIGINT', () => { process.on('SIGINT', () => {
console.log('[INFO] Server shutting down'); console.log('[INFO] Server shutting down');