forked from nathandunn/apollo-log-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logListener.js
141 lines (120 loc) · 4.32 KB
/
logListener.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
var AWS = require('aws-sdk');
var us = require('underscore');
var dns = require('dns');
///
/// Helpers.
///
// Aliases.
var each = us.each;
function _ll(arg1) {
console.log(arg1);
}
// Two or one args.
function _die(m1, m2) {
if (typeof(m2) !== 'undefined') {
console.error('ERROR:', m1, m2);
} else {
console.error('ERROR:', m1);
}
process.exit(-1);
}
var positive_re = /REST\.GET\.OBJECT ping\.json/;
var negative_re1 = /aws\-internal/;
var negative_re2 = /aws\-sdk-nodejs/;
function _of_interest_p(str) {
var ret = false;
//_ll('TEST: ' + test);
if (positive_re.test(str) && !negative_re1.test(str) && !negative_re2.test(str)) {
ret = true;
}
return ret;
}
///
/// Opts.
///
var argv = require('minimist')(process.argv.slice(2));
// AWS credentials from CLI.
var credential_file = argv['f'] || argv['file'];
if (!credential_file) {
_die('Option (f|file) is required.');
} else {
//_ll('Will use credentials: ' + credential_file);
}
AWS.config.loadFromPath(credential_file);
// Bucket/prefix to use.
var bucket = null;
var prefix = null;
var bucket_raw = argv['b'] || argv['bucket'];
if (!bucket_raw) {
_die('Option (b|bucket) is required.');
} else {
// var parts = bucket_raw.split('/');
// if( ! parts || parts.length !== 2 ){
// _die('Not a good bucket descriptor: ' + bucket_raw);
// }else{
// bucket = parts[0];
bucket = bucket_raw;
// prefix = parts[1];
// }
//_ll('Will use bucket: ' + bucket + ', with prefix: ' + prefix);
}
///
/// Main.
///
var ipv4_re = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])){3}';
var date_re = /\[(.*)\]/;
var options_re = /\?(.*)\ HTTP/;
// Get the bucket listings.
var s3 = new AWS.S3({params: {Bucket: bucket, Prefix: prefix}});
s3.listObjects(function (err, data) {
if (err) {
_die(err, err.stack);
} else {
// Extract the keys from the listing.
if (data.Contents) {
each(data.Contents, function (obj) {
var key = obj.Key;
// Get the objects.
s3.getObject({Key: key}, function (err, data) {
if (err) {
_die(err, err.stack);
} else {
// Break the body into separate lines
var body = data.Body.toString('utf8');
var lines = body.split("\n");
us.each(lines, function (line) {
// Filter out anything we don't need.
if (_of_interest_p(line)) {
// console.log(line);
// "GET /apollo-usage-2.0.5-snapshot/ping.json?server=ApolloSever-17846407921100166222&environment=test HTTP/1.1"
// 4e71fbafdde83a0bf32b0b4905cc1d61b49831eeac7e44ba2df92a80196d6ee1 apollo-usage-2.0.5-snapshot [18/Nov/2016:00:39:53 +0000] 130.211.219.203 - BF5D1356AC643431 REST.GET.OBJECT ping.j
// son "GET /apollo-usage-2.0.5-snapshot/ping.json?server=ApolloSever-17846407921100166222&environment=test HTTP/1.1" 200 - 3 3 17 16 "-" "Java/1.8.0_111" -
// Pull out IP addresses.
var ip_matches = line.match(ipv4_re);
var date_matches = line.match(date_re);
var option_matches = line.match(options_re);
//console.log(ip_matches);
//console.log(date_matches);
var ip = ip_matches[0];
var date = date_matches[1];
var option = option_matches ? option_matches[0] : "";
var option_array = "";
if(option){
if(option.endsWith(" HTTP")){
option = option.substr(0,option.length - 5);
}
if(option.startsWith("?")){
option = option.substr(1);
}
var option_array = option.split('\&')
option = option_array.join("\t");
}
console.log([ip, date, option].join("\t"));
}
});
}
});
});
}
}
});