MonitoredObject: Allow to fetch a host's customvariables for services

refs #10304
This commit is contained in:
Johannes Meyer 2015-10-05 11:57:31 +02:00
parent 98e0081d81
commit 7419c9e87c

View File

@ -5,7 +5,6 @@ namespace Icinga\Module\Monitoring\Object;
use InvalidArgumentException; use InvalidArgumentException;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Application\Logger;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Icinga\Data\Filterable; use Icinga\Data\Filterable;
use Icinga\Exception\InvalidPropertyException; use Icinga\Exception\InvalidPropertyException;
@ -50,12 +49,26 @@ abstract class MonitoredObject implements Filterable
protected $comments; protected $comments;
/** /**
* Custom variables * This object's obfuscated custom variables
* *
* @var array * @var array
*/ */
protected $customvars; protected $customvars;
/**
* The host custom variables
*
* @var array
*/
protected $hostVariables;
/**
* The service custom variables
*
* @var array
*/
protected $serviceVariables;
/** /**
* Contact groups * Contact groups
* *
@ -438,9 +451,9 @@ abstract class MonitoredObject implements Filterable
} }
/** /**
* Fetch the object's custom variables * Fetch this object's obfuscated custom variables
* *
* @return $this * @return $this
*/ */
public function fetchCustomvars() public function fetchCustomvars()
{ {
@ -463,28 +476,81 @@ abstract class MonitoredObject implements Filterable
$blacklistPattern = '/^(' . implode('|', $blacklist) . ')$/i'; $blacklistPattern = '/^(' . implode('|', $blacklist) . ')$/i';
} }
if ($this->type === self::TYPE_SERVICE) {
$this->fetchServiceVariables();
$customvars = $this->serviceVariables;
} else {
$this->fetchHostVariables();
$customvars = $this->hostVariables;
}
$this->customvars = array();
foreach ($customvars as $name => $value) {
if ($blacklistPattern && preg_match($blacklistPattern, $name)) {
$this->customvars[$name] = '***';
} else {
$this->customvars[$name] = $value;
}
}
return $this;
}
/**
* Fetch the host custom variables related to this object
*
* @return $this
*/
public function fetchHostVariables()
{
$query = $this->backend->select()->from('customvar', array( $query = $this->backend->select()->from('customvar', array(
'varname', 'varname',
'varvalue', 'varvalue',
'is_json' 'is_json'
)) ))
->where('object_type', $this->type) ->where('object_type', static::TYPE_SERVICE)
->where('host_name', $this->host_name); ->where('host_name', $this->host_name);
if ($this->type === self::TYPE_SERVICE) {
$query->where('service_description', $this->service_description); $this->hostVariables = array();
foreach ($query as $row) {
if ($row->is_json) {
$this->hostVariables[strtolower($row->varname)] = json_decode($row->varvalue);
} else {
$this->hostVariables[strtolower($row->varname)] = $row->varvalue;
}
} }
$this->customvars = array(); return $this;
}
$customvars = $query->getQuery()->fetchAll(); /**
foreach ($customvars as $cv) { * Fetch the service custom variables related to this object
$name = strtolower($cv->varname); *
if ($blacklistPattern && preg_match($blacklistPattern, $cv->varname)) { * @return $this
$this->customvars[$name] = '***'; *
} elseif ($cv->is_json) { * @throws ProgrammingError In case this object is not a service
$this->customvars[$name] = json_decode($cv->varvalue); */
public function fetchServiceVariables()
{
if ($this->type !== static::TYPE_SERVICE) {
throw new ProgrammingError('Cannot fetch service custom variables for non-service objects');
}
$query = $this->backend->select()->from('customvar', array(
'varname',
'varvalue',
'is_json'
))
->where('object_type', static::TYPE_SERVICE)
->where('host_name', $this->host_name)
->where('service_description', $this->service_description);
$this->serviceVariables = array();
foreach ($query as $row) {
if ($row->is_json) {
$this->serviceVariables[strtolower($row->varname)] = json_decode($row->varvalue);
} else { } else {
$this->customvars[$name] = $cv->varvalue; $this->serviceVariables[strtolower($row->varname)] = $row->varvalue;
} }
} }