Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / Vigilo / VigiloHost.php @ b204adb9

History | View | Annotate | Download (8.38 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 0e0753c8 Romain CHOLLET
        $template_name = $this->computer->getField("template_name");
42
43
        if ($template_name && $template_name !== "N/A") {
44 a5c9df40 Thibault Louet
            $this->children[] = new VigiloHostTemplate($this->computer->getField("template_name"));
45
        }
46 bef4271a Thibault Louet
        $refs = array(
47
            "glpi_operatingsystems" => "operatingsystems_id",
48
            "glpi_operatingsystemversions" => "operatingsystemversions_id",
49
            "glpi_operatingsystemservicepacks" => "operatingsystemservicepacks_id",
50
        );
51
52
        $model = array();
53
        foreach ($refs as $table => $field) {
54
            $id = $this->computer->fields[$field];
55
            $value = Dropdown::getDropdownName($table, $id);
56 a5c9df40 Thibault Louet
            if ($value !== "" && $value !== null && $value !== "&nbsp;"
57 0e0753c8 Romain CHOLLET
                && $value !== false && $value !== "-----" && $value !== 'N/A'
58 a5c9df40 Thibault Louet
            ) {
59 bef4271a Thibault Louet
                $model[] = $value;
60
            }
61
        }
62
63 a5c9df40 Thibault Louet
        if (!count($model)) {
64 bef4271a Thibault Louet
            $model = "default";
65 a5c9df40 Thibault Louet
        } else {
66 bef4271a Thibault Louet
            $model = implode(" - ", $model);
67 a5c9df40 Thibault Louet
        }
68 bef4271a Thibault Louet
69
        $this->children[] = new VigiloHostTemplate($model);
70
    }
71
72
    protected function selectGroups()
73
    {
74
        $location = new Location();
75
        $location->getFromDB($this->computer->fields["locations_id"]);
76 a5c9df40 Thibault Louet
        if (!($location->getName()=='N/A')) {
77
            $locationCompleteName=explode(" > ", $location->getField("completename"));
78 0e0753c8 Romain CHOLLET
            $locationRealName=implode("/", $locationCompleteName);
79 a5c9df40 Thibault Louet
            $this->children[] = new VigiloGroup($locationRealName);
80
        }
81 bef4271a Thibault Louet
82 a5c9df40 Thibault Louet
        $entity = new Entity();
83
        $entity->getFromDB($this->computer->fields["entities_id"]);
84
        if (!($entity->getName()=='N/A')) {
85
            $entityCompleteName=explode(" > ", $entity->getField("completename"));
86 0e0753c8 Romain CHOLLET
            $entityRealName=implode("/", $entityCompleteName);
87 a5c9df40 Thibault Louet
            $this->children[] = new VigiloGroup($entityRealName);
88
        }
89 bef4271a Thibault Louet
90 a5c9df40 Thibault Louet
        $manufacturer = new Manufacturer();
91
        $manufacturer->getFromDB($this->computer->fields["manufacturers_id"]);
92
        if (!($manufacturer->getName()=='N/A')) {
93
            $this->children[] = new VigiloGroup($manufacturer->getName());
94
        }
95 bef4271a Thibault Louet
    }
96
97
    protected function selectAddress()
98
    {
99
        static $address = null;
100
101
        if ($address === null && $this->agent) {
102
            $addresses = $this->agent->getIPs();
103 a5c9df40 Thibault Louet
            if (count($addresses)) {
104 bef4271a Thibault Louet
                $address = current($addresses);
105 a5c9df40 Thibault Louet
            }
106 bef4271a Thibault Louet
        }
107
108
        if ($address === null) {
109
            $address = $this->computer->getName();
110
            foreach ($this->addresses as $addr) {
111 a5c9df40 Thibault Louet
                if (!$addr->is_ipv4()) {
112 bef4271a Thibault Louet
                    continue;
113 a5c9df40 Thibault Louet
                }
114 bef4271a Thibault Louet
115
                $textual = $addr->getTextual();
116
                if (is_string($textual)) {
117
                    $address = $textual;
118
                    break;
119
                }
120
            }
121
        }
122
123
        return $address;
124
    }
125
126
    protected function monitorMemory()
127
    {
128
        global $DB;
129
130
        $total = 0;
131
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
132
            $this->computer->getType(),
133
            $this->computer->getID()
134
        );
135
136
        foreach ($DB->query($query) as $mem) {
137
            $memory = new Item_DeviceMemory();
138
            $memory->getFromDB($mem['id']);
139
            $total += $memory->fields['size'] * 1024 * 1024;
140
        }
141
142 a5c9df40 Thibault Louet
        if ($total > 0) {
143 bef4271a Thibault Louet
            $this->children[] = new VigiloTest('RAM');
144 a5c9df40 Thibault Louet
        }
