-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
executable file
·123 lines (111 loc) · 3.05 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
#! /usr/bin/env node
module.exports = function (line) {
var parsed = {};
var url = require('url');
var request_labels =
[
'request_method',
'request_uri',
'request_http_version',
'request_uri_scheme',
'request_uri_host',
'request_uri_port',
'request_uri_path',
'request_uri_query'
];
//
// Trailing newline? NOTHX
//
if (line.match(/\n$/)) {
line = line.slice(0, line.length - 1);
}
[
{ 'timestamp' : ' ' },
{ 'elb' : ' ' },
{ 'client' : ':' },
{ 'client_port' : ' ' },
{ 'backend' : ' ' },
{ 'request_processing_time' : ' ' },
{ 'backend_processing_time' : ' ' },
{ 'response_processing_time' : ' ' },
{ 'elb_status_code' : ' ' },
{ 'backend_status_code' : ' ' },
{ 'received_bytes' : ' ' },
{ 'sent_bytes' : ' "' },
{ 'request' : '" "' },
{ 'user_agent' : '" ' },
{ 'ssl_cipher' : ' ' },
{ 'ssl_protocol' : ' ' }
].some(function (t) {
var label = Object.keys(t)[0];
delimiter = t[label]
var m = line.match(delimiter);
if (m === null) {
//
// No match. Try to pick off the last element.
//
m = line.match(delimiter.slice(0, 1));
if (m === null) {
field = line;
}
else {
field = line.substr(0, m.index);
}
parsed[label] = field;
return true;
}
field = line.substr(0, m.index);
line = line.substr(m.index + delimiter.length);
parsed[label] = field;
});
// backend
if(parsed.backend != -1) {
parsed['backend_port'] = parsed.backend.split(":")[1];
parsed['backend'] = parsed.backend.split(":")[0];
} else {
parsed['backend_port'] = '-1';
}
// request
if(parsed.request != '- - - ') {
var i = 0;
var method = parsed.request.split(" ")[0];
var url = url.parse(parsed.request.split(" ")[1]);
var http_version = parsed.request.split(" ")[2];
parsed[request_labels[i]] = method;
i++;
parsed[request_labels[i]] = url.href;
i++;
parsed[request_labels[i]] = http_version;
i++;
parsed[request_labels[i]] = url.protocol;
i++;
parsed[request_labels[i]] = url.hostname;
i++;
parsed[request_labels[i]] = url.port;
i++;
parsed[request_labels[i]] = url.pathname;
i++;
parsed[request_labels[i]] = url.query;
} else {
request_labels.forEach(function(label) {
parsed[label] = '-';
});
}
return parsed;
};
if (require.main === module) {
var split = require('split');
var Transform = require('stream').Transform;
process.stdin
.pipe(split())
.pipe(new Transform({
decodeStrings: false,
transform: function (line, encoding, callback) {
if (line) {
this.push(JSON.stringify(module.exports(line)) + '\n');
}
callback();
}
}))
.pipe(process.stdout);
}