Skip to content

Commit 8696bec

Browse files
committed
TMS integration + small fixes
1 parent 46b57ff commit 8696bec

File tree

5 files changed

+186
-25
lines changed

5 files changed

+186
-25
lines changed

config

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ directory_ssl : /www/ssl/
88
directory_acme : /www/acme/
99
directory_www : /www/www/
1010
directory_console : /www/logs/
11-
directory_dump : /www/
11+
directory_dump : /www/
12+
13+
allow_tms : false

definitions/superadmin.js

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,10 @@ SuperAdmin.sysinfo = function(callback) {
498498
callback && callback(null, SuperAdmin.server);
499499
MAIN.ws && MAIN.ws.send(SuperAdmin.server);
500500
SuperAdmin.notify_system();
501+
502+
var publish = CLONE(SuperAdmin.server);
503+
publish.dttms = NOW;
504+
PUBLISH('system_monitor', publish);
501505
});
502506

503507
};
@@ -969,6 +973,28 @@ SuperAdmin.save = function(callback, stats) {
969973
for (var i = 0; i < APPLICATIONS.length; i++) {
970974
var app = APPLICATIONS[i];
971975
if (app.current) {
976+
977+
var current = app.current;
978+
var obj = {};
979+
obj.id = app.id;
980+
obj.url = app.url;
981+
obj.name = app.name;
982+
obj.category = app.category;
983+
obj.note = app.note;
984+
obj.port = current.port;
985+
obj.pid = current.pid;
986+
obj.cpu = current.cpu;
987+
obj.memory = current.memory;
988+
obj.openfiles = current.openfiles;
989+
obj.connections = current.connections;
990+
obj.hdd = current.hdd;
991+
obj.version = app.version;
992+
obj.analyzator = app.analyzatoroutput;
993+
obj.ssl = app.ssl;
994+
obj.dtmonitor = NOW;
995+
obj.dttms = NOW;
996+
PUBLISH('apps_monitor', obj);
997+
972998
delete app.current.TYPE;
973999
delete app.current.id;
9741000
delete app.current.analyzator;
@@ -1359,23 +1385,36 @@ SuperAdmin.notify = function(app, type, callback) {
13591385
data.push('FILES: ' + app.current.openfiles);
13601386
data.push('ANALYZATOR: ' + (app.analyzatoroutput || 'none'));
13611387

1388+
var type = 'warning';
13621389
var message = item.message.format(app.url) + ' (' + data.join(', ') + ')';
13631390

1364-
LOGGER('alarms', item.name, message);
1365-
EMIT('superadmin_app_alarm', app, item, message);
1391+
SuperAdmin.check_notify(type, item.message, function(notified) {
1392+
1393+
if (notified) {
1394+
next();
1395+
return;
1396+
}
13661397

1367-
if (app.stats)
1368-
app.stats.alarms++;
1369-
else
1370-
app.stats.alarms = 1;
1398+
LOGGER('alarms', item.name, message);
1399+
EMIT('superadmin_app_alarm', app, item, message);
13711400

1372-
SuperAdmin.send_notify('warning', message);
1373-
NOSQL('alarms').modify({ '+notified': 1, dtnotified: NOW }).id(item.id);
1374-
item.phone && SuperAdmin.send_sms(item.phone, message.removeTags());
1375-
item.email && SuperAdmin.send_email(item.email, message, item.name);
1376-
}
1401+
if (app.stats)
1402+
app.stats.alarms++;
1403+
else
1404+
app.stats.alarms = 1;
13771405

1378-
next();
1406+
SuperAdmin.send_notify(type, message, item.message);
1407+
NOSQL('alarms').modify({ '+notified': 1, dtnotified: NOW }).id(item.id);
1408+
item.phone && SuperAdmin.send_sms(item.phone, message.removeTags());
1409+
item.email && SuperAdmin.send_email(item.email, message, item.name);
1410+
1411+
PUBLISH('notify_apps', { type: type, body: message, message: item.message, dtnotified: NOW, dttms: NOW });
1412+
1413+
next();
1414+
});
1415+
1416+
} else
1417+
next();
13791418

13801419
}, callback);
13811420
};
@@ -1407,19 +1446,38 @@ SuperAdmin.notify_system = function() {
14071446
data.push('RAM: ' + SuperAdmin.server.memused.filesize());
14081447
data.push('HDD: ' + SuperAdmin.server.hddused.filesize());
14091448

1449+
var type = 'warning';
14101450
var message = item.message + ' (' + data.join(', ') + ')';
14111451

1412-
LOGGER('alarms', item.name, message);
1413-
EMIT('superadmin_system_alarm', SuperAdmin.server, item, message);
1452+
SuperAdmin.check_notify(type, item.message, function(notified) {
14141453

1415-
SuperAdmin.send_notify('warning', message);
1416-
NOSQL('alarms').modify({ '+notified': 1, dtnotified: NOW }).id(item.id);
1417-
item.phone && SuperAdmin.send_sms(item.phone, message.removeTags());
1418-
item.email && SuperAdmin.send_email(item.email, message, item.name);
1419-
}
1454+
if (notified) {
1455+
next();
1456+
return;
1457+
}
14201458

1421-
next();
1459+
LOGGER('alarms', item.name, message);
1460+
EMIT('superadmin_system_alarm', SuperAdmin.server, item, message);
1461+
1462+
SuperAdmin.send_notify(type, message, item.message);
1463+
NOSQL('alarms').modify({ '+notified': 1, dtnotified: NOW }).id(item.id);
1464+
item.phone && SuperAdmin.send_sms(item.phone, message.removeTags());
1465+
item.email && SuperAdmin.send_email(item.email, message, item.name);
1466+
1467+
PUBLISH('notify_system', { type: type, body: message, message: item.message, dtnotified: NOW, dttms: NOW });
1468+
1469+
next();
1470+
});
14221471

