forked from logdna/logdna-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
434 lines (368 loc) · 19 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env node
/* globals process */
const program = require('commander');
const properties = require('properties');
const qs = require('querystring');
const pkg = require('./package.json');
const WebSocket = require('./lib/logdna-websocket');
const input = require('./lib/input');
const utils = require('./lib/utils');
const EMAIL_REGEX = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
var config = require('./lib/config');
process.title = 'logdna';
program._name = 'logdna';
program
.version(pkg.version, '-v, --version')
.usage('[commands] [options]\n\nThe LogDNA CLI allows you to sign up for a new account and tail your logs right from the command line.')
.on('--help', function() {
utils.log(' Examples:');
utils.log();
utils.log(' $ logdna register [email protected]');
utils.log(' $ logdna register [email protected] b7c0487cfa5fa7327c9a166c6418598d # use this if you were assigned an Ingestion Key');
utils.log(' $ logdna tail \'("timed out" OR "connection refused") -request\'');
utils.log(' $ logdna tail -a access.log 500');
utils.log(' $ logdna tail -l error,warn');
utils.log(' $ logdna search "logdna cli" -a logdna.log -t tag1,tag2 -n 300');
utils.log(' $ logdna search "logdna" --from 1541100040931 --to 1541102940000');
utils.log(' $ logdna login [email protected]');
utils.log(' $ logdna info');
utils.log(' $ logdna update');
utils.log();
});
properties.parse(config.DEFAULT_CONF_FILE, { path: true }, (err, parsedConfig) => {
if (err && err.code !== 'ENOENT') {
return utils.log('Unable to read the configuration file: ' + err.code);
}
config = Object.assign(config, parsedConfig || {});
utils.performUpgrade(config, error => {
if (error) utils.log(error);
});
program.command('register [email]')
.description('Register a new LogDNA account')
.action(function(email) {
var nextstep = function(email) {
email = email.toLowerCase();
if (!EMAIL_REGEX.test(email)) return utils.log('Invalid email address');
input.required('First name: ', function(firstname) {
input.required('Last name: ', function(lastname) {
input.required('Company/Organization: ', function(company) {
input.done();
utils.apiPost(config, 'register', {
auth: false
, email: email
, firstname: firstname
, lastname: lastname
, company: company
}, function(error, body) {
if (error) return utils.log(error);
config.email = email;
if (config.account !== body.account) {
config.account = body.account;
config.token = null;
config.servicekey = null;
}
config.key = body.key;
if (body.token) config.token = body.token; // save token if available
if (body.servicekeys && body.servicekeys.length) config.servicekey = body.servicekeys[0];
utils.saveConfig(config, function(error, success) {
if (error) return utils.log(error);
utils.log();
utils.log('Thank you for signing up! Saving credentials to local config...');
utils.log('Your Ingestion Key is: ' + body.key + '. ');
utils.log();
utils.log('Next steps:');
utils.log('===========');
utils.log('1. We\'ve sent you a welcome email to create your password. Once set, come back here and use \'logdna login\'');
utils.log('2. Visit https://docs.logdna.com/docs/ingestion-methods for more info on collecting your logs via our Agent, Syslog, Heroku, API, or code libraries.');
return utils.log();
});
});
});
});
});
};
if (email) {
nextstep(email);
} else {
input.required('Email: ', function(email) {
nextstep(email);
});
}
});
program.command('ssologin')
.description('Log in to a LogDNA via single sign-on')
.action(function() {
var token = Math.floor((Math.random() * 4000000000) + 800000000).toString(16);
var pollTimeout;
// Overide SSO URL if using a custom environment
var sso_url = config.SSO_URL;
if (config.LOGDNA_APPHOST) sso_url = config.LOGDNA_APPHOST + config.SSO_LONG_PATH;
utils.log('To sign in via SSO, use a web browser to open the page ' + sso_url + token);
var pollToken = function() {
utils.apiPost(config, 'sso', {
auth: false
, token: token
}, function(error, body) {
if (error) return utils.log(error);
if (!body || !body.email) return;
clearTimeout(pollTimeout);
config.email = body.email;
if (body.accounts.length && config.account !== body.accounts[0]) {
config.account = body.accounts[0];
config.key = null;
}
if (body.keys && body.keys.length) config.key = body.keys[0];
config.token = body.token;
if (body.servicekeys && body.servicekeys.length) config.servicekey = body.servicekeys[0];
utils.saveConfig(config, function(error, success) {
if (error) return utils.log(error);
utils.log('Logged in successfully as: ' + body.email + '. Saving credentials to local config.');
});
});
pollTimeout = setTimeout(pollToken, config.SSO_POLL_INTERVAL);
};
pollToken(); // kick off polling
});
program.command('login [email]')
.description('Log in to LogDNA')
.action(function(email) {
var nextstep = function(email) {
input.hidden('Password: ', function(password) {
input.done();
email = email.toLowerCase();
if (!EMAIL_REGEX.test(email)) return utils.log('Invalid email address');
utils.apiPost(config, 'login', {
auth: email + ':' + password
}, function(error, body) {
if (error) return utils.log(error);
config.email = email;
if (body && body.accounts.length && config.account !== body.accounts[0]) {
config.account = body.accounts[0];
config.key = null;
}
if (body && body.keys && body.keys.length) config.key = body.keys[0];
config.token = body.token;
if (body && body.servicekeys && body.servicekeys.length) config.servicekey = body.servicekeys[0];
utils.saveConfig(config, function(error, success) {
if (error) return utils.log(error);
utils.log('Logged in successfully as: ' + email + '. Saving credentials to local config.');
});
});
});
};
if (email) {
nextstep(email);
} else {
input.required('Email: ', function(email) {
nextstep(email);
});
}
});
program.command('tail [query]')
.description('Live tail with optional filtering. Options include -h, -a, -l, -t to filter by hosts, apps, levels or tags respectively. Run logdna tail --help to learn more.')
.option('-h, --hosts <hosts>', 'Filter on hosts (separate by comma)')
.option('-a, --apps <apps>', 'Filter on apps (separate by comma)')
.option('-l, --levels <levels>', 'Filter on levels (separate by comma)')
.option('-t, --tags <tags>', 'Filter on tags (separate by comma)')
.option('-j, --json', 'Output raw JSON', false)
.action(function(query, options) {
var params = utils.authParams(config);
params.q = query || '';
if (options.hosts) params.hosts = options.hosts.replace(/, /g, ',');
if (options.apps) params.apps = options.apps.replace(/, /g, ',');
if (options.levels) params.levels = options.levels.replace(/, /g, ',');
if (options.tags) params.tags = options.tags.replace(/, /g, ',');
if (options.json) params.json = true;
params.q = params.q.trim();
var ws = new WebSocket((config.LOGDNA_APISSL ? 'https://' : 'http://') + config.LOGDNA_TAILHOST + '/ws/tail?' + qs.stringify(params));
ws.on('open', function open() {
utils.log('tail started. hosts: ' + (options.hosts || 'all') +
'. apps: ' + (options.apps || 'all') +
'. tags: ' + (options.tags || 'all') +
'. levels: ' + (options.levels || 'all') +
'. query: ' + (query || 'none'));
});
ws.on('reconnecting', function(num) {
utils.log('tail reconnect attmpt #' + num + '...');
});
ws.on('message', function(data) {
try {
data = JSON.parse(data);
} catch (err) {
return utils.log('Malformed line: ' + err);
}
const account = data.e;
const payload = data.p;
if (account === 'meta') return; // Ignore meta messages
const lines = [].concat(payload);
lines.forEach((line) => {
utils.renderLine(config, line, params);
});
});
ws.on('error', function(err) {
err = err.toString();
if (err.indexOf('401') > -1) {
// invalid token
utils.log('Access token invalid. If you created or changed your password recently, please \'logdna login\' or \'logdna ssologin\' again. Type \'logdna --help\' for more info.');
return process.exit();
}
utils.log('Error: ' + err);
});
ws.on('close', function() {
utils.log('tail lost connection');
});
});
program.command('switch')
.description('If your login has access to more than one account, this command allows you to switch between them')
.action(function(options) {
utils.apiGet(config, 'orgs', {}, function(error, response) {
if (error) {
return utils.log(error);
}
if (!response || response.length <= 1) {
return utils.log(`Your login ${config.email} doesn't belong to other accounts. Ensure the other owner has added your email.`);
}
response.forEach((org, i) => {
const option = i + 1;
const isActive = org.id === config.account;
utils.log(`${option}: ${org.name} ${isActive ? '(active)' : ''}`);
});
input.required('Choose account [1-' + response.length + ']: ', function(selection) {
input.done();
selection = parseInt(selection);
selection = selection - 1;
if (isNaN(selection) || selection >= response.length || selection < 0) {
return utils.log('Not a valid number.');
}
config.account = response[selection].id;
config.servicekey = response[selection].servicekeys[0];
utils.saveConfig(config, function(error, success) {
if (error) return utils.log(error);
utils.log('Successfully switched account to ' + response[selection].name);
});
});
});
});
program.command('search [query]')
.description('Basic search with optional filtering. Run logdna search --help for options.')
.option('-h, --hosts <hosts>', 'Filter on hosts (separate by comma)')
.option('-a, --apps <apps>', 'Filter on apps (separate by comma)')
.option('-l, --levels <levels>', 'Filter on levels (separate by comma)')
.option('-n, --number <number>', 'Set how many lines to request')
.option('-t, --tags <tags>', 'Filter on tags (separate by comma)')
.option('--prefer-head', 'Get lines from the beginning of the interval rather than the end')
.option('--next', 'Get next chunk of lines (after last search). This is a convenience wrapper around the --from and --to parameters.')
.option('--from <from>', 'Unix timestamp of beginning of search timeframe.')
.option('--to <to>', 'Unix timestamp of end of search timeframe.')
.option('-j, --json', 'Output raw JSON', false)
.action(function(query, options) {
var params = {
q: query || ''
};
if (options.preferHead) params.prefer = 'head';
if (options.from) {
try {
params.from = new Date(parseInt(options.from)).getTime();
} catch (err) {
}
}
if (options.to) {
try {
params.to = new Date(parseInt(options.to)).getTime();
} catch (err) {
}
}
if (options.next) {
// use last search timestamps to get next block of results
if (config.last_timestamp) {
if (options.preferHead) {
params.from = new Date(config.last_timestamp).getTime();
} else params.to = new Date(config.last_timestamp).getTime();
}
}
if (options.number) {
try {
params.size = parseInt(options.number);
} catch (err) {
}
}
if (options.json) params.json = true;
if (options.hosts) params.hosts = options.hosts.replace(/, /g, ',');
if (options.apps) params.apps = options.apps.replace(/, /g, ',');
if (options.levels) params.levels = options.levels.replace(/, /g, ',');
if (options.tags) params.tags = options.tags.replace(/, /g, ',');
if (config.servicekey) params.servicekey = config.servicekey;
var modifiedconfig = JSON.parse(JSON.stringify(config));
// this prevents export API from emailing the results
delete modifiedconfig.email;
var t, t2, range;
utils.apiGet(modifiedconfig, 'v1/export', params, function(error, body) {
if (error) return utils.log(error);
if (body && body.range && body.range.from && body.range.to) {
t = new Date(body.range.from);
t2 = new Date(body.range.to);
range = ' between ' + t.toString().substring(4, 11) + t.toString().substring(16, 24) +
'-' + t2.toString().substring(4, 11) + t2.toString().substring(16, 24);
}
if (typeof body === 'string') {
body = body.split('\n');
body = body.map((x) => {
try {
return JSON.parse(x);
} catch (err) {
return 0;
}
}).filter(element => element);
}
utils.log('search finished: ' + (body ? body.length : 0) + ' line(s)' + (range || '') +
'. hosts: ' + (options.hosts || 'all') +
'. apps: ' + (options.apps || 'all') +
'. levels: ' + (options.levels || 'all') +
'. tags: ' + (options.tags || 'all') +
'. query: ' + (query || 'none'));
const lines = [].concat(body);
if (!lines.length) {
return utils.log('Query returned no lines.');
}
let last_timestamp = new Date(lines[0]._ts);
lines.forEach((line) => {
t = new Date(line._ts);
if (options.preferHead && last_timestamp < t) {
last_timestamp = t;
} else if (last_timestamp > t) {
last_timestamp = t;
}
utils.renderLine(config, line, params);
});
config.last_timestamp = last_timestamp.toJSON();
utils.saveConfig(config, function(error, success) {
if (error) return utils.log(error);
});
});
});
program.command('info')
.alias('whoami')
.description('Show current logged in user info')
.action(function() {
utils.apiGet(config, 'info', function(error, body) {
if (error) return utils.log(error);
utils.log(body);
});
});
program.command('update')
.description('Update the CLI to the latest version')
.action(function() {
utils.performUpgrade(config, true, function() {
utils.log('No update available. You have the latest version: ' + pkg.version);
});
});
// helper for tail/search -h due to -h conflict for --hosts
if (process.argv && process.argv.length === 4 && ['tail', 'search'].indexOf(process.argv[2]) >= 0 && process.argv[3] === '-h') process.argv[3] = '--help';
program.parse(process.argv);
if (!process.argv.slice(2).length) return program.outputHelp(); // show help if no commands given
});
process.on('uncaughtException', function(err) {
utils.log('------------------------------------------------------------------');
utils.log('Uncaught Error: ' + (err.stack || '').split('\r\n'));
utils.log('------------------------------------------------------------------');
});