forked from Dokploy/dokploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilts.ts
145 lines (122 loc) · 4.08 KB
/
utilts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { promises } from "node:fs";
import { MONITORING_PATH } from "../constants";
import dockerstats from "dockerstats";
import osUtils from "node-os-utils";
export const recordAdvancedStats = async (
appName: string,
containerId: string,
) => {
await promises.mkdir(`${MONITORING_PATH}/${appName}`, { recursive: true });
const result = await dockerstats.dockerContainerStats(containerId);
if (!result || result.length === 0 || !result[0]) return;
const { memoryStats, cpuStats, precpuStats, netIO, blockIO } = result[0];
const memoryUsage = memoryStats.usage / 1024 / 1024;
const memoryTotal = memoryStats.limit / 1024 / 1024;
const memoryFree = memoryTotal - memoryUsage;
const memoryUsedPercentage = (memoryUsage / memoryTotal) * 100;
const cpuDelta =
cpuStats.cpu_usage.total_usage - precpuStats.cpu_usage.total_usage;
const systemDelta = cpuStats.system_cpu_usage - precpuStats.system_cpu_usage;
const onlineCpus = cpuStats.online_cpus;
// Calcular el porcentaje de uso del CPU
const cpuPercent = (cpuDelta / systemDelta) * onlineCpus * 100;
// Extraer los valores de entrada y salida del objeto netIO
const networkInBytes = netIO.rx;
const networkOutBytes = netIO.wx;
// Convertir bytes a Megabytes
const networkInMB = networkInBytes / 1024 / 1024;
const networkOutMB = networkOutBytes / 1024 / 1024;
// BlockIO
const blockRead = blockIO.r;
const blockWrite = blockIO.w;
const blockInMBBlocks = blockRead / 1024 / 1024;
const blockOutMBBlocks = blockWrite / 1024 / 1024;
// Disk
const disk = await osUtils.drive.info("/");
const diskUsage = disk.usedGb;
const diskTotal = disk.totalGb;
const diskUsedPercentage = disk.usedPercentage;
const diskFree = disk.freeGb;
await updateStatsFile(appName, "cpu", cpuPercent);
await updateStatsFile(appName, "memory", {
used: memoryUsage,
free: memoryFree,
usedPercentage: memoryUsedPercentage,
total: memoryTotal,
});
await updateStatsFile(appName, "block", {
readMb: blockInMBBlocks,
writeMb: blockOutMBBlocks,
});
await updateStatsFile(appName, "network", {
inputMb: networkInMB,
outputMb: networkOutMB,
});
if (appName === "dokploy") {
await updateStatsFile(appName, "disk", {
diskTotal: +diskTotal,
diskUsedPercentage: +diskUsedPercentage,
diskUsage: +diskUsage,
diskFree: +diskFree,
});
}
};
export const getAdvancedStats = async (appName: string) => {
return {
cpu: await readStatsFile(appName, "cpu"),
memory: await readStatsFile(appName, "memory"),
disk: await readStatsFile(appName, "disk"),
network: await readStatsFile(appName, "network"),
block: await readStatsFile(appName, "block"),
};
};
export const readStatsFile = async (
appName: string,
statType: "cpu" | "memory" | "disk" | "network" | "block",
) => {
try {
const filePath = `${MONITORING_PATH}/${appName}/${statType}.json`;
const data = await promises.readFile(filePath, "utf-8");
return JSON.parse(data);
} catch (error) {
return [];
}
};
export const updateStatsFile = async (
appName: string,
statType: "cpu" | "memory" | "disk" | "network" | "block",
value: number | string | unknown,
) => {
const stats = await readStatsFile(appName, statType);
stats.push({ value, time: new Date() });
if (stats.length > 288) {
stats.shift();
}
const content = JSON.stringify(stats);
await promises.writeFile(
`${MONITORING_PATH}/${appName}/${statType}.json`,
content,
);
};
export const readLastValueStatsFile = async (
appName: string,
statType: "cpu" | "memory" | "disk" | "network" | "block",
) => {
try {
const filePath = `${MONITORING_PATH}/${appName}/${statType}.json`;
const data = await promises.readFile(filePath, "utf-8");
const stats = JSON.parse(data);
return stats[stats.length - 1] || null;
} catch (error) {
return null;
}
};
export const getLastAdvancedStatsFile = async (appName: string) => {
return {
cpu: await readLastValueStatsFile(appName, "cpu"),
memory: await readLastValueStatsFile(appName, "memory"),
disk: await readLastValueStatsFile(appName, "disk"),
network: await readLastValueStatsFile(appName, "network"),
block: await readLastValueStatsFile(appName, "block"),
};
};