forked from fry0815/MMM-SystemStats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
104 lines (94 loc) · 3.2 KB
/
Copy pathnode_helper.js
File metadata and controls
104 lines (94 loc) · 3.2 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
'use strict';
/* Magic Mirror
* Module: MMM-SystemStats
*
* By Benjamin Roesner http://benjaminroesner.com
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const async = require('async');
const exec = require('child_process').exec;
const request = require('request');
module.exports = NodeHelper.create({
start: function() {
//console.log('Starting node helper: ' + this.name);
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
const self = this;
if (notification === 'CONFIG') {
this.config = payload;
// first call
self.getStats();
// interval call
setInterval(function() {
self.getStats();
}, this.config.updateInterval);
}
else if (notification === 'ALERT') {
this.config = payload.config;
// notif syslog
//console.log('url : ' + payload.config.baseURLSyslog);
request({ url: payload.config.baseURLSyslog + '?type=' + payload.type + '&message=' + encodeURI(payload.message), method: 'GET' }, function(error, response, body) {
console.log("notif MMM-syslog with response " + response.statusCode);
});
}
},
getStats: function() {
const self = this;
let temp_conv = '';
switch (this.config.units) {
case "imperial":
temp_conv = 'awk \'{printf("%.1f°F\\n",(($1*1.8)/1e3)+32)}\'';
break;
case "metric":
temp_conv = 'awk \'{printf("%.1f°C\\n",$1/1e3)}\'';
break;
case "default":
default:
// kelvin
temp_conv = 'awk \'{printf("%.1f°K\\n",($1/1e3)+273.15)}\'';
break;
}
function safeGet(res, index) {
return Array.isArray(res[index]) &&
res[index][0] &&
res[index][0].trim() !== ''
? res[index][0]
: 'N/A';
}
async.parallel([
// get cpu temp
async.apply(exec, temp_conv + ' /sys/class/thermal/thermal_zone0/temp'),
// get system load
// async.apply(exec, 'top -bn2 | grep "CPU(s)" | tail -n1 | awk \'{printf "%.1f", (100 - $8)}\'cat /proc/loadavg'),
async.apply(exec, 'top -bn2 | grep "Cpu(s)" | tail -n1 | sed -n \'s/.*, *\\([0-9.]*\\)%* id.*/\\1/p\' | awk \'{printf 100 - $1}\''),
// get free ram in %
async.apply(exec, 'free | awk \'/^' + this.config.memTrans + ':/ {printf "%.0f", ($7*100/$2)}\''),
// get uptime
async.apply(exec, 'cat /proc/uptime'),
// get root free-space
// async.apply(exec, "df -h|grep /dev/root|awk '{print $4}'"),
async.apply(exec, "df -h | awk '$NF == \"/\" {printf $4}'")
],
function (err, res) {
let stats = {
cpuTemp: safeGet(res, 0),
sysLoad: safeGet(res, 1),
freeMem: safeGet(res, 2),
upTime: safeGet(res, 3) !== 'N/A' ? safeGet(res, 3).split(' ') : ['N/A'],
freeSpace: safeGet(res, 4),
processUptime: process.uptime() // Add Electron process uptime in seconds
};
/* let stats = {};
stats.cpuTemp = res[0][0];
stats.sysLoad = res[1][0];
stats.freeMem = res[2][0];
stats.upTime = res[3][0].split(' ');
stats.freeSpace = res[4][0];
*/
// console.log(stats);
self.sendSocketNotification('STATS', stats);
});
},
});