Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / Vigilo / VigiloHost.php @ 0d761a49

History | View | Annotate | Download (8.95 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->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
        $template_name = $this->computer->getField("template_name");
42

    
43
        if ($template_name && $template_name !== "N/A") {
44
            $this->children[] = new VigiloHostTemplate($this->computer->getField("template_name"));
45
        }
46
        /*$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
            if ($value !== "" && $value !== null && $value !== "&nbsp;"
57
                && $value !== false && $value !== "-----" && $value !== 'N/A'
58
            ) {
59
                $model[] = $value;
60
            }
61
        }
62

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

69
        $this->children[] = new VigiloHostTemplate($model);*/
70
        
71

    
72
        $template_number = $this->computer->getField("vigilo_template");
73
        if ($template_number !== '0' && $template_number !== 'N/A') {
74
            $common_dbtm = new CommonDBTM();
75
            $template_name = PluginVigiloVigiloTemplate::getVigiloTemplateNameByID($template_number);
76
            $this->children[] = new VigiloHostTemplate($template_name);
77
        }
78
        else {
79
           if (empty($this->children)) {
80
               $template_name = "default";
81
               $this->children[] = new VigiloHostTemplate($template_name);
82
           }
83
        }
84
    }
85

    
86
    protected function selectGroups()
87
    {
88
        $location = new Location();
89
        $location->getFromDB($this->computer->fields["locations_id"]);
90
        if (!($location->getName()=='N/A')) {
91
            $locationCompleteName=explode(" > ", $location->getField("completename"));
92
            $locationRealName=implode("/", $locationCompleteName);
93
            $this->children[] = new VigiloGroup($locationRealName);
94
        }
95

    
96
        $entity = new Entity();
97
        $entity->getFromDB($this->computer->fields["entities_id"]);
98
        if (!($entity->getName()=='N/A')) {
99
            $entityCompleteName=explode(" > ", $entity->getField("completename"));
100
            $entityRealName=implode("/", $entityCompleteName);
101
            $this->children[] = new VigiloGroup($entityRealName);
102
        }
103

    
104
        $manufacturer = new Manufacturer();
105
        $manufacturer->getFromDB($this->computer->fields["manufacturers_id"]);
106
        if (!($manufacturer->getName()=='N/A')) {
107
            $this->children[] = new VigiloGroup($manufacturer->getName());
108
        }
109
    }
110

    
111
    protected function selectAddress()
112
    {
113
        static $address = null;
114

    
115
        if ($address === null && $this->agent) {
116
            $addresses = $this->agent->getIPs();
117
            if (count($addresses)) {
118
                $address = current($addresses);
119
            }
120
        }
121

    
122
        if ($address === null) {
123
            $address = $this->computer->getName();
124
            foreach ($this->addresses as $addr) {
125
                if (!$addr->is_ipv4()) {
126
                    continue;
127
                }
128

    
129
                $textual = $addr->getTextual();
130
                if (is_string($textual)) {
131
                    $address = $textual;
132
                    break;
133
                }
134
            }
135
        }
136

    
137
        return $address;
138
    }
139

    
140
    protected function monitorMemory()
141
    {
142
        global $DB;
143

    
144
        $total = 0;
145
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
146
            $this->computer->getType(),
147
            $this->computer->getID()
148
        );
149

    
150
        foreach ($DB->query($query) as $mem) {
151
            $memory = new Item_DeviceMemory();
152
            $memory->getFromDB($mem['id']);
153
            $total += $memory->fields['size'] * 1024 * 1024;
154
        }
155

    
156
        if ($total > 0) {
157
            $this->children[] = new VigiloTest('RAM');
158
        }
159
    }
160

    
161
    protected function monitorNetworkInterfaces()
162
    {
163
        global $DB;
164
        $query = NetworkPort::getSQLRequestToSearchForItem(
165
            $this->computer->getType(),
166
            $this->computer->getID()
167
        );
168

    
169
        foreach ($DB->query($query) as $np) {
170
            $query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
171
            $port = new NetworkPort();
172
            $ethport = new NetworkPortEthernet();
173
            $port->getFromDB($np['id']);
174
            if ($port->getName() == 'lo') {
175
                continue;
176
            }
177

    
178
            $args   = array();
179
            $label  = isset($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
180
            $ethport = $ethport->find('networkports_id=' . $np['id']);
181
            foreach ($ethport as $rowEthPort) {
182
                if ($rowEthPort['speed']) {
183
                    $args[] = new VigiloArg('max', $rowEthPort['speed']);
184
                    break;
185
                }
186
            }
187
            $args[] = new VigiloArg('label', $label);
188
            $args[] = new VigiloArg('ifname', $port->getName());
189
            $this->children[] = new VigiloTest('Interface', $args);
190

    
191
            // Retrieve all IP addresses associated with this interface.
192
            // This will be used later in selectAddress() to select
193
            // the most appropriate IP address to query this computer.
194
            foreach ($DB->query($query2) as $nn) {
195
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
196
                foreach ($DB->query($query3) as $ip) {
197
                    $addr = new IPAddress();
198
                    if ($addr->getFromDB($ip['id'])) {
199
                        $this->addresses[] = $addr;
200
                    }
201
                }
202
            }
203
        }
204
    }
205

    
206
    protected function monitorSoftwares()
207
    {
208
        global $DB;
209
        $listOfTest=new VigiloTestSoftware($this->computer);
210
        $computerSoftwareVersion=new Computer_SoftwareVersion();
211
        $ids=$computerSoftwareVersion->find('computers_id=' . $this->computer->getID());
212
        foreach ($ids as $id) {
213
            if ($id['softwareversions_id']) {
214
                $softwareVersion=new SoftwareVersion();
215
                $ids2=$softwareVersion->find('id=' . $id['softwareversions_id']);
216
                foreach ($ids2 as $id2) {
217
                    if ($id2['softwares_id']) {
218
                        $software=new Software();
219
                        $software->getFromDB($id2['softwares_id']);
220
                        $listOfTest->addRelevantTestWith($software->getName());
221
                    }
222
                }
223
            }
224
        }
225
        foreach ($listOfTest->getTable() as $test) {
226
             $this->children[]=$test;
227
        }
228
    }
229

    
230
    protected function monitorPartitions()
231
    {
232
        global $DB;
233

    
234
        $query = ComputerDisk::getSQLRequestToSearchForItem(
235
            $this->computer->getType(),
236
            $this->computer->getID()
237
        );
238

    
239
        foreach ($DB->query($query) as $cd) {
240
            $disk = new ComputerDisk();
241
            $disk->getFromDB($cd['id']);
242

    
243
            $args = array();
244
            $args[] = new VigiloArg('label', $disk->getName());
245
            $args[] = new VigiloArg('partname', $disk->fields['mountpoint']);
246
            $total = $disk->fields['totalsize'];
247
            if (!empty($total)) {
248
                $args[] = new VigiloArg('max', $total * 1024 * 1024);
249
            }
250
            $this->children[] = new VigiloTest('Partition', $args);
251
        }
252
    }
253

    
254
    public function __toString()
255
    {
256
        $outXML=new DOMdocument();
257
        $outXML->preserveWhiteSpace=false;
258
        $outXML->formatOutput=true;
259
        $outXML->loadXML(
260
            self::sprintf(
261
                '<?xml version="1.0"?>' .
262
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
263
                $this->computer->getName(),
264
                $this->selectAddress(),
265
                "Servers",
266
                $this->children
267
            )
268
        );
269
        return $outXML->saveXML();
270
    }
271
}