monitoring: Reorder code in MonitoredObject
This commit is contained in:
parent
4d2675659c
commit
dc5e86002b
|
@ -34,11 +34,60 @@ abstract class MonitoredObject implements Filterable
|
|||
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_'
|
||||
|
@ -54,27 +103,6 @@ abstract class MonitoredObject implements Filterable
|
|||
*/
|
||||
protected $properties;
|
||||
|
||||
/**
|
||||
* Comments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $comments;
|
||||
|
||||
/**
|
||||
* Downtimes
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $downtimes;
|
||||
|
||||
/**
|
||||
* Host groups
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hostgroups;
|
||||
|
||||
/**
|
||||
* Service groups
|
||||
*
|
||||
|
@ -83,32 +111,11 @@ abstract class MonitoredObject implements Filterable
|
|||
protected $servicegroups;
|
||||
|
||||
/**
|
||||
* Contacts
|
||||
* Type of the Icinga object, i.e. 'host' or 'service'
|
||||
*
|
||||
* @var array
|
||||
* @var string
|
||||
*/
|
||||
protected $contacts;
|
||||
|
||||
/**
|
||||
* Contact groups
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $contactgroups;
|
||||
|
||||
/**
|
||||
* Custom variables
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $customvars;
|
||||
|
||||
/**
|
||||
* Event history
|
||||
*
|
||||
* @var \Icinga\Module\Monitoring\DataView\EventHistory
|
||||
*/
|
||||
protected $eventhistory;
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* Stats
|
||||
|
@ -117,13 +124,6 @@ abstract class MonitoredObject implements Filterable
|
|||
*/
|
||||
protected $stats;
|
||||
|
||||
/**
|
||||
* Filter
|
||||
*
|
||||
* @var Filter
|
||||
*/
|
||||
protected $filter;
|
||||
|
||||
/**
|
||||
* Create a monitored object, i.e. host or service
|
||||
*
|
||||
|
@ -141,35 +141,82 @@ abstract class MonitoredObject implements Filterable
|
|||
*/
|
||||
abstract protected function getDataView();
|
||||
|
||||
public function applyFilter(Filter $filter)
|
||||
{
|
||||
$this->getFilter()->addFilter($filter);
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* Get the notes for this monitored object
|
||||
*
|
||||
* @return string The notes as a string
|
||||
*/
|
||||
public abstract function getNotes();
|
||||
|
||||
public function setFilter(Filter $filter)
|
||||
{
|
||||
// Left out on purpose. Interface is deprecated.
|
||||
}
|
||||
|
||||
public function getFilter()
|
||||
{
|
||||
if ($this->filter === null) {
|
||||
$this->filter = Filter::matchAll();
|
||||
}
|
||||
return $this->filter;
|
||||
}
|
||||
/**
|
||||
* Get all note urls configured for this monitored object
|
||||
*
|
||||
* @return array All note urls as a string
|
||||
*/
|
||||
public abstract function getNotesUrls();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addFilter(Filter $filter)
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
// 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
|
||||
*
|
||||
|
@ -195,45 +242,6 @@ abstract class MonitoredObject implements Filterable
|
|||
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
|
||||
*
|
||||
|
@ -266,54 +274,58 @@ abstract class MonitoredObject implements Filterable
|
|||
}
|
||||
|
||||
/**
|
||||
* Fetch the object's downtimes
|
||||
* Fetch the object's contact groups
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function fetchDowntimes()
|
||||
public function fetchContactgroups()
|
||||
{
|
||||
$downtimes = $this->backend->select()->from('downtime', array(
|
||||
'id' => 'downtime_internal_id',
|
||||
'objecttype' => 'object_type',
|
||||
'comment' => 'downtime_comment',
|
||||
'author_name' => 'downtime_author_name',
|
||||
'start' => 'downtime_start',
|
||||
'scheduled_start' => 'downtime_scheduled_start',
|
||||
'scheduled_end' => 'downtime_scheduled_end',
|
||||
'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->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) {
|
||||
$downtimes
|
||||
$contactsGroups
|
||||
->where('service_host_name', $this->host_name)
|
||||
->where('service_description', $this->service_description);
|
||||
} else {
|
||||
$downtimes
|
||||
->where('host_name', $this->host_name);
|
||||
$contactsGroups->where('host_name', $this->host_name);
|
||||
}
|
||||
$this->downtimes = $downtimes->getQuery()->fetchAll();
|
||||
$this->contactgroups = $contactsGroups->applyFilter($this->getFilter())->getQuery()->fetchAll();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the object's host groups
|
||||
* Fetch the object's contacts
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function fetchHostgroups()
|
||||
public function fetchContacts()
|
||||
{
|
||||
$this->hostgroups = $this->backend->select()
|
||||
->from('hostgroup', array('hostgroup_name', 'hostgroup_alias'))
|
||||
->where('host_name', $this->host_name)
|
||||
->applyFilter($this->getFilter())
|
||||
->fetchPairs();
|
||||
if ($this->backend->is('livestatus')) {
|
||||
$this->contacts = array();
|
||||
return $this;
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
|
@ -372,74 +384,39 @@ abstract class MonitoredObject implements Filterable
|
|||
}
|
||||
|
||||
/**
|
||||
* Fetch the object's contacts
|
||||
* Fetch the object's downtimes
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function fetchContacts()
|
||||
public function fetchDowntimes()
|
||||
{
|
||||
if ($this->backend->is('livestatus')) {
|
||||
$this->contacts = array();
|
||||
return $this;
|
||||
}
|
||||
|
||||
$contacts = $this->backend->select()->from('contact', array(
|
||||
'contact_name',
|
||||
'contact_alias',
|
||||
'contact_email',
|
||||
'contact_pager',
|
||||
));
|
||||
$downtimes = $this->backend->select()->from('downtime', array(
|
||||
'id' => 'downtime_internal_id',
|
||||
'objecttype' => 'object_type',
|
||||
'comment' => 'downtime_comment',
|
||||
'author_name' => 'downtime_author_name',
|
||||
'start' => 'downtime_start',
|
||||
'scheduled_start' => 'downtime_scheduled_start',
|
||||
'scheduled_end' => 'downtime_scheduled_end',
|
||||
'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) {
|
||||
$contacts
|
||||
$downtimes
|
||||
->where('service_host_name', $this->host_name)
|
||||
->where('service_description', $this->service_description);
|
||||
} else {
|
||||
$contacts->where('host_name', $this->host_name);
|
||||
$downtimes
|
||||
->where('host_name', $this->host_name);
|
||||
}
|
||||
$this->contacts = $contacts->applyFilter($this->getFilter())->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();
|
||||
$this->downtimes = $downtimes->getQuery()->fetchAll();
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -477,6 +454,37 @@ abstract class MonitoredObject implements Filterable
|
|||
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
|
||||
*
|
||||
|
@ -504,84 +512,6 @@ abstract class MonitoredObject implements Filterable
|
|||
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
|
||||
*
|
||||
|
@ -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 type
|
||||
* @return string
|
||||
*/
|
||||
protected function resolveAllStrings(array $strs)
|
||||
public function getType()
|
||||
{
|
||||
foreach ($strs as $i => $str) {
|
||||
$strs[$i] = Macro::resolveMacros($str, $this);
|
||||
}
|
||||
return $strs;
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -636,4 +562,95 @@ abstract class MonitoredObject implements Filterable
|
|||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue