Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / Vigilo / VigiloHost.php @ 7225125e

History | View | Annotate | Download (8.11 KB)

1 bef4271a Thibault Louet
<?php
2
3
class VigiloHost extends VigiloXml
4
{
5
    protected $computer;
6
    protected $addresses;
7
    protected $ventilation;
8
    protected $children;
9
    protected $agent;
10
11
    public function __construct($computer)
12
    {
13
        $this->agent        = null;
14
        $this->ventilation  = "Servers";
15
        $this->computer     = $computer;
16
        $this->addresses    = array();
17
        $this->children     = array();
18
19
        if (class_exists('PluginFusioninventoryAgent')) {
20
            $agent = new PluginFusioninventoryAgent();
21 a5c9df40 Thibault Louet
            if ($agent->getAgentWithComputerid($this->computer->getID()) !== false) {
22 bef4271a Thibault Louet
                $this->agent = $agent;
23 a5c9df40 Thibault Louet
            }
24 bef4271a Thibault Louet
        }
25
26
        $this->selectTemplates();
27
        $this->selectGroups();
28
        $this->monitorMemory();
29
        $this->monitorNetworkInterfaces();
30
        $this->monitorSoftwares();
31
        $this->monitorPartitions();
32
    }
33
34
    public function getName()
35
    {
36
        return $this->computer->getName();
37
    }
38
39
    protected function selectTemplates()
40
    {
41 077e4de7 Francois POIROTTE
        $template_name = $this->computer->getField("template_name");
42 0e0753c8 Romain CHOLLET
43 ad6689da Francois POIROTTE
        if ($template_name && "N/A" !== $template_name) {
44 a5c9df40 Thibault Louet
            $this->children[] = new VigiloHostTemplate($this->computer->getField("template_name"));
45
        }
46 2ef80588 Romain CHOLLET
47
        $template_number = $this->computer->getField("vigilo_template");
48 ad6689da Francois POIROTTE
        if ('0' !== $template_number && 'N/A' !== $template_number) {
49 2ef80588 Romain CHOLLET
            $common_dbtm = new CommonDBTM();
50 0d761a49 Romain CHOLLET
            $template_name = PluginVigiloVigiloTemplate::getVigiloTemplateNameByID($template_number);
51
            $this->children[] = new VigiloHostTemplate($template_name);
52 077e4de7 Francois POIROTTE
        } elseif (empty($this->children)) {
53
            $template_name = "default";
54
            $this->children[] = new VigiloHostTemplate($template_name);
55 2ef80588 Romain CHOLLET
        }
56 bef4271a Thibault Louet
    }
57
58
    protected function selectGroups()
59
    {
60
        $location = new Location();
61
        $location->getFromDB($this->computer->fields["locations_id"]);
62 ad6689da Francois POIROTTE
        if ('N/A' != $location->getName()) {
63
            $locationCompleteName   = explode(" > ", $location->getField("completename"));
64
            $locationRealName       = implode("/", $locationCompleteName);
65
            $this->children[]       = new VigiloGroup($locationRealName);
66 a5c9df40 Thibault Louet
        }
67 bef4271a Thibault Louet
68 a5c9df40 Thibault Louet
        $entity = new Entity();
69
        $entity->getFromDB($this->computer->fields["entities_id"]);
70 ad6689da Francois POIROTTE
        if ('N/A' != $entity->getName()) {
71
            $entityCompleteName = explode(" > ", $entity->getField("completename"));
72
            $entityRealName     = implode("/", $entityCompleteName);
73
            $this->children[]   = new VigiloGroup($entityRealName);
74 a5c9df40 Thibault Louet
        }
75 bef4271a Thibault Louet
76 a5c9df40 Thibault Louet
        $manufacturer = new Manufacturer();
77
        $manufacturer->getFromDB($this->computer->fields["manufacturers_id"]);
78 ad6689da Francois POIROTTE
        if ('N/A' != $manufacturer->getName()) {
79 a5c9df40 Thibault Louet
            $this->children[] = new VigiloGroup($manufacturer->getName());
80
        }
81 bef4271a Thibault Louet
    }
82
83
    protected function selectAddress()
84
    {
85
        static $address = null;
86
87 ad6689da Francois POIROTTE
        if (null !== $address && $this->agent) {
88 bef4271a Thibault Louet
            $addresses = $this->agent->getIPs();
89 a5c9df40 Thibault Louet
            if (count($addresses)) {
90 bef4271a Thibault Louet
                $address = current($addresses);
91 a5c9df40 Thibault Louet
            }
92 bef4271a Thibault Louet
        }
93
94 ad6689da Francois POIROTTE
        if (null !== $address) {
95 bef4271a Thibault Louet
            $address = $this->computer->getName();
96
            foreach ($this->addresses as $addr) {
97 a5c9df40 Thibault Louet
                if (!$addr->is_ipv4()) {
98 bef4271a Thibault Louet
                    continue;
99 a5c9df40 Thibault Louet
                }
100 bef4271a Thibault Louet
101
                $textual = $addr->getTextual();
102
                if (is_string($textual)) {
103
                    $address = $textual;
104
                    break;
105
                }
106
            }
107
        }
108
109
        return $address;
110
    }
111
112
    protected function monitorMemory()
113
    {
114
        global $DB;
115
116
        $total = 0;
117
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
118
            $this->computer->getType(),
119
            $this->computer->getID()
120
        );
121
122
        foreach ($DB->query($query) as $mem) {
123
            $memory = new Item_DeviceMemory();
124
            $memory->getFromDB($mem['id']);
125
            $total += $memory->fields['size'] * 1024 * 1024;
126
        }
127
128 a5c9df40 Thibault Louet
        if ($total > 0) {
129 bef4271a Thibault Louet
            $this->children[] = new VigiloTest('RAM');
130 a5c9df40 Thibault Louet
        }
131 bef4271a Thibault Louet
    }
