Compare commits

..

No commits in common. "5e8c085446603785f4d58aafc741622f41a72d19" and "58335ead6d5c0937f356b002991de9b2a5fc42c9" have entirely different histories.

3 changed files with 97 additions and 90 deletions

11
app.js
View File

@ -64,7 +64,7 @@ function startStatsInterval() {
if (window.activePeer) {
const now = Date.now();
if (now - lastStatsUpdate >= 500) { // Ensure at least 500ms between updates
// sendCommand('allStats', {}); // Adjust command if necessary
sendCommand('stats', {}); // Adjust command if necessary
lastStatsUpdate = now;
}
} else {
@ -334,9 +334,12 @@ function handlePeerData(data, topicId, peer) {
// Delegate handling based on the response type
switch (response.type) {
case 'allStats':
console.log('[INFO] Received aggregated stats for all containers.');
response.data.forEach((stats) => updateContainerStats(stats));
case 'stats':
console.log('[INFO] Updating container stats...');
const stats = response.data;
stats.ip = stats.ip || 'No IP Assigned'; // Add a fallback for missing IPs
console.log(`[DEBUG] Passing stats to updateContainerStats: ${JSON.stringify(stats, null, 2)}`);
updateContainerStats(stats);
break;
case 'containers':

View File

@ -206,9 +206,7 @@ swarm.on('connection', (peer) => {
await docker.getContainer(parsedData.args.id).start();
response = { success: true, message: `Container ${parsedData.args.id} started` };
break;
// case 'allStats':
// await handleallStatsRequest(peer);
// return; // No further response needed
case 'stopContainer':
console.log(`[INFO] Handling 'stopContainer' command for container: ${parsedData.args.id}`);
await docker.getContainer(parsedData.args.id).stop();
@ -530,6 +528,44 @@ docker.listContainers({ all: true }, async (err, containers) => {
ipAddress = networks[0].IPAddress; // Use the first network's IP
}
}
// 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('data', (data) => {
try {
const stats = JSON.parse(data.toString());
const cpuUsage = calculateCPUPercent(stats);
const memoryUsage = stats.memory_stats.usage || 0; // Default to 0 if undefined
const statsData = {
id: containerInfo.Id,
cpu: cpuUsage,
memory: memoryUsage,
ip: ipAddress, // Use the pre-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}`);
});
stream.on('close', () => {
// console.log(`[INFO] Stats stream closed for container ${containerInfo.Id}`);
});
});
});
});
});
@ -636,95 +672,63 @@ function handleKillTerminal(containerId, peer) {
}
}
async function collectContainerStats(containerStats) {
const currentContainers = await docker.listContainers({ all: true });
const currentIds = currentContainers.map((c) => c.Id);
// Collect stats for all containers, including newly added ones
for (const containerInfo of currentContainers) {
if (!containerStats[containerInfo.Id]) {
console.log(`[INFO] Found new container: ${containerInfo.Names[0]?.replace(/^\//, '')}`);
containerStats[containerInfo.Id] = await initializeContainerStats(containerInfo);
}
}
// Remove containers that no longer exist
Object.keys(containerStats).forEach((id) => {
if (!currentIds.includes(id)) {
console.log(`[INFO] Removing stats tracking for container: ${id}`);
delete containerStats[id];
}
});
return containerStats;
}
async function initializeContainerStats(containerInfo) {
function streamContainerStats(containerInfo) {
const container = docker.getContainer(containerInfo.Id);
// Inspect container for IP address
let ipAddress = 'No IP Assigned';
try {
const details = await container.inspect();
const networks = details.NetworkSettings?.Networks || {};
ipAddress = Object.values(networks)[0]?.IPAddress || 'No IP Assigned';
} catch (err) {
console.error(`[ERROR] Failed to inspect container ${containerInfo.Id}: ${err.message}`);
// Use the same logic as listContainers to get the IP address
container.inspect((inspectErr, details) => {
let ipAddress = 'No IP Assigned'; // Default IP address fallback
if (inspectErr) {
console.error(`[ERROR] Failed to inspect container ${containerInfo.Id}: ${inspectErr.message}`);
} else if (details.NetworkSettings && details.NetworkSettings.Networks) {
const networks = Object.values(details.NetworkSettings.Networks);
if (networks.length > 0 && networks[0].IPAddress) {
ipAddress = networks[0].IPAddress; // Retrieve the first network's IP address
}
}
const statsData = {
id: containerInfo.Id,
name: containerInfo.Names[0]?.replace(/^\//, '') || 'Unknown',
cpu: 0,
memory: 0,
ip: 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;
}
// Start streaming stats for the container
try {
const statsStream = await container.stats({ stream: true });
statsStream.on('data', (data) => {
stream.on('data', (data) => {
try {
const stats = JSON.parse(data.toString());
statsData.cpu = calculateCPUPercent(stats);
statsData.memory = stats.memory_stats.usage || 0;
} catch (err) {
console.error(`[ERROR] Failed to parse stats for container ${containerInfo.Id}: ${err.message}`);
const cpuUsage = calculateCPUPercent(stats);
const memoryUsage = stats.memory_stats.usage || 0; // Default to 0 if undefined
// Use the extracted IP address in the stats data
const statsData = {
id: containerInfo.Id,
cpu: cpuUsage,
memory: memoryUsage,
ip: ipAddress, // Use the IP address retrieved during inspection
};
// 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}`);
}
});
statsStream.on('error', (err) => {
console.error(`[ERROR] Stats stream error for container ${containerInfo.Id}: ${err.message}`);
stream.on('error', (streamErr) => {
console.error(`[ERROR] Stats stream error for container ${containerInfo.Id}: ${streamErr.message}`);
});
statsStream.on('close', () => {
stream.on('close', () => {
console.log(`[INFO] Stats stream closed for container ${containerInfo.Id}`);
});
} catch (err) {
console.error(`[ERROR] Failed to start stats stream for container ${containerInfo.Id}: ${err.message}`);
}
return statsData;
});
});
}
async function handleStatsBroadcast() {
const containerStats = {};
// Periodically update stats and broadcast
setInterval(async () => {
await collectContainerStats(containerStats);
const aggregatedStats = Object.values(containerStats);
const response = { type: 'allStats', data: aggregatedStats };
for (const peer of connectedPeers) {
peer.write(JSON.stringify(response));
}
}, 1000); // Send stats every 500ms
}
// Start the stats broadcast
handleStatsBroadcast();
// Handle process termination