Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / plugins / vigilo / vigilo_hooks.php @ 8ef154dd

History | View | Annotate | Download (7.29 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 cdde5484 Francois POIROTTE
WHERE `id` = $id;
16 166be9d2 Francois POIROTTE
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 cdde5484 Francois POIROTTE
INSERT INTO `glpi_plugin_vigilo_template`(`id`, `template`)
42 166be9d2 Francois POIROTTE
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 e1c378c0 Francois POIROTTE
        // Si la mise à jour modifie le technicien associé à la machine,
52
        // il peut-être nécessaire de mettre à jour les groupes de Vigilo.
53
        if (!empty($item->fields['users_id_tech'])) {
54
            $this->updateGroups(null);
55
        }
56
57 166be9d2 Francois POIROTTE
        $this->update($item);
58
    }
59
60
    public function itemPurge($item)
61
    {
62
        global $DB;
63
64
        $id         = $item->getID();
65 cdde5484 Francois POIROTTE
        $query      = "DELETE FROM `glpi_plugin_vigilo_template` WHERE `id` = $id;";
66 166be9d2 Francois POIROTTE
        $DB->query($query);
67
        $this->unmonitor($item->getField('name'));
68 e1c378c0 Francois POIROTTE
69
        // Si la mise à jour modifie le technicien associé à la machine,
70
        // il peut-être nécessaire de mettre à jour les groupes de Vigilo.
71
        if (!empty($item->fields['users_id_tech'])) {
72
            $this->updateGroups(null);
73
        }
74 166be9d2 Francois POIROTTE
    }
75
76
    // Méthodes outils / annexes
77
    public function writeVigiloConfig($obj, $objtype)
78
    {
79
        $dirs       = array(VIGILO_CONFDIR, $objtype, "managed");
80
        $confdir    = implode(DIRECTORY_SEPARATOR, $dirs);
81
        $file       = $confdir . DIRECTORY_SEPARATOR . $obj->getName() . ".xml";
82
83
        if (!file_exists($confdir)) {
84
            mkdir($confdir, 0770, true);
85
        }
86
87 92b19eed Francois POIROTTE
        $outXML = new DOMDocument();
88
        $outXML->preserveWhiteSpace = false;
89
        $outXML->formatOutput       = true;
90
        $outXML->loadXML((string) $obj);
91
        $res = file_put_contents($file, $outXML->saveXML(), LOCK_EX);
92 166be9d2 Francois POIROTTE
        if (false !== $res) {
93 0ff49d9f Francois POIROTTE
            @chgrp($file, "vigiconf");
94
            @chmod($file, 0660);
95 166be9d2 Francois POIROTTE
        }
96
    }
97
98
    // Méthodes d'ajout / mise à jour / suppression de la supervision
99
    // pour un objet équipement supporté.
100
    public function delete($computer)
101
    {
102
        global $DB;
103
104
        $this->unmonitor($computer->fields["name"]);
105
106
        $query = "UPDATE `glpi_plugin_vigilo_config` SET `value` = 1 WHERE `key` = 'needs_deploy';";
107
        $DB->query($query);
108
    }
109
110
    public function update($item)