1472+
} else
1473+
next();
1474+
1475+
});
1476+
};
1477+
1478+
SuperAdmin.check_notify = function(type, message, callback) {
1479+
NOSQL('notifications').one().where('message', message).where('type', type).error(404).callback(function(err) {
1480+
callback(!err);
14231481
});
14241482
};
14251483

@@ -1436,14 +1494,17 @@ SuperAdmin.send_sms = function(numbers, message) {
14361494
};
14371495

14381496
SuperAdmin.send_email = function(addresses, message, name) {
1497+
if (!addresses || !addresses.length)
1498+
return;
1499+
14391500
var message = LOGMAIL(addresses[0], name, message);
14401501
for (var i = 1; i < addresses.length; i++)
14411502
message.to(addresses[i]);
14421503
};
14431504

1444-
SuperAdmin.send_notify = function(type, body) {
1505+
SuperAdmin.send_notify = function(type, body, message) {
14451506
PREF.set('notifications', (PREF.notifications || 0) + 1);
1446-
NOSQL('notifications').insert({ type: type, body: body, dtcreated: NOW });
1507+
NOSQL('notifications').insert({ type: type, body: body, message: message, dtcreated: NOW });
14471508
};
14481509

14491510
SuperAdmin.init();

definitions/tms.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var notify = 'type:String,body:String,message:String,dtnotified:Date,dttms:Date';
2+
var trigger = 'userid:String,username:String,ua:String,ip:String,dttms:Date';
3+
4+
NEWPUBLISH('apps_insert', 'Apps');
5+
NEWPUBLISH('apps_update', 'Apps');
6+
NEWPUBLISH('apps_remove', 'Apps');
7+
NEWPUBLISH('apps_restart', 'Apps');
8+
NEWPUBLISH('apps_restart_all', trigger);
9+
NEWPUBLISH('apps_stop', 'Apps');
10+
NEWPUBLISH('apps_stop_all', trigger);
11+
NEWPUBLISH('apps_monitor', 'apps_monitor');
12+
NEWPUBLISH('apps_analyzator', 'id:String,name:String,type:String,category:String,url:String,dterror:Date,dttms:Date');
13+
14+
NEWPUBLISH('notify_apps', notify);
15+
NEWPUBLISH('notify_system', notify);
16+
17+
NEWPUBLISH('system_monitor', 'ip:String,uptime:Number,processes:Number,hddtotal:Number,hddused:Number,hddfree:Number,memtotal:Number,memused:Number,memfree:Number,dttms:Date');
18+
19+
// Helper
20+
FUNC.tms = function($, data) {
21+
if (!data)
22+
return data;
23+
24+
data.ua = $.ua;
25+
data.ip = $.ip;
26+
data.dttms = NOW;
27+
28+
if ($.user) {
29+
data.userid = $.user.id;
30+
data.username = $.user.name;
31+
}
32+
33+
return data;
34+
};

jsonschemas/apps_monitor.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"$id": "https://www.totaljs.com/files/apps_monitor.json",
3+
"$schema": "https://json-schema.org/draft/2020-12/schema",
4+
"type": "object",
5+
"properties": {
6+
"id": { "type": "string" },
7+
"url": { "type": "string" },
8+
"name": { "type": "string" },
9+
"category": { "type": "string" },
10+
"port": { "type": "number" },
11+
"pid": { "type": "string" },
12+
"cpu": { "type": "number" },
13+
"memory": { "type": "number" },
14+
"openfiles": { "type": "number" },
15+
"hdd": { "type": "number" },
16+
"connections": { "type": "number" },
17+
"version": { "type": "string" },
18+
"analyzator": { "type": "string" },
19+
"ssl": { "type": "boolean" },
20+
"dtmonitor": { "type": "date" },
21+
"dttms": { "type": "date" }
22+
},
23+
"required": []
24+
}

