diff --git a/.gitignore b/.gitignore index 2caf7ba..f7bf2dc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ *.log .idea/ production/ +database.sql \ No newline at end of file diff --git a/README.md b/README.md index 1972de2..845e4db 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/app.js b/app.js index 6d00261..53f8886 100644 --- a/app.js +++ b/app.js @@ -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) { diff --git a/config.json b/config.json index 03549fd..fb27625 100644 --- a/config.json +++ b/config.json @@ -192,5 +192,6 @@ "mojangStatusTimeout": 3500, "pingAll": 3000, "connectTimeout": 2500 - } + }, + "logToDatabase": true } diff --git a/lib/database.js b/lib/database.js new file mode 100644 index 0000000..4f244c2 --- /dev/null +++ b/lib/database.js @@ -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)'); + }); +}; \ No newline at end of file diff --git a/package.json b/package.json index 38e6af0..b277459 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/scripts/install.sh b/scripts/install.sh index 0864dce..bb5c4b4 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,3 +1,3 @@ -apt-get install git npm nodejs +apt-get install git npm nodejs sqlite3 npm install sh start.sh \ No newline at end of file