132
133
    protected function monitorNetworkInterfaces()
134
    {
135
        global $DB;
136
        $query = NetworkPort::getSQLRequestToSearchForItem(
137
            $this->computer->getType(),
138
            $this->computer->getID()
139
        );
140
141
        foreach ($DB->query($query) as $np) {
142 ad6689da Francois POIROTTE
            $query2     = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
143
            $port       = new NetworkPort();
144
            $ethport    = new NetworkPortEthernet();
145 bef4271a Thibault Louet
            $port->getFromDB($np['id']);
146 ad6689da Francois POIROTTE
            if ('lo' == $port->getName()) {
147 bef4271a Thibault Louet
                continue;
148 a5c9df40 Thibault Louet
            }
149 bef4271a Thibault Louet
150 ad6689da Francois POIROTTE
            $args       = array();
151 e809466b Francois POIROTTE
            $label      = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
152 ad6689da Francois POIROTTE
            $ethport    = $ethport->find('networkports_id=' . $np['id']);
153 a5c9df40 Thibault Louet
            foreach ($ethport as $rowEthPort) {
154
                if ($rowEthPort['speed']) {
155
                    $args[] = new VigiloArg('max', $rowEthPort['speed']);
156
                    break;
157
                }
158
            }
159 bef4271a Thibault Louet
            $args[] = new VigiloArg('label', $label);
160 a5c9df40 Thibault Louet
            $args[] = new VigiloArg('ifname', $port->getName());
161 bef4271a Thibault Louet
            $this->children[] = new VigiloTest('Interface', $args);
162
163
            // Retrieve all IP addresses associated with this interface.
164
            // This will be used later in selectAddress() to select
165
            // the most appropriate IP address to query this computer.
166
            foreach ($DB->query($query2) as $nn) {
167
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
168
                foreach ($DB->query($query3) as $ip) {
169
                    $addr = new IPAddress();
170 a5c9df40 Thibault Louet
                    if ($addr->getFromDB($ip['id'])) {
171 bef4271a Thibault Louet
                        $this->addresses[] = $addr;
172 a5c9df40 Thibault Louet
                    }
173 bef4271a Thibault Louet
                }
174
            }
175
        }
176
    }
177
178
    protected function monitorSoftwares()
179
    {
180 ad6689da Francois POIROTTE
        $listOfTest = new VigiloTestSoftware($this->computer);
181
        $computerSoftwareVersion = new Computer_SoftwareVersion();
182
        $ids = $computerSoftwareVersion->find('computers_id=' . $this->computer->getID());
183 a5c9df40 Thibault Louet
        foreach ($ids as $id) {
184
            if ($id['softwareversions_id']) {
185 ad6689da Francois POIROTTE
                $softwareVersion = new SoftwareVersion();
186
                $ids2 = $softwareVersion->find('id=' . $id['softwareversions_id']);
187 a5c9df40 Thibault Louet
                foreach ($ids2 as $id2) {
188
                    if ($id2['softwares_id']) {
189 ad6689da Francois POIROTTE
                        $software = new Software();
190 a5c9df40 Thibault Louet
                        $software->getFromDB($id2['softwares_id']);
191
                        $listOfTest->addRelevantTestWith($software->getName());
192
                    }
193
                }
194
            }
195
        }
196
        foreach ($listOfTest->getTable() as $test) {
197 ad6689da Francois POIROTTE
             $this->children[] = $test;
198 a5c9df40 Thibault Louet
        }
199 bef4271a Thibault Louet
    }
200
201
    protected function monitorPartitions()
202
    {
203
        global $DB;
204
205
        $query = ComputerDisk::getSQLRequestToSearchForItem(
206
            $this->computer->getType(),
207
            $this->computer->getID()
208
        );
209
210
        foreach ($DB->query($query) as $cd) {
211
            $disk = new ComputerDisk();
212
            $disk->getFromDB($cd['id']);
213
214
            $args = array();
215
            $args[] = new VigiloArg('label', $disk->getName());
216
            $args[] = new VigiloArg('partname', $disk->fields['mountpoint']);
217
            $total = $disk->fields['totalsize'];
218 a5c9df40 Thibault Louet
            if (!empty($total)) {
219 bef4271a Thibault Louet
                $args[] = new VigiloArg('max', $total * 1024 * 1024);
220 a5c9df40 Thibault Louet
            }
221 bef4271a Thibault Louet
            $this->children[] = new VigiloTest('Partition', $args);
222
        }
223
    }
224
225
    public function __toString()
226
    {
227 ad6689da Francois POIROTTE
        $outXML = new DOMDocument();
228
        $outXML->preserveWhiteSpace = false;
229
        $outXML->formatOutput       = true;
230 a5c9df40 Thibault Louet
        $outXML->loadXML(
231
            self::sprintf(
232
                '<?xml version="1.0"?>' .
233
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
234
                $this->computer->getName(),
235
                $this->selectAddress(),
236
                "Servers",
237
                $this->children
238
            )
239
        );
240
        return $outXML->saveXML();
241 bef4271a Thibault Louet
    }
242 a5c9df40 Thibault Louet
}