2016-03-20 11:18:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director;
|
|
|
|
|
|
|
|
use Icinga\Application\Icinga;
|
2021-03-18 07:01:42 +01:00
|
|
|
use Icinga\Authentication\Auth;
|
|
|
|
use Icinga\Data\Filter\Filter;
|
2016-03-20 11:18:06 +01:00
|
|
|
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
|
|
|
|
|
|
|
|
class Monitoring
|
|
|
|
{
|
|
|
|
protected $backend;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$app = Icinga::app();
|
|
|
|
$modules = $app->getModuleManager();
|
|
|
|
if (!$modules->hasLoaded('monitoring') && $app->isCli()) {
|
|
|
|
$app->getModuleManager()->loadEnabledModules();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($modules->hasLoaded('monitoring')) {
|
2016-11-01 18:28:36 +01:00
|
|
|
$this->backend = MonitoringBackend::instance();
|
2016-03-20 11:18:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isAvailable()
|
|
|
|
{
|
|
|
|
return $this->backend !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasHost($hostname)
|
|
|
|
{
|
2021-03-18 07:01:42 +01:00
|
|
|
return $this->backend->select()->from('hostStatus', [
|
2016-03-20 11:18:06 +01:00
|
|
|
'hostname' => 'host_name',
|
2021-03-18 07:01:42 +01:00
|
|
|
])->where('host_name', $hostname)->fetchOne() === $hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasService($hostname, $service)
|
|
|
|
{
|
|
|
|
return (array) $this->prepareServiceKeyColumnQuery($hostname, $service)->fetchRow() === [
|
|
|
|
'hostname' => $hostname,
|
|
|
|
'service' => $service,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authCanEditHost(Auth $auth, $hostname)
|
|
|
|
{
|
|
|
|
if ($auth->hasPermission('director/monitoring/hosts')) {
|
|
|
|
$restriction = null;
|
|
|
|
foreach ($auth->getRestrictions('director/monitoring/rw-object-filter') as $restriction) {
|
|
|
|
if ($this->hasHostWithExtraFilter($hostname, Filter::fromQueryString($restriction))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($restriction === null) {
|
|
|
|
return $this->hasHost($hostname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authCanEditService(Auth $auth, $hostname, $service)
|
|
|
|
{
|
2021-11-28 11:13:28 +01:00
|
|
|
if ($hostname === null || $service === null) {
|
|
|
|
// TODO: UUID support!
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-18 07:01:42 +01:00
|
|
|
if ($auth->hasPermission('director/monitoring/services')) {
|
|
|
|
$restriction = null;
|
|
|
|
foreach ($auth->getRestrictions('director/monitoring/rw-object-filter') as $restriction) {
|
|
|
|
if ($this->hasServiceWithExtraFilter($hostname, $service, Filter::fromQueryString($restriction))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($restriction === null) {
|
|
|
|
return $this->hasService($hostname, $service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasHostWithExtraFilter($hostname, Filter $filter)
|
|
|
|
{
|
|
|
|
return $this->backend->select()->from('hostStatus', [
|
|
|
|
'hostname' => 'host_name',
|
|
|
|
])->where('host_name', $hostname)->applyFilter($filter)->fetchOne() === $hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasServiceWithExtraFilter($hostname, $service, Filter $filter)
|
|
|
|
{
|
|
|
|
return (array) $this
|
|
|
|
->prepareServiceKeyColumnQuery($hostname, $service)
|
|
|
|
->applyFilter($filter)
|
|
|
|
->fetchRow() === [
|
|
|
|
'hostname' => $hostname,
|
|
|
|
'service' => $service,
|
|
|
|
];
|
2016-03-20 11:18:06 +01:00
|
|
|
}
|
2016-12-06 08:55:55 +01:00
|
|
|
|
|
|
|
public function getHostState($hostname)
|
|
|
|
{
|
2021-03-18 07:01:42 +01:00
|
|
|
$hostStates = [
|
2016-12-06 08:55:55 +01:00
|
|
|
'0' => 'up',
|
|
|
|
'1' => 'down',
|
|
|
|
'2' => 'unreachable',
|
|
|
|
'99' => 'pending',
|
2021-03-18 07:01:42 +01:00
|
|
|
];
|
2016-12-06 08:55:55 +01:00
|
|
|
|
2021-03-18 07:01:42 +01:00
|
|
|
$query = $this->backend->select()->from('hostStatus', [
|
2016-12-06 08:55:55 +01:00
|
|
|
'hostname' => 'host_name',
|
|
|
|
'state' => 'host_state',
|
|
|
|
'problem' => 'host_problem',
|
|
|
|
'acknowledged' => 'host_acknowledged',
|
|
|
|
'in_downtime' => 'host_in_downtime',
|
|
|
|
'output' => 'host_output',
|
2021-03-18 07:01:42 +01:00
|
|
|
])->where('host_name', $hostname);
|
2016-12-06 08:55:55 +01:00
|
|
|
|
|
|
|
$res = $query->fetchRow();
|
|
|
|
if ($res === false) {
|
2021-03-18 07:01:42 +01:00
|
|
|
$res = (object) [
|
2016-12-06 08:55:55 +01:00
|
|
|
'hostname' => $hostname,
|
|
|
|
'state' => '99',
|
|
|
|
'problem' => '0',
|
|
|
|
'acknowledged' => '0',
|
|
|
|
'in_downtime' => '0',
|
|
|
|
'output' => null,
|
2021-03-18 07:01:42 +01:00
|
|
|
];
|
2016-12-06 08:55:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$res->state = $hostStates[$res->state];
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
2021-03-18 07:01:42 +01:00
|
|
|
|
|
|
|
protected function prepareServiceKeyColumnQuery($hostname, $service)
|
|
|
|
{
|
|
|
|
return $this->backend
|
|
|
|
->select()
|
|
|
|
->from('serviceStatus', [
|
|
|
|
'hostname' => 'host_name',
|
|
|
|
'service' => 'service_description',
|
|
|
|
])
|
|
|
|
->where('host_name', $hostname)
|
|
|
|
->where('service_description', $service);
|
|
|
|
}
|
2016-03-20 11:18:06 +01:00
|
|
|
}
|