Skip to content

Commit

Permalink
monitoring/Host': Add fetchServices', fetchStats' and getStateText'
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd committed Sep 16, 2014
1 parent c04768e commit 88b3b5d
Showing 1 changed file with 115 additions and 1 deletion.
116 changes: 115 additions & 1 deletion modules/monitoring/library/Monitoring/Object/Host.php
Expand Up @@ -4,13 +4,34 @@

namespace Icinga\Module\Monitoring\Object;

use InvalidArgumentException;
use Icinga\Module\Monitoring\Backend;

/**
* A Icinga host
*/
class Host extends MonitoredObject
{
/**
* 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;

/**
* Type of the Icinga host
*
Expand All @@ -32,6 +53,20 @@ class Host extends MonitoredObject
*/
protected $host;

/**
* The services running on the hosts
*
* @var \Icinga\Module\Monitoring\Object\Service[]
*/
protected $services;

/**
* Stats
*
* @var object
*/
protected $stats;

/**
* Create a new host
*
Expand All @@ -49,7 +84,7 @@ public function __construct(Backend $backend, $host)
*
* @return string
*/
public function getHost()
public function getName()
{
return $this->host;
}
Expand Down Expand Up @@ -106,4 +141,83 @@ protected function getDataView()
))
->where('host_name', $this->host);
}

/**
* Fetch the services running on the host
*
* @return $this
*/
public function fetchServices()
{
$services = array();
foreach ($this->backend->select()->from('serviceStatus', array('service_description'))
->where('host_name', $this->host)
->getQuery()
->fetchAll() as $service) {
$services[] = new Service($this->backend, $this->host, $service->service_description);
}
$this->services = $services;
return $this;
}

/**
* Fetch stats
*
* @return $this
*/
public function fetchStats()
{
$this->stats = $this->backend->select()->from('statusSummary', array(
'services_total',
'services_ok',
'services_problem',
'services_problem_handled',
'services_problem_unhandled',
'services_critical',
'services_critical_unhandled',
'services_critical_handled',
'services_warning',
'services_warning_unhandled',
'services_warning_handled',
'services_unknown',
'services_unknown_unhandled',
'services_unknown_handled',
'services_pending',
))
->where('service_host_name', $this->host)
->getQuery()
->fetchRow();
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)
{
switch ((int) $state) {
case self::STATE_UP:
$text = $translate ? mt('monitoring', 'up') : 'up';
break;
case self::STATE_DOWN:
$text = $translate ? mt('monitoring', 'down') : 'down';
break;
case self::STATE_UNREACHABLE:
$text = $translate ? mt('monitoring', 'unreachable') : 'unreachable';
break;
case self::STATE_PENDING:
$text = $translate ? mt('monitoring', 'pending') : 'pending';
break;
default:
throw new InvalidArgumentException('Invalid host state \'%s\'', $state);
break;
}
return $text;
}
}

0 comments on commit 88b3b5d

Please sign in to comment.