mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-25 13:18:47 +00:00
Merge pull request #3021 from Dokploy/1735-bug-in-server-monitoring
1735 bug in server monitoring
This commit is contained in:
@@ -2,13 +2,12 @@ import type http from "node:http";
|
|||||||
import {
|
import {
|
||||||
docker,
|
docker,
|
||||||
execAsync,
|
execAsync,
|
||||||
|
getHostSystemStats,
|
||||||
getLastAdvancedStatsFile,
|
getLastAdvancedStatsFile,
|
||||||
recordAdvancedStats,
|
recordAdvancedStats,
|
||||||
validateRequest,
|
validateRequest,
|
||||||
} from "@dokploy/server";
|
} from "@dokploy/server";
|
||||||
import { OSUtils } from "node-os-utils";
|
|
||||||
import { WebSocketServer } from "ws";
|
import { WebSocketServer } from "ws";
|
||||||
import { formatBytes } from "@/components/dashboard/database/backups/restore-backup";
|
|
||||||
|
|
||||||
export const setupDockerStatsMonitoringSocketServer = (
|
export const setupDockerStatsMonitoringSocketServer = (
|
||||||
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>,
|
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>,
|
||||||
@@ -53,72 +52,10 @@ export const setupDockerStatsMonitoringSocketServer = (
|
|||||||
try {
|
try {
|
||||||
// Special case: when monitoring "dokploy", get host system stats instead of container stats
|
// Special case: when monitoring "dokploy", get host system stats instead of container stats
|
||||||
if (appName === "dokploy") {
|
if (appName === "dokploy") {
|
||||||
const osutils = new OSUtils();
|
const stat = await getHostSystemStats();
|
||||||
|
|
||||||
// Get CPU usage
|
|
||||||
const cpuResult = await osutils.cpu.usage();
|
|
||||||
const cpuUsage = cpuResult.success ? cpuResult.data : 0;
|
|
||||||
|
|
||||||
// Get memory info
|
|
||||||
const memResult = await osutils.memory.info();
|
|
||||||
let memUsedGB = 0;
|
|
||||||
let memTotalGB = 0;
|
|
||||||
let memUsedPercent = 0;
|
|
||||||
if (memResult.success) {
|
|
||||||
memTotalGB = memResult.data.total.toGB();
|
|
||||||
memUsedGB = memResult.data.used.toGB();
|
|
||||||
memUsedPercent = memResult.data.usagePercentage;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get network stats from network.overview() or network.statsAsync()
|
|
||||||
let netInputBytes = 0;
|
|
||||||
let netOutputBytes = 0;
|
|
||||||
const networkOverview = await osutils.network.overview();
|
|
||||||
if (networkOverview.success) {
|
|
||||||
netInputBytes = networkOverview.data.totalRxBytes.toBytes();
|
|
||||||
netOutputBytes = networkOverview.data.totalTxBytes.toBytes();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get Block I/O from disk.stats() (available in v2.0!)
|
|
||||||
// If disk.stats() doesn't work in container, fallback to /proc/diskstats
|
|
||||||
let blockReadBytes = 0;
|
|
||||||
let blockWriteBytes = 0;
|
|
||||||
const diskStats = await osutils.disk.stats();
|
|
||||||
if (diskStats.success && diskStats.data.length > 0) {
|
|
||||||
for (const stat of diskStats.data) {
|
|
||||||
blockReadBytes += stat.readBytes.toBytes();
|
|
||||||
blockWriteBytes += stat.writeBytes.toBytes();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format memory usage similar to docker stats format: "used / total"
|
|
||||||
const memUsedFormatted = `${memUsedGB.toFixed(2)}GiB`;
|
|
||||||
const memTotalFormatted = `${memTotalGB.toFixed(2)}GiB`;
|
|
||||||
const memUsageFormatted = `${memUsedFormatted} / ${memTotalFormatted}`;
|
|
||||||
|
|
||||||
// Format network I/O
|
|
||||||
const netInputMb = netInputBytes / (1024 * 1024);
|
|
||||||
const netOutputMb = netOutputBytes / (1024 * 1024);
|
|
||||||
const netIOFormatted = `${netInputMb.toFixed(2)}MB / ${netOutputMb.toFixed(2)}MB`;
|
|
||||||
|
|
||||||
// Format Block I/O
|
|
||||||
const blockIOFormatted = `${formatBytes(blockReadBytes)} / ${formatBytes(blockWriteBytes)}`;
|
|
||||||
|
|
||||||
// Create a stat object compatible with recordAdvancedStats
|
|
||||||
const stat = {
|
|
||||||
CPUPerc: `${cpuUsage.toFixed(2)}%`,
|
|
||||||
MemPerc: `${memUsedPercent.toFixed(2)}%`,
|
|
||||||
MemUsage: memUsageFormatted,
|
|
||||||
BlockIO: blockIOFormatted,
|
|
||||||
NetIO: netIOFormatted,
|
|
||||||
Container: "dokploy",
|
|
||||||
ID: "host-system",
|
|
||||||
Name: "dokploy",
|
|
||||||
};
|
|
||||||
|
|
||||||
await recordAdvancedStats(stat, appName);
|
await recordAdvancedStats(stat, appName);
|
||||||
const data = await getLastAdvancedStatsFile(appName);
|
const data = await getLastAdvancedStatsFile(appName);
|
||||||
console.log(data);
|
|
||||||
|
|
||||||
ws.send(
|
ws.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { promises } from "node:fs";
|
import { promises } from "node:fs";
|
||||||
import { OSUtils } from "node-os-utils";
|
import { OSUtils } from "node-os-utils";
|
||||||
import { paths } from "../constants";
|
import { paths } from "../constants";
|
||||||
|
|
||||||
export interface Container {
|
export interface Container {
|
||||||
BlockIO: string;
|
BlockIO: string;
|
||||||
CPUPerc: string;
|
CPUPerc: string;
|
||||||
@@ -57,6 +58,102 @@ export const recordAdvancedStats = async (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get host system statistics using node-os-utils
|
||||||
|
* This is used when monitoring "dokploy" to show host stats instead of container stats
|
||||||
|
*/
|
||||||
|
export const getHostSystemStats = async (): Promise<Container> => {
|
||||||
|
const osutils = new OSUtils({
|
||||||
|
disk: {
|
||||||
|
includeStats: true, // Enable disk I/O statistics
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get CPU usage
|
||||||
|
const cpuResult = await osutils.cpu.usage();
|
||||||
|
const cpuUsage = cpuResult.success ? cpuResult.data : 0;
|
||||||
|
|
||||||
|
// Get memory info
|
||||||
|
const memResult = await osutils.memory.info();
|
||||||
|
let memUsedGB = 0;
|
||||||
|
let memTotalGB = 0;
|
||||||
|
let memUsedPercent = 0;
|
||||||
|
if (memResult.success) {
|
||||||
|
memTotalGB = memResult.data.total.toGB();
|
||||||
|
memUsedGB = memResult.data.used.toGB();
|
||||||
|
memUsedPercent = memResult.data.usagePercentage;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get network stats from network.overview()
|
||||||
|
let netInputBytes = 0;
|
||||||
|
let netOutputBytes = 0;
|
||||||
|
const networkOverview = await osutils.network.overview();
|
||||||
|
if (networkOverview.success) {
|
||||||
|
netInputBytes = networkOverview.data.totalRxBytes.toBytes();
|
||||||
|
netOutputBytes = networkOverview.data.totalTxBytes.toBytes();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Block I/O from disk.stats()
|
||||||
|
let blockReadBytes = 0;
|
||||||
|
let blockWriteBytes = 0;
|
||||||
|
const diskStats = await osutils.disk.stats();
|
||||||
|
if (diskStats.success && diskStats.data.length > 0) {
|
||||||
|
// Filter out virtual devices (loop, ram, sr, etc.) - only include real disk devices
|
||||||
|
const excludePatterns = [/^loop/, /^ram/, /^sr\d+$/, /^fd\d+$/];
|
||||||
|
for (const stat of diskStats.data) {
|
||||||
|
// Skip virtual devices
|
||||||
|
if (
|
||||||
|
stat.device &&
|
||||||
|
excludePatterns.some((pattern) => pattern.test(stat.device))
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// readBytes and writeBytes are DataSize objects with .toBytes() method
|
||||||
|
blockReadBytes += stat.readBytes.toBytes();
|
||||||
|
blockWriteBytes += stat.writeBytes.toBytes();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format values similar to docker stats
|
||||||
|
const formatBytes = (bytes: number): string => {
|
||||||
|
if (bytes >= 1024 * 1024 * 1024) {
|
||||||
|
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)}GiB`;
|
||||||
|
}
|
||||||
|
if (bytes >= 1024 * 1024) {
|
||||||
|
return `${(bytes / (1024 * 1024)).toFixed(2)}MiB`;
|
||||||
|
}
|
||||||
|
if (bytes >= 1024) {
|
||||||
|
return `${(bytes / 1024).toFixed(2)}KiB`;
|
||||||
|
}
|
||||||
|
return `${bytes}B`;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Format memory usage similar to docker stats format: "used / total"
|
||||||
|
const memUsedFormatted = `${memUsedGB.toFixed(2)}GiB`;
|
||||||
|
const memTotalFormatted = `${memTotalGB.toFixed(2)}GiB`;
|
||||||
|
const memUsageFormatted = `${memUsedFormatted} / ${memTotalFormatted}`;
|
||||||
|
|
||||||
|
// Format network I/O
|
||||||
|
const netInputMb = netInputBytes / (1024 * 1024);
|
||||||
|
const netOutputMb = netOutputBytes / (1024 * 1024);
|
||||||
|
const netIOFormatted = `${netInputMb.toFixed(2)}MB / ${netOutputMb.toFixed(2)}MB`;
|
||||||
|
|
||||||
|
// Format Block I/O
|
||||||
|
const blockIOFormatted = `${formatBytes(blockReadBytes)} / ${formatBytes(blockWriteBytes)}`;
|
||||||
|
|
||||||
|
// Create a stat object compatible with recordAdvancedStats
|
||||||
|
return {
|
||||||
|
CPUPerc: `${cpuUsage.toFixed(2)}%`,
|
||||||
|
MemPerc: `${memUsedPercent.toFixed(2)}%`,
|
||||||
|
MemUsage: memUsageFormatted,
|
||||||
|
BlockIO: blockIOFormatted,
|
||||||
|
NetIO: netIOFormatted,
|
||||||
|
Container: "dokploy",
|
||||||
|
ID: "host-system",
|
||||||
|
Name: "dokploy",
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const getAdvancedStats = async (appName: string) => {
|
export const getAdvancedStats = async (appName: string) => {
|
||||||
return {
|
return {
|
||||||
cpu: await readStatsFile(appName, "cpu"),
|
cpu: await readStatsFile(appName, "cpu"),
|
||||||
|
|||||||
Reference in New Issue
Block a user