Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / Vigilo / VigiloHost.php @ a5c9df40

History | View | Annotate | Download (8.46 KB)

1
<?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
            if ($agent->getAgentWithComputerid($this->computer->getID()) !== false) {
22
                $this->agent = $agent;
23
            }
24
        }
25

    
26
        $this->selectTemplates();
27
        $this->selectGroups();
28
        $this->monitorProcessor();
29
        $this->monitorMemory();
30
        $this->monitorNetworkInterfaces();
31
        $this->monitorSoftwares();
32
        $this->monitorPartitions();
33
    }
34

    
35
    public function getName()
36
    {
37
        return $this->computer->getName();
38
    }
39

    
40
    protected function selectTemplates()
41
    {
42
        if ($this->computer->getField("template_name")) {
43
            $this->children[] = new VigiloHostTemplate($this->computer->getField("template_name"));
44
        }
45
        $refs = array(
46
            "glpi_operatingsystems" => "operatingsystems_id",
47
            "glpi_operatingsystemversions" => "operatingsystemversions_id",
48
            "glpi_operatingsystemservicepacks" => "operatingsystemservicepacks_id",
49
        );
50

    
51
        $model = array();
52
        foreach ($refs as $table => $field) {
53
            $id = $this->computer->fields[$field];
54
            $value = Dropdown::getDropdownName($table, $id);
55
            if ($value !== "" && $value !== null && $value !== "&nbsp;"
56
                && $value !== false && $value !== "-----"
57
            ) {
58
                $model[] = $value;
59
            }
60
        }
61

    
62
        if (!count($model)) {
63
            $model = "default";
64
        } else {
65
            $model = implode(" - ", $model);
66
        }
67

    
68
        $this->children[] = new VigiloHostTemplate($model);
69
    }
70

    
71
    protected function selectGroups()
72
    {
73
        $location = new Location();
74
        $location->getFromDB($this->computer->fields["locations_id"]);
75
        if (!($location->getName()=='N/A')) {
76
            $locationCompleteName=explode(" > ", $location->getField("completename"));
77
            $locationRealName="/" . implode("/", $locationCompleteName);
78
            $this->children[] = new VigiloGroup($locationRealName);
79
        }
80

    
81
        $entity = new Entity();
82
        $entity->getFromDB($this->computer->fields["entities_id"]);
83
        if (!($entity->getName()=='N/A')) {
84
            $entityCompleteName=explode(" > ", $entity->getField("completename"));
85
            $entityRealName="/" . implode("/", $entityCompleteName);
86
            $this->children[] = new VigiloGroup($entityRealName);
87
        }
88

    
89
        $manufacturer = new Manufacturer();
90
        $manufacturer->getFromDB($this->computer->fields["manufacturers_id"]);
91
        if (!($manufacturer->getName()=='N/A')) {
92
            $this->children[] = new VigiloGroup($manufacturer->getName());
93
        }
94
    }
95

    
96
    protected function selectAddress()
97
    {
98
        static $address = null;
99

    
100
        if ($address === null && $this->agent) {
101
            $addresses = $this->agent->getIPs();
102
            if (count($addresses)) {
103
                $address = current($addresses);
104
            }
105
        }
106

    
107
        if ($address === null) {
108
            $address = $this->computer->getName();
109
            foreach ($this->addresses as $addr) {
110
                if (!$addr->is_ipv4()) {
111
                    continue;
112
                }
113

    
114
                $textual = $addr->getTextual();
115
                if (is_string($textual)) {
116
                    $address = $textual;
117
                    break;
118
                }
119
            }
120
        }
121

    
122
        return $address;
123
    }
124

    
125
    protected function monitorProcessor()
126
    {
127
        $this->children[] = new VigiloTest('CPU');
128
    }
129

    
130
    protected function monitorMemory()
131
    {
132
        global $DB;
133

    
134
        $total = 0;
135
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
136
            $this->computer->getType(),
137
            $this->computer->getID()
138
        );
139

    
140
        foreach ($DB->query($query) as $mem) {
141
            $memory = new Item_DeviceMemory();
142
            $memory->getFromDB($mem['id']);
143
            $total += $memory->fields['size'] * 1024 * 1024;
144
        }
