Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.57 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
        $this->update($item);
52
    }
53
54
    public function itemPurge($item)
55
    {
56
        global $DB;
57
58
        $id         = $item->getID();
59 cdde5484 Francois POIROTTE
        $query      = "DELETE FROM `glpi_plugin_vigilo_template` WHERE `id` = $id;";
60 166be9d2 Francois POIROTTE
        $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
    // Méthodes d'ajout / mise à jour / suppression de la supervision
83
    // pour un objet équipement supporté.
84
    public function delete($computer)
85
    {
86
        global $DB;
87
88
        $this->unmonitor($computer->fields["name"]);
89
90
        $query = "UPDATE `glpi_plugin_vigilo_config` SET `value` = 1 WHERE `key` = 'needs_deploy';";
91
        $DB->query($query);
92
    }
93
94
    public function update($item)
95
    {
96
        global $DB;
97
98
        if (isset($item->oldvalues["name"])) {
99
            $this->unmonitor($item->oldvalues["name"]);
100
        }
101
102
        $query = "UPDATE `glpi_plugin_vigilo_config` SET `value` = 1 WHERE `key` = 'needs_deploy';";
103
        $DB->query($query);
104
105
        if ($item->getField("is_template")) {
106
            return;
107
        }
108
109
        $cls = "PluginVigiloMonitored" . $item->getType();
110
        if (class_exists($cls, true)) {
111
            $obj = new $cls($item);
112
            $this->writeVigiloConfig($obj, "hosts");
113
        }
114
    }
115
116
    public function unmonitor($host)
117
    {
118
        $dirs = array(VIGILO_CONFDIR, "hosts", "managed", $host . ".xml");
119
        $filename = implode(DIRECTORY_SEPARATOR, $dirs);
120
        if (file_exists($filename)) {
121
            unlink($filename);
122
        }
123
    }
124
125
    // Méthodes de mise à jour d'un équipement
126
    // lorsque l'un de ses composants change.
127
    public function refreshSoftwareVersion($version)
128
    {
129
        $computer = new Computer();
130
        $computer->getFromDB($version->getField("computers_id"));
131
        $this->update($computer);
132
    }
133
134
    public function refreshSoftware($software)
135
    {
136 c60a37ae Francois POIROTTE
        $softwareVer    = new SoftwareVersion();
137
        $versions       = $softwareVer->find('softwares_id=' . $software->getID());
138
        foreach ($versions as $version) {
139
            if (!$version['id']) {
140 166be9d2 Francois POIROTTE
                continue;
141
            }
142
143 c60a37ae Francois POIROTTE
            $installations  = new Computer_SoftwareVersion();
144
            $filter         = 'softwareversions_id=' . $version['id'];
145
            $installations  = $installations->find($filter);
146
            foreach ($installations as $installation) {
147
                if (-1 === $installation['computers_id']) {
148 166be9d2 Francois POIROTTE
                    continue;
149
                }
150
151
                $computer = new Computer();
152 c60a37ae Francois POIROTTE
                $computer->getFromDB($installation['computers_id']);
153 166be9d2 Francois POIROTTE
                $this->update($computer);
154
            }
155
        }
156
    }
157
158
    public function refreshDisk($disk)
159
    {
160
        $id = $disk->getField('computers_id');
161
        $computer = new Computer();
162
        $computer->getFromDB($id);
163
        $this->update($computer);
164
    }
165
166
    public function refreshAddress($address)
167
    {
168
        $id         = $address->getField('mainitems_id');
169
        $itemtype   = $address->getField('mainitemtype');
170
        $item       = new $itemtype();
171
        $item->getFromDB($id);
172
        $this->update($item);
173
    }
174
175
    public function refreshDevice($device)
176
    {
177
        $id         = $device->getField('items_id');
178
        $itemtype   = $device->getField('itemtype');
179
        $item       = new $itemtype();
180
        $item->getFromDB($id);
181
        $this->update($item);
182
    }
183
184
    // Méthode de mise à jour en cas d'évolution de l'emplacement,
185
    // de l'entité ou du fabricant d'un équipement.
186
    public function updateGroups($obj)
187
    {
188
        $groups = new PluginVigiloGroups();
189
        $this->writeVigiloConfig($groups, "groups");
190
    }
191
}