Revision bef4271a
Nouvelle structure des fichiers
Nouveau dossier Vigilo avec toute les classes. Hook et Setup modifiés
pour mettre à jour le fichier XML de l'ordinateur automatique dès que
l'un de ses élément a été modifié. Autoloader rajouté.
Change-Id: Iaffecfc3bd6aae191e0cb87fa1dc6a6da7956451
Reviewed-on: https://vigilo-dev.si.c-s.fr/review/2234
Tested-by: Francois POIROTTE <francois.poirotte@c-s.fr>
Reviewed-by: Francois POIROTTE <francois.poirotte@c-s.fr>
hook.php | ||
---|---|---|
1 | 1 |
<?php |
2 | 2 |
|
3 | 3 |
//require_once(__DIR__ . DIRECTORY_SEPARATOR . ''); |
4 |
|
|
5 |
abstract class VigiloXml |
|
6 |
{ |
|
7 |
abstract public function __toString(); |
|
8 |
|
|
9 |
public static function sprintf($s) |
|
10 |
{ |
|
11 |
$args = func_get_args(); |
|
12 |
array_shift($args); // pop $s |
|
13 |
|
|
14 |
$new_args = array(); |
|
15 |
foreach ($args as $arg) { |
|
16 |
if (is_string($arg)) { |
|
17 |
$new_args[] = htmlspecialchars($arg, ENT_XML1 | ENT_QUOTES, "utf-8"); |
|
18 |
} else if (is_array($arg)) { |
|
19 |
$acc = ''; |
|
20 |
foreach ($arg as $sub) { |
|
21 |
if (is_object($sub)) |
|
22 |
$acc .= (string) $sub; |
|
23 |
} |
|
24 |
$new_args[] = $acc; |
|
25 |
} else { |
|
26 |
$new_args[] = (string) $arg; |
|
27 |
} |
|
28 |
} |
|
29 |
|
|
30 |
return vsprintf($s, $new_args); |
|
31 |
} |
|
32 |
} |
|
33 |
|
|
34 |
class VigiloHostTemplate extends VigiloXml |
|
35 |
{ |
|
36 |
protected $name; |
|
37 |
|
|
38 |
public function __construct($tpl) |
|
39 |
{ |
|
40 |
$this->name = $tpl; |
|
41 |
} |
|
42 |
|
|
43 |
public function __toString() |
|
44 |
{ |
|
45 |
return self::sprintf('<template>%s</template>', $this->name); |
|
46 |
} |
|
47 |
} |
|
48 |
|
|
49 |
class VigiloGroup extends VigiloXml |
|
50 |
{ |
|
51 |
protected $name; |
|
52 |
|
|
53 |
public function __construct($group) |
|
54 |
{ |
|
55 |
$this->name = $group; |
|
56 |
} |
|
57 |
|
|
58 |
public function __toString() |
|
59 |
{ |
|
60 |
return self::sprintf('<group>%s</group>', $this->name); |
|
61 |
} |
|
62 |
} |
|
63 |
|
|
64 |
class VigiloAttribute extends VigiloXml |
|
65 |
{ |
|
66 |
protected $name; |
|
67 |
protected $value; |
|
68 |
|
|
69 |
public function __construct($name, $value) |
|
70 |
{ |
|
71 |
$this->name = $name; |
|
72 |
$this->value = $value; |
|
73 |
} |
|
74 |
|
|
75 |
public function __toString() |
|
76 |
{ |
|
77 |
return self::sprintf( |
|
78 |
'<attribute name="%s">%s</attribute>', |
|
79 |
$this->name, |
|
80 |
$this->value |
|
81 |
); |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
class VigiloItem extends VigiloXml |
|
86 |
{ |
|
87 |
protected $value; |
|
88 |
|
|
89 |
public function __construct($value) |
|
90 |
{ |
|
91 |
$this->value = $value; |
|
92 |
} |
|
93 |
|
|
94 |
public function getValue() |
|
95 |
{ |
|
96 |
return $this->value; |
|
97 |
} |
|
98 |
|
|
99 |
public function __toString() |
|
100 |
{ |
|
101 |
return self::sprintf('<item>%s</item>', $this->value); |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
class VigiloArg extends VigiloXml |
|
106 |
{ |
|
107 |
protected $name; |
|
108 |
protected $values; |
|
109 |
|
|
110 |
public function __construct($name, $values) |
|
111 |
{ |
|
112 |
if (is_array($values)) { |
|
113 |
$new_values = array(); |
|
114 |
foreach ($values as $value) { |
|
115 |
if (is_string($value)) |
|
116 |
$new_values[] = new VigiloItem($value); |
|
117 |
else if (!is_a($value, 'VigiloItem')) |
|
118 |
throw new \RuntimeException(); |
|
119 |
else |
|
120 |
$new_values[] = $value; |
|
121 |
} |
|
122 |
$values = $new_values; |
|
123 |
} else if (!is_string($values) && !is_int($values) && |
|
124 |
!is_bool($values) && !is_float($values)) |
|
125 |
throw new \RuntimeException(); |
|
126 |
else { |
|
127 |
$values = (string) $values; |
|
128 |
} |
|
129 |
|
|
130 |
$this->name = $name; |
|
131 |
$this->values = $values; |
|
132 |
} |
|
133 |
|
|
134 |
public function getName() |
|
135 |
{ |
|
136 |
return $this->name; |
|
137 |
} |
|
138 |
|
|
139 |
public function getValue() |
|
140 |
{ |
|
141 |
if (is_string($this->values)) |
|
142 |
return $this->values; |
|
143 |
return array_map('getValue', $this->values); |
|
144 |
} |
|
145 |
|
|
146 |
public function __toString() |
|
147 |
{ |
|
148 |
return self::sprintf( |
|
149 |
'<arg name="%s">%s</arg>', |
|
150 |
$this->name, |
|
151 |
$this->values |
|
152 |
); |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
class VigiloTest extends VigiloXml |
|
157 |
{ |
|
158 |
protected $name; |
|
159 |
protected $args; |
|
160 |
|
|
161 |
public function __construct($name, array $args = array()) |
|
162 |
{ |
|
163 |
$new_args = array(); |
|
164 |
foreach ($args as $arg) { |
|
165 |
if (!is_a($arg, 'VigiloArg')) |
|
166 |
throw new \RuntimeException(); |
|
167 |
$new_args[$arg->getName()] = $arg; |
|
168 |
} |
|
169 |
|
|
170 |
$this->name = $name; |
|
171 |
$this->args = $new_args; |
|
172 |
} |
|
173 |
|
|
174 |
public function __toString() |
|
175 |
{ |
|
176 |
return self::sprintf( |
|
177 |
'<test name="%s">%s</test>', |
|
178 |
$this->name, |
|
179 |
$this->args |
|
180 |
); |
|
181 |
} |
|
182 |
} |
|
183 |
|
|
184 |
class VigiloHost extends VigiloXml |
|
185 |
{ |
|
186 |
protected $computer; |
|
187 |
protected $addresses; |
|
188 |
protected $ventilation; |
|
189 |
protected $children; |
|
190 |
protected $agent; |
|
191 |
|
|
192 |
public function __construct($computer) |
|
193 |
{ |
|
194 |
$this->agent = null; |
|
195 |
$this->ventilation = "Servers"; |
|
196 |
$this->computer = $computer; |
|
197 |
$this->addresses = array(); |
|
198 |
$this->children = array(); |
|
199 |
|
|
200 |
if (class_exists('PluginFusioninventoryAgent')) { |
|
201 |
$agent = new PluginFusioninventoryAgent(); |
|
202 |
if ($agent->getAgentWithComputerid($this->computer->getID()) !== false) |
|
203 |
$this->agent = $agent; |
|
204 |
} |
|
205 |
|
|
206 |
$this->selectTemplates(); |
|
207 |
$this->selectGroups(); |
|
208 |
$this->monitorProcessor(); |
|
209 |
$this->monitorMemory(); |
|
210 |
$this->monitorNetworkInterfaces(); |
|
211 |
$this->monitorSoftwares(); |
|
212 |
$this->monitorPartitions(); |
|
213 |
} |
|
214 |
|
|
215 |
public function getName() |
|
216 |
{ |
|
217 |
return $this->computer->getName(); |
|
218 |
} |
|
219 |
|
|
220 |
protected function selectTemplates() |
|
221 |
{ |
|
222 |
$refs = array( |
|
223 |
"glpi_operatingsystems" => "operatingsystems_id", |
|
224 |
"glpi_operatingsystemversions" => "operatingsystemversions_id", |
|
225 |
"glpi_operatingsystemservicepacks" => "operatingsystemservicepacks_id", |
|
226 |
); |
|
227 |
|
|
228 |
$model = array(); |
|
229 |
foreach ($refs as $table => $field) { |
|
230 |
$id = $this->computer->fields[$field]; |
|
231 |
$value = Dropdown::getDropdownName($table, $id); |
|
232 |
if ($value !== "" && $value !== null && $value !== " " && |
|
233 |
$value !== false && $value !== "-----") { |
|
234 |
$model[] = $value; |
|
235 |
} |
|
236 |
} |
|
237 |
|
|
238 |
if (!count($model)) |
|
239 |
$model = "default"; |
|
240 |
else |
|
241 |
$model = implode(" - ", $model); |
|
242 |
|
|
243 |
$this->children[] = new VigiloHostTemplate($model); |
|
244 |
} |
|
245 |
|
|
246 |
protected function selectGroups() |
|
247 |
{ |
|
248 |
$location = new Location(); |
|
249 |
$location->getFromDB($this->computer->fields["locations_id"]); |
|
250 |
$location = $location->getName(); |
|
251 |
|
|
252 |
if (!$location) |
|
253 |
$location = "Servers"; |
|
254 |
|
|
255 |
$this->children[] = new VigiloGroup($location); |
|
256 |
} |
|
257 |
|
|
258 |
protected function selectAddress() |
|
259 |
{ |
|
260 |
static $address = null; |
|
261 |
|
|
262 |
if ($address === null && $this->agent) { |
|
263 |
$addresses = $this->agent->getIPs(); |
|
264 |
if (count($addresses)) |
|
265 |
$address = current($addresses); |
|
266 |
} |
|
267 |
|
|
268 |
if ($address === null) { |
|
269 |
$address = $this->$computer->getName(); |
|
270 |
foreach ($this->addresses as $addr) { |
|
271 |
if (!$addr->is_ipv4()) |
|
272 |
continue; |
|
273 |
|
|
274 |
$textual = $addr->getTextual(); |
|
275 |
if (is_string($textual)) { |
|
276 |
$address = $textual; |
|
277 |
break; |
|
278 |
} |
|
279 |
} |
|
280 |
} |
|
281 |
|
|
282 |
return $address; |
|
283 |
} |
|
284 |
|
|
285 |
protected function monitorProcessor() |
|
286 |
{ |
|
287 |
$this->children[] = new VigiloTest('CPU'); |
|
288 |
} |
|
289 |
|
|
290 |
protected function monitorMemory() |
|
291 |
{ |
|
292 |
global $DB; |
|
293 |
|
|
294 |
$total = 0; |
|
295 |
$query = Item_DeviceMemory::getSQLRequestToSearchForItem( |
|
296 |
$this->computer->getType(), |
|
297 |
$this->computer->getID() |
|
298 |
); |
|
299 |
|
|
300 |
foreach ($DB->query($query) as $mem) { |
|
301 |
$memory = new Item_DeviceMemory(); |
|
302 |
$memory->getFromDB($mem['id']); |
|
303 |
$total += $memory->fields['size'] * 1024 * 1024; |
|
304 |
} |
|
305 |
|
|
306 |
if ($total > 0) |
|
307 |
$this->children[] = new VigiloTest('RAM'); |
|
308 |
} |
|
309 |
|
|
310 |
protected function monitorNetworkInterfaces() |
|
311 |
{ |
|
312 |
global $DB; |
|
313 |
|
|
314 |
$query = NetworkPort::getSQLRequestToSearchForItem( |
|
315 |
$this->computer->getType(), |
|
316 |
$this->computer->getID() |
|
317 |
); |
|
318 |
|
|
319 |
foreach ($DB->query($query) as $np) { |
|
320 |
$query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']); |
|
321 |
|
|
322 |
$port = new NetworkPort(); |
|
323 |
$port->getFromDB($np['id']); |
|
324 |
if ($port->getName() == 'lo') |
|
325 |
continue; |
|
326 |
|
|
327 |
$args = array(); |
|
328 |
$label = isset($port->fields['comment']) ? $port->fields['comment'] : $port->getName(); |
|
329 |
$args[] = new VigiloArg('label', $label); |
|
330 |
$args[] = new VigiloArg('name', $port->getName()); |
|
331 |
// TODO: retrieve interface speed (from glpi_networkportethernets) |
|
332 |
$this->children[] = new VigiloTest('Interface', $args); |
|
333 |
|
|
334 |
// Retrieve all IP addresses associated with this interface. |
|
335 |
// This will be used later in selectAddress() to select |
|
336 |
// the most appropriate IP address to query this computer. |
|
337 |
foreach ($DB->query($query2) as $nn) { |
|
338 |
$query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']); |
|
339 |
foreach ($DB->query($query3) as $ip) { |
|
340 |
$addr = new IPAddress(); |
|
341 |
if ($addr->getFromDB($ip['id'])) |
|
342 |
$this->addresses[] = $addr; |
|
343 |
} |
|
344 |
} |
|
345 |
} |
|
346 |
} |
|
347 |
|
|
348 |
protected function monitorSoftwares() |
|
349 |
{ |
|
350 |
|
|
351 |
} |
|
352 |
|
|
353 |
protected function monitorPartitions() |
|
354 |
{ |
|
355 |
global $DB; |
|
356 |
|
|
357 |
$query = ComputerDisk::getSQLRequestToSearchForItem( |
|
358 |
$this->computer->getType(), |
|
359 |
$this->computer->getID() |
|
360 |
); |
|
361 |
|
|
362 |
foreach ($DB->query($query) as $cd) { |
|
363 |
$disk = new ComputerDisk(); |
|
364 |
$disk->getFromDB($cd['id']); |
|
365 |
|
|
366 |
$args = array(); |
|
367 |
$args[] = new VigiloArg('label', $disk->getName()); |
|
368 |
$args[] = new VigiloArg('partname', $disk->fields['mountpoint']); |
|
369 |
$total = $disk->fields['totalsize']; |
|
370 |
if (!empty($total)) |
|
371 |
$args[] = new VigiloArg('max', $total * 1024 * 1024); |
|
372 |
$this->children[] = new VigiloTest('Partition', $args); |
|
373 |
} |
|
374 |
} |
|
375 |
|
|
376 |
public function __toString() |
|
377 |
{ |
|
378 |
return self::sprintf( |
|
379 |
'<?xml version="1.0"?>' . |
|
380 |
'<host name="%s" address="%s" ventilation="%s">%s</host>', |
|
381 |
$this->computer->getName(), |
|
382 |
$this->selectAddress(), |
|
383 |
"Servers", |
|
384 |
$this->children |
|
385 |
); |
|
386 |
} |
|
387 |
} |
|
388 |
|
|
4 |
require __DIR__ . '/autoloader.php'; |
|
389 | 5 |
class VigiloHooks |
390 | 6 |
{ |
391 | 7 |
private $confdir; |
392 | 8 |
|
393 | 9 |
public function __construct($confdir="/etc/vigilo/vigiconf/conf.d") |
394 | 10 |
{ |
11 |
spl_autoload_register('vigilo_autoloader'); |
|
395 | 12 |
$this->confdir = $confdir; |
396 | 13 |
} |
397 | 14 |
|
... | ... | |
435 | 52 |
$dirs = array($this->confdir, "hosts", "managed", $host . ".xml"); |
436 | 53 |
unlink(implode(DIRECTORY_SEPARATOR, $dirs)); |
437 | 54 |
} |
55 |
|
|
56 |
public function manageDisks($disk){ |
|
57 |
|
|
58 |
global $DB; |
|
59 |
|
|
60 |
$query="SELECT computers_id FROM glpi_" . strtolower($disks->getType()) . "s WHERE id=" . $disks->getID() . ";"; |
|
61 |
foreach($DB->query($query) as $id){ |
|
62 |
$computer= new Computer(); |
|
63 |
$computer->getFromDB($id['id']); |
|
64 |
$this->update($computer); |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
public function manageNetworks($network){ |
|
69 |
|
|
70 |
global $DB; |
|
71 |
|
|
72 |
$query="SELECT computers_id FROM glpi_computers WHERE networks_id=" . $network->getID() . ";"; |
|
73 |
foreach($DB->query($query) as $id){ |
|
74 |
$computer=new Computer(); |
|
75 |
$computer->getFromDB($id['id']); |
|
76 |
$this->update($computer); |
|
77 |
} |
|
78 |
} |
|
438 | 79 |
} |
439 | 80 |
|
Also available in: Unified diff