Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / plugins / vigilo / vigilo_hooks.php @ e1c378c0

History | View | Annotate | Download (6.07 KB)

1
<?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 `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`(`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
        // 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
        $this->update($item);
58
    }
59

    
60
    public function itemPurge($item)
61
    {
62
        global $DB;
63

    
64
        $id         = $item->getID();
65
        $query      = "DELETE FROM `glpi_plugin_vigilo_template` WHERE `id` = $id;";
66
        $DB->query($query);
67
        $this->unmonitor($item->getField('name'));
68

    
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
    }
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
        $res = file_put_contents($file, $obj, LOCK_EX);
88
        if (false !== $res) {
89
            @chgrp($file, "vigiconf");
90
            @chmod($file, 0660);
91
        }
92
    }
93

    
94
    // Méthodes d'ajout / mise à jour / suppression de la supervision
95
    // pour un objet équipement supporté.
96
    public function delete($computer)
97
    {
98
        global $DB;
99

    
100
        $this->unmonitor($computer->fields["name"]);
101

    
102
        $query = "UPDATE `glpi_plugin_vigilo_config` SET `value` = 1 WHERE `key` = 'needs_deploy';";
103
        $DB->query($query);
104
    }
105

    
106
    public function update($item)
107
    {
108
        global $DB;
109

    
110
        if (isset($item->oldvalues["name"])) {
111
            $this->unmonitor($item->oldvalues["name"]);
112
        }
113

    
114
        $query = "UPDATE `glpi_plugin_vigilo_config` SET `value` = 1 WHERE `key` = 'needs_deploy';";
115
        $DB->query($query);
116

    
117
        if ($item->getField("is_template")) {
118
            return;
119
        }
120

    
121
        $cls = "PluginVigiloMonitored" . $item->getType();
122
        if (class_exists($cls, true)) {
123
            $obj = new $cls($item);
124
            $this->writeVigiloConfig($obj, "hosts");
125
        }
126
    }
127

    
128
    public function unmonitor($host)
129
    {
130
        $dirs = array(VIGILO_CONFDIR, "hosts", "managed", $host . ".xml");
131
        $filename = implode(DIRECTORY_SEPARATOR, $dirs);
132
        if (file_exists($filename)) {
133
            unlink($filename);
134
        }
135
    }
136

    
137
    // Méthodes de mise à jour d'un équipement
138
    // lorsque l'un de ses composants change.
139
    public function refreshSoftwareVersion($version)
140
    {
141
        $computer = new Computer();
142
        $computer->getFromDB($version->getField("computers_id"));
143
        $this->update($computer);
144
    }
145

    
146
    public function refreshSoftware($software)
147
    {
148
        $softwareVer    = new SoftwareVersion();
149
        $versions       = $softwareVer->find('softwares_id=' . $software->getID());
150
        foreach ($versions as $version) {
151
            if (!$version['id']) {
152
                continue;
153
            }
154

    
155
            $installations  = new Computer_SoftwareVersion();
156
            $filter         = 'softwareversions_id=' . $version['id'];
157
            $installations  = $installations->find($filter);
158
            foreach ($installations as $installation) {
159
                if (-1 === $installation['computers_id']) {
160
                    continue;
161
                }
162

    
163
                $computer = new Computer();
164
                $computer->getFromDB($installation['computers_id']);
165
                $this->update($computer);
166
            }
167
        }
168
    }
169

    
170
    public function refreshDisk($disk)
171
    {
172
        $id = $disk->getField('computers_id');
173
        $computer = new Computer();
174
        $computer->getFromDB($id);
175
        $this->update($computer);
176
    }
177

    
178
    public function refreshAddress($address)
179
    {
180
        $id         = $address->getField('mainitems_id');
181
        $itemtype   = $address->getField('mainitemtype');
182
        $item       = new $itemtype();
183
        $item->getFromDB($id);
184
        $this->update($item);
185
    }
186

    
187
    public function refreshDevice($device)
188
    {
189
        $id         = $device->getField('items_id');
190
        $itemtype   = $device->getField('itemtype');
191
        $item       = new $itemtype();
192
        $item->getFromDB($id);
193
        $this->update($item);
194
    }
195

    
196
    // Méthode de mise à jour en cas d'évolution de l'emplacement,
197
    // de l'entité ou du fabricant d'un équipement.
198
    public function updateGroups($obj)
199
    {
200
        $groups = new PluginVigiloGroups();
201
        $this->writeVigiloConfig($groups, "groups");
202
    }
203
}