Merge pull request #1075 from opnsense/ngx_log_performance

www/nginx: backend performance improvements
This commit is contained in:
Fabian Franz BSc
2018-12-17 19:42:33 +01:00
committed by GitHub
3 changed files with 53 additions and 16 deletions
@@ -42,7 +42,7 @@ class LogsController extends ApiControllerBase
return $this->list_vhosts();
} else {
// emulate REST call for a specific log /accesses/uuid
return $this->call_configd('access', $uuid);
$this->call_configd('access', $uuid);
}
}
@@ -54,9 +54,10 @@ class LogsController extends ApiControllerBase
return $this->list_vhosts();
} else {
// emulate REST call for a specific log /errors/uuid
return $this->call_configd('error', $uuid);
$this->call_configd('error', $uuid);
}
}
public function stream_accessesAction($uuid = null)
{
$this->nginx = new Nginx();
@@ -65,9 +66,10 @@ class LogsController extends ApiControllerBase
return $this->list_streams();
} else {
// emulate REST call for a specific log /stream_accesses/uuid
return $this->call_configd_stream('streamaccess', $uuid);
$this->call_configd_stream('streamaccess', $uuid);
}
}
public function stream_errorsAction($uuid = null)
{
$this->nginx = new Nginx();
@@ -76,19 +78,18 @@ class LogsController extends ApiControllerBase
return $this->list_streams();
} else {
// emulate REST call for a specific log /stream_errors/uuid
return $this->call_configd_stream('streamerror', $uuid);
$this->call_configd_stream('streamerror', $uuid);
}
}
private function call_configd($type, $uuid)
{
if (!$this->vhost_exists($uuid)) {
$this->response->setStatusCode(404, "Not Found");
}
$backend = new Backend();
$data = $backend->configdRun('nginx log ' . $type . ' ' . $uuid);
return json_decode($data, true);
return $this->sendConfigdToClient('nginx log ' . $type . ' ' . $uuid);
}
private function call_configd_stream($type, $uuid)
{
@@ -96,9 +97,7 @@ class LogsController extends ApiControllerBase
$this->response->setStatusCode(404, "Not Found");
}
$backend = new Backend();
$data = $backend->configdRun('nginx log ' . $type . ' ' . $uuid);
return json_decode($data, true);
return $this->sendConfigdToClient('nginx log ' . $type . ' ' . $uuid);
}
private function list_vhosts()
@@ -128,4 +127,19 @@ class LogsController extends ApiControllerBase
$data = $this->nginx->getNodeByReference('stream_server.'. $uuid);
return isset($data);
}
/**
* @param $command String JSON generating configd command
* @return null
* @throws \Exception ?
*/
private function sendConfigdToClient($command)
{
$backend = new Backend();
// must be passed directly -> OOM Problem
$this->response->setContent($backend->configdRun($command));
$this->response->setStatusCode(200, "OK");
$this->response->setContentType('application/json', 'UTF-8');
return $this->response->send();
}
}
@@ -31,7 +31,6 @@ namespace OPNsense\Nginx;
class AccessLogParser
{
private $file_name;
private $lines;
private $result;
private const LogLineRegex = '/(\S+) - (\S+) \[([\d\sa-z\:\-\/\+]+)\] "([^"]+?)" (\d+) (\d+) "([^"]*?)" "([^"]*?)" "([^"]*?)"/i';
@@ -39,9 +38,21 @@ class AccessLogParser
function __construct($file_name)
{
$this->file_name = $file_name;
$this->lines = file($this->file_name);
$this->result = array_map([$this, 'parse_line'], $this->lines);
$this->result = array();
$this->parse_file();
}
private function parse_file()
{
$handle = @fopen($this->file_name, 'r');
if ($handle) {
while (($buffer = fgets($handle)) !== false) {
$this->result[] = $this->parse_line($buffer);
}
fclose($handle);
}
}
private function parse_line($line)
{
$container = new AccessLogLine();
@@ -31,17 +31,29 @@ namespace OPNsense\Nginx;
class StreamAccessLogParser
{
private $file_name;
private $lines;
private $result;
private const LogLineRegex = '/(\S+) \[([\d\sa-z\:\-\/\+]+)\] (\S+?) (\d+) (\d+) (\d+) (\d+(?:\.\d+)?)/i';
function __construct($file_name)
{
$this->file_name = $file_name;
$this->lines = file($this->file_name);
$this->result = array_map([$this, 'parse_line'], $this->lines);
$this->result = array();
$this->parse_file();
}
private function parse_file()
{
$handle = @fopen($this->file_name, 'r');
if ($handle) {
while (($buffer = fgets($handle)) !== false) {
$this->result[] = $this->parse_line($buffer);
}
fclose($handle);
}
}
private function parse_line($line)
{
$container = new StreamAccessLogLine();