Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to add nodes based on filter #300

Merged
merged 1 commit into from
Aug 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions application/forms/AddNodeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Icinga\Module\Businessprocess\Forms;

use Icinga\Data\Filter\Filter;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\ImportedNode;
Expand Down Expand Up @@ -65,6 +66,12 @@ public function setup()
case 'new-process':
$this->addNewProcess();
break;
case 'hosts_from_filter':
$this->selectHostsFromFilter();
break;
case 'services_from_filter':
$this->selectServicesFromFilter();
break;
case null:
$this->setSubmitLabel($this->translate('Next'));
return;
Expand Down Expand Up @@ -157,6 +164,8 @@ protected function selectNodeType()
if ($this->hasParentNode()) {
$types['host'] = $this->translate('Host');
$types['service'] = $this->translate('Service');
$types['hosts_from_filter'] = $this->translate('Hosts from filter');
$types['services_from_filter'] = $this->translate('Services from filter');
} elseif (! $this->hasProcesses()) {
$this->addElement('hidden', 'node_type', array(
'ignore' => true,
Expand Down Expand Up @@ -248,6 +257,53 @@ protected function addServicesElement($host)
]);
}

protected function addFilteredHostsElement($filter)
{
$this->addElement('submit', 'refresh', [
'label' => $this->translate('Refresh'),
'class' => 'refresh-filter'
]);
$this->addElement('multiselect', 'children', [
'label' => $this->translate('Hosts'),
'required' => true,
'size' => 8,
'style' => 'width: 25em',
'multiOptions' => $this->enumHostListByFilter($filter),
'description' => $this->translate(
'Hosts that should be part of this business process node'
),
'validators' => [[new NoDuplicateChildrenValidator($this, $this->bp, $this->parent), true]]
]);
}

protected function addFilteredServicesElement($filter)
{
$this->addElement('submit', 'refresh', [
'label' => $this->translate('Refresh'),
'class' => 'refresh-filter'
]);
$this->addElement('multiselect', 'children', [
'label' => $this->translate('Services'),
'required' => true,
'size' => 8,
'style' => 'width: 25em',
'multiOptions' => $this->enumServiceListByFilter($filter),
'description' => $this->translate(
'Services that should be part of this business process node'
),
'validators' => [[new NoDuplicateChildrenValidator($this, $this->bp, $this->parent), true]]
]);
}

protected function addFilterElement()
{
$this->addElement('text', 'filter', array(
'label' => $this->translate('Filter'),
'required' => true,
'ignore' => true
));
}

protected function addFileElement()
{
$this->addElement('select', 'file', [
Expand Down Expand Up @@ -301,6 +357,26 @@ protected function addServiceOverrideElement()
]);
}

protected function selectHostsFromFilter()
{
$this->addFilterElement();
if ($filter = $this->getSentValue('filter')) {
$this->addFilteredHostsElement($filter);
} else {
$this->setSubmitLabel($this->translate('Next'));
}
}

protected function selectServicesFromFilter()
{
$this->addFilterElement();
if ($filter = $this->getSentValue('filter')) {
$this->addFilteredServicesElement($filter);
} else {
$this->setSubmitLabel($this->translate('Next'));
}
}

protected function selectProcess()
{
if ($this->hasParentNode()) {
Expand Down Expand Up @@ -463,6 +539,47 @@ protected function enumServiceStateList()
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 hasProcesses()
{
return count($this->enumProcesses()) > 0;
Expand Down Expand Up @@ -552,6 +669,8 @@ public function onSuccess()

// Fallthrough
case 'process':
case 'hosts_from_filter':
case 'services_from_filter':
if ($this->hasParentNode()) {
$changes->addChildrenToNode($this->getValue('children'), $this->parent);
} else {
Expand Down