Skip to content

Commit

Permalink
TMS integration + small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Helferino committed Jun 11, 2021
1 parent 46b57ff commit 8696bec
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 25 deletions.
4 changes: 3 additions & 1 deletion config
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ directory_ssl : /www/ssl/
directory_acme : /www/acme/
directory_www : /www/www/
directory_console : /www/logs/
directory_dump : /www/
directory_dump : /www/

allow_tms : false
105 changes: 83 additions & 22 deletions definitions/superadmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ SuperAdmin.sysinfo = function(callback) {
callback && callback(null, SuperAdmin.server);
MAIN.ws && MAIN.ws.send(SuperAdmin.server);
SuperAdmin.notify_system();

var publish = CLONE(SuperAdmin.server);
publish.dttms = NOW;
PUBLISH('system_monitor', publish);
});

};
Expand Down Expand Up @@ -969,6 +973,28 @@ SuperAdmin.save = function(callback, stats) {
for (var i = 0; i < APPLICATIONS.length; i++) {
var app = APPLICATIONS[i];
if (app.current) {

var current = app.current;
var obj = {};
obj.id = app.id;
obj.url = app.url;
obj.name = app.name;
obj.category = app.category;
obj.note = app.note;
obj.port = current.port;
obj.pid = current.pid;
obj.cpu = current.cpu;
obj.memory = current.memory;
obj.openfiles = current.openfiles;
obj.connections = current.connections;
obj.hdd = current.hdd;
obj.version = app.version;
obj.analyzator = app.analyzatoroutput;
obj.ssl = app.ssl;
obj.dtmonitor = NOW;
obj.dttms = NOW;
PUBLISH('apps_monitor', obj);

delete app.current.TYPE;
delete app.current.id;
delete app.current.analyzator;
Expand Down Expand Up @@ -1359,23 +1385,36 @@ SuperAdmin.notify = function(app, type, callback) {
data.push('FILES: ' + app.current.openfiles);
data.push('ANALYZATOR: ' + (app.analyzatoroutput || 'none'));

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

LOGGER('alarms', item.name, message);
EMIT('superadmin_app_alarm', app, item, message);
SuperAdmin.check_notify(type, item.message, function(notified) {

if (notified) {
next();
return;
}

if (app.stats)
app.stats.alarms++;
else
app.stats.alarms = 1;
LOGGER('alarms', item.name, message);
EMIT('superadmin_app_alarm', app, item, message);

SuperAdmin.send_notify('warning', message);
NOSQL('alarms').modify({ '+notified': 1, dtnotified: NOW }).id(item.id);
item.phone && SuperAdmin.send_sms(item.phone, message.removeTags());
item.email && SuperAdmin.send_email(item.email, message, item.name);
}
if (app.stats)
app.stats.alarms++;
else
app.stats.alarms = 1;

next();
SuperAdmin.send_notify(type, message, item.message);
NOSQL('alarms').modify({ '+notified': 1, dtnotified: NOW }).id(item.id);
item.phone && SuperAdmin.send_sms(item.phone, message.removeTags());
item.email && SuperAdmin.send_email(item.email, message, item.name);

PUBLISH('notify_apps', { type: type, body: message, message: item.message, dtnotified: NOW, dttms: NOW });

next();
});

} else
next();

}, callback);
};
Expand Down Expand Up @@ -1407,19 +1446,38 @@ SuperAdmin.notify_system = function() {
data.push('RAM: ' + SuperAdmin.server.memused.filesize());
data.push('HDD: ' + SuperAdmin.server.hddused.filesize());

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

LOGGER('alarms', item.name, message);
EMIT('superadmin_system_alarm', SuperAdmin.server, item, message);
SuperAdmin.check_notify(type, item.message, function(notified) {

SuperAdmin.send_notify('warning', message);
NOSQL('alarms').modify({ '+notified': 1, dtnotified: NOW }).id(item.id);
item.phone && SuperAdmin.send_sms(item.phone, message.removeTags());
item.email && SuperAdmin.send_email(item.email, message, item.name);
}
if (notified) {
next();
return;
}

next();
LOGGER('alarms', item.name, message);
EMIT('superadmin_system_alarm', SuperAdmin.server, item, message);

SuperAdmin.send_notify(type, message, item.message);
NOSQL('alarms').modify({ '+notified': 1, dtnotified: NOW }).id(item.id);
item.phone && SuperAdmin.send_sms(item.phone, message.removeTags());
item.email && SuperAdmin.send_email(item.email, message, item.name);

PUBLISH('notify_system', { type: type, body: message, message: item.message, dtnotified: NOW, dttms: NOW });

next();
});

} else
next();

});
};

SuperAdmin.check_notify = function(type, message, callback) {
NOSQL('notifications').one().where('message', message).where('type', type).error(404).callback(function(err) {
callback(!err);
});
};

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

SuperAdmin.send_email = function(addresses, message, name) {
if (!addresses || !addresses.length)
return;

var message = LOGMAIL(addresses[0], name, message);
for (var i = 1; i < addresses.length; i++)
message.to(addresses[i]);
};

