forked from GeekyTheory/Raspberry-Pi-Status
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·169 lines (153 loc) · 5.41 KB
/
server.js
File metadata and controls
executable file
·169 lines (153 loc) · 5.41 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
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
/**
* Autor: Mario Pérez Esteso <mario@geekytheory.com>
* Web: geekytheory.com
*/
var port = 8000;
var app = require('http').createServer(handler).listen(port, "0.0.0.0"),
io = require('socket.io').listen(app),
fs = require('fs'),
sys = require('util'),
exec = require('child_process').exec,
child, child1;
var connectCounter = 0;
//Escuchamos en el puerto $port
app.listen(port);
//Si todo va bien al abrir el navegador, cargaremos el archivo index.html
function handler(req, res) {
fs.readFile(__dirname+'/index.html', function(err, data) {
if (err) {
//Si hay error, mandaremos un mensaje de error 500
console.log(err);
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
//Cuando abramos el navegador estableceremos una conexión con socket.io.
//Cada X segundos mandaremos a la gráfica un nuevo valor.
io.sockets.on('connection', function(socket) {
var memTotal, memUsed = 0, memFree = 0, memBuffered = 0, memCached = 0, sendData = 1, percentBuffered, percentCached, percentUsed, percentFree;
var address = socket.handshake.address;
console.log("New connection from " + address.address + ":" + address.port);
connectCounter++;
console.log("NUMBER OF CONNECTIONS++: "+connectCounter);
socket.on('disconnect', function() { connectCounter--; console.log("NUMBER OF CONNECTIONS--: "+connectCounter);});
// Function for checking memory
child = exec("egrep --color 'MemTotal' /proc/meminfo | egrep '[0-9.]{4,}' -o", function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
} else {
memTotal = stdout;
socket.emit('memoryTotal', stdout);
}
});
child = exec("hostname", function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
} else {
socket.emit('hostname', stdout);
}
});
child = exec("uptime | tail -n 1 | awk '{print $1}'", function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
} else {
socket.emit('uptime', stdout);
}
});
child = exec("uname -r", function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
} else {
socket.emit('kernel', stdout);
}
});
child = exec("top -d 0.5 -b -n2 | tail -n 10 | awk '{print $12}'", function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
} else {
socket.emit('toplist', stdout);
}
});
setInterval(function(){
// Function for checking memory free and used
child1 = exec("egrep --color 'MemFree' /proc/meminfo | egrep '[0-9.]{4,}' -o", function (error, stdout, stderr) {
if (error == null) {
memFree = stdout;
memUsed = parseInt(memTotal)-parseInt(memFree);
percentUsed = Math.round(parseInt(memUsed)*100/parseInt(memTotal));
percentFree = 100 - percentUsed;
} else {
sendData = 0;
console.log('exec error: ' + error);
}
});
// Function for checking memory buffered
child1 = exec("egrep --color 'Buffers' /proc/meminfo | egrep '[0-9.]{4,}' -o", function (error, stdout, stderr) {
if (error == null) {
memBuffered = stdout;
percentBuffered = Math.round(parseInt(memBuffered)*100/parseInt(memTotal));
} else {
sendData = 0;
console.log('exec error: ' + error);
}
});
// Function for checking memory buffered
child1 = exec("egrep --color 'Cached' /proc/meminfo | egrep '[0-9.]{4,}' -o", function (error, stdout, stderr) {
if (error == null) {
memCached = stdout;
percentCached = Math.round(parseInt(memCached)*100/parseInt(memTotal));
} else {
sendData = 0;
console.log('exec error: ' + error);
}
});
if (sendData == 1) {
socket.emit('memoryUpdate', percentFree, percentUsed, percentBuffered, percentCached);
} else {
sendData = 1;
}
}, 5000);
// Function for measuring temperature
setInterval(function(){
child = exec("cat /sys/class/thermal/thermal_zone0/temp", function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
} else {
//Es necesario mandar el tiempo (eje X) y un valor de temperatura (eje Y).
var date = new Date().getTime();
var temp = parseFloat(stdout)/1000;
socket.emit('temperatureUpdate', date, temp);
}
});}, 5000);
setInterval(function(){
child = exec("top -d 0.5 -b -n2 | grep 'Cpu(s)'|tail -n 1 | awk '{print $2 + $4}'", function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
} else {
//Es necesario mandar el tiempo (eje X) y un valor de temperatura (eje Y).
var date = new Date().getTime();
socket.emit('cpuUsageUpdate', date, parseFloat(stdout));
}
});}, 5000);
// Uptime
setInterval(function(){
child = exec("uptime | tail -n 1 | awk '{print $1}'", function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
} else {
socket.emit('uptime', stdout);
}
});}, 5000);
// TOP list
setInterval(function(){
child = exec("top -d 0.5 -b -n2 | tail -n 10 | awk '{print $12}'", function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
} else {
socket.emit('toplist', stdout);
}
});}, 5000);
});