forked from slametps/MMM-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
237 lines (214 loc) · 7.15 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* Magic Mirror
* Node Helper: MMM-Tools
*
* By
* MIT Licensed.
*/
var async = require('async')
var exec = require('child_process').exec
var request = require('request')
var moment = require('moment')
var myMath= {}
myMath.round = function(number, precision) {
var factor = Math.pow(10, precision)
var tempNumber = number * factor
var roundedTempNumber = Math.round(tempNumber)
return roundedTempNumber / factor
}
const scripts = {
//onStart
IP : "hostname -I",
MEMORY_TOTAL : "head -5 /proc/meminfo | awk '{print}' ORS=' ' | awk '{print ($2)/1024}' | cut -f1 -d\".\" | sed 's/$/Mb/'",
STORAGE_TOTAL : "df -h | grep /$ | awk '{print}' ORS=' ' | awk '{print $2}'",
//onSchedule
CPU_TEMPERATURE : "cat /sys/devices/virtual/thermal/thermal_zone0/temp",
GPU_TEMPERATURE : "cat /sys/devices/virtual/thermal/thermal_zone1/temp",
//@FIXME uptime format check!!!
//UPTIME : "cat /proc/uptime | awk '{print $1}'", //cat /proc/uptime
UPTIME : "uptime | awk -F'( |,|:)+' '{print $4,$5,$6,\"hours\",$7,\"minutes\"}'",
CPU_USAGE : "top -bn 2 | grep Cpu | awk '{print $8}' | awk '{print}' ORS=' ' | awk '{print 100-$2}'", // A bit slower to get result but more accurate , actually reflecting what the task manager shows.
MEMORY_USED : "head -5 /proc/meminfo | awk '{print}' ORS=' ' | awk '{print (($2-$5)-($11+$14))/1024}' | cut -f1 -d\".\" | sed 's/$/Mb/'",
MEMORY_USED_PERCENT : "head -5 /proc/meminfo | awk '{print}' ORS=' ' | awk '{print (($2-$5)-($11+$14))/$2*100}'",
STORAGE_USED : "df -h | grep /$ | awk '{print}' ORS=' ' | awk '{print $3}'",
STORAGE_USED_PERCENT : "df -h | grep /$ | awk '{print}' ORS=' ' | awk '{print $3/$2*100}'",
//onDemand
SCREEN_ON : "xset dpms force on",
SCREEN_OFF : "xset dpms force off",
SCREEN_STATUS : "xset q | grep 'Monitor is' | awk '{print $3}'",
SCREEN_CAPTURE : "scrot screencapture.png"
}
const rpi_scripts = {
CPU_TEMPERATURE : "cat /sys/class/thermal/thermal_zone0/temp",
GPU_TEMPERATURE : "/opt/vc/bin/vcgencmd measure_temp", // frankly, I think these two in RPI are internally same...
//Is it better to use tvservice???
SCREEN_ON : "vcgencmd display_power 1",
SCREEN_OFF : "vcgencmd display_power 0",
SCREEN_STATUS : "vcgencmd display_power | grep -q 'display_power=1' && echo 'Display ON' || echo 'Display OFF'", // really Better !
UPTIME : "uptime -p | awk '{print}' ORS=' ' | awk '{print ($2,$3,$4,$5,$6,$7)}'"
}
var NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
start : function() {
this.config = {}
this.timer = null
this.scripts = {}
this.status = {
IP : "Loading...",
MEMORY_TOTAL : "0",
STORAGE_TOTAL : "0",
CPU_TEMPERATURE : "0.0",
GPU_TEMPERATURE : "0.0",
UPTIME : "00:00",
CPU_USAGE : "0.00",
MEMORY_USED : "0",
MEMORY_USED_PERCENT : "0",
STORAGE_USED : "0",
STORAGE_USED_PERCENT : "0",
SCREEN_STATUS : "Loading...",
}
this.sendSocketNotification('STATUS', this.status)
},
socketNotificationReceived : function(notification, payload) {
if (notification === "CONFIG") {
this.config = payload
this.scripts = scripts
if (this.config.device == 'RPI') {
this.scripts = Object.assign({}, this.scripts, rpi_scripts)
}
this.getIP()
this.getMemoryTotal()
this.getStorageTotal()
this.scheduler()
}
if (notification == 'SCREEN_ON') {
exec (this.scripts['SCREEN_ON'], (err, stdout, stderr)=>{})
}
if (notification == 'SCREEN_OFF') {
exec (this.scripts['SCREEN_OFF'], (err, stdout, stderr)=>{})
}
if (notification == 'SCREEN_CAPTURE') {
exec (this.scripts['SCREEN_CAPTURE'], (err, stdout, stderr)=>{
if (!err) {
this.sendSocketNotification('SCREEN_CAPTURED', payload)
}
})
}
},
scheduler : function() {
this.timer = null
this.monitor()
timer = setTimeout(()=>{
this.scheduler()
}, this.config.refresh_interval_ms)
},
monitor : function() {
this.getCPUTemp()
this.getGPUTemp()
this.getUpTime()
this.getCPUUsage()
this.getMemoryUsed()
this.getStorageUsed()
this.getMemoryUsedPercent()
this.getStorageUsedPercent()
this.getScreen()
this.sendSocketNotification('STATUS', this.status)
},
getIP : function() {
exec (this.scripts['IP'], (err, stdout, stderr)=>{
if (err == null) {
var matched = stdout.trim().match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/)
this.status['IP'] = (matched) ? matched[0] : "Unknown"
}
})
},
getMemoryTotal : function() {
exec (this.scripts['MEMORY_TOTAL'], (err, stdout, stderr)=>{
if (err == null) {
var value = parseInt(stdout.trim())
this.status['MEMORY_TOTAL'] = stdout.trim()
}
})
},
getStorageTotal : function() {
exec (this.scripts['STORAGE_TOTAL'], (err, stdout, stderr)=>{
if (err == null) {
this.status['STORAGE_TOTAL'] = stdout.trim()
}
})
},
getCPUTemp : function() {
exec (this.scripts['CPU_TEMPERATURE'], (err, stdout, stderr)=>{
if (err == null) {
var value = stdout.trim()
value = myMath.round((value / 1000), 1)
this.status['CPU_TEMPERATURE'] = value
}
})
},
getGPUTemp : function() {
exec (this.scripts['GPU_TEMPERATURE'], (err, stdout, stderr)=>{
if (err == null) {
var value = stdout.trim()
if (this.config.device == 'RPI') {
value = value.replace('temp=','').replace('\'C','')
} else {
value = myMath.round((value / 1000), 1)
}
this.status['GPU_TEMPERATURE'] = value
}
})
},
getUpTime : function() {
exec (this.scripts['UPTIME'], (err, stdout, stderr)=>{
if (err == null) {
var value = stdout.trim()
// var mv = moment.duration(value, 'seconds').humanize() // not anymore necessary
this.status['UPTIME'] = value
}
})
},
getCPUUsage : function() {
exec (this.scripts['CPU_USAGE'], (err, stdout, stderr)=>{
if (err == null) {
this.status['CPU_USAGE'] = myMath.round(stdout.trim(), 2)
}
})
},
getMemoryUsed : function() {
exec (this.scripts['MEMORY_USED'], (err, stdout, stderr)=>{
if (err == null) {
this.status['MEMORY_USED'] = stdout.trim().replace(",", "")
}
})
},
getStorageUsed : function() {
exec (this.scripts['STORAGE_USED'], (err, stdout, stderr)=>{
if (err == null) {
this.status['STORAGE_USED'] = stdout.trim()
}
})
},
getMemoryUsedPercent : function() {
exec (this.scripts['MEMORY_USED_PERCENT'], (err, stdout, stderr)=>{
if (err == null) {
this.status['MEMORY_USED_PERCENT']
= myMath.round(stdout.trim(), 1)
}
})
},
getStorageUsedPercent : function() {
exec (this.scripts['STORAGE_USED_PERCENT'], (err, stdout, stderr)=>{
if (err == null) {
this.status['STORAGE_USED_PERCENT']
= myMath.round(parseInt(stdout.trim()), 1)
}
})
},
getScreen : function() {
exec (this.scripts['SCREEN_STATUS'], (err, stdout, stderr)=>{
if (err == null) {
this.status['SCREEN_STATUS'] = stdout.trim()
}
})
}
});