-
Notifications
You must be signed in to change notification settings - Fork 5
/
getSystemStats.js
132 lines (118 loc) · 3.66 KB
/
getSystemStats.js
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
import si from "systeminformation";
import { debugToFile } from "./helpers.js";
import axios from "axios";
import macaddress from "macaddress";
export function getMemoryUsage() {
return new Promise((resolve, reject) => {
si.mem()
.then((memory) => {
const totalMemory = memory.total;
const usedMemory = memory.active; // 'active' is usually what's actually used
const memoryUsagePercent = (usedMemory / totalMemory) * 100;
resolve(memoryUsagePercent.toFixed(1)); // Return memory usage as a percentage
})
.catch((error) => {
debugToFile(`getMemoryUsage(): ${error}`);
reject(error);
});
});
}
export function getCpuUsage() {
return new Promise((resolve, reject) => {
si.currentLoad()
.then((load) => {
const currentLoad = load.currentLoad;
resolve(currentLoad);
})
.catch((error) => {
debugToFile(`getCpuUsage(): ${error}`);
reject(error);
});
});
}
export function getDiskUsage(installDir) {
return new Promise((resolve, reject) => {
si.fsSize()
.then((drives) => {
let diskUsagePercent = 0;
// Sort drives by the length of their mount point, descending
drives.sort((a, b) => b.mount.length - a.mount.length);
// Find the drive with the longest mount point that is a prefix of installDir
const installDrive = drives.find((drive) => {
return installDir.startsWith(drive.mount);
});
if (installDrive) {
// debugToFile(`Drive info: ${JSON.stringify(installDrive, null, 2)}`);
if (process.platform === "darwin") {
// macOS (installDrive.use is weird on macOS)
diskUsagePercent =
((installDrive.size - installDrive.available) /
installDrive.size) *
100;
} else {
// Linux and others
diskUsagePercent = installDrive.use;
}
} else {
debugToFile(
`getDiskUsage(): Drive for ${installDir} not found.`,
() => {}
);
}
resolve(diskUsagePercent.toFixed(1));
})
.catch((error) => {
debugToFile(`getDiskUsage(): ${error}`);
reject(error);
});
});
}
export function getCpuTemperature() {
return new Promise((resolve, reject) => {
si.cpuTemperature()
.then((data) => {
// debugToFile(`CPU data: ${JSON.stringify(data, null, 2)}`);
const cpuTemp = data.main;
if (cpuTemp !== null && cpuTemp !== undefined) {
resolve(cpuTemp.toFixed(1)); // Return CPU temperature as a fixed-point number
} else {
resolve(0);
}
})
.catch((error) => {
debugToFile(`Error fetching CPU temperature: ${error}`);
reject(error);
});
});
}
export async function getPublicIPAddress() {
while (true) {
try {
const response = await axios.get("https://api.ipify.org?format=json");
return response.data.ip;
} catch (error) {
debugToFile(`Error fetching public IP address: ${error}`);
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}
}
export function getMacAddress() {
return new Promise((resolve, reject) => {
macaddress.all((err, all) => {
if (err) {
reject(`Error getting MAC address: ${err}`);
return;
}
// Get the first non-internal MAC address
let macAddress = null;
for (const interfaceName in all) {
const mac = all[interfaceName].mac;
if (mac && mac !== "00:00:00:00:00:00") {
macAddress = mac;
break;
}
}
resolve(macAddress);
});
});
}