-
Notifications
You must be signed in to change notification settings - Fork 10
/
al_log.js
238 lines (215 loc) · 7.78 KB
/
al_log.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
234
235
236
237
/* -----------------------------------------------------------------------------
* @copyright (C) 2018, Alert Logic, Inc
* @doc
*
* Helper utilities for Alert Logic log collector.
*
* @end
* -----------------------------------------------------------------------------
*/
const crypto = require('crypto');
const async = require('async');
const zlib = require('zlib');
const alcHealthPb = require('./proto/alc_health.piqi_pb').alc_health;
const commonProtoPb = require('./proto/common_proto.piqi_pb').common_proto;
const dictPb = require('./proto/dict.piqi_pb').alc_dict;
const hostMetadataPb = require('./proto/host_metadata.piqi_pb').host_metadata;
const PAYLOAD_BATCH_SIZE = 700000;
/**
* @function builds incoming log messages into protobuf and compresses it. The payload returned is
* ready to be passed to Ingest client for transport.
*
* @param hostId - host uuid obtained at collector registration
* @param sourceId - source/collector id obtained at collector registration
* @param hostmetaElems - a list of hostmeta JSON objects. For example,
* var hostTypeElem = {
* key: 'host_type',
* value: {str: 'azure_fun'}
* };
* var localHostnameElem = {
* key: 'local_hostname',
* value: {str: process.env.WEBSITE_HOSTNAME}
* };
* var hostmetaElems = [hostTypeElem, localHostnameElem];
* Consult 'metadata' definition in ./proto/host_metadata.piqi.proto
* @param content - a list of log messages to be ingested. Content should be batched on the caller level.
* @param parseCallback(message) - a function to parse a log message into a JSON object which is converted into protobuf.
* The parse callback is expected to construct the following object out of each log message:
* var parsedMessage = {
* messageTs: 1542138053,
* priority: 11,
* progName: 'o365webhook',
* pid: undefined,
* message: 'some message string',
* messageType: 'json/azure.o365',
* messageTypeId: 'AzureActiveDirectory',
* messageTsUs: undefined
* };
* Consult 'collected_message' definition in proto/common_proto.piqi.proto
* @param callback
*
* @return callback - (error, builtPayload, payloadSize)
* @NOTE: Batch size should be tweaked on a caller level in order to avoid "Maximum payload size exceeded" errors.
* For an Azure function consult eventHub.maxBatchSize property in host.json.
* For an AWS Lambda via kinesis trigger batch size configuration.
*/
var buildPayload = function (hostId, sourceId, hostmetaElems, content, parseCallback, mainCallback) {
async.waterfall([
function(callback) {
buildMessages(content, parseCallback, function(err, msg) {
return callback(err, msg);
});
},
function(msg, callback) {
buildHostmeta(hostId, hostmetaElems, function(err, meta) {
return callback(err, meta, msg);
});
},
function(meta, msg, callback) {
buildBatch(sourceId, meta, msg, function(err, batch) {
return callback(err, batch);
});
},
function(batchBuf, callback) {
buildBatchList(batchBuf, function(err, batchList) {
return callback(err, batchList);
});
},
function(batchList, callback) {
var batchListType = commonProtoPb.collected_batch_list;
var buf = batchListType.encode(batchList).finish();
return callback(null, buf);
}],
function(err, result) {
if (err) {
return mainCallback(err);
} else {
zlib.deflate(result, function(defalteErr, compressed) {
if (defalteErr) {
return mainCallback(defalteErr);
} else {
var payloadSize = compressed.byteLength;
if (payloadSize > PAYLOAD_BATCH_SIZE) {
return mainCallback(`Maximum payload size exceeded: ${payloadSize}`, compressed);
} else {
return mainCallback(null, compressed);
}
}
});
}
});
};
/**
*
* Private functions
*
*/
function buildType(type, payload, callback) {
var verify = type.verify(payload);
if (verify)
return callback(verify);
var payloadCreated = type.create(payload);
return callback(null, payloadCreated);
}
/**
* @function build hostmeta protobuf out of a list of {key, value} metadata pairs.
*
* @param hostId - a host uuid obtain at collector registration.
* @param hostmetaElems - a list of metadata JSON objects. For example,
* var hostTypeElem = {
* key: 'host_type',
* value: {str: 'azure_fun'}
* };
* var localHostnameElem = {
* key: 'local_hostname',
* value: {str: process.env.WEBSITE_HOSTNAME}
* };
* var hostmetaElems = [hostTypeElem, localHostnameElem];
*
* @param callback
* @returns callback
*/
function buildHostmeta(hostId, hostmetaElems, callback) {
var hostmetaType = hostMetadataPb.metadata;
var hostmeta = {
elem : hostmetaElems
};
buildType(dictPb.dict, hostmeta, function(err, hostmetaData){
if (err) {
return callback(err);
} else {
var meta = {
hostUuid : hostId,
data : hostmetaData,
dataChecksum : new Buffer('')
};
var sha = crypto.createHash('sha1');
var hashPayload = hostmetaType.encode(meta).finish();
var hashValue = sha.update(hashPayload).digest();
var metadataPayload = {
hostUuid : hostId,
dataChecksum : hashValue,
timestamp : Math.floor(Date.now() / 1000),
data : hostmetaData
};
return buildType(hostmetaType, metadataPayload, callback);
}
});
}
/**
* @function builds protobuf out of JSON definition of a log message
* @param content - raw log messages retrieved from a source.
* @param parseContentFun - function to parse a single message into a json structure. For example,
* var parsedMessage = {
* messageTs: 1542138053,
* priority: 11,
* progName: 'o365webhook',
* pid: undefined,
* message: 'some message string',
* messageType: 'json/azure.o365',
* messageTypeId: 'AzureActiveDirectory',
* messageTsUs: undefined
* };
* Consult 'collected_message' definition in proto/common_proto.piqi.proto
* @param callback
* @returns
*/
function buildMessages(content, parseContentFun, callback) {
async.reduce(content, [], function(memo, item, asyncCallback) {
var messageType = commonProtoPb.collected_message;
var messagePayload = parseContentFun(item);
buildType(messageType, messagePayload, function(err, buf) {
if (err) {
return asyncCallback(err);
} else {
memo.push(buf);
return asyncCallback(err, memo);
}
});
},
callback);
}
function buildBatch(sId, metadata, messages, callback) {
var batchType = commonProtoPb.collected_batch;
var batchPayload = {
sourceId: sId,
metadata: metadata,
message: messages
};
buildType(batchType, batchPayload, callback);
}
function buildBatchList(batches, callback) {
var batchListType = commonProtoPb.collected_batch_list;
var batchListPayload = {
elem: [batches]
};
buildType(batchListType, batchListPayload, callback);
}
module.exports = {
AlcHealthPb : alcHealthPb,
CommonProto : commonProtoPb,
DictPb : dictPb,
HostMetadataPb : hostMetadataPb,
buildPayload : buildPayload,
PAYLOAD_BATCH_SIZE : PAYLOAD_BATCH_SIZE
};