glpi / src / plugins / vigilo / vigilo_hooks.php @ 166be9d2
History | View | Annotate | Download (6.3 KB)
1 | 166be9d2 | Francois POIROTTE | <?php
|
---|---|---|---|
2 | |||
3 | class VigiloHooks |
||
4 | { |
||
5 | // Callbacks pour différents événements
|
||
6 | // concernant les équipements supportés.
|
||
7 | public function preItemUpdate($item) |
||
8 | { |
||
9 | global $DB; |
||
10 | |||
11 | $id = $item->getID(); |
||
12 | $query = <<<SQL |
||
13 | SELECT `template`
|
||
14 | FROM glpi_plugin_vigilo_template
|
||
15 | WHERE `item_id` = $id;
|
||
16 | SQL;
|
||
17 | |||
18 | $item->fields['vigilo_template'] = 0; |
||
19 | $result = $DB->query($query); |
||
20 | if ($result) { |
||
21 | $tpl = $DB->result($result, 0, "template"); |
||
22 | $templates = PluginVigiloTemplate::getTemplates(); |
||
23 | $index = array_search($tpl, $templates, true); |
||
24 | if (false !== $index) { |
||
25 | $item->fields['vigilo_template'] = $index; |
||
26 | } |
||
27 | } |
||
28 | } |
||
29 | |||
30 | public function itemAddOrUpdate($item) |
||
31 | { |
||
32 | global $DB; |
||
33 | |||
34 | $templates = PluginVigiloTemplate::getTemplates(); |
||
35 | $tplId = (int) $item->input['vigilo_template']; |
||
36 | |||
37 | if ($tplId > 0 && $tplId < count($templates)) { |
||
38 | $id = $item->getID(); |
||
39 | $template = $DB->escape($templates[$tplId]); |
||
40 | $query = <<<SQL |
||
41 | INSERT INTO `glpi_plugin_vigilo_template`(`item_id`, `template`)
|
||
42 | VALUES ($id, '$template')
|
||
43 | ON DUPLICATE KEY UPDATE `template` = '$template';
|
||
44 | SQL;
|
||
45 | $DB->query($query); |
||
46 | $item->fields['vigilo_template'] = $templates[$tplId]; |
||
47 | } else {
|
||
48 | $item->fields['vigilo_template'] = null; |
||
49 | } |
||
50 | |||
51 | $this->update($item); |
||
52 | } |
||
53 | |||
54 | public function itemPurge($item) |
||
55 | { |
||
56 | global $DB; |
||
57 | |||
58 | $id = $item->getID(); |
||
59 | $query = "DELETE FROM `glpi_plugin_vigilo_template` WHERE `item_id` = $id;"; |
||
60 | $DB->query($query); |
||
61 | $this->unmonitor($item->getField('name')); |
||
62 | } |
||
63 | |||
64 | // Méthodes outils / annexes
|
||
65 | public function writeVigiloConfig($obj, $objtype) |
||
66 | { |
||
67 | $dirs = array(VIGILO_CONFDIR, $objtype, "managed"); |
||
68 | $confdir = implode(DIRECTORY_SEPARATOR, $dirs); |
||
69 | $file = $confdir . DIRECTORY_SEPARATOR . $obj->getName() . ".xml"; |
||
70 | |||
71 | if (!file_exists($confdir)) { |
||
72 | mkdir($confdir, 0770, true); |
||
73 | } |
||
74 | |||
75 | $res = file_put_contents($file, $obj, LOCK_EX); |
||
76 | if (false !== $res) { |
||
77 | chgrp($file, "vigiconf"); |
||
78 | chmod($file, 0660); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | // @codingStandardsIgnoreStart
|
||
83 | public function plugin_vigilo_getAddSearchOptions($itemtype) |
||
84 | { |
||
85 | // Le nom de la méthode est imposé par GLPI.
|
||
86 | // @codingStandardsIgnoreEnd
|
||
87 | $options = array(); |
||
88 | |||
89 | if (!in_array($itemtype, array('Computer', 'NetworkEquipment', 'Printer'))) { |
||
90 | return $options; |
||
91 | } |
||
92 | |||
93 | $options['7007']['table'] = 'glpi_plugin_vigilo_template'; |
||
94 | $options['7007']['field'] = 'template'; |
||
95 | $options['7007']['linkfield'] = 'id'; |
||
96 | $options['7007']['name'] = 'vigilo_template'; |
||
97 | $options['7007']['massiveaction'] = true; |
||
98 | $options['7007']['datatype'] = 'dropdown'; |
||
99 | |||
100 | return $options; |
||
101 | } |
||
102 | |||
103 | // Méthodes d'ajout / mise à jour / suppression de la supervision
|
||
104 | // pour un objet équipement supporté.
|
||
105 | public function delete($computer) |
||
106 | { |
||
107 | global $DB; |
||
108 | |||
109 | $this->unmonitor($computer->fields["name"]); |
||
110 | |||
111 | $query = "UPDATE `glpi_plugin_vigilo_config` SET `value` = 1 WHERE `key` = 'needs_deploy';"; |
||
112 | $DB->query($query); |
||
113 | } |
||
114 | |||
115 | public function update($item) |
||
116 | { |
||
117 | global $DB; |
||
118 | |||
119 | if (isset($item->oldvalues["name"])) { |
||
120 | $this->unmonitor($item->oldvalues["name"]); |
||
121 | } |
||
122 | |||
123 | $query = "UPDATE `glpi_plugin_vigilo_config` SET `value` = 1 WHERE `key` = 'needs_deploy';"; |
||
124 | $DB->query($query); |
||
125 | |||
126 | if ($item->getField("is_template")) { |
||
127 | return;
|
||
128 | } |
||
129 | |||
130 | $cls = "PluginVigiloMonitored" . $item->getType(); |
||
131 | if (class_exists($cls, true)) { |
||
132 | $obj = new $cls($item); |
||
133 | $this->writeVigiloConfig($obj, "hosts"); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | public function unmonitor($host) |
||
138 | { |
||
139 | $dirs = array(VIGILO_CONFDIR, "hosts", "managed", $host . ".xml"); |
||
140 | $filename = implode(DIRECTORY_SEPARATOR, $dirs); |
||
141 | if (file_exists($filename)) { |
||
142 | unlink($filename); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | // Méthodes de mise à jour d'un équipement
|
||
147 | // lorsque l'un de ses composants change.
|
||
148 | public function refreshSoftwareVersion($version) |
||
149 | { |
||
150 | $computer = new Computer(); |
||
151 | $computer->getFromDB($version->getField("computers_id")); |
||
152 | $this->update($computer); |
||
153 | } |
||
154 | |||
155 | public function refreshSoftware($software) |
||
156 | { |
||
157 | $softwareVer = new SoftwareVersion(); |
||
158 | $idSoftwareVersion = $softwareVer->find('softwares_id=' . $software->getID()); |
||
159 | foreach ($idSoftwareVersion as $idVersion) { |
||
160 | if (!$idVersion['id']) { |
||
161 | continue;
|
||
162 | } |
||
163 | |||
164 | $computerVer = new Computer_SoftwareVersion(); |
||
165 | $goodField = 'softwareversions_id=' . $idVersion['id']; |
||
166 | $updateComp = $computerVer->find($goodField); |
||
167 | |||
168 | foreach ($updateComp as $idComputer) { |
||
169 | if (-1 === $idComputer['computers_id']) { |
||
170 | continue;
|
||
171 | } |
||
172 | |||
173 | $computer = new Computer(); |
||
174 | $computer->getFromDB($idComputer['computers_id']); |
||
175 | $this->update($computer); |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | |||
180 | public function refreshDisk($disk) |
||
181 | { |
||
182 | $id = $disk->getField('computers_id'); |
||
183 | $computer = new Computer(); |
||
184 | $computer->getFromDB($id); |
||
185 | $this->update($computer); |
||
186 | } |
||
187 | |||
188 | public function refreshAddress($address) |
||
189 | { |
||
190 | $id = $address->getField('mainitems_id'); |
||
191 | $itemtype = $address->getField('mainitemtype'); |
||
192 | $item = new $itemtype(); |
||
193 | $item->getFromDB($id); |
||
194 | $this->update($item); |
||
195 | } |
||
196 | |||
197 | public function refreshDevice($device) |
||
198 | { |
||
199 | $id = $device->getField('items_id'); |
||
200 | $itemtype = $device->getField('itemtype'); |
||
201 | $item = new $itemtype(); |
||
202 | $item->getFromDB($id); |
||
203 | $this->update($item); |
||
204 | } |
||
205 | |||
206 | // Méthode de mise à jour en cas d'évolution de l'emplacement,
|
||
207 | // de l'entité ou du fabricant d'un équipement.
|
||
208 | public function updateGroups($obj) |
||
209 | { |
||
210 | $groups = new PluginVigiloGroups(); |
||
211 | $this->writeVigiloConfig($groups, "groups"); |
||
212 | } |
||
213 | } |