|
1 | 1 | import { promises } from "node:fs"; |
2 | 2 | import { OSUtils } from "node-os-utils"; |
3 | 3 | import { paths } from "../constants"; |
| 4 | + |
4 | 5 | export interface Container { |
5 | 6 | BlockIO: string; |
6 | 7 | CPUPerc: string; |
@@ -57,6 +58,102 @@ export const recordAdvancedStats = async ( |
57 | 58 | } |
58 | 59 | }; |
59 | 60 |
|
| 61 | +/** |
| 62 | + * Get host system statistics using node-os-utils |
| 63 | + * This is used when monitoring "dokploy" to show host stats instead of container stats |
| 64 | + */ |
| 65 | +export const getHostSystemStats = async (): Promise<Container> => { |
| 66 | + const osutils = new OSUtils({ |
| 67 | + disk: { |
| 68 | + includeStats: true, // Enable disk I/O statistics |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + // Get CPU usage |
| 73 | + const cpuResult = await osutils.cpu.usage(); |
| 74 | + const cpuUsage = cpuResult.success ? cpuResult.data : 0; |
| 75 | + |
| 76 | + // Get memory info |
| 77 | + const memResult = await osutils.memory.info(); |
| 78 | + let memUsedGB = 0; |
| 79 | + let memTotalGB = 0; |
| 80 | + let memUsedPercent = 0; |
| 81 | + if (memResult.success) { |
| 82 | + memTotalGB = memResult.data.total.toGB(); |
| 83 | + memUsedGB = memResult.data.used.toGB(); |
| 84 | + memUsedPercent = memResult.data.usagePercentage; |
| 85 | + } |
| 86 | + |
| 87 | + // Get network stats from network.overview() |
| 88 | + let netInputBytes = 0; |
| 89 | + let netOutputBytes = 0; |
| 90 | + const networkOverview = await osutils.network.overview(); |
| 91 | + if (networkOverview.success) { |
| 92 | + netInputBytes = networkOverview.data.totalRxBytes.toBytes(); |
| 93 | + netOutputBytes = networkOverview.data.totalTxBytes.toBytes(); |
| 94 | + } |
| 95 | + |
| 96 | + // Get Block I/O from disk.stats() |
| 97 | + let blockReadBytes = 0; |
| 98 | + let blockWriteBytes = 0; |
| 99 | + const diskStats = await osutils.disk.stats(); |
| 100 | + if (diskStats.success && diskStats.data.length > 0) { |
| 101 | + // Filter out virtual devices (loop, ram, sr, etc.) - only include real disk devices |
| 102 | + const excludePatterns = [/^loop/, /^ram/, /^sr\d+$/, /^fd\d+$/]; |
| 103 | + for (const stat of diskStats.data) { |
| 104 | + // Skip virtual devices |
| 105 | + if ( |
| 106 | + stat.device && |
| 107 | + excludePatterns.some((pattern) => pattern.test(stat.device)) |
| 108 | + ) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + // readBytes and writeBytes are DataSize objects with .toBytes() method |
| 112 | + blockReadBytes += stat.readBytes.toBytes(); |
| 113 | + blockWriteBytes += stat.writeBytes.toBytes(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Format values similar to docker stats |
| 118 | + const formatBytes = (bytes: number): string => { |
| 119 | + if (bytes >= 1024 * 1024 * 1024) { |
| 120 | + return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)}GiB`; |
| 121 | + } |
| 122 | + if (bytes >= 1024 * 1024) { |
| 123 | + return `${(bytes / (1024 * 1024)).toFixed(2)}MiB`; |
| 124 | + } |
| 125 | + if (bytes >= 1024) { |
| 126 | + return `${(bytes / 1024).toFixed(2)}KiB`; |
| 127 | + } |
| 128 | + return `${bytes}B`; |
| 129 | + }; |
| 130 | + |
| 131 | + // Format memory usage similar to docker stats format: "used / total" |
| 132 | + const memUsedFormatted = `${memUsedGB.toFixed(2)}GiB`; |
| 133 | + const memTotalFormatted = `${memTotalGB.toFixed(2)}GiB`; |
| 134 | + const memUsageFormatted = `${memUsedFormatted} / ${memTotalFormatted}`; |
| 135 | + |
| 136 | + // Format network I/O |
| 137 | + const netInputMb = netInputBytes / (1024 * 1024); |
| 138 | + const netOutputMb = netOutputBytes / (1024 * 1024); |
| 139 | + const netIOFormatted = `${netInputMb.toFixed(2)}MB / ${netOutputMb.toFixed(2)}MB`; |
| 140 | + |
| 141 | + // Format Block I/O |
| 142 | + const blockIOFormatted = `${formatBytes(blockReadBytes)} / ${formatBytes(blockWriteBytes)}`; |
| 143 | + |
| 144 | + // Create a stat object compatible with recordAdvancedStats |
| 145 | + return { |
| 146 | + CPUPerc: `${cpuUsage.toFixed(2)}%`, |
| 147 | + MemPerc: `${memUsedPercent.toFixed(2)}%`, |
| 148 | + MemUsage: memUsageFormatted, |
| 149 | + BlockIO: blockIOFormatted, |
| 150 | + NetIO: netIOFormatted, |
| 151 | + Container: "dokploy", |
| 152 | + ID: "host-system", |
| 153 | + Name: "dokploy", |
| 154 | + }; |
| 155 | +}; |
| 156 | + |
60 | 157 | export const getAdvancedStats = async (appName: string) => { |
61 | 158 | return { |
62 | 159 | cpu: await readStatsFile(appName, "cpu"), |
|
0 commit comments