forked from BenRoe/MMM-SystemStats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-SystemStats.js
113 lines (95 loc) · 4.39 KB
/
MMM-SystemStats.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
/* global Module */
/* Magic Mirror
* Module: MMM-SystemStats
*
* By Benjamin Roesner http://benjaminroesner.com
* MIT Licensed.
*/
Module.register('MMM-SystemStats', {
defaults: {
updateInterval: 10000,
animationSpeed: 0,
align: 'right',
language: config.language,
units: config.units,
useSyslog: false,
thresholdCPUTemp: 75, // in configured units
baseURLSyslog: 'http://127.0.0.1:8080/syslog'
},
getStyles: function () {
return [
"MMM-SystemStats.css",
];
},
// Define required scripts.
getScripts: function () {
return ["moment.js", "moment-duration-format.js"];
},
// Define required translations.
getTranslations: function() {
return {
'en': 'translations/en.json',
'id': 'translations/id.json'
};
},
// Define start sequence
start: function() {
Log.log('Starting module: ' + this.name);
// set locale
moment.locale(this.config.language);
this.stats = {};
this.stats.cpuTemp = this.translate('LOADING').toLowerCase();
this.stats.sysLoad = this.translate('LOADING').toLowerCase();
this.stats.freeMem = this.translate('LOADING').toLowerCase();
this.stats.upTime = this.translate('LOADING').toLowerCase();
this.sendSocketNotification('CONFIG', this.config);
},
socketNotificationReceived: function(notification, payload) {
//Log.log('MMM-SystemStats: socketNotificationReceived ' + notification);
//Log.log(payload);
if (notification === 'STATS') {
this.stats.cpuTemp = payload.cpuTemp;
//console.log("this.config.useSyslog-" + this.config.useSyslog + ', this.stats.cpuTemp-'+parseInt(this.stats.cpuTemp)+', this.config.thresholdCPUTemp-'+this.config.thresholdCPUTemp);
if (this.config.useSyslog) {
var cpuTemp = Math.ceil(parseFloat(this.stats.cpuTemp));
//console.log('before compare (' + cpuTemp + '/' + this.config.thresholdCPUTemp + ')');
if (cpuTemp > this.config.thresholdCPUTemp) {
console.log('alert for threshold violation (' + cpuTemp + '/' + this.config.thresholdCPUTemp + ')');
this.sendSocketNotification('ALERT', {config: this.config, type: 'WARNING', message: this.translate("TEMP_THRESHOLD_WARNING") + ' (' + this.config.thresholdCPUTemp + ')' });
}
}
this.stats.sysLoad = payload.sysLoad[0];
this.stats.freeMem = Number(payload.freeMem).toFixed() + '%';
upTime = parseInt(payload.upTime[0]);
this.stats.upTime = moment.duration(upTime, "seconds").humanize();
this.updateDom(this.config.animationSpeed);
}
},
// Override dom generator.
getDom: function() {
var self = this;
var wrapper = document.createElement('table');
wrapper.className = "small";
wrapper.innerHTML = '<tr class="normal">' +
'<td class="symbol"> <span class="fa fa-thermometer"></span></td>' +
'<td class="title light" style="text-align:' + self.config.align + ';">' + this.translate("CPU_TEMP") + ': </td>' +
'<td class="value bright" style="text-align:left;">' + this.stats.cpuTemp + '</td>' +
'</tr>' +
'<tr class="normal">' +
'<td class="symbol"><span class="fa fa-tasks"></span></td>' +
'<td class="title light" style="text-align:' + self.config.align + ';">' + this.translate("SYS_LOAD") + ': </td>' +
'<td class="value bright" style="text-align:left;">' + this.stats.sysLoad + '</td>' +
'</tr>' +
'<tr class="normal">' +
'<td class="symbol"><span class="fa fa-microchip"></span></td>' +
'<td class="title light" style="text-align:' + self.config.align + ';">' + this.translate("RAM_FREE") + ': </td>' +
'<td class="value bright" style="text-align:left;">' + this.stats.freeMem + '</td>' +
'</tr>' +
'<tr class="normal">' +
'<td class="symbol"><span class="fa fa-clock-o"></span></td>' +
'<td class="title light" style="text-align:' + self.config.align + ';">' + this.translate("UPTIME") + ': </td>' +
'<td class="value bright" style="text-align:left;">' + this.stats.upTime + '</td>' +
'</tr>';
return wrapper;
},
});