-
Notifications
You must be signed in to change notification settings - Fork 0
/
logGroups.js
68 lines (60 loc) · 1.93 KB
/
logGroups.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
'use strict'
const aws=require('aws-sdk'),
moment=require('moment');
const logs = new aws.CloudWatchLogs();
const cloudwatch = new aws.CloudWatch();
const pageSize=10;
const getPaginatedResults = async (fn) => {
const EMPTY = Symbol("empty");
const res = [];
for await (const lf of (async function*() {
let NextMarker = EMPTY;
while (NextMarker || NextMarker === EMPTY) {
const {marker, results} = await fn(NextMarker !== EMPTY ? NextMarker : undefined);
yield* results;
NextMarker = marker;
}
})()) {
res.push(lf);
}
return res;
};
async function getLogGroupInfo() {
return await getPaginatedResults(async (NextMarker) => {
const logGroups = await logs.describeLogGroups({nextToken: NextMarker}).promise();
return {
marker: logGroups.nextToken,
results: logGroups.logGroups,
};
});
}
async function getLogGroupStats() {
let now = moment();
return await getPaginatedResults(async (NextMarker) => {
const logGroups = await logs.describeLogGroups({limit: pageSize, nextToken: NextMarker}).promise();
const metrics = await Promise.all(logGroups.logGroups.map(async lg=>{
let m = await cloudwatch.getMetricStatistics({
Namespace: 'AWS/Logs',
MetricName:'IncomingBytes',
Dimensions: [{Name: 'LogGroupName', Value: lg.logGroupName}],
StartTime: moment().subtract(7, 'day').toISOString(),
EndTime: now.toISOString(),
Period: 3600*24*7,
Statistics: ['Sum'],
Unit: 'Bytes'
}).promise();
return {
logGroupName: lg,
IncomingBytes: m.Datapoints
}
}));
return {
marker: logGroups.nextToken,
results: metrics
};
})
}
// gets basic info on log groups
//getlogGroupInfo().then(console.log);
// combine log group info with incoming byte datapoints in single structure
//getLogGroupStats().then(r=>console.log(JSON.stringify(r, null, 2)));