-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
76 lines (58 loc) · 1.83 KB
/
node_helper.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
/* Magic Mirror
* Node Helper: MMM-SystemMonitor
*
* By Ben Konsemüller
* MIT Licensed.
*/
const Log = require("logger");
const NodeHelper = require("node_helper");
const util = require('util');
const exec = util.promisify(require('child_process').exec);
module.exports = NodeHelper.create({
config: {},
currentFile: null,
async socketNotificationReceived(notification, payload) {
if (notification === "CONFIG") {
this.config = payload;
if (this.fetchTimerId) {
clearTimeout(this.fetchTimerId);
}
await this.fetchData();
}
},
async fetchData() {
const self = this;
const cpu_temp = await this.getCpuTemperature();
const available_memory = await this.getAvailableMemoryPercentage();
const uptime = await this.getUptimeSeconds();
const available_space = await this.getAvailableSpacePercentage();
this.sendSocketNotification("SYSTEM_MONITOR_DATA", { cpu_temp, available_memory, uptime, available_space });
this.fetchTimerId = setTimeout(async function () {
await self.fetchData();
}, this.config.updateInterval);
},
async getCpuTemperature() {
return await this.exec(`cat /sys/class/thermal/thermal_zone${this.config.cpuThermalZone}/temp`);
},
async getAvailableMemoryPercentage() {
return await this.exec("free | awk '/^Mem/ { print (($4+$7)/$2 * 100) }'");
},
async getUptimeSeconds() {
const result = await this.exec("cat /proc/uptime");
if (!result) {
return null;
}
return result.split(" ")[0];
},
async getAvailableSpacePercentage() {
return await this.exec("df | awk '$6 == \"/\" {print $5}'");
},
async exec(cmd) {
const { stdout, stderr } = await exec(cmd);
if (stderr) {
Log.error(`${this.name} - Error getting data. Command: ${cmd}`)
return null;
}
return stdout;
}
});