-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
executable file
·573 lines (472 loc) · 20.5 KB
/
main.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// Developed and Designed by : Mohamed ELMesseiry, Ahmed Hatem @ 2016
"use strict";
// Imports
var cproc = require('child_process');
var spawn = cproc.spawn;
var snmp = require ("net-snmp");
var fs = require('fs');
var schedule = require('node-schedule');
// Reading app.json
let appConfFile = fs.readFileSync("app.json");
let appConf = JSON.parse(appConfFile);
// Set vars from app.json
var mainConfFileName = appConf["mainConfFileName"]
//SNMP Trap Generator
var enableTrapSend = appConf["enableTrapSend"];
var enableConsoleLog = appConf["enableConsoleLog"];
var snmpTrapServer = appConf["snmpTrapServer"];
var snmpCommunity = appConf["snmpCommunity"]
var enterpriseOid = appConf["enterpriseOid"]
var options = appConf["options"]
options.version = snmp.Version2c;
/*
loop for every entry in the conf.json file
each entry is for a server in the format User@ServerIP
then another nested loop for the nested log files within a server. this process is where a process is created to listen to log files.
the command used in listening to processes is the same for all the files.
the following command should return the last changed files however when it's called from ssh directly it's returning an error accessing teh files.
tail -F -n 1 $(ls -t /var/log/NodeJsTest2* | head -n1)
this will be skipped for now. we only need this option in case we need to pass wild card characters to get the lat log.
Note: using * in the fileName is not recommended and most probably will not work, the app is design to tail one single file at a time.
*/
function main(confFileName){
let confFile = fs.readFileSync(confFileName);
let conf = JSON.parse(confFile);
for (let key in conf) {
log("Processing conf for server: " + key);
for (let i=0; i < conf[key].length; i++) {
let itemConf = conf[key][i];
// Get possible actions
let nixCmd = itemConf['nixCmd'];
let WMI = itemConf['WMI'];
let nixTail = itemConf['nixTail'];
let nixTailLatest = itemConf['nixTailLatest'];
let cron = itemConf['cron'];
// Handle execution to handlers
if (nixTail) handleNixTail(key, nixTail, itemConf);
else if(nixTailLatest) handleNixTailLatest(key, nixTailLatest, itemConf);
else if(nixCmd) {
if (cron === "@start" || !cron) handleLinuxCmd(key, nixCmd, itemConf);
else {
log("Cron created for :" + nixCmd + " with val:" + cron);
handleLinuxCmd(key, nixCmd, itemConf);
schedule.scheduleJob(cron, ()=>{
handleLinuxCmd(key, nixCmd, itemConf);
});
}
}
else if(WMI) {
if (cron === "@start" || !cron) handleWMI(key, WMI, itemConf);
else {
log("Cron created for :" + WMI + " with val:" + cron);
handleWMI(key, WMI, itemConf);
schedule.scheduleJob(cron, ()=>{
handleWMI(key, WMI, itemConf);
});
}
}
}
}
// Hack to keep running forever, this will be enhanced to handle the repition of execution needed for the scripts, so keep it like this for now.
//setTimeout(exitf = function(){ setTimeout(exitf, 99999999999999999); }, 99999999999999999);
}
main(mainConfFileName);
/********************************************************************************************************/
/**************************** Tag handlers *******************************************************/
/********************************************************************************************************/
/******** Nix handlers *******/
function handleLinuxCmd(key, command, itemConf){
// This is simply here because no one would ever dream that he needs to add this param for it to work.
if (command.includes("grep")){
command = command.replace("grep", "grep --line-buffered ");
}
// Spawn and listen.
let child = null;
if (key === "local") {
log("Setting local command:" + command)
child= spawn("bash", ["-c", command]);
}
else {
log("Setting remote (" + key + ") command:" + command)
child= spawn("ssh", ["-t", key, command]);
}
child.stdout.on('data', (data)=> {
let serverInProcess = key;
let childProcesID = child.pid.toString();
ParseReceviedData(serverInProcess, command, data, childProcesID, itemConf);
});
child.stderr.on('data', (data)=> {
log("ERROR: " + data);
});
}
function handleNixTail (key, arg, itemConf){
let command = "tail -F -n 1 " + arg;
handleLinuxCmd(key, command, itemConf);
}
function handleNixTailLatest (key, arg, itemConf){
let command = "ls -t " + arg + " | head -1 | xargs -I % tail -f -n1 %"
handleLinuxCmd(key, command, itemConf);
}
/******** Win handlers *******/
function handleWMI(key, command, itemConf){
//wmic -U wmiuser%wmipasswd //wmi-server.localnet "select caption, name, parentprocessid, processid from win32_process"
// Spawn and listen.
let commandargs = '';
log("Invoking -> WMI " + key + " " + command + " " + commandargs );
let keyParts = key.split(" ");
let child = spawn("wmic", ["-U" , keyParts[0], keyParts[1], command]);
child.stderr.on('data', (data)=> {
log("ERROR: " + data);
});
child.stdout.on('data', (data)=> {
//log(data);
let serverInProcess = key;
let childProcesID = child.pid.toString();
ParseReceviedData(serverInProcess, command, data, childProcesID, itemConf);
});
}
/********************************************************************************************************/
/**************************** DATA Processsing functions ************************************************/
/********************************************************************************************************/
function ParseReceviedData(serverInProcess, command, data, childProcesID, itemConf) {
let logFormateRegex = itemConf['OutputEntryFormatRegex'];
let logFormateScope = itemConf['OutputEntryFormatScope'];
let GlobalFilterRegex = new RegExp(itemConf["GlobalFilterRegex"], "g");
let EventMap = itemConf["EventMap"];
// exclude all notification that is not matching the GlobalRegex
if (GlobalFilterRegex.test(data)) {
// only executes when a GlobalRegex is matching the data comming.
//console.log("Recieved Log Message from server matching Global RegEx=" + serverInProcess + "\t processID = " + childProcesID + "\t from logfile:" + command + "\n" + data);
DetectBatchMessages(data, EventMap, command, childProcesID, serverInProcess, logFormateRegex, logFormateScope);
}
}
/*
* DetectBatchMessages is an evaluator that detect batch messages based on a regex, if the log message occures as a result of regex matching multiople times that means its a batch,
* the regex is different from one log file to another so it needs to be re-evaluated for each case.
*
*/
function DetectBatchMessages(data, EventMap, command, childProcesID, serverInProcess, logFormateRegex, logFormateScope) {
var regex = new RegExp(logFormateRegex, logFormateScope);
let m;
while ((m = regex.exec(data)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
if (groupIndex === 0) {
//console.log(groupIndex, match);
Evaluatemessage(match, EventMap, command, childProcesID, serverInProcess);
}
//console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
}
function Evaluatemessage(msg, EventMap, source, childProcessID, serverInProcess) {
var instanceNameRegex;
var severityRegex;
var classNameRegex;
var elementNameRegex;
var filterName;
var filterRegex;
var timeStampRegex;
var eventNameRegex;
var userDefined1Regex;
var userDefined2Regex;
var userDefined3Regex;
var userDefined4Regex;
var userDefined5Regex;
for (var i = 0; i < EventMap.length; i++) {
filterName = EventMap[i]["filterName"];
filterRegex = EventMap[i]["filterRegex"];
timeStampRegex = EventMap[i]["timeStampRegex"];
eventNameRegex = EventMap[i]["eventNameRegex"];
elementNameRegex = EventMap[i]["elementNameRegex"];
instanceNameRegex = EventMap[i]["instanceNameRegex"];
classNameRegex = EventMap[i]["classNameRegex"];
severityRegex = EventMap[i]["severityRegex"];
userDefined1Regex = EventMap[i]["userDefined1Regex"];
userDefined2Regex = EventMap[i]["userDefined2Regex"];
userDefined3Regex = EventMap[i]["userDefined3Regex"];
userDefined4Regex = EventMap[i]["userDefined4Regex"];
userDefined5Regex = EventMap[i]["userDefined5Regex"];
var timeStampValue, eventNameValue, elementNameValue, instanceNameValue, classNameValue, severityValue,userDefined1Value, userDefined2Value, userDefined3Value, userDefined4Value, userDefined5Value;
//log(msg);
msg = msg.toString().replace(/\n/g, "");
// if the message is matching the regex then its gonna be parsed and will create a notification
if (new RegExp(filterRegex).test(msg)) {
if (timeStampValue === "" || !timeStampRegex) {
timeStampValue = new Date().toLocaleString();
} else {
if (timeStampRegex.startsWith("default:")) {
timeStampValue = new Date().toLocaleString();
} else {
match = new RegExp(timeStampRegex, "g").exec(msg);
if (match !== null) {
timeStampValue = match[1];
} else {
timeStampValue = new Date().toLocaleString();
}
}
}
if (eventNameValue === "" || !eventNameRegex) {
eventNameValue = "default";
} else {
if (eventNameRegex.startsWith("default:")) {
eventNameValue = eventNameRegex.replace("default:", "");
} else {
let match = new RegExp(eventNameRegex, "g").exec(msg);
if (match !== null) {
eventNameValue = match[1];
} else {
eventNameValue = "default";
}
}
}
if (elementNameValue === "" || !elementNameRegex) {
elementNameValue = "default";
} else {
if (elementNameRegex.startsWith("default:")) {
elementNameValue = elementNameRegex.replace("default:", "");
} else {
let match = new RegExp(elementNameRegex, "g").exec(msg);
if (match !== null) {
elementNameValue = match[1];
} else {
elementNameValue = "default";
}
}
}
if (instanceNameValue === "" || !instanceNameRegex) {
instanceNameValue = "default";
} else {
if (instanceNameRegex.startsWith("default:")) {
instanceNameValue = instanceNameRegex.replace("default:", "");
} else {
let match = new RegExp(instanceNameRegex, "g").exec(msg);
if (match !== null) {
instanceNameValue = match[1];
} else {
instanceNameValue = "default";
}
}
}
if (classNameValue === "" || !classNameRegex) {
classNameValue = "default";
} else {
if (classNameRegex.startsWith("default:")) {
classNameValue = classNameRegex.replace("default:", "");
} else {
let match = new RegExp(classNameRegex, "g").exec(msg);
if (match !== null) {
classNameValue = match[1];
} else {
classNameValue = "default";
}
}
}
if (severityValue === "" || !severityRegex) {
severityValue = "default";
} else {
if (severityRegex.startsWith("default:")) {
severityValue = severityRegex.replace("default:", "");
} else {
let match = new RegExp(severityRegex, "g").exec(msg);
if (match !== null) {
severityValue = match[1];
} else {
severityValue = "default";
}
}
}
// UserDefined vars
if (userDefined1Value === "" || !userDefined1Regex) {
userDefined1Value = "default";
} else {
if (userDefined1Regex.startsWith("default:")) {
userDefined1Value = userDefined1Regex.replace("default:", "");
} else {
let match = new RegExp(userDefined1Regex, "g").exec(msg);
if (match !== null) {
userDefined1Value = match[1];
} else {
userDefined1Value = "default";
}
}
}
if (userDefined2Value === "" || !userDefined2Regex) {
userDefined2Value = "default";
} else {
if (userDefined2Regex.startsWith("default:")) {
userDefined2Value = userDefined2Regex.replace("default:", "");
} else {
let match = new RegExp(userDefined2Regex, "g").exec(msg);
if (match !== null) {
userDefined2Value = match[1];
} else {
userDefined2Value = "default";
}
}
}
if (userDefined3Value === "" || !userDefined3Regex) {
userDefined3Value = "default";
} else {
if (userDefined3Regex.startsWith("default:")) {
userDefined3Value = userDefined3Regex.replace("default:", "");
} else {
let match = new RegExp(userDefined3Regex, "g").exec(msg);
if (match !== null) {
userDefined3Value = match[1];
} else {
userDefined3Value = "default";
}
}
}
if (userDefined4Value === "" || !userDefined4Regex) {
userDefined4Value = "default";
} else {
if (userDefined4Regex.startsWith("default:")) {
userDefined4Value = userDefined4Regex.replace("default:", "");
} else {
let match = new RegExp(userDefined4Regex, "g").exec(msg);
if (match !== null) {
userDefined4Value = match[1];
} else {
userDefined4Value = "default";
}
}
}
if (userDefined5Value === "" || !userDefined5Regex) {
userDefined5Value = "default";
} else {
if (userDefined5Regex.startsWith("default:")) {
userDefined5Value = userDefined5Regex.replace("default:", "");
} else {
let match = new RegExp(userDefined5Regex, "g").exec(msg);
if (match !== null) {
userDefined5Value = match[1];
} else {
userDefined5Value = "default";
}
}
}
if (enableConsoleLog) {
log("\n");
log("<--- Notification Recieved ---<");
log("---- TimeStamp: \t" + timeStampValue);
log("---- EventName: \t" + eventNameValue);
log("---- ElementName: \t" + elementNameValue);
log("---- InstanceName: \t" + instanceNameValue);
log("---- ClassName: \t" + classNameValue);
log("---- SeverityName: \t" + severityValue);
log("---- EventSource: \t" + serverInProcess);
log("---- filterName: \t" + filterName);
log("---- logFile: \t" + source);
log("---- OS Process: \t" + childProcessID);
log("---- EventText: \t" + msg);
log("---- UserDefined1: \t" + userDefined1Value);
log("---- UserDefined2: \t" + userDefined2Value);
log("---- UserDefined3: \t" + userDefined3Value);
log("---- UserDefined4: \t" + userDefined4Value);
log("---- UserDefined5: \t" + userDefined5Value);
}
if (enableTrapSend) {
sendSNMPTrap(timeStampValue, eventNameValue, elementNameValue, instanceNameValue, classNameValue, severityValue, source, msg, filterName, serverInProcess ,userDefined1Value ,userDefined2Value ,userDefined3Value ,userDefined4Value ,userDefined5Value);
}
// the Break is to get the first match in the filters only
break;
}
}
}
function sendSNMPTrap(timeStampValue, eventNameValue,elementNameValue,instanceNameValue,classNameValue,severityValue,source,msg, filterName, serverInProcess, userDefined1Value ,userDefined2Value ,userDefined3Value ,userDefined4Value ,userDefined5Value) {
// combining the varbindsl of the trap from JSON array
var varbinds = [
{
oid: "1.3.6.1.2.1.1.0.0.7.1",
type: snmp.ObjectType.OctetString,
value: timeStampValue
},
{
oid: "1.3.6.1.2.1.1.0.0.7.2",
type: snmp.ObjectType.OctetString,
value: eventNameValue
},
{
oid: "1.3.6.1.2.1.1.0.0.7.3",
type: snmp.ObjectType.OctetString,
value: elementNameValue
},
{
oid: "1.3.6.1.2.1.1.0.0.7.4",
type: snmp.ObjectType.OctetString,
value: instanceNameValue
},
{
oid: "1.3.6.1.2.1.1.0.0.7.5",
type: snmp.ObjectType.OctetString,
value: classNameValue
},
{
oid: "1.3.6.1.2.1.1.0.0.7.6",
type: snmp.ObjectType.OctetString,
value: severityValue
},
{
oid: "1.3.6.1.2.1.1.0.0.7.7",
type: snmp.ObjectType.OctetString,
value: msg
},
{
oid: "1.3.6.1.2.1.1.0.0.7.8",
type: snmp.ObjectType.OctetString,
value: serverInProcess
},
{
oid: "1.3.6.1.2.1.1.0.0.7.9",
type: snmp.ObjectType.OctetString,
value: filterName
},
{
oid: "1.3.6.1.2.1.1.0.0.7.10",
type: snmp.ObjectType.OctetString,
value: source
},
{
oid: "1.3.6.1.2.1.1.0.0.7.11",
type: snmp.ObjectType.OctetString,
value: userDefined1Value
},
{
oid: "1.3.6.1.2.1.1.0.0.7.12",
type: snmp.ObjectType.OctetString,
value: userDefined2Value
},
{
oid: "1.3.6.1.2.1.1.0.0.7.13",
type: snmp.ObjectType.OctetString,
value: userDefined3Value
},
{
oid: "1.3.6.1.2.1.1.0.0.7.14",
type: snmp.ObjectType.OctetString,
value: userDefined4Value
},
{
oid: "1.3.6.1.2.1.1.0.0.7.15",
type: snmp.ObjectType.OctetString,
value: userDefined5Value
}
];
var session = snmp.createSession(snmpTrapServer,snmpCommunity,options)
session.trap (enterpriseOid, varbinds,
function (error) {
if (error)
console.error (error);
});
log("-------------> SNMP Trap Sent to " + snmpTrapServer);
}
function log(msg) {
console.log(new Date().toLocaleString() + " : " + msg);
}