icingaweb2-module-director/library/Director/Objects/IcingaService.php

201 lines
5.5 KiB
PHP
Raw Normal View History

<?php
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
class IcingaService extends IcingaObject
{
protected $table = 'icinga_service';
protected $defaultProperties = array(
'id' => null,
'object_name' => null,
'object_type' => null,
'disabled' => 'n',
'display_name' => null,
'host_id' => null,
'check_command_id' => null,
'max_check_attempts' => null,
'check_period_id' => null,
'check_interval' => null,
'retry_interval' => null,
'enable_notifications' => null,
'enable_active_checks' => null,
'enable_passive_checks' => null,
'enable_event_handler' => null,
'enable_flapping' => null,
'enable_perfdata' => null,
'event_command_id' => null,
'flapping_threshold' => null,
'volatile' => null,
'zone_id' => null,
'command_endpoint_id' => null,
'notes' => null,
'notes_url' => null,
'action_url' => null,
'icon_image' => null,
'icon_image_alt' => null,
2015-12-18 11:07:23 +01:00
'use_agent' => null,
);
protected $relations = array(
'host' => 'IcingaHost',
'check_command' => 'IcingaCommand',
'event_command' => 'IcingaCommand',
'check_period' => 'IcingaTimePeriod',
'command_endpoint' => 'IcingaEndpoint',
'zone' => 'IcingaZone',
);
protected $booleans = array(
2015-12-18 12:07:26 +01:00
'enable_notifications' => 'enable_notifications',
'enable_active_checks' => 'enable_active_checks',
'enable_passive_checks' => 'enable_passive_checks',
'enable_event_handler' => 'enable_event_handler',
'enable_flapping' => 'enable_flapping',
'enable_perfdata' => 'enable_perfdata',
'volatile' => 'volatile',
'use_agent' => 'use_agent',
);
protected $intervalProperties = array(
'check_interval' => 'check_interval',
'retry_interval' => 'retry_interval',
);
protected $supportsGroups = true;
protected $supportsCustomVars = true;
2015-07-31 17:34:12 +02:00
protected $supportsFields = true;
2015-06-26 16:20:16 +02:00
protected $supportsImports = true;
protected $supportsApplyRules = true;
protected $keyName = array('host_id', 'object_name');
public function getCheckCommand()
{
$id = $this->getResolvedProperty('check_command_id');
return IcingaCommand::loadWithAutoIncId(
$id,
$this->getConnection()
);
}
public function isApplyRule()
{
if ($this->hasBeenAssignedToHostTemplate()) {
return true;
}
return $this->hasProperty('object_type')
&& $this->object_type === 'apply';
}
protected function setKey($key)
{
if (is_int($key)) {
$this->id = $key;
} elseif (is_array($key)) {
foreach (array('id', 'host_id', 'object_name') as $k) {
if (array_key_exists($k, $key)) {
$this->set($k, $key[$k]);
}
}
} else {
return parent::setKey($key);
}
return $this;
}
2016-02-26 11:58:37 +01:00
/**
* Render host_id as host_name
*
* Avoid complaints for method names with underscore:
* @codingStandardsIgnoreStart
*
* @return string
*/
public function renderHost_id()
{
2016-02-26 11:58:37 +01:00
// @codingStandardsIgnoreEnd
if ($this->hasBeenAssignedToHostTemplate()) {
return '';
}
return $this->renderRelationProperty('host', $this->host_id, 'host_name');
}
protected function renderAssignments()
{
if (! $this->hasBeenAssignedToHostTemplate()) {
return parent::renderAssignments();
}
// TODO: use assignment renderer, escape host
$filter = sprintf(
'assign where "%s" in host.templates',
$this->host
);
return "\n " . $filter . "\n";
}
protected function hasBeenAssignedToHostTemplate()
{
return $this->host_id && $this->getRelatedObject(
'host',
$this->host_id
)->object_type === 'template';
}
protected function renderCustomExtensions()
{
// A hand-crafted command endpoint overrides use_agent
if ($this->command_endpoint_id !== null) {
2016-02-26 11:58:37 +01:00
return '';
}
// In case use_agent isn't defined, do nothing
// TODO: what if we inherit use_agent and override it with 'n'?
if ($this->use_agent !== 'y') {
return '';
}
return c::renderKeyValue('command_endpoint', 'host_name');
}
2016-02-26 11:58:37 +01:00
/**
* Do not render internal property
*
* Avoid complaints for method names with underscore:
* @codingStandardsIgnoreStart
*
* @return string
*/
2015-12-18 11:07:23 +01:00
public function renderUse_agent()
{
2016-02-26 11:58:37 +01:00
// @codingStandardsIgnoreEnd
2015-12-18 11:07:23 +01:00
return '';
}
public function hasCheckCommand()
{
return $this->getResolvedProperty('check_command_id') !== null;
}
public function getOnDeleteUrl()
{
if ($this->host_id) {
return 'director/host/services?name=' . rawurlencode($this->host);
} else {
return parent::getOnDeleteUrl();
}
}
}