111
    {
112
        global $DB;
113
114
        if (isset($item->oldvalues["name"])) {
115
            $this->unmonitor($item->oldvalues["name"]);
116
        }
117
118
        $query = "UPDATE `glpi_plugin_vigilo_config` SET `value` = 1 WHERE `key` = 'needs_deploy';";
119
        $DB->query($query);
120
121 c13f1c28 Francois POIROTTE
        // "is_template" vaut "1" (sous forme de chaîne de caractères)
122
        // lorsque l'objet passé fait référence à un modèle dans GLPI,
123
        // et "0" lorsque ce n'est pas le cas.
124
        // MAIS, il peut aussi valoir NOT_AVAILABLE ("N/A") lors de la création
125
        // d'un nouvel objet (car l'attribut n'est pas encore défini).
126
        // Le cast sur le champ permet de gérer ces 3 cas.
127
        if ((int) $item->getField("is_template")) {
128 166be9d2 Francois POIROTTE
            return;
129
        }
130
131
        $cls = "PluginVigiloMonitored" . $item->getType();
132
        if (class_exists($cls, true)) {
133
            $obj = new $cls($item);
134 92b19eed Francois POIROTTE
135
            // Ecriture du fichier de configuration principal (host).
136 166be9d2 Francois POIROTTE
            $this->writeVigiloConfig($obj, "hosts");
137 92b19eed Francois POIROTTE
138
            // Création d'un service de haut niveau "services:<nom>"
139
            // qui affichera le pire état des services de la machine "<nom>",
140
            // et d'un service "machine:<nom>" qui affichera le pire état
141
            // entre l'état de la machine "<nom>" et de ses services.
142
            $hls = new PluginVigiloHls($obj);
143
            $this->writeVigiloConfig($hls, "hlservices");
144 166be9d2 Francois POIROTTE
        }
145
    }
146
147
    public function unmonitor($host)
148
    {
149 22344d19 Francois POIROTTE
        $dirs = array(
150
            array(VIGILO_CONFDIR, "hosts", "managed", $host . ".xml"),
151
            array(VIGILO_CONFDIR, "hlservices", "managed", $host . ".xml"),
152
        );
153
        foreach ($dirs as $components) {
154
            $filename = implode(DIRECTORY_SEPARATOR, $components);
155
            if (file_exists($filename)) {
156
                unlink($filename);
157
            }
158 166be9d2 Francois POIROTTE
        }
159
    }
160
161
    // Méthodes de mise à jour d'un équipement
162
    // lorsque l'un de ses composants change.
163
    public function refreshSoftwareVersion($version)
164
    {
165
        $computer = new Computer();
166
        $computer->getFromDB($version->getField("computers_id"));
167
        $this->update($computer);
168
    }
169
170
    public function refreshSoftware($software)
171
    {
172 c60a37ae Francois POIROTTE
        $softwareVer    = new SoftwareVersion();
173
        $versions       = $softwareVer->find('softwares_id=' . $software->getID());
174
        foreach ($versions as $version) {
175
            if (!$version['id']) {
176 166be9d2 Francois POIROTTE
                continue;
177
            }
178
179 c60a37ae Francois POIROTTE
            $installations  = new Computer_SoftwareVersion();
180
            $filter         = 'softwareversions_id=' . $version['id'];
181
            $installations  = $installations->find($filter);
182
            foreach ($installations as $installation) {
183
                if (-1 === $installation['computers_id']) {
184 166be9d2 Francois POIROTTE
                    continue;
185
                }
186
187
                $computer = new Computer();
188 c60a37ae Francois POIROTTE
                $computer->getFromDB($installation['computers_id']);
189 166be9d2 Francois POIROTTE
                $this->update($computer);
190
            }
191
        }
192
    }
193
194
    public function refreshDisk($disk)
195
    {
196
        $id = $disk->getField('computers_id');
197
        $computer = new Computer();
198
        $computer->getFromDB($id);
199
        $this->update($computer);
200
    }
201
202
    public function refreshAddress($address)
203
    {
204
        $id         = $address->getField('mainitems_id');
205
        $itemtype   = $address->getField('mainitemtype');
206
        $item       = new $itemtype();
207
        $item->getFromDB($id);
208
        $this->update($item);
209
    }
210
211
    public function refreshDevice($device)
212
    {
213
        $id         = $device->getField('items_id');
214
        $itemtype   = $device->getField('itemtype');
215
        $item       = new $itemtype();
216
        $item->getFromDB($id);
217
        $this->update($item);
218
    }
219
220
    // Méthode de mise à jour en cas d'évolution de l'emplacement,
221
    // de l'entité ou du fabricant d'un équipement.
222
    public function updateGroups($obj)
223
    {
224
        $groups = new PluginVigiloGroups();
225
        $this->writeVigiloConfig($groups, "groups");
226
    }
227
}