monitoring: Reorder code in MonitoredObject

This commit is contained in:
Eric Lippmann 2015-09-03 14:07:38 +02:00
parent 4d2675659c
commit dc5e86002b
1 changed files with 304 additions and 287 deletions

View File

@ -34,11 +34,60 @@ abstract class MonitoredObject implements Filterable
protected $backend; protected $backend;
/** /**
* Type of the Icinga object, i.e. 'host' or 'service' * Comments
* *
* @var string * @var array
*/ */
protected $type; protected $comments;
/**
* Custom variables
*
* @var array
*/
protected $customvars;
/**
* Contact groups
*
* @var array
*/
protected $contactgroups;
/**
* Contacts
*
* @var array
*/
protected $contacts;
/**
* Downtimes
*
* @var array
*/
protected $downtimes;
/**
* Event history
*
* @var \Icinga\Module\Monitoring\DataView\EventHistory
*/
protected $eventhistory;
/**
* Filter
*
* @var Filter
*/
protected $filter;
/**
* Host groups
*
* @var array
*/
protected $hostgroups;
/** /**
* Prefix of the Icinga object, i.e. 'host_' or 'service_' * Prefix of the Icinga object, i.e. 'host_' or 'service_'
@ -54,27 +103,6 @@ abstract class MonitoredObject implements Filterable
*/ */
protected $properties; protected $properties;
/**
* Comments
*
* @var array
*/
protected $comments;
/**
* Downtimes
*
* @var array
*/
protected $downtimes;
/**
* Host groups
*
* @var array
*/
protected $hostgroups;
/** /**
* Service groups * Service groups
* *
@ -83,32 +111,11 @@ abstract class MonitoredObject implements Filterable
protected $servicegroups; protected $servicegroups;
/** /**
* Contacts * Type of the Icinga object, i.e. 'host' or 'service'
* *
* @var array * @var string
*/ */
protected $contacts; protected $type;
/**
* Contact groups
*
* @var array
*/
protected $contactgroups;
/**
* Custom variables
*
* @var array
*/
protected $customvars;
/**
* Event history
*
* @var \Icinga\Module\Monitoring\DataView\EventHistory
*/
protected $eventhistory;
/** /**
* Stats * Stats
@ -117,13 +124,6 @@ abstract class MonitoredObject implements Filterable
*/ */
protected $stats; protected $stats;
/**
* Filter
*
* @var Filter
*/
protected $filter;
/** /**
* Create a monitored object, i.e. host or service * Create a monitored object, i.e. host or service
* *
@ -141,35 +141,82 @@ abstract class MonitoredObject implements Filterable
*/ */
abstract protected function getDataView(); abstract protected function getDataView();
public function applyFilter(Filter $filter) /**
{ * Get the notes for this monitored object
$this->getFilter()->addFilter($filter); *
return $this; * @return string The notes as a string
} */
public abstract function getNotes();
public function setFilter(Filter $filter) /**
{ * Get all note urls configured for this monitored object
// Left out on purpose. Interface is deprecated. *
} * @return array All note urls as a string
*/
public function getFilter() public abstract function getNotesUrls();
{
if ($this->filter === null) {
$this->filter = Filter::matchAll();
}
return $this->filter;
}
/**
* {@inheritdoc}
*/
public function addFilter(Filter $filter) public function addFilter(Filter $filter)
{ {
// Left out on purpose. Interface is deprecated. // Left out on purpose. Interface is deprecated.
} }
/**
* {@inheritdoc}
*/
public function applyFilter(Filter $filter)
{
$this->getFilter()->addFilter($filter);
return $this;
}
/**
* {@inheritdoc}
*/
public function getFilter()
{
if ($this->filter === null) {
$this->filter = Filter::matchAll();
}
return $this->filter;
}
/**
* {@inheritdoc}
*/
public function setFilter(Filter $filter)
{
// Left out on purpose. Interface is deprecated.
}
/**
* {@inheritdoc}
*/
public function where($condition, $value = null) public function where($condition, $value = null)
{ {
// Left out on purpose. Interface is deprecated. // Left out on purpose. Interface is deprecated.
} }
/**
* Require the object's type to be one of the given types
*
* @param array $oneOf
*
* @return bool
* @throws InvalidArgumentException If the object's type is not one of the given types.
*/
public function assertOneOf(array $oneOf)
{
if (! in_array($this->type, $oneOf)) {
throw new InvalidArgumentException;
}
return true;
}
/** /**
* Fetch the object's properties * Fetch the object's properties
* *
@ -195,45 +242,6 @@ abstract class MonitoredObject implements Filterable
return true; return true;
} }
/**
* Get the type of the object
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Require the object's type to be one of the given types
*
* @param array $oneOf
*
* @return bool
* @throws InvalidArgumentException If the object's type is not one of the given types.
*/
public function assertOneOf(array $oneOf)
{
if (! in_array($this->type, $oneOf)) {
throw new InvalidArgumentException;
}
return true;
}
/**
* Set the object's properties
*
* @param object $properties
*
* @return $this
*/
public function setProperties($properties)
{
$this->properties = (object) $properties;
return $this;
}
/** /**
* Fetch the object's comments * Fetch the object's comments
* *
@ -266,54 +274,58 @@ abstract class MonitoredObject implements Filterable
} }
/** /**
* Fetch the object's downtimes * Fetch the object's contact groups
* *
* @return $this * @return $this
*/ */
public function fetchDowntimes() public function fetchContactgroups()
{ {
$downtimes = $this->backend->select()->from('downtime', array( if ($this->backend->is('livestatus')) {
'id' => 'downtime_internal_id', $this->contactgroups = array();
'objecttype' => 'object_type', return $this;
'comment' => 'downtime_comment', }
'author_name' => 'downtime_author_name',
'start' => 'downtime_start', $contactsGroups = $this->backend->select()->from('contactgroup', array(
'scheduled_start' => 'downtime_scheduled_start', 'contactgroup_name',
'scheduled_end' => 'downtime_scheduled_end', 'contactgroup_alias'
'end' => 'downtime_end', ));
'duration' => 'downtime_duration',
'is_flexible' => 'downtime_is_flexible',
'is_fixed' => 'downtime_is_fixed',
'is_in_effect' => 'downtime_is_in_effect',
'entry_time' => 'downtime_entry_time'
))
->where('object_type', $this->type)
->order('downtime_is_in_effect', 'DESC')
->order('downtime_scheduled_start', 'ASC');
if ($this->type === self::TYPE_SERVICE) { if ($this->type === self::TYPE_SERVICE) {
$downtimes $contactsGroups
->where('service_host_name', $this->host_name) ->where('service_host_name', $this->host_name)
->where('service_description', $this->service_description); ->where('service_description', $this->service_description);
} else { } else {
$downtimes $contactsGroups->where('host_name', $this->host_name);
->where('host_name', $this->host_name);
} }
$this->downtimes = $downtimes->getQuery()->fetchAll(); $this->contactgroups = $contactsGroups->applyFilter($this->getFilter())->getQuery()->fetchAll();
return $this; return $this;
} }
/** /**
* Fetch the object's host groups * Fetch the object's contacts
* *
* @return $this * @return $this
*/ */
public function fetchHostgroups() public function fetchContacts()
{ {
$this->hostgroups = $this->backend->select() if ($this->backend->is('livestatus')) {
->from('hostgroup', array('hostgroup_name', 'hostgroup_alias')) $this->contacts = array();
->where('host_name', $this->host_name) return $this;
->applyFilter($this->getFilter()) }
->fetchPairs();
$contacts = $this->backend->select()->from('contact', array(
'contact_name',
'contact_alias',
'contact_email',
'contact_pager',
));
if ($this->type === self::TYPE_SERVICE) {
$contacts
->where('service_host_name', $this->host_name)
->where('service_description', $this->service_description);
} else {
$contacts->where('host_name', $this->host_name);
}
$this->contacts = $contacts->applyFilter($this->getFilter())->getQuery()->fetchAll();
return $this; return $this;
} }
@ -372,74 +384,39 @@ abstract class MonitoredObject implements Filterable
} }
/** /**
* Fetch the object's contacts * Fetch the object's downtimes
* *
* @return $this * @return $this
*/ */
public function fetchContacts() public function fetchDowntimes()
{ {
if ($this->backend->is('livestatus')) { $downtimes = $this->backend->select()->from('downtime', array(
$this->contacts = array(); 'id' => 'downtime_internal_id',
return $this; 'objecttype' => 'object_type',
} 'comment' => 'downtime_comment',
'author_name' => 'downtime_author_name',
$contacts = $this->backend->select()->from('contact', array( 'start' => 'downtime_start',
'contact_name', 'scheduled_start' => 'downtime_scheduled_start',
'contact_alias', 'scheduled_end' => 'downtime_scheduled_end',
'contact_email', 'end' => 'downtime_end',
'contact_pager', 'duration' => 'downtime_duration',
)); 'is_flexible' => 'downtime_is_flexible',
'is_fixed' => 'downtime_is_fixed',
'is_in_effect' => 'downtime_is_in_effect',
'entry_time' => 'downtime_entry_time'
))
->where('object_type', $this->type)
->order('downtime_is_in_effect', 'DESC')
->order('downtime_scheduled_start', 'ASC');
if ($this->type === self::TYPE_SERVICE) { if ($this->type === self::TYPE_SERVICE) {
$contacts $downtimes
->where('service_host_name', $this->host_name) ->where('service_host_name', $this->host_name)
->where('service_description', $this->service_description); ->where('service_description', $this->service_description);
} else { } else {
$contacts->where('host_name', $this->host_name); $downtimes
->where('host_name', $this->host_name);
} }
$this->contacts = $contacts->applyFilter($this->getFilter())->getQuery()->fetchAll(); $this->downtimes = $downtimes->getQuery()->fetchAll();
return $this;
}
/**
* Fetch the object's service groups
*
* @return $this
*/
public function fetchServicegroups()
{
$this->servicegroups = $this->backend->select()
->from('servicegroup', array('servicegroup_name', 'servicegroup_alias'))
->where('host_name', $this->host_name)
->where('service_description', $this->service_description)
->applyFilter($this->getFilter())
->fetchPairs();
return $this;
}
/**
* Fetch the object's contact groups
*
* @return $this
*/
public function fetchContactgroups()
{
if ($this->backend->is('livestatus')) {
$this->contactgroups = array();
return $this;
}
$contactsGroups = $this->backend->select()->from('contactgroup', array(
'contactgroup_name',
'contactgroup_alias'
));
if ($this->type === self::TYPE_SERVICE) {
$contactsGroups
->where('service_host_name', $this->host_name)
->where('service_description', $this->service_description);
} else {
$contactsGroups->where('host_name', $this->host_name);
}
$this->contactgroups = $contactsGroups->applyFilter($this->getFilter())->getQuery()->fetchAll();
return $this; return $this;
} }
@ -477,6 +454,37 @@ abstract class MonitoredObject implements Filterable
return $this; return $this;
} }
/**
* Fetch the object's host groups
*
* @return $this
*/
public function fetchHostgroups()
{
$this->hostgroups = $this->backend->select()
->from('hostgroup', array('hostgroup_name', 'hostgroup_alias'))
->where('host_name', $this->host_name)
->applyFilter($this->getFilter())
->fetchPairs();
return $this;
}
/**
* Fetch the object's service groups
*
* @return $this
*/
public function fetchServicegroups()
{
$this->servicegroups = $this->backend->select()
->from('servicegroup', array('servicegroup_name', 'servicegroup_alias'))
->where('host_name', $this->host_name)
->where('service_description', $this->service_description)
->applyFilter($this->getFilter())
->fetchPairs();
return $this;
}
/** /**
* Fetch stats * Fetch stats
* *
@ -504,84 +512,6 @@ abstract class MonitoredObject implements Filterable
return $this; return $this;
} }
/**
* Fetch all available data of the object
*
* @return $this
*/
public function populate()
{
$this
->fetchComments()
->fetchContacts()
->fetchContactgroups()
->fetchCustomvars()
->fetchDowntimes();
// Call fetchHostgroups or fetchServicegroups depending on the object's type
$fetchGroups = 'fetch' . ucfirst($this->type) . 'groups';
$this->$fetchGroups();
return $this;
}
public function __get($name)
{
if (property_exists($this->properties, $name)) {
return $this->properties->$name;
} elseif (property_exists($this, $name) && $this->$name !== null) {
return $this->$name;
} elseif (property_exists($this, $name)) {
$fetchMethod = 'fetch' . ucfirst($name);
$this->$fetchMethod();
return $this->$name;
}
if (substr($name, 0, strlen($this->prefix)) !== $this->prefix) {
$prefixedName = $this->prefix . strtolower($name);
if (property_exists($this->properties, $prefixedName)) {
return $this->properties->$prefixedName;
}
}
throw new InvalidPropertyException('Can\'t access property \'%s\'. Property does not exist.', $name);
}
public function __isset($name)
{
if (property_exists($this->properties, $name)) {
return isset($this->properties->$name);
} elseif (property_exists($this, $name)) {
return isset($this->$name);
}
return false;
}
/**
* @deprecated
*/
public static function fromParams(UrlParams $params)
{
if ($params->has('service') && $params->has('host')) {
return new Service(MonitoringBackend::instance(), $params->get('host'), $params->get('service'));
} elseif ($params->has('host')) {
return new Host(MonitoringBackend::instance(), $params->get('host'));
}
return null;
}
/**
* The notes for this monitored object
*
* @return string The notes as a string
*/
public abstract function getNotes();
/**
* Get all note urls configured for this monitored object
*
* @return array All note urls as a string
*/
public abstract function getNotesUrls();
/** /**
* Get all action urls configured for this monitored object * Get all action urls configured for this monitored object
* *
@ -595,17 +525,13 @@ abstract class MonitoredObject implements Filterable
} }
/** /**
* Resolve macros in all given strings in the current object context * Get the type of the object
* *
* @param array $strs An array of urls as string * @return string
* @return type
*/ */
protected function resolveAllStrings(array $strs) public function getType()
{ {
foreach ($strs as $i => $str) { return $this->type;
$strs[$i] = Macro::resolveMacros($str, $this);
}
return $strs;
} }
/** /**
@ -636,4 +562,95 @@ abstract class MonitoredObject implements Filterable
} }
return $links; return $links;
} }
/**
* Fetch all available data of the object
*
* @return $this
*/
public function populate()
{
$this
->fetchComments()
->fetchContacts()
->fetchContactgroups()
->fetchCustomvars()
->fetchDowntimes();
// Call fetchHostgroups or fetchServicegroups depending on the object's type
$fetchGroups = 'fetch' . ucfirst($this->type) . 'groups';
$this->$fetchGroups();
return $this;
}
/**
* Resolve macros in all given strings in the current object context
*
* @param array $strs An array of urls as string
* @return type
*/
protected function resolveAllStrings(array $strs)
{
foreach ($strs as $i => $str) {
$strs[$i] = Macro::resolveMacros($str, $this);
}
return $strs;
}
/**
* Set the object's properties
*
* @param object $properties
*
* @return $this
*/
public function setProperties($properties)
{
$this->properties = (object) $properties;
return $this;
}
public function __isset($name)
{
if (property_exists($this->properties, $name)) {
return isset($this->properties->$name);
} elseif (property_exists($this, $name)) {
return isset($this->$name);
}
return false;
}
public function __get($name)
{
if (property_exists($this->properties, $name)) {
return $this->properties->$name;
} elseif (property_exists($this, $name) && $this->$name !== null) {
return $this->$name;
} elseif (property_exists($this, $name)) {
$fetchMethod = 'fetch' . ucfirst($name);
$this->$fetchMethod();
return $this->$name;
}
if (substr($name, 0, strlen($this->prefix)) !== $this->prefix) {
$prefixedName = $this->prefix . strtolower($name);
if (property_exists($this->properties, $prefixedName)) {
return $this->properties->$prefixedName;
}
}
throw new InvalidPropertyException('Can\'t access property \'%s\'. Property does not exist.', $name);
}
/**
* @deprecated
*/
public static function fromParams(UrlParams $params)
{
if ($params->has('service') && $params->has('host')) {
return new Service(MonitoringBackend::instance(), $params->get('host'), $params->get('service'));
} elseif ($params->has('host')) {
return new Host(MonitoringBackend::instance(), $params->get('host'));
}
return null;
}
} }