Fixed compatibility with current Docker versions

pull/8/head
Heiner 2020-04-23 20:35:55 +02:00
rodzic eb665148ba
commit ef45321c78
3 zmienionych plików z 27 dodań i 42 usunięć

2
.gitignore vendored
Wyświetl plik

@ -1,2 +1,4 @@
node_modules/ node_modules/
db/ db/
test/
package-lock.json

Wyświetl plik

@ -2,7 +2,6 @@ var DB_FILE = 'db/stats.db';
var sqlite3 = require('sqlite3'); var sqlite3 = require('sqlite3');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
var exec = require('child_process').execFileSync;
var moment = require('moment'); var moment = require('moment');
var express = require("express"); var express = require("express");

Wyświetl plik

@ -9,33 +9,11 @@ var moment = require('moment');
var fs = require('fs'); var fs = require('fs');
var db = new sqlite3.Database(DB_FILE); var db = new sqlite3.Database(DB_FILE);
var getContainers = function() {
var containers = {};
var out;
if (TEST) {
out = fs.readFileSync('test/docker_ps.txt', {encoding: 'utf-8'});
} else {
out = spawn(DOCKER, ['ps', '-a']).stdout.toString();
}
var lines = out.split('\n');
for (var i=0; i<lines.length; i++) {
var line = lines[i];
var columns = line.split(/\s{3,}/g);
if (i > 0 && columns.length >= 2) {
containers[columns[0]] = {
'name': columns[columns.length-1]
};
}
}
return containers;
};
var getBytes = function(s) { var getBytes = function(s) {
var tokens = s.split(' ');
var bytes = 0; var bytes = 0;
var unit = tokens[1].trim(); var value = s.match(/\d+/g)[0];
var value = tokens[0].trim(); var unit = s.match(/[a-zA-Z]+/g)[0].toUpperCase();
if (unit == 'kB') { if (unit == 'KB') {
return value*1024; return value*1024;
} else if (unit == 'MB') { } else if (unit == 'MB') {
return value*1024*1024; return value*1024*1024;
@ -47,12 +25,13 @@ var getBytes = function(s) {
return bytes; return bytes;
}; };
var addStatsToContainerList = function(containers) { var getContainers = function() {
var containers = {};
var out; var out;
if (TEST) { if (TEST) {
out = fs.readFileSync('test/docker_stats.txt', {encoding: 'utf-8'}); out = fs.readFileSync('test/docker_stats.txt', {encoding: 'utf-8'});
} else { } else {
out = spawn(DOCKER, ['stats', '-a', '--no-stream']).stdout.toString(); out = spawn(DOCKER, ['stats', '-a', '--no-stream', '--format', 'table {{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}']).stdout.toString();
} }
var lines = out.split('\n'); var lines = out.split('\n');
for (var i=0; i<lines.length; i++) { for (var i=0; i<lines.length; i++) {
@ -62,25 +41,30 @@ var addStatsToContainerList = function(containers) {
if (columns.length >= 6) { if (columns.length >= 6) {
var containerId = columns[0]; var containerId = columns[0];
if (containerId) { if (containerId) {
var cpu = columns[1]; var name = columns[1];
var mem = columns[2]; var cpu = columns[2];
var mem = columns[3];
var net = columns[4]; var net = columns[4];
var block = columns[5]; var block = columns[5];
containers[containerId].cpu = cpu.replace('%', ''); containers[containerId] = {
containers[containerId].mem = getBytes(mem.split(' / ')[0]); 'name': name,
containers[containerId].net = { 'cpu': cpu.replace('%', ''),
'mem': getBytes(mem.split(' / ')[0]),
'net': {
'in': getBytes(net.split(' / ')[0]), 'in': getBytes(net.split(' / ')[0]),
'out': getBytes(net.split(' / ')[1]) 'out': getBytes(net.split(' / ')[1])
}; },
containers[containerId].block = { 'block': {
'in': getBytes(block.split(' / ')[0]), 'in': getBytes(block.split(' / ')[0]),
'out': getBytes(block.split(' / ')[1]) 'out': getBytes(block.split(' / ')[1])
}
}; };
} }
} }
} }
} }
return containers;
}; };
var getCreateContainerId = function(name, cid, cb) { var getCreateContainerId = function(name, cid, cb) {
@ -115,8 +99,8 @@ var writeStats = function(containers) {
var main = function() { var main = function() {
var containers = getContainers(); var containers = getContainers();
addStatsToContainerList(containers);
writeStats(containers); writeStats(containers);
setTimeout(main, INTERVAL*1000);
}; };
db.run("PRAGMA journal_mode=WAL"); db.run("PRAGMA journal_mode=WAL");
@ -135,4 +119,4 @@ db.run("CREATE TABLE IF NOT EXISTS stats ( " +
"block_in REAL NOT NULL, " + "block_in REAL NOT NULL, " +
"block_out REAL NOT NULL)"); "block_out REAL NOT NULL)");
setInterval(main, INTERVAL*1000); main();