From cbd3beeb7dac85c50c86774c9d71c198563c5133 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Thu, 28 Nov 2019 16:13:28 +0100 Subject: [PATCH] log: complete new log module for https://github.com/opnsense/core/issues/3831 - add module (core is standard log directory) - add clear endpoint (/api/diagnostics/log/{module}/{file}/clear - parse various date formats into iso dates for the frontend --- .../Diagnostics/Api/LogController.php | 53 ++++++++------ .../OPNsense/Diagnostics/LogController.php | 7 +- .../app/views/OPNsense/Diagnostics/log.volt | 43 +++++++++++- src/opnsense/scripts/systemhealth/clearlog | 70 +++++++++++++++++++ src/opnsense/scripts/systemhealth/queryLog.py | 61 +++++++++++++--- .../conf/actions.d/actions_system.conf | 8 ++- 6 files changed, 207 insertions(+), 35 deletions(-) create mode 100755 src/opnsense/scripts/systemhealth/clearlog diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/Api/LogController.php b/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/Api/LogController.php index e29b2cd64..1bd7e364d 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/Api/LogController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/Api/LogController.php @@ -40,31 +40,40 @@ class LogController extends ApiControllerBase { public function __call($name, $arguments) { + $module = substr($name, 0, strlen($name)-6); + $scope = count($arguments) > 0 ? $arguments[0] : ""; + $action = count($arguments) > 1 ? $arguments[1] : ""; if ($this->request->isPost() && substr($name, -6) == 'Action') { $this->sessionClose(); - // create filter to sanitize input data - $filter = new Filter(); - $filter->add('query', new QueryFilter()); - - // fetch query parameters (limit results to prevent out of memory issues) - $itemsPerPage = $this->request->getPost('rowCount', 'int', 9999); - $currentPage = $this->request->getPost('current', 'int', 1); - - if ($this->request->getPost('searchPhrase', 'string', '') != "") { - $searchPhrase = $filter->sanitize($this->request->getPost('searchPhrase'), "query"); - } else { - $searchPhrase = ''; - } - $backend = new Backend(); - $response = $backend->configdpRun("system diag log", array($itemsPerPage, - ($currentPage-1)*$itemsPerPage, $searchPhrase, substr($name,0, strlen($name)-6))); - $result = json_decode($response, true); - if ($result != null) { - $result['rowCount'] = count($result['rows']); - $result['total'] = $result['total_rows']; - $result['current'] = (int)$currentPage; - return $result; + if ($action == "clear") { + $backend->configdpRun("system clear log", array($module, $scope)); + return ["status" => "ok"]; + } else { + // create filter to sanitize input data + $filter = new Filter(); + $filter->add('query', new QueryFilter()); + + // fetch query parameters (limit results to prevent out of memory issues) + $itemsPerPage = $this->request->getPost('rowCount', 'int', 9999); + $currentPage = $this->request->getPost('current', 'int', 1); + + if ($this->request->getPost('searchPhrase', 'string', '') != "") { + $searchPhrase = $filter->sanitize($this->request->getPost('searchPhrase'), "query"); + } else { + $searchPhrase = ''; + } + + $response = $backend->configdpRun("system diag log", array($itemsPerPage, + ($currentPage-1)*$itemsPerPage, $searchPhrase, $module, $scope) + ); + $result = json_decode($response, true); + if ($result != null) { + $result['rowCount'] = count($result['rows']); + $result['total'] = $result['total_rows']; + $result['current'] = (int)$currentPage; + return $result; + } } } return array(); diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/LogController.php b/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/LogController.php index be4167dd5..b1a277601 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/LogController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/LogController.php @@ -37,16 +37,19 @@ use OPNsense\Base\IndexController; */ class LogController extends IndexController { - public function indexAction($scope) + public function renderPage($module, $scope) { $this->view->pick('OPNsense/Diagnostics/log'); + $this->view->module = $module; $this->view->scope = $scope; } public function __call($name, $arguments) { if (substr($name, -6) == 'Action') { - return $this->indexAction(substr($name,0, strlen($name)-6)); + $scope = count($arguments) > 0 ? $arguments[0] : "core"; + $module = substr($name,0, strlen($name)-6); + return $this->renderPage($module, $scope); } } } diff --git a/src/opnsense/mvc/app/views/OPNsense/Diagnostics/log.volt b/src/opnsense/mvc/app/views/OPNsense/Diagnostics/log.volt index ce7161ae8..650798b03 100644 --- a/src/opnsense/mvc/app/views/OPNsense/Diagnostics/log.volt +++ b/src/opnsense/mvc/app/views/OPNsense/Diagnostics/log.volt @@ -29,7 +29,36 @@ POSSIBILITY OF SUCH DAMAGE. @@ -41,6 +70,7 @@ POSSIBILITY OF SUCH DAMAGE. # + {{ lang._('Date') }} {{ lang._('Line') }} @@ -49,6 +79,17 @@ POSSIBILITY OF SUCH DAMAGE. + + + + + + +
+ +
diff --git a/src/opnsense/scripts/systemhealth/clearlog b/src/opnsense/scripts/systemhealth/clearlog new file mode 100755 index 000000000..2c41f667e --- /dev/null +++ b/src/opnsense/scripts/systemhealth/clearlog @@ -0,0 +1,70 @@ +#!/usr/local/bin/php += offset: + record['timestamp'] = None + if len(record['line']) > 15 and \ + re.match(r'(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)', record['line'][7:15]): + # syslog format, strip timestamp and return actual log data + ts = datetime.datetime.strptime(record['line'][0:15], "%b %d %H:%M:%S") + ts = ts.replace(year=startup_timestamp.year) + if (startup_timestamp - ts).days < 0: + # likely previous year, (month for this year not reached yet) + ts = ts.replace(year=ts.year - 1) + record['timestamp'] = ts.isoformat() + # strip timestamp from log line + record['line'] = record['line'][16:] + # strip hostname from log line + record['line'] = record['line'][record['line'].find(' ')+1:].strip() + elif len(record['line']) > 15 and record['line'][0:10].isdigit() and \ + record['line'][10] == '.' and record['line'][11:13].isdigit(): + # looks like an epoch + ts = datetime.datetime.fromtimestamp(float(record['line'][0:13])) + record['timestamp'] = ts.isoformat() + # strip timestamp + record['line'] = record['line'][14:].strip() + elif re.match(squid_ext_timeformat, record['line']): + tmp = re.match(squid_ext_timeformat, record['line']) + grp = tmp.group(1) + ts = datetime.datetime.strptime(grp[1:].split()[0], "%d/%b/%Y:%H:%M:%S") + record['timestamp'] = ts.isoformat() + # strip timestamp + record['line'] = record['line'].replace(grp, '') result['rows'].append(record) elif result['total_rows'] > offset + limit: # do not fetch data until end of file... diff --git a/src/opnsense/service/conf/actions.d/actions_system.conf b/src/opnsense/service/conf/actions.d/actions_system.conf index 8b34461f6..d059d6396 100644 --- a/src/opnsense/service/conf/actions.d/actions_system.conf +++ b/src/opnsense/service/conf/actions.d/actions_system.conf @@ -6,10 +6,16 @@ message:Show system activity [diag.log] command:/usr/local/opnsense/scripts/systemhealth/queryLog.py -parameters:/limit %s /offset %s /filter %s /filename %s +parameters:--limit %s --offset %s --filter %s --module %s --filename %s type:script_output message:Show log +[clear.log] +command:/usr/local/opnsense/scripts/systemhealth/clearlog +parameters:-m%s -f%s +type:script +message:clear log (%s %s) + [list.interrupts] command:/usr/local/opnsense/scripts/system/list_interrupts.py parameters:%s