-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
executable file
·125 lines (101 loc) · 3.14 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
#!/usr/bin/env node
var ms = require('ms')
, moment = require('moment')
, async = require('async')
, fs = require('fs')
, now = +moment()
, argv = process.argv.slice(2);
const {
S3
} = require("@aws-sdk/client-s3");
const { fromEnv } = require("@aws-sdk/credential-providers");
require('moment-range');
if (argv.length < 4) {
console.log('usage: [accountId] [bucket] [lbName (--all for all)] [from ms|Date(ex: YYYY-MM-DDTHH:MM)] [to ms|Date(ex: YYYY-MM-DDTHH:MM) -optional]');
process.exit();
}
var accountId = argv[0]
, bucket = argv[1]
, lbName = argv[2]
, from = now - ( ms(argv[3]) || moment().diff(argv[3]) )
, to = now - (argv[4] ? ms(argv[4]) ? ms(argv[4]) : moment().diff(argv[4]) : 0);
var fromDate = moment(from)
, toDate = moment(to);
if (!( fromDate.isValid() && toDate.isValid && (toDate > fromDate) )) {
console.log('given dates are not valid');
process.exit();
}
(function rmDir (dirPath) {
try {
var files = fs.readdirSync(dirPath);
}
catch(e) {
fs.mkdirSync(dirPath);
return;
}
if (files.length > 0)
for(var i = 0; i < files.length; i++) {
var filePath = dirPath + '/' + files[i];
if (fs.statSync(filePath).isFile())
fs.unlinkSync(filePath);
else
rmDir(filePath);
}
})('./logs');
var range = moment.range(fromDate, toDate);
var days = []
, keys = [];
range.by('days', function (m) {
days.push(m);
});
const region = process.env.AWS_DEFAULT_REGION;
const s3 = new S3({region, credentials: fromEnv()});
async.each(days, function (i, next) {
var attr = {
Bucket: bucket,
Prefix: ['AWSLogs', accountId, 'elasticloadbalancing', region, i.format('YYYY/MM/DD/')].join('/')
};
(function list (next, attr) {
s3.listObjects(attr,
function (err, data) {
if (err || !data) return setImmediate(next, err || new Error('No data'));
var contents = data.Contents || []
, lastItem = contents.length - 1;
for(var i = 0; i < contents.length; i++) {
keys.push(contents[i]);
if (i == lastItem) {
attr.Marker = contents[i].Key;
return list(next, attr)
}
attr.Marker = null;
}
next()
}
)
})(next, attr)
},
function () {
async.each(keys, function (k, next) {
var key = k.Key;
if (lbName != '--all') {
var nameMatch = key.match(new RegExp('_' + lbName + '_', 'g'));
if (!nameMatch) return next();
}
var match = key.match(/_([0-9T]+)/);
if (!match[1]) return next();
var date = moment.utc(match[1], 'YYYYMMDDHHmm');
if (!date.isValid() || !range.contains(date)) return next();
s3.getObject({
Bucket: bucket,
Key: key
}, function (err, data) {
if (err) return setImmediate(next || new Error("No data"));
var filename = key.replace(/\//g, '_');
const body = await data.Body.transformToByteArray();
fs.writeFile('./logs/' + filename, new Buffer.from(body), function (err, data) {
if (err) return setImmediate(next,err);
next()
});
})
})
});