-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
166 lines (144 loc) · 5.51 KB
/
node_helper.js
File metadata and controls
166 lines (144 loc) · 5.51 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* node_helper.js */
const NodeHelper = require("node_helper");
const fs = require("fs");
const os = require("os");
const { exec } = require("child_process");
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node helper for: " + this.name);
this.lastCPUInfo = null;
},
socketNotificationReceived: function(notification, payload) {
if (notification === "START_MONITORING") {
this.config = payload;
this.getSystemData();
} else if (notification === "GET_SYSTEM_DATA") {
this.getSystemData();
}
},
getSystemData: function() {
var self = this;
var systemData = {};
// Get CPU temperature
this.getCPUTemperature().then(temp => {
systemData.cpuTemp = temp;
// Get GPU temperature
return this.getGPUTemperature();
}).then(temp => {
systemData.gpuTemp = temp;
// Get CPU usage
return this.getCPUUsage();
}).then(usage => {
systemData.cpuUsage = usage;
// Get memory info
const memInfo = this.getMemoryInfo();
systemData.memTotal = memInfo.total;
systemData.memUsed = memInfo.used;
systemData.memFree = memInfo.free;
// Get disk info
return this.getDiskInfo();
}).then(diskInfo => {
systemData.diskTotal = diskInfo.total;
systemData.diskUsed = diskInfo.used;
systemData.diskFree = diskInfo.free;
// Get load average
systemData.loadAverage = os.loadavg();
// Get uptime
systemData.uptime = os.uptime();
// Send data to frontend
self.sendSocketNotification("SYSTEM_DATA", systemData);
}).catch(error => {
console.error("Error getting system data:", error);
// Send partial data even if some operations fail
self.sendSocketNotification("SYSTEM_DATA", systemData);
});
},
getCPUTemperature: function() {
return new Promise((resolve) => {
fs.readFile("/sys/class/thermal/thermal_zone0/temp", "utf8", (err, data) => {
if (err) {
console.log("CPU temp read error:", err);
resolve(null);
} else {
resolve(parseInt(data) / 1000);
}
});
});
},
getGPUTemperature: function() {
return new Promise((resolve) => {
exec("vcgencmd measure_temp", (error, stdout) => {
if (error) {
console.log("GPU temp error:", error);
resolve(null);
} else {
const match = stdout.match(/temp=([0-9.]+)/);
resolve(match ? parseFloat(match[1]) : null);
}
});
});
},
getCPUUsage: function() {
return new Promise((resolve) => {
// Read CPU info from /proc/stat
fs.readFile("/proc/stat", "utf8", (err, data) => {
if (err) {
console.log("CPU usage read error:", err);
resolve(null);
return;
}
const lines = data.split("\n");
const cpuLine = lines[0];
const cpuInfo = cpuLine.split(/\s+/);
const idle = parseInt(cpuInfo[4]);
const total = cpuInfo.slice(1, 8).reduce((acc, val) => acc + parseInt(val), 0);
if (this.lastCPUInfo) {
const idleDelta = idle - this.lastCPUInfo.idle;
const totalDelta = total - this.lastCPUInfo.total;
const usage = 100 - (100 * idleDelta / totalDelta);
this.lastCPUInfo = { idle, total };
resolve(Math.max(0, Math.min(100, usage)));
} else {
this.lastCPUInfo = { idle, total };
// First run, can't calculate usage yet
setTimeout(() => this.getCPUUsage().then(resolve), 1000);
}
});
});
},
getMemoryInfo: function() {
const total = os.totalmem();
const free = os.freemem();
const used = total - free;
return {
total: total,
used: used,
free: free
};
},
getDiskInfo: function() {
return new Promise((resolve) => {
exec("df -B1 / | grep -v Filesystem", (error, stdout) => {
if (error) {
console.log("Disk info error:", error);
resolve({ total: 0, used: 0, free: 0 });
} else {
try {
// Remove extra spaces and split
const data = stdout.trim().split(/\s+/);
console.log("Disk info raw data:", data);
// df output format: Filesystem 1B-blocks Used Available Use% Mounted
resolve({
total: parseInt(data[1]) || 0,
used: parseInt(data[2]) || 0,
free: parseInt(data[3]) || 0
});
} catch (parseError) {
console.log("Disk info parse error:", parseError);
resolve({ total: 0, used: 0, free: 0 });
}
}
});
});
}
});