2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2014-07-15 13:39:22 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
namespace Icinga\Protocol\Livestatus;
|
|
|
|
|
2014-11-16 15:06:58 +01:00
|
|
|
use Icinga\Data\SimpleQuery;
|
2014-08-27 16:03:15 +02:00
|
|
|
use Icinga\Exception\IcingaException;
|
2013-07-12 13:41:48 +02:00
|
|
|
|
2014-11-16 15:06:58 +01:00
|
|
|
class Query extends SimpleQuery
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
public function hasColumns()
|
|
|
|
{
|
|
|
|
return $this->columns !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getColumns()
|
|
|
|
{
|
|
|
|
return $this->columns;
|
|
|
|
}
|
|
|
|
|
2014-11-16 15:22:54 +01:00
|
|
|
/**
|
|
|
|
* Parse the given encoded array
|
|
|
|
*
|
|
|
|
* @param string $str the encoded array string
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function parseArray($str)
|
|
|
|
{
|
|
|
|
if (empty($str)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
$entries = preg_split('/,/', $str);
|
|
|
|
foreach ($entries as $e) {
|
|
|
|
$result[] = preg_split('/;/', $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
public function getColumnAliases()
|
|
|
|
{
|
|
|
|
$aliases = array();
|
|
|
|
foreach ($this->getColumns() as $key => $val) {
|
|
|
|
if (is_int($key)) {
|
|
|
|
$aliases[] = $val;
|
|
|
|
} else {
|
|
|
|
$aliases[] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $aliases;
|
|
|
|
}
|
2014-11-16 15:12:13 +01:00
|
|
|
/*
|
2013-06-07 11:44:37 +02:00
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
$this->count = true;
|
|
|
|
return $this;
|
|
|
|
}
|
2014-11-16 15:12:13 +01:00
|
|
|
*/
|
2014-11-16 15:13:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Automagic string casting
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function __toString()
|
2014-11-16 15:13:07 +01:00
|
|
|
{
|
|
|
|
return $this->toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render query string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function toString()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
if ($this->table === null) {
|
2014-08-27 16:03:15 +02:00
|
|
|
throw new IcingaException('Table is required');
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2014-11-16 15:28:17 +01:00
|
|
|
|
|
|
|
// Headers we always send
|
2013-06-07 11:44:37 +02:00
|
|
|
$default_headers = array(
|
2014-11-16 15:28:17 +01:00
|
|
|
// Our preferred output format is CSV as it allows us to fetch and
|
|
|
|
// process the result row by row
|
|
|
|
'OutputFormat: csv',
|
2013-06-07 11:44:37 +02:00
|
|
|
'ResponseHeader: fixed16',
|
2014-11-16 15:28:17 +01:00
|
|
|
// Tried to find a save list of separators, this might be subject to
|
|
|
|
// change and eventually be transforment into constants
|
|
|
|
'Separators: ' . implode(' ', array(ord("\n"), ord('`'), ord(','), ord(';'))),
|
|
|
|
// We always use the keepalive feature, connection teardown happens
|
|
|
|
// in the connection destructor
|
2013-06-07 11:44:37 +02:00
|
|
|
'KeepAlive: on'
|
|
|
|
);
|
|
|
|
$parts = array(
|
|
|
|
sprintf('GET %s', $this->table)
|
|
|
|
);
|
|
|
|
if ($this->count === false && $this->columns !== null) {
|
|
|
|
$parts[] = 'Columns: ' . implode(' ', $this->columns);
|
|
|
|
}
|
|
|
|
foreach ($this->filters as $key => $val) {
|
|
|
|
if ($key === 'search') {
|
|
|
|
$parts[] = 'Filter: host_name ~~ ' . $val;
|
|
|
|
$parts[] = 'Filter: description ~~ ' . $val;
|
|
|
|
$parts[] = 'Or: 2';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($val === null) {
|
|
|
|
$parts[] = 'Filter: ' . $key;
|
|
|
|
} elseif (strpos($key, '?') === false) {
|
|
|
|
$parts[] = sprintf('Filter: %s = %s', $key, $val);
|
|
|
|
} else {
|
|
|
|
$parts[] = sprintf('Filter: %s', str_replace('?', $val, $key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->count === true) {
|
|
|
|
$parts[] = 'Stats: state >= 0';
|
|
|
|
}
|
|
|
|
if (! $this->count && $this->hasLimit() && ! $this->hasOrder()) {
|
|
|
|
$parts[] = 'Limit: ' . ($this->limit_count + $this->limit_offset);
|
|
|
|
}
|
|
|
|
$lql = implode("\n", $parts)
|
|
|
|
. "\n"
|
|
|
|
. implode("\n", $default_headers)
|
|
|
|
. "\n\n";
|
|
|
|
return $lql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
unset($this->connection);
|
|
|
|
}
|
|
|
|
}
|