145 bef4271a Thibault Louet
    }
146
147
    protected function monitorNetworkInterfaces()
148
    {
149
        global $DB;
150
        $query = NetworkPort::getSQLRequestToSearchForItem(
151
            $this->computer->getType(),
152
            $this->computer->getID()
153
        );
154
155
        foreach ($DB->query($query) as $np) {
156
            $query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
157
            $port = new NetworkPort();
158 a5c9df40 Thibault Louet
            $ethport = new NetworkPortEthernet();
159 bef4271a Thibault Louet
            $port->getFromDB($np['id']);
160 a5c9df40 Thibault Louet
            if ($port->getName() == 'lo') {
161 bef4271a Thibault Louet
                continue;
162 a5c9df40 Thibault Louet
            }
163 bef4271a Thibault Louet
164
            $args   = array();
165
            $label  = isset($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
166 a5c9df40 Thibault Louet
            $ethport = $ethport->find('networkports_id=' . $np['id']);
167
            foreach ($ethport as $rowEthPort) {
168
                if ($rowEthPort['speed']) {
169
                    $args[] = new VigiloArg('max', $rowEthPort['speed']);
170
                    break;
171
                }
172
            }
173 bef4271a Thibault Louet
            $args[] = new VigiloArg('label', $label);
174 a5c9df40 Thibault Louet
            $args[] = new VigiloArg('ifname', $port->getName());
175 bef4271a Thibault Louet
            $this->children[] = new VigiloTest('Interface', $args);
176
177
            // Retrieve all IP addresses associated with this interface.
178
            // This will be used later in selectAddress() to select
179
            // the most appropriate IP address to query this computer.
180
            foreach ($DB->query($query2) as $nn) {
181
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
182
                foreach ($DB->query($query3) as $ip) {
183
                    $addr = new IPAddress();
184 a5c9df40 Thibault Louet
                    if ($addr->getFromDB($ip['id'])) {
185 bef4271a Thibault Louet
                        $this->addresses[] = $addr;
186 a5c9df40 Thibault Louet
                    }
187 bef4271a Thibault Louet
                }
188
            }
189
        }
190
    }
191
192
    protected function monitorSoftwares()
193
    {
194 a5c9df40 Thibault Louet
        global $DB;
195
        $listOfTest=new VigiloTestSoftware($this->computer);
196
        $computerSoftwareVersion=new Computer_SoftwareVersion();
197
        $ids=$computerSoftwareVersion->find('computers_id=' . $this->computer->getID());
198
        foreach ($ids as $id) {
199
            if ($id['softwareversions_id']) {
200
                $softwareVersion=new SoftwareVersion();
201
                $ids2=$softwareVersion->find('id=' . $id['softwareversions_id']);
202
                foreach ($ids2 as $id2) {
203
                    if ($id2['softwares_id']) {
204
                        $software=new Software();
205
                        $software->getFromDB($id2['softwares_id']);
206
                        $listOfTest->addRelevantTestWith($software->getName());
207
                    }
208
                }
209
            }
210
        }
211
        foreach ($listOfTest->getTable() as $test) {
212
             $this->children[]=$test;
213
        }
214 bef4271a Thibault Louet
    }
215
216
    protected function monitorPartitions()
217
    {
218
        global $DB;
219
220
        $query = ComputerDisk::getSQLRequestToSearchForItem(
221
            $this->computer->getType(),
222
            $this->computer->getID()
223
        );
224
225
        foreach ($DB->query($query) as $cd) {
226
            $disk = new ComputerDisk();
227
            $disk->getFromDB($cd['id']);
228
229
            $args = array();
230
            $args[] = new VigiloArg('label', $disk->getName());
231
            $args[] = new VigiloArg('partname', $disk->fields['mountpoint']);
232
            $total = $disk->fields['totalsize'];
233 a5c9df40 Thibault Louet
            if (!empty($total)) {
234 bef4271a Thibault Louet
                $args[] = new VigiloArg('max', $total * 1024 * 1024);
235 a5c9df40 Thibault Louet
            }
236 bef4271a Thibault Louet
            $this->children[] = new VigiloTest('Partition', $args);
237
        }
238
    }
239
240
    public function __toString()
241
    {
242
        $outXML=new DOMdocument();
243 a5c9df40 Thibault Louet
        $outXML->preserveWhiteSpace=false;
244
        $outXML->formatOutput=true;
245
        $outXML->loadXML(
246
            self::sprintf(
247
                '<?xml version="1.0"?>' .
248
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
249
                $this->computer->getName(),
250
                $this->selectAddress(),
251
                "Servers",
252
                $this->children
253
            )
254
        );
255
        return $outXML->saveXML();
256 bef4271a Thibault Louet
    }
257 a5c9df40 Thibault Louet
}