2013-07-12 14:33:17 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
2013-07-12 14:33:17 +02:00
|
|
|
|
2013-08-20 15:32:25 +02:00
|
|
|
namespace Icinga\Module\Monitoring\Object;
|
2013-07-12 14:33:17 +02:00
|
|
|
|
2022-05-03 10:37:17 +02:00
|
|
|
use Icinga\Data\Filter\FilterEqual;
|
2023-08-16 16:58:07 +02:00
|
|
|
use Icinga\Module\Monitoring\DataView\Hoststatus;
|
2014-09-16 18:39:13 +02:00
|
|
|
use InvalidArgumentException;
|
2014-11-11 16:29:07 +01:00
|
|
|
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
|
2013-07-12 14:33:17 +02:00
|
|
|
|
2014-09-12 10:17:46 +02:00
|
|
|
/**
|
2015-05-28 14:21:05 +02:00
|
|
|
* An Icinga host
|
2014-09-12 10:17:46 +02:00
|
|
|
*/
|
2014-09-04 13:07:03 +02:00
|
|
|
class Host extends MonitoredObject
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2014-09-16 18:39:13 +02:00
|
|
|
/**
|
|
|
|
* Host state 'UP'
|
|
|
|
*/
|
|
|
|
const STATE_UP = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Host state 'DOWN'
|
|
|
|
*/
|
|
|
|
const STATE_DOWN = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Host state 'UNREACHABLE'
|
|
|
|
*/
|
|
|
|
const STATE_UNREACHABLE = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Host state 'PENDING'
|
|
|
|
*/
|
|
|
|
const STATE_PENDING = 99;
|
|
|
|
|
2014-09-12 10:17:46 +02:00
|
|
|
/**
|
|
|
|
* Type of the Icinga host
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $type = self::TYPE_HOST;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefix of the Icinga host
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-03-09 19:29:23 +01:00
|
|
|
public $prefix = 'host_';
|
2013-07-12 14:33:17 +02:00
|
|
|
|
2014-09-12 10:17:46 +02:00
|
|
|
/**
|
2015-01-20 16:11:41 +01:00
|
|
|
* Hostname
|
2014-09-12 10:17:46 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $host;
|
|
|
|
|
2014-09-16 18:39:13 +02:00
|
|
|
/**
|
|
|
|
* The services running on the hosts
|
|
|
|
*
|
|
|
|
* @var \Icinga\Module\Monitoring\Object\Service[]
|
|
|
|
*/
|
|
|
|
protected $services;
|
|
|
|
|
2014-09-12 10:17:46 +02:00
|
|
|
/**
|
|
|
|
* Create a new host
|
|
|
|
*
|
2015-01-20 16:11:41 +01:00
|
|
|
* @param MonitoringBackend $backend Backend to fetch host information from
|
|
|
|
* @param string $host Hostname
|
2014-09-12 10:17:46 +02:00
|
|
|
*/
|
2014-11-11 16:29:07 +01:00
|
|
|
public function __construct(MonitoringBackend $backend, $host)
|
2014-03-04 13:59:26 +01:00
|
|
|
{
|
2014-09-12 10:17:46 +02:00
|
|
|
parent::__construct($backend);
|
|
|
|
$this->host = $host;
|
2014-03-04 13:59:26 +01:00
|
|
|
}
|
2013-07-12 14:33:17 +02:00
|
|
|
|
2014-09-12 10:17:46 +02:00
|
|
|
/**
|
2015-01-20 16:11:41 +01:00
|
|
|
* Get the hostname
|
2014-09-12 10:17:46 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-09-16 18:39:13 +02:00
|
|
|
public function getName()
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2014-09-12 10:17:46 +02:00
|
|
|
return $this->host;
|
2013-07-12 14:33:17 +02:00
|
|
|
}
|
2013-09-04 18:27:16 +02:00
|
|
|
|
2014-09-12 10:17:46 +02:00
|
|
|
/**
|
|
|
|
* Get the data view to fetch the host information from
|
|
|
|
*
|
2023-08-16 16:58:07 +02:00
|
|
|
* @return Hoststatus
|
2014-09-12 10:17:46 +02:00
|
|
|
*/
|
|
|
|
protected function getDataView()
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2014-11-16 19:28:08 +01:00
|
|
|
$columns = array(
|
2014-05-21 00:19:26 +02:00
|
|
|
'host_acknowledged',
|
2015-09-03 16:23:04 +02:00
|
|
|
'host_acknowledgement_type',
|
2015-03-11 13:10:55 +01:00
|
|
|
'host_action_url',
|
|
|
|
'host_active_checks_enabled',
|
|
|
|
'host_active_checks_enabled_changed',
|
|
|
|
'host_address',
|
2015-07-16 10:35:28 +02:00
|
|
|
'host_address6',
|
2015-03-11 13:10:55 +01:00
|
|
|
'host_alias',
|
2015-09-04 15:35:52 +02:00
|
|
|
'host_attempt',
|
2015-03-11 13:10:55 +01:00
|
|
|
'host_check_command',
|
2014-05-21 00:19:26 +02:00
|
|
|
'host_check_execution_time',
|
2018-07-18 15:42:34 +02:00
|
|
|
'host_check_interval',
|
2014-05-21 00:19:26 +02:00
|
|
|
'host_check_latency',
|
|
|
|
'host_check_source',
|
2015-09-14 12:05:15 +02:00
|
|
|
'host_check_timeperiod',
|
2015-03-11 13:10:55 +01:00
|
|
|
'host_current_check_attempt',
|
|
|
|
'host_current_notification_number',
|
|
|
|
'host_display_name',
|
2014-05-21 00:19:26 +02:00
|
|
|
'host_event_handler_enabled',
|
|
|
|
'host_event_handler_enabled_changed',
|
|
|
|
'host_flap_detection_enabled',
|
|
|
|
'host_flap_detection_enabled_changed',
|
2015-03-11 13:10:55 +01:00
|
|
|
'host_handled',
|
2015-09-04 15:35:32 +02:00
|
|
|
'host_icon_image',
|
|
|
|
'host_icon_image_alt',
|
2015-03-11 13:10:55 +01:00
|
|
|
'host_in_downtime',
|
2014-05-21 00:19:26 +02:00
|
|
|
'host_is_flapping',
|
2015-03-13 00:25:44 +01:00
|
|
|
'host_is_reachable',
|
2015-03-11 13:10:55 +01:00
|
|
|
'host_last_check',
|
|
|
|
'host_last_notification',
|
|
|
|
'host_last_state_change',
|
|
|
|
'host_long_output',
|
|
|
|
'host_max_check_attempts',
|
|
|
|
'host_name',
|
|
|
|
'host_next_check',
|
2015-09-04 16:22:31 +02:00
|
|
|
'host_next_update',
|
2015-05-26 15:47:01 +02:00
|
|
|
'host_notes',
|
2014-05-21 00:19:26 +02:00
|
|
|
'host_notes_url',
|
2015-03-11 13:10:55 +01:00
|
|
|
'host_notifications_enabled',
|
|
|
|
'host_notifications_enabled_changed',
|
|
|
|
'host_obsessing',
|
|
|
|
'host_obsessing_changed',
|
|
|
|
'host_output',
|
|
|
|
'host_passive_checks_enabled',
|
|
|
|
'host_passive_checks_enabled_changed',
|
|
|
|
'host_percent_state_change',
|
|
|
|
'host_perfdata',
|
|
|
|
'host_process_perfdata' => 'host_process_performance_data',
|
|
|
|
'host_state',
|
2015-09-04 15:35:32 +02:00
|
|
|
'host_state_type',
|
2015-09-14 12:05:15 +02:00
|
|
|
'instance_name'
|
2014-11-16 19:28:08 +01:00
|
|
|
);
|
2015-06-12 14:42:49 +02:00
|
|
|
return $this->backend->select()->from('hoststatus', $columns)
|
2022-05-03 10:37:17 +02:00
|
|
|
->whereEx(new FilterEqual('host_name', '=', $this->host));
|
2013-07-12 14:33:17 +02:00
|
|
|
}
|
2014-09-16 18:39:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the services running on the host
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function fetchServices()
|
|
|
|
{
|
|
|
|
$services = array();
|
2015-06-12 14:42:49 +02:00
|
|
|
foreach ($this->backend->select()->from('servicestatus', array('service_description'))
|
2014-09-16 18:39:13 +02:00
|
|
|
->where('host_name', $this->host)
|
2015-06-16 17:44:29 +02:00
|
|
|
->applyFilter($this->getFilter())
|
|
|
|
->getQuery() as $service) {
|
2014-09-16 18:39:13 +02:00
|
|
|
$services[] = new Service($this->backend, $this->host, $service->service_description);
|
|
|
|
}
|
|
|
|
$this->services = $services;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the optional translated textual representation of a host state
|
|
|
|
*
|
|
|
|
* @param int $state
|
|
|
|
* @param bool $translate
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws InvalidArgumentException If the host state is not valid
|
|
|
|
*/
|
|
|
|
public static function getStateText($state, $translate = false)
|
|
|
|
{
|
2014-10-29 13:37:20 +01:00
|
|
|
$translate = (bool) $translate;
|
2014-09-16 18:39:13 +02:00
|
|
|
switch ((int) $state) {
|
|
|
|
case self::STATE_UP:
|
2015-01-22 14:36:22 +01:00
|
|
|
$text = $translate ? mt('monitoring', 'UP') : 'up';
|
2014-09-16 18:39:13 +02:00
|
|
|
break;
|
|
|
|
case self::STATE_DOWN:
|
2015-01-22 14:36:22 +01:00
|
|
|
$text = $translate ? mt('monitoring', 'DOWN') : 'down';
|
2014-09-16 18:39:13 +02:00
|
|
|
break;
|
|
|
|
case self::STATE_UNREACHABLE:
|
2015-01-22 14:36:22 +01:00
|
|
|
$text = $translate ? mt('monitoring', 'UNREACHABLE') : 'unreachable';
|
2014-09-16 18:39:13 +02:00
|
|
|
break;
|
|
|
|
case self::STATE_PENDING:
|
2015-01-22 14:36:22 +01:00
|
|
|
$text = $translate ? mt('monitoring', 'PENDING') : 'pending';
|
2014-09-16 18:39:13 +02:00
|
|
|
break;
|
|
|
|
default:
|
2023-06-22 09:57:40 +02:00
|
|
|
throw new InvalidArgumentException(sprintf('Invalid host state \'%s\'', $state));
|
2014-09-16 18:39:13 +02:00
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
2015-05-26 15:47:01 +02:00
|
|
|
|
|
|
|
public function getNotesUrls()
|
|
|
|
{
|
2015-05-28 14:58:16 +02:00
|
|
|
return $this->resolveAllStrings(
|
|
|
|
MonitoredObject::parseAttributeUrls($this->host_notes_url)
|
|
|
|
);
|
2015-05-26 15:47:01 +02:00
|
|
|
}
|
2013-07-12 14:33:17 +02:00
|
|
|
}
|