Skip to content

Commit

Permalink
Create a multi-container app
Browse files Browse the repository at this point in the history
  • Loading branch information
jo12bar committed Jul 29, 2019
1 parent 522deec commit 10d7f72
Show file tree
Hide file tree
Showing 22 changed files with 1,354 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
10 changes: 10 additions & 0 deletions data/Dockerfile.template
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"]
51 changes: 51 additions & 0 deletions data/index.js
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);
});
});
Loading

0 comments on commit 10d7f72

Please sign in to comment.