-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_helper.js
executable file
·93 lines (82 loc) · 2.88 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const NodeHelper = require("node_helper");
const {execSync} = require('child_process');
const Log = require("logger");
const os = require('os');
module.exports = NodeHelper.create({
start: function () {
Log.log("Starting node helper: " + this.name);
},
socketNotificationReceived: function (notification, payload) {
if (notification === "CONFIG") {
this.config = payload;
this.getStats();
}
},
getStats: function () {
const self = this;
this.stats = {
cpuUsage: this.getCpuUsage(),
ramUsage: this.getRamUsage(),
diskUsage: this.getAvailableSpacePercentage(),
cpuTemperature: this.getCpuTemperature(),
privateIp: this.getPrivateIP(),
volume: this.getVolume()
}
this.stats = JSON.parse(JSON.stringify(this.stats));
this.sendSocketNotification("STATS", this.stats);
setTimeout(function () {
self.getStats();
}, this.config.updateInterval);
},
getCpuUsage: function() {
return this.config.showCpuUsage ? parseFloat(this.exec(this.config.cpuUsageCommand)) : '';
},
getRamUsage: function() {
return this.config.showRamUsage ? parseFloat(this.exec(this.config.ramUsageCommand)) : '';
},
getAvailableSpacePercentage: function() {
return this.config.showDiskUsage ? this.exec(this.config.diskUsageCommand) : '';
},
getCpuTemperature: function() {
if (this.config.showCpuTemperature) {
const t = this.exec(this.config.cpuTemperatureCommand);
return this.convertTemperature(t);
}
},
getPrivateIP() {
if (this.config.showPrivateIp) {
const interfaces = os.networkInterfaces();
for (const iface in interfaces) {
for (const addr of interfaces[iface]) {
if (!addr.internal && addr.family === 'IPv4') {
return addr.address;
}
}
}
return null; // Return null if no private IP found
}
},
getVolume() {
return this.config.showVolume ? parseFloat(this.exec(this.config.showVolumeCommand)) : '';
},
exec: function(cmd){
try {
const result = execSync(cmd);
return result.toString();
} catch (error) {
Log.error(`${this.name} - Error getting data. Command: ${cmd}`)
}
},
convertTemperature: function(temperature) {
let convertedTemp;
switch(this.config.units) {
case "imperial":
convertedTemp = ((temperature / 1000) * 1.8 + 32).toFixed(0);
break;
case "metric":
default:
convertedTemp = (temperature / 1000).toFixed(0);
}
return convertedTemp;
},
});