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

136 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-06-19 20:43:08 +03:00
'require fs';
2023-11-26 16:49:59 +03:00
'require rpc';
2020-06-19 20:43:08 +03:00
'require ui';
2023-11-10 02:39:58 +03:00
'require view.ruantiblock.log-widget 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-12-05 19:18:32 +03:00
viewName : 'ruantiblock',
2020-06-19 20:43:08 +03:00
2021-12-05 19:18:32 +03:00
title : _('Ruantiblock') + ' - ' + _('Log'),
2020-06-19 20:43:08 +03:00
2024-02-27 17:31:44 +03:00
logdRegexp : new RegExp(/^([^\s]{3}\s+[^\s]{3}\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}\s+\d{4})\s+([a-z]+)\.([a-z]+)\s+(.*)$/),
syslog_ngRegexp: new RegExp(/^([^\s]{3}\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2})\s+([^\s]+)\s+(.*)$/),
entryRegexp : null,
2021-03-15 21:02:38 +03:00
isLoggerChecked: false,
2021-11-04 18:57:08 +03:00
entriesHandler : null,
2021-03-15 21:02:38 +03:00
2024-02-27 17:31:44 +03:00
logger : null,
2023-12-02 01:17:40 +03:00
getLogHash : null,
2023-11-26 16:49:59 +03:00
2021-03-15 21:02:38 +03:00
// logd
2023-11-01 19:48:59 +03:00
logdHandler(strArray, lineNum) {
2021-03-15 21:02:38 +03:00
return [
2024-02-27 17:31:44 +03:00
lineNum, // # (Number)
strArray[1], // Timestamp (String)
null, // Host (String)
strArray[2], // Facility (String)
strArray[3], // Level (String)
this.htmlEntities(strArray[4]) || ' ', // Message (String)
2021-03-15 21:02:38 +03:00
];
},
// syslog-ng
2023-11-01 19:48:59 +03:00
syslog_ngHandler(strArray, lineNum) {
2024-02-27 17:31:44 +03:00
if(!(strArray[2] in this.logHosts)) {
this.logHosts[strArray[2]] = this.makeLogHostsDropdownItem(strArray[2]);
2021-03-26 23:49:38 +03:00
};
2021-03-15 21:02:38 +03:00
return [
2024-02-27 17:31:44 +03:00
lineNum, // # (Number)
strArray[1], // Timestamp (String)
strArray[2], // Host (String)
null, // Facility (String)
null, // Level (String)
this.htmlEntities(strArray[3]) || ' ', // Message (String)
2021-03-15 21:02:38 +03:00
];
},
2023-12-02 01:17:40 +03:00
checkLogread() {
2021-03-11 18:24:24 +03:00
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;
if(logger) {
2023-12-02 01:17:40 +03:00
this.logger = logger;
} else {
throw new Error(_('Logread not found'));
2021-03-11 18:24:24 +03:00
};
});
},
2023-12-02 01:17:40 +03:00
async getLogData(tail, extraTstamp=false) {
if(!this.logger) {
await this.checkLogread();
};
let loggerArgs = [];
loggerArgs.push('-e', tools.appName + ':');
if(extraTstamp) {
loggerArgs.push('-t');
};
return fs.exec_direct(this.logger, loggerArgs, 'text').catch(err => {
throw new Error(_('Unable to load log data:') + ' ' + err.message);
});
},
2023-11-01 19:48:59 +03:00
parseLogData(logdata, tail) {
2021-03-11 18:24:24 +03:00
if(!logdata) {
return [];
};
2023-11-01 19:48:59 +03:00
let unsupportedLog = false;
let strings = logdata.trim().split(/\n/);
2021-03-11 18:24:24 +03:00
if(tail && tail > 0 && strings) {
strings = strings.slice(-tail);
};
this.totalLogLines = strings.length;
2023-11-01 19:48:59 +03:00
let entriesArray = strings.map((e, i) => {
2021-03-15 21:02:38 +03:00
if(!this.isLoggerChecked) {
2024-02-27 17:31:44 +03:00
if(this.logdRegexp.test(e)) {
this.entryRegexp = this.logdRegexp;
2023-11-01 19:48:59 +03:00
this.isFacilities = true;
this.isLevels = true;
this.logHosts = {};
this.entriesHandler = this.logdHandler;
}
2024-02-27 17:31:44 +03:00
else if(this.syslog_ngRegexp.test(e)) {
this.entryRegexp = this.syslog_ngRegexp;
2021-12-05 19:18:32 +03:00
this.isHosts = true;
2023-11-01 19:48:59 +03:00
this.logFacilities = {};
2021-12-05 19:18:32 +03:00
this.logLevels = {};
2021-03-15 21:02:38 +03:00
this.entriesHandler = this.syslog_ngHandler;
} else {
2023-11-01 19:48:59 +03:00
unsupportedLog = true;
return;
2021-03-15 21:02:38 +03:00
};
this.isLoggerChecked = true;
};
2024-02-27 17:31:44 +03:00
let strArray = e.match(this.entryRegexp);
if(strArray) {
return this.entriesHandler(strArray, i + 1);
} else {
unsupportedLog = true;
return;
};
2021-03-11 18:24:24 +03:00
});
2023-11-01 19:48:59 +03:00
if(unsupportedLog) {
2023-12-02 01:17:40 +03:00
throw new Error(_('Unable to load log data:') + ' ' + _('Unsupported log format'));
2023-11-01 19:48:59 +03:00
} else {
if(this.logSortingValue === 'desc') {
entriesArray.reverse();
};
return entriesArray;
};
2021-03-11 18:24:24 +03:00
},
2020-06-19 20:43:08 +03:00
});