This repository has been archived by the owner on Sep 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
MMM-SystemStats.js
137 lines (121 loc) · 4.19 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* 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',
label: 'textAndIcon'
},
// Define required styles.
getStyles: function() {
return ["font-awesome.css"];
},
// Define required scripts.
getScripts: function () {
return ["moment.js", "moment-duration-format.js"];
},
// Define required translations.
getTranslations: function() {
return {
'en': 'translations/en.json',
'fr': 'translations/fr.json',
'id': 'translations/id.json',
'de': 'translations/de.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.stats.freeSpace = 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.stats.cpuTemp + '/' + 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.stats.freeSpace = payload.freeSpace;
this.updateDom(this.config.animationSpeed);
}
},
// Override dom generator.
getDom: function() {
var self = this;
var wrapper = document.createElement('table');
var sysData = {
cpuTemp: {
text: 'CPU_TEMP',
icon: 'fa-thermometer',
},
sysLoad: {
text: 'SYS_LOAD',
icon: 'fa-tachometer',
},
freeMem: {
text: 'RAM_FREE',
icon: 'fa-microchip',
},
upTime: {
text: 'UPTIME',
icon: 'fa-clock-o',
},
freeSpace: {
text: 'DISK_FREE',
icon: 'fa-hdd-o',
},
};
Object.keys(sysData).forEach(function (item){
var row = document.createElement('tr');
if (self.config.label.match(/^(text|textAndIcon)$/)) {
var c1 = document.createElement('td');
c1.setAttribute('class', 'title');
c1.style.textAlign = self.config.align;
c1.innerText = self.translate(sysData[item].text);
row.appendChild(c1);
}
if (self.config.label.match(/^(icon|textAndIcon)$/)) {
var c2 = document.createElement('td');
c2.innerHTML = `<i class="fa ${sysData[item].icon} fa-fw"></i>`;
row.appendChild(c2);
}
var c3 = document.createElement('td');
c3.setAttribute('class', 'value');
c3.style.textAlign = self.config.align;
c3.innerText = self.stats[item];
row.appendChild(c3);
wrapper.appendChild(row);
});
return wrapper;
},
});