forked from kennu/cwtail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·233 lines (227 loc) · 7.06 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
#!/usr/bin/env node
/**
* CloudWatch Logs Tail
* Copyright (C) Kenneth Falck 2015-2016
* License: MIT
*
* Installation:
* $ npm install -g cwtail
*
* Usage (for help):
* $ cwtail -h
*/
var fs = require('fs');
var path = require('path');
var ini = require('ini');
var Promise = require('bluebird');
var Getopt = require('node-getopt');
var os = require('os');
var DEFAULT_NUM_RECORDS = 30; // Default number of records to tail
var FOLLOW_INTERVAL = 5000; // How often to read more
/**
* List available log groups
*/
function list(logs, nextToken) {
return logs.describeLogGroupsAsync({
nextToken: nextToken
})
.then(function (result) {
if (result && result.logGroups) {
result.logGroups.map(function (group) {
console.log(group.logGroupName);
})
}
if (result && result.nextToken) {
// Load next set of results
return list(logs, result.nextToken);
}
});
}
function getStreamEvents(logs, logGroup, logStream) {
return logs.getLogEventsAsync({
logGroupName: logGroup,
logStreamName: logStream
})
.then(function (result) {
return (result && result.events) || [];
});
}
/**
* Tail specified log group
*/
function tail(logs, logGroup, numRecords, showTimes, showStreams, seenStreamTimestamps, eol) {
return logs.describeLogStreamsAsync({
logGroupName: logGroup,
descending: true,
limit: 10,
orderBy: 'LastEventTime'
})
.then(function (result) {
if (result && result.logStreams) {
var latestStreams = [];
result.logStreams.map(function (logStream) {
if (logStream.lastEventTimestamp) {
latestStreams.push(logStream.logStreamName);
}
});
}
return latestStreams;
})
.then(function (latestStreams) {
if (!latestStreams || !latestStreams.length) {
// No streams in group
return Promise.resolve();
}
// The streams are in descending time order; show until N records have been shown
var promise = Promise.resolve();
var numRead = 0;
var allRecords = [];
function readMore() {
if (!latestStreams.length) {
// No more streams left
return allRecords;
}
var logStream = latestStreams.shift();
return getStreamEvents(logs, logGroup, logStream)
.then(function (records) {
records.map(function (record) {
record.logStream = logStream;
});
allRecords = allRecords.concat(records);
//console.log('', records.length, 'record(s)');
numRead += records.length;
if (numRead < numRecords) {
// Keep reading more
return readMore();
} else {
return allRecords;
}
});
}
return readMore();
})
.then(function (records) {
if (!records) return;
var prevStream;
var newTimestamps = {};
records.map(function (record) {
// Have we already seen this record?
var seenTimestamp = seenStreamTimestamps[record.logStream];
if (seenTimestamp && record.timestamp <= seenTimestamp) {
// Yes, skip it
return;
}
if (!newTimestamps[record.logStream] || record.timestamp > newTimestamps[record.logStream]) {
newTimestamps[record.logStream] = record.timestamp;
}
if (showStreams) {
if (record.logStream != prevStream) {
prevStream = record.logStream;
console.log('------------------------------------------------------------------------------');
console.log(record.logStream);
console.log('------------------------------------------------------------------------------');
}
}
if (showTimes) {
process.stdout.write('[' + new Date(record.timestamp) + '] ')
}
process.stdout.write(record.message);
if (eol) {
process.stdout.write(os.EOL);
}
});
Object.keys(newTimestamps).map(function (key) {
if (!seenStreamTimestamps[key] || newTimestamps[key] > seenStreamTimestamps[key]) {
seenStreamTimestamps[key] = newTimestamps[key];
}
});
});
}
function main(argv) {
return Promise.resolve()
.then(function () {
var opt = new Getopt([
['f', 'follow', 'Follow the log (default is to exit)'],
['n', 'num=ARG', 'Number of log records to show'],
['s', 'streams', 'Show log stream names'],
['t', 'time', 'Show timestamps in log records'],
['e', 'eol', 'Append platform end-of-line to log records'],
['l', 'list', 'List available log groups'],
['p', 'profile=ARG', 'Select AWS profile'],
['h', 'help', 'Show this help'],
['v', 'version', 'Show cwtail version']
]);
opt.setHelp("CloudWatch Logs Tail (C) Kenneth Falck <kennu@iki.fi> 2015-2016\n\nUsage: cwtail [options] <log group>\n\n[[OPTIONS]]\n");
var arg = opt.bindHelp().parse(argv);
if (arg.options.version) {
console.log('cwtail ' + JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'))).version);
return Promise.reject(0);
}
if (arg.options.profile) {
process.env.AWS_PROFILE = arg.options.profile;
}
var AWS = require('aws-sdk');
if (process.env.AWS_REGION) {
AWS.config.update({ region: process.env.AWS_REGION });
} else {
try {
var iniFile = fs.readFileSync(path.join(process.env.HOME || os.homedir(), '.aws', 'config'), 'utf8');
var iniData = ini.decode(iniFile);
var section = iniData[process.env.AWS_PROFILE ? 'profile ' + process.env.AWS_PROFILE : 'default'];
if (section.region) {
// Use region from config
AWS.config.update({region: section.region});
}
} catch (err) {
// Ini file not found, ignore
console.error(err);
}
}
if (process.env.https_proxy) {
var proxy = require('proxy-agent');
AWS.config.update({
httpOptions: { agent: proxy(process.env.https_proxy) }
})
}
var logs = new AWS.CloudWatchLogs();
Promise.promisifyAll(logs);
if (!arg.options.list && !arg.argv.length) {
// Need log group name
opt.showHelp();
return Promise.reject(1);
}
if (arg.options.list) {
return list(logs);
} else if (arg.options.follow) {
var seenStreamTimestamps = {};
function readNext() {
return tail(logs, arg.argv[0], opt.num || DEFAULT_NUM_RECORDS, arg.options.time, arg.options.streams, seenStreamTimestamps, arg.options.eol)
.then(function () {
return new Promise(function (resolve, reject) { setTimeout(resolve, FOLLOW_INTERVAL)});
})
.then(function () {
return readNext();
})
}
return readNext();
} else {
var seenStreamTimestamps = {};
return tail(logs, arg.argv[0], opt.num || DEFAULT_NUM_RECORDS, arg.options.time, arg.options.streams, seenStreamTimestamps, arg.options.eol);
}
})
.then(function () {
// Successful exit
process.exit(0);
})
.then(null, function (err) {
if (err == 0) {
process.exit(0);
} else if (err == 1) {
process.exit(1);
} else {
console.error(err.stack || err);
process.exit(1);
}
});
}
main(process.argv.slice(2));