-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathMonitoringRulesTree.php
More file actions
216 lines (178 loc) · 5.59 KB
/
MonitoringRulesTree.php
File metadata and controls
216 lines (178 loc) · 5.59 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace Icinga\Module\Vspheredb\Monitoring\Rule;
use Icinga\Module\Vspheredb\Db;
use Icinga\Module\Vspheredb\DbObject\BaseDbObject;
use ipl\I18n\Translation;
class MonitoringRulesTree
{
use Translation;
public const ROOT_OBJECT_TYPE = 'root';
/** @var Db */
protected $db;
/** @var string */
protected $baseObjectFolderName;
/** @var ?array */
protected $fetchedTree;
/** @var ?array */
protected $configList;
/** @var ?array */
protected $allNodes;
public function __construct(Db $db, $baseObjectFolderName)
{
$this->db = $db;
$this->baseObjectFolderName = $baseObjectFolderName;
}
public function getBaseObjectFolderName(): string
{
return $this->baseObjectFolderName;
}
/**
* @param string $uuid
*
* @return string
*/
public function getNameForUuid(string $uuid): string
{
if ($uuid === MonitoringRuleSet::NO_OBJECT) {
return $this->translate('All vCenters');
}
return $this->allNodes[$uuid]->object_name;
}
/**
* @param string $uuid
*
* @return array
*/
public function listParentUuidsFor(string $uuid): array
{
if ($uuid === MonitoringRuleSet::NO_OBJECT) {
return [];
}
if ($this->allNodes === null) {
$this->fetchTree();
}
$parents = [];
while (isset($this->allNodes[$uuid]->parent)) {
$parents[] = $uuid = $this->allNodes[$uuid]->parent->uuid ?? '';
}
$parents[] = MonitoringRuleSet::NO_OBJECT;
return $parents;
}
public function getRootNode()
{
return (object) [
'object_name' => $this->translate('All vCenters'),
'object_type' => self::ROOT_OBJECT_TYPE,
'uuid' => MonitoringRuleSet::NO_OBJECT,
'children' => $this->getTree()
];
}
/**
* @param string $uuid
*
* @return bool
*/
public function hasConfigurationForUuid(string $uuid): bool
{
if ($this->configList === null) {
$this->configList = $this->listAllConfiguredRuleSets();
}
return isset($this->configList[$uuid]);
}
/**
* @throws \Icinga\Exception\NotFoundError
*/
public function getInheritedSettingsFor(BaseDbObject $object): InheritedSettings
{
$uuid = $object->object()->get('parent_uuid');
$parents = [$uuid, ...$this->listParentUuidsFor($uuid)];
return InheritedSettings::loadForUuids($parents, $this, $this->db);
}
/**
* Hint: every DataCenter has 'datastore', 'host', 'network' and 'vm' as subfolders
*
* @return array
*/
protected function fetchBaseObjectTypeFolders(): array
{
$db = $this->db->getDbAdapter();
$query = "SELECT o.*, po.object_name, o.vcenter_uuid AS parent_uuid FROM object o"
. ' JOIN object po on o.parent_uuid = po.uuid'
. ' WHERE o.object_type = ? AND o.level = 2 AND o.object_name = ? ORDER BY po.object_name';
return $db->fetchAll($query, ['Folder', $this->baseObjectFolderName]);
}
protected function fetchAllvCenters(): array
{
$db = $this->db->getDbAdapter();
$query = "SELECT instance_uuid AS uuid, name AS object_name, 'vCenter' AS object_type, -1 AS level"
. ' FROM vcenter ORDER BY name';
return $db->fetchAll($query);
}
protected function fetchAllSubFolders(): array
{
$db = $this->db->getDbAdapter();
$query = 'SELECT * FROM object'
. ' WHERE object_type IN (?, ?, ?) AND level > 2 ORDER BY level, parent_uuid, object_name';
return $db->fetchAll($query, [
'ComputeResource',
'ClusterComputeResource',
'Folder'
]);
}
protected function listAllConfiguredRuleSets(): array
{
$db = $this->db->getDbAdapter();
$query = $db->select()->from(MonitoringRuleSet::TABLE, [
'k' => 'object_uuid',
'v' => 'object_uuid'
])->where('object_folder = ?', $this->baseObjectFolderName);
return $db->fetchPairs($query);
}
protected function getTree(): array
{
if ($this->fetchedTree === null) {
$this->fetchedTree = $this->fetchTree();
}
return $this->fetchedTree;
}
protected function fetchTree(): array
{
$vCenters = $this->fetchAllvCenters();
$baseFolders = $this->fetchBaseObjectTypeFolders();
$allSubFolders = $this->fetchAllSubFolders();
$all = [];
$root = [];
foreach ($vCenters as $folder) {
$uuid = $folder->uuid ?? '';
$folder->children = [];
$root[$uuid] = $folder;
$all[$uuid] = &$root[$uuid];
$root[$uuid] = &$all[$uuid];
}
foreach (array_merge($baseFolders, $allSubFolders) as $folder) {
$parent = $folder->parent_uuid ?? '';
$uuid = $folder->uuid ?? '';
if (isset($all[$parent])) {
$folder->children = [];
$folder->parent = &$all[$parent];
$all[$uuid] = $folder;
$all[$parent]->children[] = &$all[$uuid];
}
}
$this->allNodes = $all;
// 0 => rootfolders
// 1 => datacenters
// 2 => object folders
return $root;
}
public function discard()
{
$this->allNodes = null;
$this->fetchedTree = null;
}
public function __destruct()
{
$this->discard();
$this->db = null;
}
}