2013-07-12 14:33:17 +02:00
|
|
|
<?php
|
|
|
|
|
2013-08-20 15:32:25 +02:00
|
|
|
namespace Icinga\Module\Monitoring\Object;
|
2013-07-12 14:33:17 +02:00
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
use Icinga\Module\Monitoring\DataView\Contact;
|
|
|
|
use Icinga\Module\Monitoring\DataView\Contactgroup;
|
|
|
|
use Icinga\Module\Monitoring\DataView\Downtime;
|
|
|
|
use Icinga\Module\Monitoring\DataView\EventHistory;
|
|
|
|
use Icinga\Module\Monitoring\DataView\Hostgroup;
|
|
|
|
use Icinga\Module\Monitoring\DataView\Comment;
|
|
|
|
use Icinga\Module\Monitoring\DataView\Servicegroup;
|
2014-01-30 16:34:23 +01:00
|
|
|
use Icinga\Module\Monitoring\DataView\Customvar;
|
2013-10-15 19:56:33 +02:00
|
|
|
use Icinga\Web\Request;
|
2013-07-12 14:33:17 +02:00
|
|
|
|
|
|
|
abstract class AbstractObject
|
|
|
|
{
|
2014-03-09 19:29:23 +01:00
|
|
|
public $type;
|
|
|
|
public $prefix;
|
2013-07-12 14:33:17 +02:00
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public $comments = array();
|
|
|
|
public $downtimes = array();
|
|
|
|
public $hostgroups = array();
|
|
|
|
public $servicegroups = array();
|
|
|
|
public $contacts = array();
|
|
|
|
public $contactgroups = array();
|
|
|
|
public $customvars = array();
|
|
|
|
public $events = array();
|
2013-07-12 14:33:17 +02:00
|
|
|
|
2013-10-19 20:09:17 +02:00
|
|
|
private $properties = array();
|
2013-10-15 19:56:33 +02:00
|
|
|
private $request = null;
|
2013-07-12 14:33:17 +02:00
|
|
|
|
2014-03-09 19:43:04 +01:00
|
|
|
// TODO: Fetching parent states if any would be nice
|
|
|
|
// Same goes for host/service dependencies
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public function __construct(Request $request)
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2013-10-15 19:56:33 +02:00
|
|
|
$this->request = $request;
|
2013-10-19 20:09:17 +02:00
|
|
|
$this->properties = $this->getProperties();
|
2013-07-12 14:33:17 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
abstract protected function getProperties();
|
2013-07-12 14:33:17 +02:00
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public function fetchComments()
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2014-03-04 13:59:26 +01:00
|
|
|
// WTF???
|
|
|
|
$query = Comment::fromParams(array('backend' => null), array(
|
2014-03-09 19:29:23 +01:00
|
|
|
'id' => 'comment_internal_id',
|
|
|
|
'timestamp' => 'comment_timestamp',
|
|
|
|
'author' => 'comment_author',
|
|
|
|
'comment' => 'comment_data',
|
|
|
|
'type' => 'comment_type',
|
2014-03-04 13:59:26 +01:00
|
|
|
))->getQuery();
|
2014-03-08 18:28:19 +01:00
|
|
|
$query->where('comment_type', array('comment', 'ack'));
|
2014-03-09 19:29:23 +01:00
|
|
|
$query->where('comment_objecttype', $this->type);
|
|
|
|
$query->where('host_name', $this->host_name);
|
|
|
|
if ($this->type === 'service') {
|
|
|
|
$query->where('service_description', $this->service_description);
|
|
|
|
}
|
2014-03-04 13:59:26 +01:00
|
|
|
$this->comments = $query->fetchAll();
|
|
|
|
return $this;
|
2013-07-12 14:33:17 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public function fetchDowntimes()
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2014-03-08 18:28:19 +01:00
|
|
|
// TODO: We want to check for objecttype = 'host', not type_id = 1
|
|
|
|
|
|
|
|
// WTF???
|
|
|
|
$query = Downtime::fromParams(array('backend' => null), array(
|
|
|
|
'downtime_author',
|
|
|
|
'downtime_comment',
|
|
|
|
'downtime_entry_time',
|
|
|
|
'downtime_is_fixed',
|
|
|
|
'downtime_is_flexible',
|
|
|
|
'downtime_scheduled_start_time',
|
|
|
|
'downtime_start',
|
|
|
|
'downtime_end',
|
|
|
|
'downtime_duration',
|
|
|
|
'downtime_is_in_effect',
|
|
|
|
'downtime_triggered_by_id',
|
|
|
|
'downtime_internal_downtime_id'
|
|
|
|
))->getQuery();
|
|
|
|
$query->where('downtime_objecttype_id', $this->type);
|
|
|
|
$this->applyObjectFilter($query);
|
|
|
|
$this->downtimes = $query->fetchAll();
|
|
|
|
return $this;
|
|
|
|
|
2013-10-19 17:24:28 +02:00
|
|
|
$this->downtimes = Downtime::fromRequest($this->request)->getQuery()->fetchAll();
|
2013-07-12 14:33:17 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public function fetchHostgroups()
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2013-10-15 19:56:33 +02:00
|
|
|
$this->hostgroups = Hostgroup::fromRequest(
|
|
|
|
$this->request,
|
|
|
|
array(
|
|
|
|
'hostgroup_name',
|
|
|
|
'hostgroup_alias'
|
2013-10-15 12:48:33 +02:00
|
|
|
)
|
2013-10-15 19:56:33 +02:00
|
|
|
)->getQuery()->fetchPairs();
|
2013-10-19 20:09:17 +02:00
|
|
|
|
2013-07-12 14:33:17 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:34:23 +01:00
|
|
|
public function fetchCustomvars()
|
|
|
|
{
|
|
|
|
$query = Customvar::fromRequest(
|
|
|
|
$this->request,
|
|
|
|
array(
|
|
|
|
'varname',
|
|
|
|
'varvalue'
|
|
|
|
)
|
|
|
|
)->getQuery();
|
|
|
|
|
2014-03-09 19:29:23 +01:00
|
|
|
if ($this->type === 'host') {
|
2014-01-30 16:34:23 +01:00
|
|
|
$query->where('host_name', $this->host_name)
|
|
|
|
->where('object_type', 'host');
|
|
|
|
} else {
|
|
|
|
$query->where('host_name', $this->host_name)
|
|
|
|
->where('object_type', 'service')
|
|
|
|
->where('service_description', $this->service_description);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->customvars = $query->fetchPairs();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public function fetchContacts()
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2013-10-15 19:56:33 +02:00
|
|
|
$this->contacts = Contact::fromRequest(
|
|
|
|
$this->request,
|
|
|
|
array(
|
|
|
|
'contact_name',
|
|
|
|
'contact_alias',
|
|
|
|
'contact_email',
|
|
|
|
'contact_pager',
|
2013-10-15 12:48:33 +02:00
|
|
|
)
|
2013-10-15 19:56:33 +02:00
|
|
|
)->getQuery()
|
|
|
|
->where('host_name', $this->host_name)
|
|
|
|
->fetchAll();
|
2013-07-12 14:33:17 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public function fetchServicegroups()
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2013-10-15 19:56:33 +02:00
|
|
|
$this->servicegroups = Servicegroup::fromRequest(
|
|
|
|
$this->request,
|
|
|
|
array(
|
|
|
|
'servicegroup_name',
|
|
|
|
'servicegroup_alias',
|
2013-10-15 12:48:33 +02:00
|
|
|
)
|
2013-10-15 19:56:33 +02:00
|
|
|
)->getQuery()->fetchPairs();
|
2013-07-12 14:33:17 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public function fetchContactgroups()
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2013-10-15 19:56:33 +02:00
|
|
|
$this->contactgroups = Contactgroup::fromRequest(
|
|
|
|
$this->request,
|
|
|
|
array(
|
|
|
|
'contactgroup_name',
|
|
|
|
'contactgroup_alias'
|
2013-10-15 12:48:33 +02:00
|
|
|
)
|
2013-10-15 19:56:33 +02:00
|
|
|
)->getQuery()->fetchAll();
|
|
|
|
|
2013-07-12 14:33:17 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public function fetchEventHistory()
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2013-10-15 19:56:33 +02:00
|
|
|
$this->eventhistory = EventHistory::fromRequest(
|
|
|
|
$this->request,
|
|
|
|
array(
|
|
|
|
'object_type',
|
|
|
|
'host_name',
|
|
|
|
'service_description',
|
|
|
|
'timestamp',
|
|
|
|
'state',
|
|
|
|
'attempt',
|
|
|
|
'max_attempts',
|
|
|
|
'output',
|
|
|
|
'type'
|
|
|
|
)
|
2014-02-21 11:32:06 +01:00
|
|
|
)->sort('raw_timestamp', 'DESC')->getQuery();
|
2013-07-12 14:33:17 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public function __get($param)
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2013-10-19 20:09:17 +02:00
|
|
|
|
|
|
|
if (isset($this->properties->$param)) {
|
|
|
|
return $this->properties->$param;
|
|
|
|
} elseif (isset($this->$param)) {
|
|
|
|
return $this->$param;
|
|
|
|
}
|
2013-10-15 19:56:33 +02:00
|
|
|
if (substr($param, 0, strlen($this->prefix)) === $this->prefix) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$expandedName = $this->prefix . strtolower($param);
|
|
|
|
return $this->$expandedName;
|
2013-07-12 14:33:17 +02:00
|
|
|
}
|
2013-09-26 16:29:48 +02:00
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public function getRequest()
|
2013-09-26 16:29:48 +02:00
|
|
|
{
|
2013-10-15 19:56:33 +02:00
|
|
|
return $this->request;
|
2013-09-26 16:29:48 +02:00
|
|
|
}
|
2013-10-14 18:17:58 +02:00
|
|
|
|
2013-10-15 19:56:33 +02:00
|
|
|
public static function fromRequest(Request $request)
|
2013-10-14 18:17:58 +02:00
|
|
|
{
|
2013-10-15 19:56:33 +02:00
|
|
|
if ($request->has('service') && $request->has('host')) {
|
|
|
|
return new Service($request);
|
2013-10-17 21:40:02 +02:00
|
|
|
} elseif ($request->has('host')) {
|
2013-10-15 19:56:33 +02:00
|
|
|
return new Host($request);
|
|
|
|
}
|
2013-10-14 18:17:58 +02:00
|
|
|
}
|
2013-10-19 20:09:17 +02:00
|
|
|
|
|
|
|
abstract public function populate();
|
2013-07-12 14:33:17 +02:00
|
|
|
}
|