-
Notifications
You must be signed in to change notification settings - Fork 2
/
logmapper.js
58 lines (51 loc) · 1.68 KB
/
logmapper.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
var express = require('express'), now = require("now"), http = require('http'), gf = require('growing-file'), geoip = require('geoip');
var city = new geoip.City('./GeoLiteCity.dat');
var logfile;
if (process.argv[2] != undefined){
logfile = process.argv[2];
if (!path.existsSync(filename)){
throw(filename + ' does not exists');
}
}
else {
logfile = "/var/log/httpd/access.log";
}
var file = gf.open(logfile);
var app = express();
var server = http.createServer(app);
var everyone = now.initialize(server);
//Express Configuration
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// send html page back
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
console.log('conntected from ' + req.connection.remoteAddress);
});
//file watcher
file.on('data', function(arr) {
arr = arr.toString('utf-8');
arr = arr.split("\n");
var pattern = /((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})/g;
var ips = new Array();
var addr = arr[0].match(pattern);
if (addr != null) {
var geo = city.lookupSync(addr.toString('utf-8'));
if(geo != null){
ips.push(geo['latitude'].toString().substring(0, 8) + ', ' + geo['longitude'].toString().substring(0, 8));
}
}
if(everyone.now.receiveMessage != null)
everyone.now.receiveMessage(ips);
});
server.listen(8000);
console.log("Express server listening on port %d in %s mode", server.address().port, app.settings.env);