Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / Vigilo / VigiloHost.php @ 2ef80588

History | View | Annotate | Download (8.76 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') {
74
            $common_dbtm = new CommonDBTM();
75
            $template_name = $this->computer->getVigiloTemplateName($template_number);
76
        }
77
        else {
78
           $template_name = "default";
79
        }
80
        $this->children[] = new VigiloHostTemplate($template_name);
81
    }
82

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

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

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

    
108
    protected function selectAddress()
109
    {
110
        static $address = null;
111

    
112
        if ($address === null && $this->agent) {
113
            $addresses = $this->agent->getIPs();
114
            if (count($addresses)) {
115
                $address = current($addresses);
116
            }
117
        }
118

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

    
126
                $textual = $addr->getTextual();
127
                if (is_string($textual)) {
128
                    $address = $textual;
129
                    break;
130
                }
131
            }
132
        }
133

    
134
        return $address;
135
    }
136

    
137
    protected function monitorMemory()
138
    {
139
        global $DB;
140

    
141
        $total = 0;
142
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
143
            $this->computer->getType(),
144
            $this->computer->getID()
145
        );
146

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

    
153
        if ($total > 0) {
154
            $this->children[] = new VigiloTest('RAM');
155
        }
156
    }
157

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

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

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

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

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

    
227
    protected function monitorPartitions()
228
    {
229
        global $DB;
230

    
231
        $query = ComputerDisk::getSQLRequestToSearchForItem(
232
            $this->computer->getType(),
233
            $this->computer->getID()
234
        );
235

    
236
        foreach ($DB->query($query) as $cd) {
237
            $disk = new ComputerDisk();
238
            $disk->getFromDB($cd['id']);
239

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

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