forked from pelger/docker-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.js
More file actions
executable file
·130 lines (103 loc) · 3.2 KB
/
stats.js
File metadata and controls
executable file
·130 lines (103 loc) · 3.2 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
#! /usr/bin/env node
'use strict';
var nes = require('never-ending-stream');
var through = require('through2');
var split = require('split2');
var pump = require('pump');
var allContainers = require('docker-allcontainers');
function stats(opts) {
opts = opts || {};
var result = through.obj();
var events = opts.events || allContainers(opts);
var streams = {};
var oldDestroy = result.destroy;
var interval = opts.statsinterval || 1;
result.setMaxListeners(0);
result.destroy = function() {
Object.keys(streams).forEach(detachContainer);
events.destroy();
oldDestroy.call(this);
};
events.on('start', attachContainer);
events.on('stop', function(meta) {
detachContainer(meta.id);
});
return result;
function detachContainer(id) {
if (streams[id]) {
streams[id].destroy();
delete streams[id];
}
}
function attachContainer(data, container) {
// we are trying to tap into this container
// we should not do that, or we might be stuck in
// an output loop
if (data.id.indexOf(process.env.HOSTNAME) === 0) {
return;
}
var stream = nes(container.stats.bind(container));
streams[data.Id] = stream;
var previousSystem = 0;
var previousCpu = 0;
var sampleCount = 0;
var cpuSum = 0;
var sysSum = 0;
pump(
stream,
split(JSON.parse),
through.obj(function(stats, enc, cb) {
sampleCount++
cpuSum += stats.cpu_stats.cpu_usage.total_usage
sysSum += stats.cpu_stats.system_cpu_usage
if (sampleCount >= interval) {
stats.cpu_stats.cpu_usage.total_usage = cpuSum/sampleCount;
stats.cpu_stats.system_cpu_usage = sysSum/sampleCount;
var percent = calculateCPUPercent(stats, previousCpu, previousSystem)
stats.cpu_stats.cpu_usage.cpu_percent = percent
this.push({
v: 0,
id: data.id.slice(0, 12),
image: data.image,
name: data.name,
stats: stats
})
previousCpu = cpuSum
previousSystem = sysSum
sampleCount = 0
cpuSum = 0
sysSum = 0
}
cb()
})
).pipe(result, { end: false });
}
// Code taken from https://github.com/icecrime/docker-mon/blob/ee9ac3fbaffcdec60d26eedd16204ca0370041d8/widgets/cpu.js
function calculateCPUPercent(statItem, previousCpu, previousSystem) {
var cpuDelta = statItem.cpu_stats.cpu_usage.total_usage - previousCpu
var systemDelta = statItem.cpu_stats.system_cpu_usage - previousSystem
var cpuPercent = 0.0
if (systemDelta > 0.0 && cpuDelta > 0.0) {
cpuPercent = (cpuDelta / systemDelta) * statItem.cpu_stats.cpu_usage.percpu_usage.length * 100.0
}
return cpuPercent
}
}
module.exports = stats
function cli() {
var argv = require('minimist')(process.argv.slice(2))
stats({
statsinterval: argv.statsinterval,
matchByName: argv.matchByName,
matchByImage: argv.matchByImage,
skipByName: argv.skipByName,
skipByImage: argv.skipByImage
}).pipe(through.obj(function(chunk, enc, cb) {
this.push(JSON.stringify(chunk))
this.push('\n')
cb()
})).pipe(process.stdout)
}
if (require.main === module) {
cli()
}