forked from ListenerApproved/RealTimeLogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
105 lines (81 loc) · 2.35 KB
/
app.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
var http = require('http'),
socketio = require('socket.io'),
fs = require('fs'),
util = require('util'),
spawn = require('child_process').spawn,
connect = require('connect');
var lineCount = 1,
buffer = [],
startTime = new Date();
const SECOND = 1000;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
///// Config file stuff
var config;
!function requireAndValidateConfig() {
try {
config = require('/etc/rtlogs/config.js');
} catch(e) {
console.error('Could not load configuration file [ %s ]', '/etc/rtlogs/config.js');
process.exit(0);
} finally {
var validConfig = (config && Array.isArray(config.servers) && config.servers.length > 0);
if(!validConfig) {
console.error('Configuration file has no servers defined');
process.exit(0);
}
}
}();
config.servers.forEach(function(elem) {
streamLog(elem.server, elem.group, elem.log)
console.log(elem.server)
});
///// Server creation stuff
var connect = connect(
connect.static(__dirname + '/public/')
);
var app = http.createServer(connect);
app.listen(8080);
var io = socketio.listen(app);
io.set("log level", 1); //Set the log level of socket.io
io.sockets.on('connection', function(socket) {
var packagedTime = new Date();
packagedTime.setTime(Date.now() - startTime.getTime());
socket.emit('system', {
text: 'Connection Accepted',
server: 'socket.io',
group: 'socket.io'
});
socket.emit('system', {
servers: config.servers,
time: packagedTime.getTime()
})
socket.emit('buffer', { buffer: buffer })
socket.on('my other event', function(data) {
console.log(data);
});
});
///// Actual Streamer
function streamLog(server, group, file) {
fileWatcher = spawn('ssh', [server, 'tail', '-f', '-n', '0', file]);
fileWatcher.stdout.setEncoding('utf8');
fileWatcher.stdout.on('data', function(data) {
var lines = ("" + data).split('\n');
lines.forEach(function(line) {
if(line === "") return;
var logEntry = { text: line, server: server, group: group, lineCount: lineCount };
io.sockets.emit('log', logEntry);
buffer.push(logEntry);
if (buffer.length > 100) {
// use copy-on-write.
// Shift reindexes the array, slice(-100) COWs the last 100 elements
buffer = buffer.slice(-100);
}
lineCount++;
});
});
fileWatcher.on('exit', function(code) {
console.log("ERROR: file watcher died " + code);
});
}