Skip to content

Commit

Permalink
[Dev] add RAM Usage
Browse files Browse the repository at this point in the history
  • Loading branch information
qfdk committed Jul 8, 2019
1 parent c5dc676 commit 52858f3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
34 changes: 31 additions & 3 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ $(document).ready(function () {

if (codePageCourante == 'containers') {
getContainersCPU();
getContainersRAM();
}
if (codePageCourante == 'terminal') {
terminal();
Expand Down Expand Up @@ -70,19 +71,46 @@ function getContainersCPU() {
}
}

function getContainersRAM() {
var containers = $('.container-cpu');
for (var i = 0; i < containers.length; i++) {
var containerId = $('.container-ram').eq(i).attr('container-id')
getContainerRAMInfoById(containerId);
}
}

function getContainerCPUInfoById(id) {
var host = window.location.origin;
var socket = io.connect(host);
socket.emit('getCPU', id);
socket.emit('getSysInfo', id);
socket.on(id, (data) => {
var res = calculateCPUPercentUnix(JSON.parse(data));
$('.container-cpu[container-id=' + id + ']').text(res + ' %');
var json = JSON.parse(data);
var res = calculateCPUPercentUnix(json);
if (json.precpu_stats.system_cpu_usage) {
$('.container-cpu[container-id=' + id + ']').text(res + ' %');
}
});
socket.on('end', (status) => {
console.log("[END] getContainerCPUInfoById");
});
}

function getContainerRAMInfoById(id) {
var host = window.location.origin;
var socket = io.connect(host);
socket.emit('getSysInfo', id);
socket.on(id, (data) => {
var json = JSON.parse(data);
if (json.memory_stats.usage) {
var tmp = ((json.memory_stats.usage / json.memory_stats.limit) * 100).toFixed(2)
$('.container-ram[container-id=' + id + ']').text(tmp + ' %');
}
});
socket.on('end', (status) => {
console.log("[END] getContainerRAMInfoById");
});
}

function logs() {
Terminal.applyAddon(attach);
Terminal.applyAddon(fit);
Expand Down
44 changes: 41 additions & 3 deletions routes/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var returnContainersRouter = function (io) {

if (req.body.containerCmd != "") {
options.Cmd = ['/bin/bash', '-c', req.body.containerCmd];
console.log(options)
// console.log(options)
docker.createContainer(options, function (err, container) {
if (err) throw err
container.start(function (err, data) {
Expand Down Expand Up @@ -187,21 +187,59 @@ var returnContainersRouter = function (io) {
container.logs(logs_opts, handler);
});

socket.on('getCPU', function (id) {
// var exp = {
// "read": "0001-01-01T00:00:00Z",
// "preread": "0001-01-01T00:00:00Z",
// "pids_stats": {},
// "blkio_stats": {
// "io_service_bytes_recursive": null,
// "io_serviced_recursive": null,
// "io_queue_recursive": null,
// "io_service_time_recursive": null,
// "io_wait_time_recursive": null,
// "io_merged_recursive": null,
// "io_time_recursive": null,
// "sectors_recursive": null
// },
// "num_procs": 0,
// "storage_stats": {},
// "cpu_stats": {
// "cpu_usage": {
// "total_usage": 0,
// "usage_in_kernelmode": 0,
// "usage_in_usermode": 0
// },
// "throttling_data": {
// "periods": 0,
// "throttled_periods": 0,
// "throttled_time": 0
// }
// },
// "precpu_stats": {
// "cpu_usage": {
// "total_usage": 0,
// "usage_in_kernelmode": 0,
// "usage_in_usermode": 0
// }, "throttling_data": { "periods": 0, "throttled_periods": 0, "throttled_time": 0 }
// }, "memory_stats": {}, "name": "/nervous_jang", "id": "ab61765a4d91bc6c68779508dfe75774da3717c539f9d9b5002379c83939c7e7"
// }

socket.on('getSysInfo', function (id) {
var container = docker.getContainer(id);
container.stats(function (err, stream) {
if (!err && stream != null) {
stream.on('data', function (data) {
socket.emit(id, data.toString('utf8'));
});

stream.on('end', function () {
socket.emit('end', 'ended');
stream.destroy();
});
}
});
});


});


Expand Down
2 changes: 1 addition & 1 deletion routes/overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var returnOverviewRouter = function (io) {

router.get('/', function (req, res, next) {
docker.info(function (err, info) {
console.log(info)
// console.log(info)
if (err) {
res.render('error', {
message: "Docker is running ?"
Expand Down
3 changes: 3 additions & 0 deletions views/containers.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<th>Port</th>
<th>State</th>
<th>CPU</th>
<th>RAM</th>
<th>Action</th>
</tr>
</thead>
Expand Down Expand Up @@ -52,6 +53,8 @@
<% } %>
</td>
<td class="container-cpu" container-id="<%= container.Id %>">No data</td>
<td class="container-ram" container-id="<%= container.Id %>">No data</td>

<td>
<% if(container.State=='running'){ %>
<a class="btn btn-warning btn-xs" data-loading-text="<i class='fa fa-spinner fa-spin fa-fw' aria-hidden='true'></i> Waiting..." href="/containers/stop/<%= container.Id %>">
Expand Down

0 comments on commit 52858f3

Please sign in to comment.