Initial commit

refs #4514
This commit is contained in:
Alexander Klimov 2014-03-19 16:57:11 +01:00
parent 2d8cd6ae6c
commit 933a851f65
6 changed files with 346 additions and 2 deletions

View File

@ -56,8 +56,7 @@ class Datasource implements DatasourceInterface
public function fetchAll(Query $query)
{
$result = $this->getResult($query);
return $result;
return $this->getResult($query);
}
public function count(Query $query)

View File

@ -37,6 +37,7 @@ use Icinga\Data\Db\Connection as DbConnection;
use Icinga\Protocol\Livestatus\Connection as LivestatusConnection;
use Icinga\Protocol\Statusdat\Reader as StatusdatReader;
use Icinga\Protocol\Ldap\Connection as LdapConnection;
use Icinga\Protocol\File\Reader as FileReader;
class ResourceFactory implements ConfigAwareFactory
{
@ -133,6 +134,9 @@ class ResourceFactory implements ConfigAwareFactory
case 'livestatus':
$resource = new LivestatusConnection($config->socket);
break;
case 'file':
$resource = new FileReader($config);
break;
default:
throw new ConfigurationError('Unsupported resource type "' . $config->type . '"');
}

View File

@ -0,0 +1,42 @@
<?php
namespace Icinga\Protocol\File;
use Icinga\Data\BaseQuery;
class Query extends BaseQuery
{
private $sortDir;
private $filters = array();
public function applyFilter()
{}// ?
public function count()
{
return $this->ds->count();
}
public function order($dir)
{
$this->sortDir = ($dir === null || strtoupper(trim($dir)) === 'DESC') ? self::SORT_DESC : self::SORT_ASC;
return $this;
}
public function sortDesc()
{
return $this->sortDir === self::SORT_DESC;
}
public function where($expression)
{
$this->filters[] = $expression;
return $this;
}
public function getFilters()
{
return $this->filters;
}
}

View File

@ -0,0 +1,208 @@
<?php
namespace Icinga\Protocol\File;
use Icinga\Data\DatasourceInterface;
class Reader implements DatasourceInterface
{
private $filename;
private $config;
private $queryCache = null;
public function __construct($config)
{
$this->config = $config;
$this->filename = $config->filename;
var_dump($this->readFromStart());//NO!
//var_dump($this->fetchOne(new Query($this)));
die;//NO!
}
public function select()
{
return new Query($this);
}
public function fetchAll(Query $query)
{
$all = array();
foreach ($this->fetchPairs($query) as $index => $value) {
$all[$index] = new \stdClass();
foreach ($value as $key => $value_2) {
$all[$index]->{$key} = $value_2;
}
}
return $all;
}
public function fetchRow(Query $query)
{
$all = $this->fetchAll($query);
if (isset($all[0])) {
return $all[0];
}
return null;
}
public function fetchColumn(Query $query)
{
$column = array();
foreach ($this->fetchPairs($query) as $value) {
foreach ($value as $value_2) {
$column[] = $value_2;
break;
}
}
return $column;
}
public function fetchOne(Query $query)
{
$pairs = $this->fetchPairs($query);
if (isset($pairs[0])) {
foreach ($pairs[0] as $value) {
return $value;
}
}
return null;
}
public function fetchPairs(Query $query)
{
return $this->read($query);
}
public function validateLine($line)
{
$data = array();
$PCRE_result = @preg_match($this->config->fields, $line, $data);
if ($PCRE_result === false) {
throw new \Exception('Failed parsing regular expression!');
} else if ($PCRE_result === 1) {
foreach ($this->queryCache->getFilters() as $filter) {
if (strpos($line, $filter) === false) {
return false;
}
}
foreach ($data as $key => $value) {
if (is_int($key)) {
unset($data[$key]);
}
}
return $data;
}
return false;
}
public function read(Query $query)
{
$skip_lines = $query->getOffset();
$read_lines = $query->getLimit();
if ($skip_lines === null) {
$skip_lines = 0;
}
$this->queryCache = $query;
if ($query->sortDesc()) {
$data = $this->readFromEnd($skip_lines, $read_lines);
} else {
$data = $this->readFromStart($skip_lines, $read_lines);
}
$this->queryCache = null;
return $data;
}
public function readFromEnd($skip_lines = null, $read_lines = null)
{
$PHP_EOL_len = strlen(PHP_EOL);
$lines = array();
$s = '';
$f = fopen($this->filename, 'rb');
fseek($f, 0, SEEK_END);
$pos = ftell($f);
while ($read_lines === null || count($lines) < $read_lines) {
fseek($f, --$pos);
$c = fgetc($f);
if ($c === false || $pos < 0) {
$l = $this->validateLine($s);
if (!($l === false || $skip_lines)) {
$lines[] = $l;
}
break;
}
$s = $c . $s;
if (strpos($s, PHP_EOL) === 0) {
$l = $this->validateLine((string)substr($s, $PHP_EOL_len));
if ($l !== false) {
if ($skip_lines) {
$skip_lines--;
} else {
$lines[] = $l;
}
}
$s = '';
}
}
return $lines;
}
public function readFromStart($skip_lines = null, $read_lines = null)
{
$PHP_EOL_len = strlen(PHP_EOL);
$lines = array();
$s = '';
$f = fopen($this->filename, 'rb');
while ($read_lines === null || count($lines) < $read_lines) {
$c = fgetc($f);
if ($c === false) {
$l = $this->validateLine($s);
if (!($l === false || $skip_lines)) {
$lines[] = $l;
}
break;
}
$s .= $c;
if (strpos($s, PHP_EOL) !== false) {
$l = $this->validateLine((string)substr($s, 0, strlen($s) - $PHP_EOL_len));
if ($l !== false) {
if ($skip_lines) {
$skip_lines--;
} else {
$lines[] = $l;
}
}
$s = '';
}
}
return $lines;
}
public function count() {
$PHP_EOL_len = strlen(PHP_EOL);
$lines = 0;
$s = '';
$f = fopen($this->filename, 'rb');
fseek($f, 0, SEEK_END);
$pos = ftell($f);
while (true) {
fseek($f, --$pos);
$c = fgetc($f);
if ($c === false || $pos < 0) {
if ($this->validateLine($s) !== false) {
$lines++;
}
break;
}
$s = $c . $s;
if (strpos($s, PHP_EOL) === 0) {
if ($this->validateLine((string)substr($s, $PHP_EOL_len)) !== false) {
$lines++;
}
$s = '';
}
}
return $lines;
}
}

