Skip to content

Commit b489b82

Browse files
committed
CustomVarRenderer: Fix custom var rennderer for apply-for-rules
1 parent de8fe10 commit b489b82

File tree

2 files changed

+68
-26
lines changed

2 files changed

+68
-26
lines changed

library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

+34-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Icinga\Application\Config;
66
use Icinga\Exception\ConfigurationError;
77
use Icinga\Exception\NotFoundError;
8+
use Icinga\Module\Director\CustomVariable\CustomVariableArray;
89
use Icinga\Module\Director\Daemon\Logger;
910
use Icinga\Module\Director\Db;
1011
use Icinga\Module\Director\Db\AppliedServiceSetLoader;
@@ -113,22 +114,42 @@ public function prefetchForObject(Model $object): bool
113114
$appliedFilterQuery = IcingaHostAppliedServicesTable::load($directorHostObj)->getQuery();
114115

115116
foreach ($appliedFilterQuery->fetchAll() as $appliedService) {
116-
if ($appliedService->name === $serviceName) {
117-
$query = $db->getDbAdapter()->select()->from('icinga_service')
118-
->where('object_name = ?', $serviceName)
119-
->where("object_type = 'apply'")
120-
->where('assign_filter = ?', $appliedService->assign_filter);
117+
if ($appliedService->apply_for === null) {
118+
$isAppliedService = $appliedService->name === $serviceName;
119+
} else {
120+
/** @var ?CustomVariableArray $hostVar */
121+
$hostVar = $directorHostObj->vars()->get((substr($appliedService->apply_for, 10)));
122+
if ($hostVar) {
123+
$appliedServiceName = $appliedService->name;
124+
$appliedForServiceLookup = [];
125+
foreach ($hostVar->getValue() as $val) {
126+
$appliedForServiceLookup[$appliedServiceName . $val] = true;
127+
}
128+
129+
$isAppliedService = isset($appliedForServiceLookup[$serviceName]);
130+
} else {
131+
$isAppliedService = false;
132+
}
133+
}
134+
135+
if (! $isAppliedService) {
136+
continue;
137+
}
121138

122-
$directorAppliedServices = IcingaService::loadAll(
123-
$db,
124-
$query,
125-
'object_name'
126-
);
139+
$query = $db->getDbAdapter()->select()->from('icinga_service')
140+
->where('object_name = ?', $appliedService->name)
141+
->where("object_type = 'apply'")
142+
->where('assign_filter = ?', $appliedService->assign_filter);
127143

128-
$directorServiceObj = current($directorAppliedServices);
144+
$directorAppliedServices = IcingaService::loadAll(
145+
$db,
146+
$query,
147+
'object_name'
148+
);
129149

130-
break;
131-
}
150+
$directorServiceObj = current($directorAppliedServices);
151+
152+
break;
132153
}
133154
} elseif ($serviceOrigin[$i] === 'service-set') {
134155
$templateResolver = new IcingaTemplateResolver($directorHostObj);

library/Director/ProvidedHook/Monitoring/CustomVarRenderer.php

+34-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Icinga\Application\Config;
66
use Icinga\Exception\ConfigurationError;
77
use Icinga\Exception\NotFoundError;
8+
use Icinga\Module\Director\CustomVariable\CustomVariableArray;
89
use Icinga\Module\Director\Daemon\Logger;
910
use Icinga\Module\Director\Db;
1011
use Icinga\Module\Director\Db\AppliedServiceSetLoader;
@@ -114,22 +115,42 @@ public function prefetchForObject(MonitoredObject $object)
114115
$appliedFilterQuery = IcingaHostAppliedServicesTable::load($directorHostObj)->getQuery();
115116

116117
foreach ($appliedFilterQuery->fetchAll() as $appliedService) {
117-
if ($appliedService->name === $serviceName) {
118-
$query = $db->getDbAdapter()->select()->from('icinga_service')
119-
->where('object_name = ?', $serviceName)
120-
->where("object_type = 'apply'")
121-
->where('assign_filter = ?', $appliedService->assign_filter);
118+
if ($appliedService->apply_for === null) {
119+
$isAppliedService = $appliedService->name === $serviceName;
120+
} else {
121+
/** @var ?CustomVariableArray $hostVar */
122+
$hostVar = $directorHostObj->vars()->get((substr($appliedService->apply_for, 10)));
123+
if ($hostVar) {
124+
$appliedServiceName = $appliedService->name;
125+
$appliedForServiceLookup = [];
126+
foreach ($hostVar->getValue() as $val) {
127+
$appliedForServiceLookup[$appliedServiceName . $val] = true;
128+
}
129+
130+
$isAppliedService = isset($appliedForServiceLookup[$serviceName]);
131+
} else {
132+
$isAppliedService = false;
133+
}
134+
}
135+
136+
if (! $isAppliedService) {
137+
continue;
138+
}
122139

123-
$directorAppliedServices = IcingaService::loadAll(
124-
$db,
125-
$query,
126-
'object_name'
127-
);
140+
$query = $db->getDbAdapter()->select()->from('icinga_service')
141+
->where('object_name = ?', $appliedService->name)
142+
->where("object_type = 'apply'")
143+
->where('assign_filter = ?', $appliedService->assign_filter);
128144

129-
$directorServiceObj = current($directorAppliedServices);
145+
$directorAppliedServices = IcingaService::loadAll(
146+
$db,
147+
$query,
148+
'object_name'
149+
);
130150

131-
break;
132-
}
151+
$directorServiceObj = current($directorAppliedServices);
152+
153+
break;
133154
}
134155
} elseif ($serviceOrigin[$i] === 'service-set') {
135156
$templateResolver = new IcingaTemplateResolver($directorHostObj);

0 commit comments

Comments
 (0)