schemas/apps.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ NEWSCHEMA('Apps', function(schema) {
3434
schema.define('highpriority', Boolean); // App with high priority
3535
schema.define('unixsocket', Boolean); // Enables unixsocket
3636

37+
// TMS - Fields are added only for TMS of this schema
38+
schema.jsonschema_define('userid', 'String');
39+
schema.jsonschema_define('username', 'String');
40+
schema.jsonschema_define('ua', 'String');
41+
schema.jsonschema_define('ip', 'String');
42+
schema.jsonschema_define('dtupdated', 'Date');
43+
schema.jsonschema_define('dttms', 'Date');
44+
3745
schema.setQuery(function($) {
3846
$.callback(APPLICATIONS);
3947
});
@@ -133,7 +141,20 @@ NEWSCHEMA('Apps', function(schema) {
133141
if (type) {
134142
item.analyzatoroutput !== type && EMIT('superadmin_app_analyzator', item);
135143
item.analyzatoroutput = type;
136-
output.push({ id: item.id, type: type, url: item.url });
144+
145+
var e = { id: item.id, type: type, url: item.url };
146+
output.push(e);
147+
148+
var publish = {};
149+
publish.id = e.id;
150+
publish.name = item.name;
151+
publish.category = item.category;
152+
publish.type = e.type;
153+
publish.url = e.url;
154+
publish.dterror = NOW;
155+
publish.dttms = NOW;
156+
PUBLISH('apps_analyzator', e);
157+
137158
} else
138159
item.analyzatoroutput = null;
139160

@@ -166,6 +187,8 @@ NEWSCHEMA('Apps', function(schema) {
166187
SuperAdmin.logger('restart: {0}', $, app);
167188
EMIT('superadmin_app_restart', app);
168189

190+
PUBLISH('apps_restart', FUNC.tms($, app));
191+
169192
app.current = null;
170193
app.analyzatoroutput = null;
171194

@@ -213,6 +236,8 @@ NEWSCHEMA('Apps', function(schema) {
213236
SuperAdmin.wsnotify('apps_restart');
214237
MAIN.restarting = true;
215238

239+
PUBLISH('apps_restart_all', FUNC.tms($, {}));
240+
216241
APPLICATIONS.wait(function(app, next) {
217242

218243
if (app.stopped) {
@@ -225,6 +250,8 @@ NEWSCHEMA('Apps', function(schema) {
225250
SuperAdmin.wsnotify('app_restart', app);
226251
SuperAdmin.restart(app.port, () => next());
227252

253+
PUBLISH('apps_restart', FUNC.tms($, app));
254+
228255
}, function() {
229256

230257
MAIN.restarting = false;
@@ -313,10 +340,15 @@ NEWSCHEMA('Apps', function(schema) {
313340
item.analyzatoroutput = app.analyzatoroutput;
314341
item.dtupdated = NOW;
315342

343+
PUBLISH('apps_update', FUNC.tms($, item));
344+
316345
} else {
317346
item.id = model.id = UID();
318347
item.dtcreated = NOW;
319348
model.restart = true;
349+
350+
PUBLISH('apps_insert', FUNC.tms($, item));
351+
320352
}
321353

322354
TASK('nginx/init', $.successful(function() {
@@ -358,6 +390,8 @@ NEWSCHEMA('Apps', function(schema) {
358390

359391
var app = APPLICATIONS[index];
360392

393+
PUBLISH('apps_remove', FUNC.tms($, app));
394+
361395
SuperAdmin.kill(app.port, function() {
362396

363397
var linker = app.linker;
@@ -404,6 +438,8 @@ NEWSCHEMA('Apps', function(schema) {
404438
SuperAdmin.logger('stop: {0}', $, app);
405439
SuperAdmin.kill(app.port, $.done());
406440

441+
PUBLISH('apps_stop', FUNC.tms($, app));
442+
407443
if (!app.stopped) {
408444
app.stopped = true;
409445
app.current = null;
@@ -426,6 +462,8 @@ NEWSCHEMA('Apps', function(schema) {
426462
SuperAdmin.wsnotify('apps_stop');
427463
SuperAdmin.logger('stops: all', $);
428464

465+
PUBLISH('apps_stop_all', FUNC.tms($, {}));
466+
429467
APPLICATIONS.wait(function(item, next) {
430468

431469
if (item.stopped) {
@@ -436,10 +474,12 @@ NEWSCHEMA('Apps', function(schema) {
436474
item.stopped = true;
437475
item.current = null;
438476

477+
PUBLISH('apps_stop', FUNC.tms($, item));
478+
439479
SuperAdmin.kill(item.port, function() {
440480
if (MAIN.ws) {
441481
var current = {};
442-
current.id = app.id;
482+
current.id = item.id;
443483
current.TYPE = 'appinfo';
444484
current.analyzator = null;
445485
MAIN.ws.send(current);

0 commit comments

Comments
 (0)