View File

@ -57,6 +57,7 @@ use Icinga\Module\Monitoring\Filter\UrlViewFilter;
use Icinga\Module\Monitoring\DataView\ServiceStatus;
use Icinga\Filter\Filterable;
use Icinga\Web\Url;
use Icinga\Data\ResourceFactory;
class Monitoring_ListController extends Controller
{
@ -627,5 +628,59 @@ class Monitoring_ListController extends Controller
$tabs->extend(new OutputFormat())->extend(new DashboardAction());
}
}
public function applicationlogAction()
{
$this->addTitleTab('application log');
$config_ini = IcingaConfig::app()->toArray();
if (!in_array('logging', $config_ini) || (
in_array('type', $config_ini['logging']) &&
$config_ini['logging']['type'] === 'stream' &&
in_array('target', $config_ini['logging']) &&
file_exists($config_ini['logging']['target'])
)
) {
$config = ResourceFactory::getResourceConfig('logfile');
$resource = ResourceFactory::createResource($config);
$resource->select()->andWhere('error')->order('desc')->limit(200, 50)->fetchAll();
var_dump($config, $resource);
die;
$log = new LogFile($config_ini['logging']['target']);
$this->view->logLines = $log->count();
$this->view->logData = $log->readFromEnd(1, 38);
} else {
$this->view->logData = null;
}
/*$dataview = EventHistoryView::fromRequest(
$this->getRequest(),
array(
'host_name',
'service_description',
'object_type',
'timestamp',
'raw_timestamp',
'state',
'attempt',
'max_attempts',
'output',
'type',
'host',
'service'
)
);
$this->setupFilterControl($dataview, 'eventhistory');
$this->setupSortControl(
array(
'raw_timestamp' => 'Occurence'
)
);
$query = $dataview->getQuery();
$this->handleFormatRequest($query);
$this->view->history = $query->paginate();*/
}
}
// @codingStandardsIgnoreEnd

View File

@ -0,0 +1,36 @@
<?php
$logData = $this->logData;
foreach ($logData as $key => $value) {
list($datetime, $remaining) = explode(' ', $value, 2);
list($loglevel, $msg) = explode(': ', $remaining, 2);
$loglevel = explode(' ', $loglevel, 2);
$logData[$key] = array(
new DateTime($datetime),
$loglevel[0],
$msg
);
$logData[$key][0] = $logData[$key][0]->format('d.m. H:i');
}
?>
<div class="controls">
<?= $this->tabs->render($this); ?>
</div>
<div class="content">
<table class="action">
<tbody>
<?php foreach ($logData as $value): ?>
<tr class="state">
<td style="width: 6em; text-align: center">
<?= $this->escape($value[0]) ?><br />
<?= $this->escape($value[1]) ?>
</td>
<td>
<?= $this->escape($value[2]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>