-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,354 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM balenalib/%%BALENA_MACHINE_NAME%%-debian-node:10-stretch-run | ||
|
||
WORKDIR /usr/src/app | ||
|
||
COPY package*.json ./ | ||
|
||
RUN JOBS=MAX npm install --production | ||
COPY . ./ | ||
|
||
CMD ["npm", "start"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const express = require('express'); | ||
const app = express(); | ||
const server = require('http').createServer(app); | ||
const io = require('socket.io')(server); | ||
const { exec } = require('child_process'); | ||
|
||
server.listen(8080); | ||
|
||
const getCpuLoad = socket => exec('cat /proc/loadavg', (err, text) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
// Get overall load average from the last minute | ||
const matchLoad = text.match(/(\d+\.\d+)\s+/); | ||
|
||
if (matchLoad) { | ||
const load = parseFloat(matchLoad[1]); | ||
socket.emit('loadavg', { onemin: load }); | ||
} | ||
}); | ||
|
||
const getMemoryInfo = socket => exec('cat /proc/meminfo', (err, text) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
const matchTotal = text.match(/MemTotal:\s+([0-9]+)/); | ||
const matchFree = text.match(/MemFree:\s+([0-9]+)/); | ||
|
||
if (matchTotal && matchFree) { | ||
const total = parseInt(matchTotal[1], 10); | ||
const free = parseInt(matchFree[1], 10); | ||
const percentageUsed = (total - free) / total * 100; | ||
socket.emit('memory', { used: percentageUsed }); | ||
} | ||
}); | ||
|
||
io.on('connection', socket => { | ||
console.log('A user connected!'); | ||
|
||
let dataloop = setInterval(() => { | ||
getCpuLoad(socket); | ||
getMemoryInfo(socket); | ||
}, 1000); | ||
|
||
socket.on('disconnect', () => { | ||
console.log('A user disconnected.'); | ||
clearInterval(dataloop); | ||
}); | ||
}); |
Oops, something went wrong.