diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 0115516..1249ed6 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -19,16 +19,17 @@ class HostController extends Controller if ($icingadb) { $hostName = $this->params->shift('host'); - $host = Host::on($this->getDb()); - $host->getSelectBase() - ->where(['host.name = ?' => $hostName]); - IcingaDbBackend::applyMonitoringRestriction($host); + $query = Host::on($this->getDb()); + IcingaDbBackend::applyIcingaDbRestrictions($query); - $rs = $host->columns('host.name')->first(); + $query->getSelectBase() + ->where(['host.name = ?' => $hostName]); + + $host = $query->first(); $this->params->add('name', $hostName); - if ($rs !== false) { + if ($host !== false) { $this->redirectNow(Url::fromPath('icingadb/host')->setParams($this->params)); } } else { diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index 5b03aed..ddc717d 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -20,18 +20,19 @@ class ServiceController extends Controller $hostName = $this->params->shift('host'); $serviceName = $this->params->shift('service'); - $service = Service::on($this->getDb())->with('host'); - $service->getSelectBase() - ->where(['service_host.name = ?' => $hostName, 'service.name = ?' => $serviceName]); + $query = Service::on($this->getDb())->with('host'); + IcingaDbBackend::applyIcingaDbRestrictions($query); - IcingaDbBackend::applyMonitoringRestriction($service); + $query->getSelectBase() + ->where(['service.name = ?' => $serviceName]) + ->where(['service_host.name = ?' => $hostName]); - $rs = $service->columns('host.name')->first(); + $service = $query->first(); $this->params->add('name', $serviceName); $this->params->add('host.name', $hostName); - if ($rs !== false) { + if ($service !== false) { $this->redirectNow(Url::fromPath('icingadb/service')->setParams($this->params)); } } else { diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index 94786c0..e2125dc 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -2,7 +2,6 @@ namespace Icinga\Module\Businessprocess\Forms; -use Icinga\Data\Filter\Filter; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; @@ -464,43 +463,6 @@ class AddNodeForm extends QuickForm return $this; } - protected function enumHostForServiceList() - { - $names = $this->backend - ->select() - ->from('hostStatus', ['hostname' => 'host_name']) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('host_name') - ->getQuery() - ->fetchColumn(); - - // fetchPairs doesn't seem to work when using the same column with - // different aliases twice - - return array_combine((array) $names, (array) $names); - } - - protected function enumHostList() - { - $names = $this->backend - ->select() - ->from('hostStatus', ['hostname' => 'host_name']) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('host_name') - ->getQuery() - ->fetchColumn(); - - // fetchPairs doesn't seem to work when using the same column with - // different aliases twice - $res = array(); - $suffix = ';Hoststatus'; - foreach ($names as $name) { - $res[$name . $suffix] = $name; - } - - return $res; - } - protected function enumHostStateList() { $hostStateList = [ @@ -512,25 +474,6 @@ class AddNodeForm extends QuickForm return $hostStateList; } - protected function enumServiceList($host) - { - $names = $this->backend - ->select() - ->from('serviceStatus', ['service' => 'service_description']) - ->where('host_name', $host) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('service_description') - ->getQuery() - ->fetchColumn(); - - $services = array(); - foreach ($names as $name) { - $services[$host . ';' . $name] = $name; - } - - return $services; - } - protected function enumServiceStateList() { $serviceStateList = [ @@ -544,46 +487,46 @@ class AddNodeForm extends QuickForm return $serviceStateList; } - protected function enumHostListByFilter($filter) - { - $names = $this->backend - ->select() - ->from('hostStatus', ['hostname' => 'host_name']) - ->applyFilter(Filter::fromQueryString($filter)) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('host_name') - ->getQuery() - ->fetchColumn(); - - // fetchPairs doesn't seem to work when using the same column with - // different aliases twice - $res = array(); - $suffix = ';Hoststatus'; - foreach ($names as $name) { - $res[$name . $suffix] = $name; - } - - return $res; - } - - protected function enumServiceListByFilter($filter) - { - $objects = $this->backend - ->select() - ->from('serviceStatus', ['host' => 'host_name', 'service' => 'service_description']) - ->applyFilter(Filter::fromQueryString($filter)) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('service_description') - ->getQuery() - ->fetchAll(); - - $services = array(); - foreach ($objects as $object) { - $services[$object->host . ';' . $object->service] = $object->host . ':' . $object->service; - } - - return $services; - } +// protected function enumHostListByFilter($filter) +// { +// $names = $this->backend +// ->select() +// ->from('hostStatus', ['hostname' => 'host_name']) +// ->applyFilter(Filter::fromQueryString($filter)) +// ->applyFilter($this->getRestriction('monitoring/filter/objects')) +// ->order('host_name') +// ->getQuery() +// ->fetchColumn(); +// +// // fetchPairs doesn't seem to work when using the same column with +// // different aliases twice +// $res = array(); +// $suffix = ';Hoststatus'; +// foreach ($names as $name) { +// $res[$name . $suffix] = $name; +// } +// +// return $res; +// } +// +// protected function enumServiceListByFilter($filter) +// { +// $objects = $this->backend +// ->select() +// ->from('serviceStatus', ['host' => 'host_name', 'service' => 'service_description']) +// ->applyFilter(Filter::fromQueryString($filter)) +// ->applyFilter($this->getRestriction('monitoring/filter/objects')) +// ->order('service_description') +// ->getQuery() +// ->fetchAll(); +// +// $services = array(); +// foreach ($objects as $object) { +// $services[$object->host . ';' . $object->service] = $object->host . ':' . $object->service; +// } +// +// return $services; +// } protected function hasProcesses() { diff --git a/library/Businessprocess/Common/EnumList.php b/library/Businessprocess/Common/EnumList.php index b1446fb..0940751 100644 --- a/library/Businessprocess/Common/EnumList.php +++ b/library/Businessprocess/Common/EnumList.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess\Common; +use Icinga\Data\Filter\Filter; use Icinga\Module\Businessprocess\IcingaDbBackend; use Icinga\Module\Businessprocess\MonitoringRestrictions; @@ -79,6 +80,57 @@ trait EnumList return $services; } + protected function enumHostListByFilter($filter) + { + if ($this->useIcingaDbBackend()) { + $names = (new IcingaDbBackend())->yieldHostnames($filter); + } else { + $names = $this->backend + ->select() + ->from('hostStatus', ['hostname' => 'host_name']) + ->applyFilter(MonitoringRestrictions::getRestriction('monitoring/filter/objects')) + ->order('host_name') + ->getQuery() + ->fetchColumn(); + } + + // fetchPairs doesn't seem to work when using the same column with + // different aliases twice + $res = array(); + $suffix = ';Hoststatus'; + foreach ($names as $name) { + $res[$name . $suffix] = $name; + } + + return $res; + } + + protected function enumServiceListByFilter($filter) + { + $services = array(); + + if ($this->useIcingaDbBackend()) { + $objects = (new IcingaDbBackend())->fetchServices($filter); + foreach ($objects as $object) { + $services[$object->host->name . ';' . $object->name] = $object->host->name . ':' . $object->name; + } + } else { + $objects = $this->backend + ->select() + ->from('serviceStatus', ['host' => 'host_name', 'service' => 'service_description']) + ->applyFilter(Filter::fromQueryString($filter)) + ->applyFilter(MonitoringRestrictions::getRestriction('monitoring/filter/objects')) + ->order('service_description') + ->getQuery() + ->fetchAll(); + foreach ($objects as $object) { + $services[$object->host . ';' . $object->service] = $object->host . ':' . $object->service; + } + } + + return $services; + } + protected function useIcingaDbBackend() { return $this->backendName === '_icingadb'; diff --git a/library/Businessprocess/IcingaDbBackend.php b/library/Businessprocess/IcingaDbBackend.php index 9dee1e0..ba5c194 100644 --- a/library/Businessprocess/IcingaDbBackend.php +++ b/library/Businessprocess/IcingaDbBackend.php @@ -3,16 +3,17 @@ namespace Icinga\Module\Businessprocess; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; +use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; -use Icinga\Module\Monitoring\Backend\MonitoringBackend; -use ipl\Orm\Compat\FilterProcessor; -use ipl\Orm\Query; +use ipl\Web\Filter\QueryString; class IcingaDbBackend { use IcingadbDatabase; + use Auth; + /** @var BpConfig */ protected $config; @@ -24,51 +25,63 @@ class IcingaDbBackend $this->conn = $this->getDb(); } - public function fetchHosts() + public function fetchHosts($filter = null) { - $hosts = Host::on($this->conn) - ->orderBy('host.name'); - self::applyMonitoringRestriction($hosts); + $hosts = Host::on($this->conn); + + if ($filter !== null) { + $filterQuery = QueryString::parse($filter); + + $hosts->filter($filterQuery); + } + + $hosts->orderBy('host.name'); + + $this->applyIcingaDbRestrictions($hosts); return $hosts; } - public function fetchServices($host) + public function fetchServices($filter) { $services = Service::on($this->conn) ->with('host'); - $services->getSelectBase() - ->where(['service_host.name = ?' => $host]) - ->orderBy('service.name'); + if ($filter !== null) { + $filterQuery = QueryString::parse($filter); - self::applyMonitoringRestriction($services); + $services->filter($filterQuery); + } + + $services->orderBy('service.name'); + + $this->applyIcingaDbRestrictions($services); return $services; } - public function yieldHostnames() + public function yieldHostnames($filter = null) { - foreach ($this->fetchHosts() as $host) { + foreach ($this->fetchHosts($filter) as $host) { yield $host->name; } } public function yieldServicenames($host) { - foreach ($this->fetchServices($host) as $service) { + $filter = "host.name=$host"; + + foreach ($this->fetchServices($filter) as $service) { yield $service->name; } } - public static function applyMonitoringRestriction(Query $query) + public static function applyIcingaDbRestrictions($query) { - $restriction = FilterProcessor::apply( - MonitoringRestrictions::getRestriction('monitoring/filter/objects'), - $query - ); + $object = new self; + $object->applyRestrictions($query); - return $restriction; + return $object; } } diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index 018ded4..a5dc0fd 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -8,11 +8,14 @@ use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\IcingaDbBackend; use Icinga\Module\Businessprocess\ServiceNode; +use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; class IcingaDbState extends IcingaDbBackend { + use Auth; + /** @var BpConfig */ protected $config; @@ -62,11 +65,12 @@ class IcingaDbState extends IcingaDbBackend } $queryHost = Host::on($this->backend)->with('state'); + IcingaDbBackend::applyIcingaDbRestrictions($queryHost); $queryHost->getSelectBase() ->where(['host.name IN (?)' => $hosts]); - IcingaDbBackend::applyMonitoringRestriction($queryHost); + IcingaDbBackend::applyIcingaDbRestrictions($queryHost); if ($this->config->usesHardStates()) { $stateCol = 'state.hard_state'; @@ -97,15 +101,15 @@ class IcingaDbState extends IcingaDbBackend $queryService->getSelectBase() ->where(['service_host.name IN (?)' => $hosts]); - IcingaDbBackend::applyMonitoringRestriction($queryService); + IcingaDbBackend::applyIcingaDbRestrictions($queryService); $serviceStatusCols = [ 'hostname' => 'host.name', 'service' => 'service.name', 'last_state_change' => 'state.last_state_change', 'in_downtime' => 'state.in_downtime', - 'ack' => 'host.state.is_acknowledged', - 'state' => $stateCol, + 'ack' => 'state.is_acknowledged', + 'state' => 'state.soft_state', 'display_name' => 'service.display_name', 'host_display_name' => 'host.display_name' ];