1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
<?php
namespace Icinga\Module\Director\Data;
use gipfl\IcingaWeb2\Table\QueryBasedTable;
use gipfl\ZfDb\Select;
use Icinga\Data\SimpleQuery;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\AppliedServiceSetLoader;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Repository\IcingaTemplateRepository;
use Icinga\Module\Director\Web\Table\IcingaHostAppliedServicesTable;
use Icinga\Module\Director\Web\Table\IcingaServiceSetServiceTable;
use Icinga\Module\Director\Web\Table\ObjectsTableService;
use Ramsey\Uuid\Uuid;
use RuntimeException;
use Zend_Db_Select;
class HostServiceLoader
{
/** @var Db */
protected $connection;
/** @var \Zend_Db_Adapter_Abstract */
protected $db;
/** @var bool */
protected $resolveHostServices = false;
/** @var bool */
protected $resolveObjects = false;
public function __construct(Db $connection)
{
$this->connection = $connection;
$this->db = $connection->getDbAdapter();
}
public function fetchServicesForHost(IcingaHost $host)
{
$table = (new ObjectsTableService($this->connection))->setHost($host);
$services = $this->fetchServicesForTable($table);
if ($this->resolveHostServices) {
foreach ($this->fetchAllServicesForHost($host) as $service) {
$services[] = $service;
}
}
return $services;
}
public function resolveHostServices($enable = true)
{
$this->resolveHostServices = $enable;
return $this;
}
public function resolveObjects($resolve = true)
{
$this->resolveObjects = $resolve;
return $this;
}
protected function fetchAllServicesForHost(IcingaHost $host)
{
$services = [];
/** @var IcingaHost[] $parents */
$parents = IcingaTemplateRepository::instanceByObject($host)->getTemplatesFor($host, true);
foreach ($parents as $parent) {
$table = (new ObjectsTableService($this->connection))
->setHost($parent)
->setInheritedBy($host);
foreach ($this->fetchServicesForTable($table) as $service) {
$services[] = $service;
}
}
foreach ($this->getHostServiceSetTables($host) as $table) {
foreach ($this->fetchServicesForTable($table) as $service) {
$services[] = $service;
}
}
foreach ($parents as $parent) {
foreach ($this->getHostServiceSetTables($parent, $host) as $table) {
foreach ($this->fetchServicesForTable($table) as $service) {
$services[] = $service;
}
}
}
$appliedSets = AppliedServiceSetLoader::fetchForHost($host);
foreach ($appliedSets as $set) {
$table = IcingaServiceSetServiceTable::load($set)
// ->setHost($host)
->setAffectedHost($host);
foreach ($this->fetchServicesForTable($table) as $service) {
$services[] = $service;
}
}
$table = IcingaHostAppliedServicesTable::load($host);
foreach ($this->fetchServicesForTable($table) as $service) {
$services[] = $service;
}
return $services;
}
/**
* Duplicates Logic in HostController
*
* @param IcingaHost $host
* @param IcingaHost|null $affectedHost
* @return IcingaServiceSetServiceTable[]
*/
protected function getHostServiceSetTables(IcingaHost $host, IcingaHost $affectedHost = null)
{
$tables = [];
$db = $this->connection;
if ($affectedHost === null) {
$affectedHost = $host;
}
if ($host->get('id') === null) {
return $tables;
}
$query = $db->getDbAdapter()->select()
->from(['ss' => 'icinga_service_set'], 'ss.*')
->join(['hsi' => 'icinga_service_set_inheritance'], 'hsi.parent_service_set_id = ss.id', [])
->join(['hs' => 'icinga_service_set'], 'hs.id = hsi.service_set_id', [])
->where('hs.host_id = ?', $host->get('id'));
$sets = IcingaServiceSet::loadAll($db, $query, 'object_name');
/** @var IcingaServiceSet $set*/
foreach ($sets as $name => $set) {
$tables[] = IcingaServiceSetServiceTable::load($set)
->setHost($host)
->setAffectedHost($affectedHost);
}
return $tables;
}
protected function fetchServicesForTable(QueryBasedTable $table)
{
$query = $table->getQuery();
if ($query instanceof Select || $query instanceof Zend_Db_Select) {
// What about SimpleQuery? IcingaHostAppliedServicesTable with branch in place?
$query->reset(Select::LIMIT_COUNT);
$query->reset(Select::LIMIT_OFFSET);
$rows = $this->db->fetchAll($query);
} elseif ($query instanceof SimpleQuery) {
$rows = $query->fetchAll();
} else {
throw new RuntimeException('Table query needs to be either a Select or a SimpleQuery instance');
}
$services = [];
foreach ($rows as $row) {
$service = IcingaService::loadWithUniqueId(Uuid::fromBytes($row->uuid), $this->connection);
if ($this->resolveObjects) {
$service = $service::fromPlainObject($service->toPlainObject(true), $this->connection);
}
$services[] = $service;
}
return $services;
}
}
|