SuperAdmin.send_notify = function(type, body) {
SuperAdmin.send_notify = function(type, body, message) {
PREF.set('notifications', (PREF.notifications || 0) + 1);
NOSQL('notifications').insert({ type: type, body: body, dtcreated: NOW });
NOSQL('notifications').insert({ type: type, body: body, message: message, dtcreated: NOW });
};

SuperAdmin.init();
Expand Down
34 changes: 34 additions & 0 deletions definitions/tms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var notify = 'type:String,body:String,message:String,dtnotified:Date,dttms:Date';
var trigger = 'userid:String,username:String,ua:String,ip:String,dttms:Date';

NEWPUBLISH('apps_insert', 'Apps');
NEWPUBLISH('apps_update', 'Apps');
NEWPUBLISH('apps_remove', 'Apps');
NEWPUBLISH('apps_restart', 'Apps');
NEWPUBLISH('apps_restart_all', trigger);
NEWPUBLISH('apps_stop', 'Apps');
NEWPUBLISH('apps_stop_all', trigger);
NEWPUBLISH('apps_monitor', 'apps_monitor');
NEWPUBLISH('apps_analyzator', 'id:String,name:String,type:String,category:String,url:String,dterror:Date,dttms:Date');

NEWPUBLISH('notify_apps', notify);
NEWPUBLISH('notify_system', notify);

NEWPUBLISH('system_monitor', 'ip:String,uptime:Number,processes:Number,hddtotal:Number,hddused:Number,hddfree:Number,memtotal:Number,memused:Number,memfree:Number,dttms:Date');

// Helper
FUNC.tms = function($, data) {
if (!data)
return data;

data.ua = $.ua;
data.ip = $.ip;
data.dttms = NOW;

if ($.user) {
data.userid = $.user.id;
data.username = $.user.name;
}

return data;
};
24 changes: 24 additions & 0 deletions jsonschemas/apps_monitor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$id": "https://www.totaljs.com/files/apps_monitor.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"id": { "type": "string" },
"url": { "type": "string" },
"name": { "type": "string" },
"category": { "type": "string" },
"port": { "type": "number" },
"pid": { "type": "string" },
"cpu": { "type": "number" },
"memory": { "type": "number" },
"openfiles": { "type": "number" },
"hdd": { "type": "number" },
"connections": { "type": "number" },
"version": { "type": "string" },
"analyzator": { "type": "string" },
"ssl": { "type": "boolean" },
"dtmonitor": { "type": "date" },
"dttms": { "type": "date" }
},
"required": []
}
44 changes: 42 additions & 2 deletions schemas/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ NEWSCHEMA('Apps', function(schema) {
schema.define('highpriority', Boolean); // App with high priority
schema.define('unixsocket', Boolean); // Enables unixsocket

// TMS - Fields are added only for TMS of this schema
schema.jsonschema_define('userid', 'String');
schema.jsonschema_define('username', 'String');
schema.jsonschema_define('ua', 'String');
schema.jsonschema_define('ip', 'String');
schema.jsonschema_define('dtupdated', 'Date');
schema.jsonschema_define('dttms', 'Date');

schema.setQuery(function($) {
$.callback(APPLICATIONS);
});
Expand Down Expand Up @@ -133,7 +141,20 @@ NEWSCHEMA('Apps', function(schema) {
if (type) {
item.analyzatoroutput !== type && EMIT('superadmin_app_analyzator', item);
item.analyzatoroutput = type;
output.push({ id: item.id, type: type, url: item.url });

var e = { id: item.id, type: type, url: item.url };
output.push(e);

var publish = {};
publish.id = e.id;
publish.name = item.name;
publish.category = item.category;
publish.type = e.type;
publish.url = e.url;
publish.dterror = NOW;
publish.dttms = NOW;
PUBLISH('apps_analyzator', e);

} else
item.analyzatoroutput = null;

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

PUBLISH('apps_restart', FUNC.tms($, app));

app.current = null;
app.analyzatoroutput = null;

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

PUBLISH('apps_restart_all', FUNC.tms($, {}));

APPLICATIONS.wait(function(app, next) {

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

PUBLISH('apps_restart', FUNC.tms($, app));

}, function() {

MAIN.restarting = false;
Expand Down Expand Up @@ -313,10 +340,15 @@ NEWSCHEMA('Apps', function(schema) {
item.analyzatoroutput = app.analyzatoroutput;
item.dtupdated = NOW;

PUBLISH('apps_update', FUNC.tms($, item));

} else {
item.id = model.id = UID();
item.dtcreated = NOW;
model.restart = true;

PUBLISH('apps_insert', FUNC.tms($, item));

}

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

var app = APPLICATIONS[index];

PUBLISH('apps_remove', FUNC.tms($, app));

SuperAdmin.kill(app.port, function() {

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

PUBLISH('apps_stop', FUNC.tms($, app));

if (!app.stopped) {
app.stopped = true;
app.current = null;
Expand All @@ -426,6 +462,8 @@ NEWSCHEMA('Apps', function(schema) {
SuperAdmin.wsnotify('apps_stop');
SuperAdmin.logger('stops: all', $);

PUBLISH('apps_stop_all', FUNC.tms($, {}));

APPLICATIONS.wait(function(item, next) {

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

PUBLISH('apps_stop', FUNC.tms($, item));

SuperAdmin.kill(item.port, function() {
if (MAIN.ws) {
var current = {};
current.id = app.id;
current.id = item.id;
current.TYPE = 'appinfo';
current.analyzator = null;
MAIN.ws.send(current);
Expand Down

0 comments on commit 8696bec

Please sign in to comment.