Files
ruantiblock_openwrt/luci-app-ruantiblock/htdocs/luci-static/resources/view/ruantiblock/log.js
T

106 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-06-19 20:43:08 +03:00
'require fs';
'require ui';
2021-03-26 23:49:38 +03:00
'require view.log.abstract-log as abc';
2020-06-19 20:43:08 +03:00
'require view.ruantiblock.tools as tools';
2021-03-26 23:49:38 +03:00
return abc.view.extend({
2021-03-15 21:02:38 +03:00
viewName: 'ruantiblock',
2020-06-19 20:43:08 +03:00
2021-03-11 18:24:24 +03:00
title: _('Ruantiblock') + ' - ' + _('Log'),
2020-06-19 20:43:08 +03:00
2021-03-15 21:02:38 +03:00
appRegexp: new RegExp(`^.*${tools.app_name}\[[0-9]+\].*$`, 'gm'),
testRegexp: new RegExp(/([0-9]{2}:){2}[0-9]{2}/),
isLoggerChecked: false,
entriesHandler: null,
// logd
logdHandler: function(strArray, lineNum) {
let logLevel = strArray[5].split('.');
return [
lineNum, // # (Number)
strArray.slice(0, 5).join(' '), // Timestamp (String)
2021-03-26 23:49:38 +03:00
null, // Host (String)
2021-03-15 21:02:38 +03:00
logLevel[1], // Level (String)
logLevel[0], // Facility (String)
this.htmlEntities(strArray.slice(6).join(' ')), // Message (String)
];
},
// syslog-ng
syslog_ngHandler: function(strArray, lineNum) {
2021-03-26 23:49:38 +03:00
if(!(strArray[3] in this.logHosts)) {
this.logHosts[strArray[3]] = this.makelogHostsDropdownItem(strArray[3]);
};
2021-03-15 21:02:38 +03:00
return [
lineNum, // # (Number)
strArray.slice(0, 3).join(' '), // Timestamp (String)
2021-03-26 23:49:38 +03:00
strArray[3], // Host (String)
2021-03-15 21:02:38 +03:00
null, // Level (String)
null, // Facility (String)
this.htmlEntities(strArray.slice(4).join(' ')), // Message (String)
];
},
2021-03-11 18:24:24 +03:00
getLogData: function(tail) {
return Promise.all([
L.resolveDefault(fs.stat('/sbin/logread'), null),
L.resolveDefault(fs.stat('/usr/sbin/logread'), null),
]).then(stat => {
let logger = (stat[0]) ? stat[0].path : (stat[1]) ? stat[1].path : null;
2020-06-19 20:43:08 +03:00
2021-03-11 18:24:24 +03:00
if(logger) {
2021-03-15 21:02:38 +03:00
return fs.exec_direct(logger, [ '-e', tools.app_name ]).catch(err => {
2021-03-26 23:49:38 +03:00
ui.addNotification(null, E('p', {}, _('Unable to load log data:') + ' ' + err.message));
2021-03-11 18:24:24 +03:00
return '';
});
};
});
},
parseLogData: function(logdata, tail) {
if(!logdata) {
return [];
};
2021-03-15 21:02:38 +03:00
let strings = logdata.trim().match(this.appRegexp) || [];
2021-03-11 18:24:24 +03:00
if(tail && tail > 0 && strings) {
strings = strings.slice(-tail);
};
this.totalLogLines = strings.length;
let entriesArray = strings.map((e, i) => {
let strArray = e.split(/\s+/);
2021-03-15 21:02:38 +03:00
if(!this.isLoggerChecked) {
/**
* Checking the third field of a line.
* If it contains time then syslog-ng.
*/
if(this.testRegexp.test(strArray[2])) {
2021-03-27 18:35:35 +03:00
this.isHosts = true;
2021-03-15 21:02:38 +03:00
this.logLevels = {};
this.entriesHandler = this.syslog_ngHandler;
} else {
2021-03-27 18:35:35 +03:00
this.isLevels = true;
2021-03-15 21:02:38 +03:00
this.entriesHandler = this.logdHandler;
};
this.isLoggerChecked = true;
};
return this.entriesHandler(strArray, i + 1);
2021-03-11 18:24:24 +03:00
});
if(this.logSortingValue === 'desc') {
entriesArray.reverse();
};
return entriesArray;
},
2020-06-19 20:43:08 +03:00
});