Server fixes & MongoDB performance improvements.

Ylian Saint-Hilaire committed Feb 26, 2019 at 14:39 UTC 30eedab17755685faed73909c0dce079602966bd
7 files changed +53 -34
db.js
+3 -2
@@ -258,7 +258,6 @@ module.exports.CreateDB = function (parent) {
258 obj.GetUserWithEmail = function (domain, email, func) { obj.file.find({ type: 'user', domain: domain, email: email }, { type: 0 }, func); };
259 obj.GetUserWithVerifiedEmail = function (domain, email, func) { obj.file.find({ type: 'user', domain: domain, email: email, emailVerified: true }, { type: 0 }, func); };
260 obj.Remove = function (id) { obj.file.remove({ _id: id }); };
261 - obj.RemoveNode = function (id) { obj.file.remove({ node: id }, { multi: true }); };
261 obj.RemoveAll = function (func) { obj.file.remove({}, { multi: true }, func); };
262 obj.RemoveAllOfType = function (type, func) { obj.file.remove({ type: type }, { multi: true }, func); };
263 obj.InsertMany = function (data, func) { obj.file.insert(data, func); };
@@ -292,12 +291,14 @@ module.exports.CreateDB = function (parent) {
291 };
292 obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { if (obj.databaseType == 1) { obj.eventsfile.find({ domain: domain, nodeid: nodeid }, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).exec(func); } else { obj.eventsfile.find({ domain: domain, nodeid: nodeid }, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit, func); } };
293 obj.RemoveAllEvents = function (domain) { obj.eventsfile.remove({ domain: domain }, { multi: true }); };
294 + obj.RemoveAllNodeEvents = function (domain, nodeid) { obj.eventsfile.remove({ domain: domain, nodeid: nodeid }, { multi: true }); };
295
296 // Database actions on the power collection
297 obj.getAllPower = function (func) { obj.powerfile.find({}, func); };
298 obj.storePowerEvent = function (event, multiServer, func) { if (multiServer != null) { event.server = multiServer.serverid; } obj.powerfile.insert(event, func); };
299 obj.getPowerTimeline = function (nodeid, func) { if (obj.databaseType == 1) { obj.powerfile.find({ nodeid: { $in: ['*', nodeid] } }, { _id: 0, nodeid: 0, s: 0 }).sort({ time: 1 }).exec(func); } else { obj.powerfile.find({ nodeid: { $in: ['*', nodeid] } }, { _id: 0, nodeid: 0, s: 0 }).sort({ time: 1 }, func); } };
300 - obj.removeAllPowerEvents = function (domain) { obj.powerfile.remove({ }, { multi: true }); };
300 + obj.removeAllPowerEvents = function () { obj.powerfile.remove({}, { multi: true }); };
301 + obj.removeAllPowerEventsForNode = function (nodeid) { obj.powerfile.remove({ nodeid: nodeid }, { multi: true }); };
302
303 // Database actions on the SMBIOS collection
304 obj.SetSMBIOS = function (smbios, func) { obj.smbiosfile.update({ _id: smbios._id }, smbios, { upsert: true }, func); };
meshagent.js
+7 -6
@@ -73,12 +73,13 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
73 // If this is a temporary or recovery agent, or all devices in this group are temporary, remove the agent (0x20 = Temporary, 0x40 = Recovery)
74 if (((obj.agentInfo) && (obj.agentInfo.capabilities) && ((obj.agentInfo.capabilities & 0x20) || (obj.agentInfo.capabilities & 0x40))) || ((mesh) && (mesh.flags) && (mesh.flags & 1))) {
75 // Delete this node including network interface information and events
76 - obj.db.Remove(obj.dbNodeKey); // Remove node with that id
77 - obj.db.Remove('if' + obj.dbNodeKey); // Remove interface information
78 - obj.db.Remove('nt' + obj.dbNodeKey); // Remove notes
79 - obj.db.Remove('lc' + obj.dbNodeKey); // Remove last connect time
80 - obj.db.RemoveSMBIOS(obj.dbNodeKey); // Remove SMBios data
81 - obj.db.RemoveNode(obj.dbNodeKey); // Remove all entries with node:id
76 + obj.db.Remove(obj.dbNodeKey); // Remove node with that id
77 + obj.db.Remove('if' + obj.dbNodeKey); // Remove interface information
78 + obj.db.Remove('nt' + obj.dbNodeKey); // Remove notes
79 + obj.db.Remove('lc' + obj.dbNodeKey); // Remove last connect time
80 + obj.db.RemoveSMBIOS(obj.dbNodeKey); // Remove SMBios data
81 + obj.db.RemoveAllNodeEvents(obj.dbNodeKey); // Remove all events for this node
82 + obj.db.removeAllPowerEventsForNode(obj.dbNodeKey); // Remove all power events for this node
83
84 // Event node deletion
85 obj.parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, { etype: 'node', action: 'removenode', nodeid: obj.dbNodeKey, domain: obj.domain.id, nolog: 1 });
meshcentral.js
+22 -15
@@ -60,7 +60,7 @@ function CreateMeshCentralServer(config, args) {
60 obj.serverKey = Buffer.from(obj.crypto.randomBytes(48), 'binary');
61 obj.loginCookieEncryptionKey = null;
62 obj.serverSelfWriteAllowed = true;
63 - obj.taskLimiter = obj.common.createTaskLimiterQueue(8, 20, 60); // This is a task limiter queue to smooth out server work.
63 + obj.taskLimiter = obj.common.createTaskLimiterQueue(20, 20, 60); // This is a task limiter queue to smooth out server work.
64 try { obj.currentVer = JSON.parse(obj.fs.readFileSync(obj.path.join(__dirname, 'package.json'), 'utf8')).version; } catch (e) { } // Fetch server version
65
66 // Setup the default configuration and files paths
@@ -281,20 +281,26 @@ function CreateMeshCentralServer(config, args) {
281 console.log(' --dbpulldatafiles * This will import files from meshcentral-data into the db.');
282 process.exit();
283 } else {
284 - obj.db.RemoveAllOfType('cfile', function () {
285 - if (obj.args.dbpushconfigfiles == '*') { obj.args.dbpushconfigfiles = obj.datapath; }
286 - obj.fs.readdir(obj.datapath, (err, files) => {
287 - var lockCount = 1
288 - for (var i in files) {
289 - const file = files[i];
290 - if ((file == 'config.json') || file.endsWith('.key') || file.endsWith('.crt') || (file == 'terms.txt') || file.endsWith('.jpg') || file.endsWith('.png')) {
291 - const path = obj.path.join(obj.args.dbpushconfigfiles, files[i]), binary = Buffer.from(obj.fs.readFileSync(path, { encoding: 'binary' }), 'binary');
292 - console.log('Pushing ' + file + ', ' + binary.length + ' bytes.');
293 - lockCount++;
294 - obj.db.setConfigFile(file, obj.db.encryptData(obj.args.configkey, binary), function () { if ((--lockCount) == 0) { console.log('Done.'); process.exit(); } });
284 + if (obj.args.dbpushconfigfiles == '*') { obj.args.dbpushconfigfiles = obj.datapath; }
285 + obj.fs.readdir(obj.args.dbpushconfigfiles, function (err, files) {
286 + if (err != null) { console.log('Unable to read from folder ' + obj.args.dbpushconfigfiles); process.exit(); return; }
287 + var configFound = false;
288 + for (var i in files) { if (files[i] == 'config.json') { configFound = true; } }
289 + if (configFound == false) { console.log('No config.json in folder ' + obj.args.dbpushconfigfiles); process.exit(); return; }
290 + obj.db.RemoveAllOfType('cfile', function () {
291 + obj.fs.readdir(obj.args.dbpushconfigfiles, function (err, files) {
292 + var lockCount = 1
293 + for (var i in files) {
294 + const file = files[i];
295 + if ((file == 'config.json') || file.endsWith('.key') || file.endsWith('.crt') || (file == 'terms.txt') || file.endsWith('.jpg') || file.endsWith('.png')) {
296 + const path = obj.path.join(obj.args.dbpushconfigfiles, files[i]), binary = Buffer.from(obj.fs.readFileSync(path, { encoding: 'binary' }), 'binary');
297 + console.log('Pushing ' + file + ', ' + binary.length + ' bytes.');
298 + lockCount++;
299 + obj.db.setConfigFile(file, obj.db.encryptData(obj.args.configkey, binary), function () { if ((--lockCount) == 0) { console.log('Done.'); process.exit(); } });
300 + }
301 }
296 - }
297 - if (--lockCount == 0) { process.exit(); }
302 + if (--lockCount == 0) { process.exit(); }
303 + });
304 });
305 });
306 }
@@ -318,7 +324,8 @@ function CreateMeshCentralServer(config, args) {
324 if (binary == null) {
325 console.log('Invalid config key.');
326 } else {
321 - obj.fs.writeFileSync(obj.path.join(obj.args.dbpullconfigfiles, file), binary);
327 + var fullFileName = obj.path.join(obj.args.dbpullconfigfiles, file);
328 + try { obj.fs.writeFileSync(fullFileName, binary); } catch (ex) { console.log('Unable to write to ' + fullFileName); process.exit(); return; }
329 console.log('Pulling ' + file + ', ' + binary.length + ' bytes.');
330 }
331 }
meshuser.js
+7 -6
@@ -1272,12 +1272,13 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1272 if (mesh.links[user._id] == null || ((mesh.links[user._id].rights & 4) == 0)) return;
1273
1274 // Delete this node including network interface information, events and timeline
1275 - obj.db.Remove(node._id); // Remove node with that id
1276 - obj.db.Remove('if' + node._id); // Remove interface information
1277 - obj.db.Remove('nt' + node._id); // Remove notes
1278 - obj.db.Remove('lc' + node._id); // Remove last connect time
1279 - obj.db.RemoveSMBIOS(node._id); // Remove SMBios data
1280 - obj.db.RemoveNode(node._id); // Remove all entries with node:id
1275 + obj.db.Remove(node._id); // Remove node with that id
1276 + obj.db.Remove('if' + node._id); // Remove interface information
1277 + obj.db.Remove('nt' + node._id); // Remove notes
1278 + obj.db.Remove('lc' + node._id); // Remove last connect time
1279 + obj.db.RemoveSMBIOS(node._id); // Remove SMBios data
1280 + obj.db.RemoveAllNodeEvents(node._id); // Remove all events for this node
1281 + obj.db.removeAllPowerEventsForNode(node._id); // Remove all power events for this node
1282
1283 // Event node deletion
1284 obj.parent.parent.DispatchEvent(['*', node.meshid], obj, { etype: 'node', username: user.name, action: 'removenode', nodeid: node._id, msg: 'Removed device ' + node.name + ' from group ' + mesh.name, domain: domain.id });
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.2.9-c",
3 + "version": "0.2.9-d",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",
sample-config.json
+4 -3
@@ -41,10 +41,11 @@
41 "": {
42 "Title": "MyServer",
43 "Title2": "Servername",
44 - "TitlePicture": "title-sample.png",
45 - "UserQuota": 1048576,
46 - "MeshQuota": 248576,
44 + "_TitlePicture": "title-sample.png",
45 + "_UserQuota": 1048576,
46 + "_MeshQuota": 248576,
47 "NewAccounts": 1,
48 + "_NewAccountEmailDomains": [ "sample.com" ],
49 "Footer": "<a href='https://twitter.com/mytwitter'>Twitter</a>",
50 "_CertUrl": "https://192.168.2.106:443/",
51 "_PasswordRequirements": { "min": 8, "max": 128, "upper": 1, "lower": 1, "numeric": 1, "nonalpha": 1 },
views/default.handlebars
+9 -1
@@ -1567,7 +1567,7 @@
1567 // This is an existing mesh
1568 if (message.event.name) { meshes[message.event.meshid].name = message.event.name; }
1569 if (message.event.desc) { meshes[message.event.meshid].desc = message.event.desc; }
1570 - if (message.event.flags) { meshes[message.event.meshid].flags = message.event.flags; }
1570 + if (message.event.flags != null) { meshes[message.event.meshid].flags = message.event.flags; }
1571 if (message.event.links) { meshes[message.event.meshid].links = message.event.links; }
1572 if (message.event.amt) { meshes[message.event.meshid].amt = message.event.amt; }
1573
@@ -6562,6 +6562,14 @@
6562 if (user.quota) x += addDeviceAttribute('Server Quota', EscapeHtml(parseInt(user.quota) / 1024) + ' k');
6563 x += addDeviceAttribute('Creation', new Date(user.creation * 1000).toLocaleString());
6564 if (user.login) x += addDeviceAttribute('Last Login', new Date(user.login * 1000).toLocaleString());
6565 +
6566 + var linkCount = 0, linkCountStr = '<i>None<i>';
6567 + if (user.links) {
6568 + for (var i in user.links) { linkCount++; }
6569 + if (linkCount == 1) { linkCountStr = '1 group'; } else if (linkCount > 1) { linkCountStr = linkCount + ' groups'; }
6570 + }
6571 + x += addDeviceAttribute('Device Groups', linkCountStr);
6572 +
6573 var multiFactor = 0;
6574 if ((user.otpsecret > 0) || (user.otphkeys > 0)) {
6575 multiFactor = 1;