Move enumHostListByFilter and enumServiceListByFilter to EnumList trait.

Move enumHostListByFilter and enumServiceListByFilter to EnumList trait from AddNodeForm and change fetchServices and fetchHosts
method to work with filters in IcingaDbBackEnd class.
Also, applyMonitoringRestriction is changed to applyIcingaDbRestrictions in IcingaDbBackend.
This commit is contained in:
raviks789 2021-11-17 10:10:00 +01:00
parent a6dad05445
commit df72ff65be
6 changed files with 148 additions and 134 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -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()
{

View file

@ -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';

View file

@ -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;
}
}

View file

@ -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'
];