Database logging via sqlite3

This commit is contained in:
Cryptkeeper 2015-12-10 22:06:27 -06:00
parent 0571107eee
commit 01f977b16e
7 changed files with 40 additions and 5 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ node_modules/
*.log
.idea/
production/
database.sql

View File

@ -10,9 +10,11 @@ You can see an up-to-date copy of the master branch running on http://rewrite.mi
2. Run ```npm install```.
2. Run ```node app.js``` to boot the system (may need sudo!)
(There's also ```install.sh``` and ```start.sh```, but they may not for your installations.)
(There's also ```install.sh``` and ```start.sh```, but they may not work for your OS.)
Database logging is disabled by default. You can enable it in ```config.json``` by setting ```logToDatabase``` to true.
This requires sqlite3 drivers to be installed.
#### TODO
- Add public API (similiar to old server.json)
- Add ingame server, similiar to old mc.minetrack.me
- History graphs (database?)
- Add ingame server, similiar to old mc.minetrack.me

13
app.js
View File

@ -3,6 +3,7 @@ var ping = require('./lib/ping');
var logger = require('./lib/logger');
var mojang = require('./lib/mojang_services');
var util = require('./lib/util');
var db = require('./lib/database');
var config = require('./config.json');
@ -73,6 +74,11 @@ function pingAll() {
if (_networkHistory.length > 72) { // 60/2.5 = 24, so 24 is one minute
_networkHistory.shift();
}
// Log it to the database if needed.
if (config.logToDatabase) {
db.log(network.ip, util.getCurrentTimeMs(), res ? res.players.online : 0);
}
});
})(servers[i]);
}
@ -89,6 +95,13 @@ function startMainLoop() {
}, config.rates.upateMojangStatus);
}
if (config.logToDatabase) {
// Setup our database.
db.setup();
} else {
logger.warn('Database logging is not enabled. You can enable it by setting "logToDatabase" to true in config.json. This requires sqlite3 to be installed.');
}
server.start(function() {
// Track how many people are currently connected.
server.io.on('connect', function(client) {

View File

@ -192,5 +192,6 @@
"mojangStatusTimeout": 3500,
"pingAll": 3000,
"connectTimeout": 2500
}
},
"logToDatabase": true
}

17
lib/database.js Normal file
View File

@ -0,0 +1,17 @@
var sqlite = require('sqlite3');
var fs = require('fs');
var util = require('./util');
var db = new sqlite.Database('database.sql');
exports.log = function(ip, timestamp, playerCount) {
var insertStatement = db.prepare('INSERT INTO pings (timestamp, ip, player) VALUES (?, ?, ?)');
insertStatement.run(timestamp, ip, playerCount);
};
exports.setup = function(fileName) {
db.serialize(function() {
db.run('CREATE TABLE IF NOT EXISTS pings (id INT AUTO INCREMENT PRIMARY KEY, timestamp BIGINT NOT NULL, ip TINYTEXT, player MEDIUMINT)');
});
};

View File

@ -9,6 +9,7 @@
"mime": "^1.3.4",
"request": "^2.65.0",
"socket.io": "^1.3.7",
"sqlite3": "^3.1.1",
"winston": "^2.0.0"
},
"repository": {

View File

@ -1,3 +1,3 @@
apt-get install git npm nodejs
apt-get install git npm nodejs sqlite3
npm install
sh start.sh