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,43 +412,56 @@ 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; return;
} }
stream.on('data', (data) => { // Retrieve the IP address from the NetworkSettings
try { const ipAddress = data.NetworkSettings.Networks
const stats = JSON.parse(data.toString()); ? Object.values(data.NetworkSettings.Networks)[0].IPAddress
const cpuUsage = calculateCPUPercent(stats); : '-';
const memoryUsage = stats.memory_stats.usage;
const networks = stats.networks;
const ipAddress = networks ? Object.values(networks)[0].IPAddress : '-';
const statsData = { // Start streaming container stats
id: containerInfo.Id, container.stats({ stream: true }, (statsErr, stream) => {
cpu: cpuUsage, if (statsErr) {
memory: memoryUsage, console.error(`[ERROR] Failed to get stats for container ${containerInfo.Id}: ${statsErr.message}`);
ip: ipAddress, return;
};
// 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}`);
} }
});
stream.on('error', (err) => { stream.on('data', (data) => {
console.error(`[ERROR] Stats stream error for container ${containerInfo.Id}: ${err.message}`); 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 // Handle process termination
process.on('SIGINT', () => { process.on('SIGINT', () => {
console.log('[INFO] Server shutting down'); console.log('[INFO] Server shutting down');