145

    
146
        if ($total > 0) {
147
            $this->children[] = new VigiloTest('RAM');
148
        }
149
    }
150

    
151
    protected function monitorNetworkInterfaces()
152
    {
153
        global $DB;
154
        $query = NetworkPort::getSQLRequestToSearchForItem(
155
            $this->computer->getType(),
156
            $this->computer->getID()
157
        );
158

    
159
        foreach ($DB->query($query) as $np) {
160
            $query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
161
            $port = new NetworkPort();
162
            $ethport = new NetworkPortEthernet();
163
            $port->getFromDB($np['id']);
164
            if ($port->getName() == 'lo') {
165
                continue;
166
            }
167

    
168
            $args   = array();
169
            $label  = isset($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
170
            $ethport = $ethport->find('networkports_id=' . $np['id']);
171
            foreach ($ethport as $rowEthPort) {
172
                if ($rowEthPort['speed']) {
173
                    $args[] = new VigiloArg('max', $rowEthPort['speed']);
174
                    break;
175
                }
176
            }
177
            $args[] = new VigiloArg('label', $label);
178
            $args[] = new VigiloArg('ifname', $port->getName());
179
            ;
180
            $this->children[] = new VigiloTest('Interface', $args);
181

    
182
            // Retrieve all IP addresses associated with this interface.
183
            // This will be used later in selectAddress() to select
184
            // the most appropriate IP address to query this computer.
185
            foreach ($DB->query($query2) as $nn) {
186
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
187
                foreach ($DB->query($query3) as $ip) {
188
                    $addr = new IPAddress();
189
                    if ($addr->getFromDB($ip['id'])) {
190
                        $this->addresses[] = $addr;
191
                    }
192
                }
193
            }
194
        }
195
    }
196

    
197
    protected function monitorSoftwares()
198
    {
199
        global $DB;
200
        $listOfTest=new VigiloTestSoftware($this->computer);
201
        $computerSoftwareVersion=new Computer_SoftwareVersion();
202
        $ids=$computerSoftwareVersion->find('computers_id=' . $this->computer->getID());
203
        foreach ($ids as $id) {
204
            if ($id['softwareversions_id']) {
205
                $softwareVersion=new SoftwareVersion();
206
                $ids2=$softwareVersion->find('id=' . $id['softwareversions_id']);
207
                foreach ($ids2 as $id2) {
208
                    if ($id2['softwares_id']) {
209
                        $software=new Software();
210
                        $software->getFromDB($id2['softwares_id']);
211
                        $listOfTest->addRelevantTestWith($software->getName());
212
                    }
213
                }
214
            }
215
        }
216
        foreach ($listOfTest->getTable() as $test) {
217
             $this->children[]=$test;
218
        }
219
    }
220

    
221
    protected function monitorPartitions()
222
    {
223
        global $DB;
224

    
225
        $query = ComputerDisk::getSQLRequestToSearchForItem(
226
            $this->computer->getType(),
227
            $this->computer->getID()
228
        );
229

    
230
        foreach ($DB->query($query) as $cd) {
231
            $disk = new ComputerDisk();
232
            $disk->getFromDB($cd['id']);
233

    
234
            $args = array();
235
            $args[] = new VigiloArg('label', $disk->getName());
236
            $args[] = new VigiloArg('partname', $disk->fields['mountpoint']);
237
            $total = $disk->fields['totalsize'];
238
            if (!empty($total)) {
239
                $args[] = new VigiloArg('max', $total * 1024 * 1024);
240
            }
241
            $this->children[] = new VigiloTest('Partition', $args);
242
        }
243
    }
244

    
245
    public function __toString()
246
    {
247
        $outXML=new DOMdocument();
248
        $outXML->preserveWhiteSpace=false;
249
        $outXML->formatOutput=true;
250
        $outXML->loadXML(
251
            self::sprintf(
252
                '<?xml version="1.0"?>' .
253
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
254
                $this->computer->getName(),
255
                $this->selectAddress(),
256
                "Servers",
257
                $this->children
258
            )
259
        );
260
        return $outXML->saveXML();
261
    }
262
}