-
Notifications
You must be signed in to change notification settings - Fork 59
/
index.js
executable file
·223 lines (186 loc) · 5.28 KB
/
index.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#! /usr/bin/env node
'use strict';
var tls = require('tls');
var net = require('net');
var eos = require('end-of-stream');
var through = require('through2');
var minimist = require('minimist');
var allContainers = require('docker-allcontainers');
var statsFactory = require('docker-stats');
var logFactory = require('docker-loghose');
var eventsFactory = require('docker-event-log');
var os = require('os');
function connect(opts) {
var stream;
if (opts.secure) {
stream = tls.connect(opts.port, opts.server, onSecure);
} else {
stream = net.createConnection(opts.port, opts.server);
}
function onSecure() {
// let's just crash if we are not secure
if (!stream.authorized) throw new Error('secure connection not authorized');
}
return stream;
}
function start(opts) {
var logsToken = opts.logstoken || opts.token;
var statsToken = opts.statstoken || opts.token;
var eventsToken = opts.eventstoken || opts.token;
var out;
var noRestart = function() {};
var filter = through.obj(function(obj, enc, cb) {
addAll(opts.add, obj);
var token = '';
if (obj.line) {
token = logsToken;
}
else if (obj.type) {
token = eventsToken;
}
else if (obj.stats) {
token = statsToken;
}
if (token) {
this.push(token);
this.push(' ');
this.push(JSON.stringify(obj));
this.push('\n');
}
cb()
});
var events = allContainers(opts);
var loghose;
var stats;
var dockerEvents;
var streamsOpened = 0;
opts.events = events;
if (opts.logs !== false && logsToken) {
loghose = logFactory(opts);
loghose.pipe(filter);
streamsOpened++;
}
if (opts.stats !== false && statsToken) {
stats = statsFactory(opts);
stats.pipe(filter);
streamsOpened++;
}
if (opts.dockerEvents !== false && eventsToken) {
dockerEvents = eventsFactory(opts);
dockerEvents.pipe(filter);
streamsOpened++;
}
if (!stats && !loghose && !dockerEvents) {
throw new Error('you should enable at least one of stats, logs or dockerEvents');
}
pipe();
// destroy out if all streams are destroyed
loghose && eos(loghose, function() {
streamsOpened--;
streamClosed(streamsOpened);
});
stats && eos(stats, function() {
streamsOpened--;
streamClosed(streamsOpened);
});
dockerEvents && eos(dockerEvents, function() {
streamsOpened--;
streamClosed(streamsOpened);
});
return loghose;
function addAll(proto, obj) {
if (!proto) { return; }
var key;
for (key in proto) {
if (proto.hasOwnProperty(key)) {
obj[key] = proto[key];
}
}
}
function pipe() {
if (out) {
filter.unpipe(out);
}
out = connect(opts);
filter.pipe(out, { end: false });
// automatically reconnect on socket failure
noRestart = eos(out, pipe);
}
function streamClosed(streamsOpened) {
if (streamsOpened <= 0) {
noRestart()
out.destroy();
}
}
}
var unbound;
function cli() {
var argv = minimist(process.argv.slice(2), {
boolean: ['json', 'secure', 'stats', 'logs', 'dockerEvents'],
string: ['token', 'logstoken', 'statstoken', 'eventstoken', 'server', 'port'],
alias: {
'token': 't',
'logstoken': 'l',
'newline': 'n',
'statstoken': 'k',
'eventstoken': 'e',
'secure': 's',
'json': 'j',
'statsinterval': 'i',
'add': 'a'
},
default: {
json: false,
newline: true,
stats: true,
logs: true,
dockerEvents: true,
statsinterval: 30,
add: [ 'host=' + os.hostname() ],
token: process.env.LOGENTRIES_TOKEN,
logstoken: process.env.LOGENTRIES_LOGSTOKEN || process.env.LOGENTRIES_TOKEN,
statstoken: process.env.LOGENTRIES_STATSTOKEN || process.env.LOGENTRIES_TOKEN,
eventstoken: process.env.LOGENTRIES_EVENTSTOKEN || process.env.LOGENTRIES_TOKEN,
server: 'data.logentries.com',
port: unbound
}
});
if (argv.help || !(argv.token || argv.logstoken || argv.statstoken || argv.eventstoken)) {
console.log('Usage: docker-logentries [-l LOGSTOKEN] [-k STATSTOKEN] [-e EVENTSTOKEN]\n' +
' [-t TOKEN] [--secure] [--json]\n' +
' [--no-newline] [--no-stats] [--no-logs] [--no-dockerEvents]\n' +
' [-i STATSINTERVAL] [-a KEY=VALUE]\n' +
' [--matchByImage REGEXP] [--matchByName REGEXP]\n' +
' [--skipByImage REGEXP] [--skipByName REGEXP]\n' +
' [--server HOSTNAME] [--port PORT]\n' +
' [--help]');
process.exit(1);
}
if (argv.port == unbound) {
if (argv.secure) {
argv.port = 443;
} else {
argv.port = 80;
}
} else {
argv.port = parseInt(argv.port);
// TODO: support service names
if (isNaN(argv.port)) {
console.log('port must be a number');
process.exit(1);
}
}
if (argv.add && !Array.isArray(argv.add)) {
argv.add = [argv.add];
}
argv.add = argv.add.reduce(function(acc, arg) {
arg = arg.split('=');
acc[arg[0]] = arg[1];
return acc
}, {});
start(argv);
}
module.exports = start;
if (require.main === module) {
cli();
}