Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / Vigilo / VigiloHost.php @ 0e0753c8

History | View | Annotate | Download (8.38 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
    protected function selectGroups()
73
    {
74
        $location = new Location();
75
        $location->getFromDB($this->computer->fields["locations_id"]);
76
        if (!($location->getName()=='N/A')) {
77
            $locationCompleteName=explode(" > ", $location->getField("completename"));
78
            $locationRealName=implode("/", $locationCompleteName);
79
            $this->children[] = new VigiloGroup($locationRealName);
80
        }
81

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

    
90
        $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
    }
96

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

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

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

    
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
        if ($total > 0) {
143
            $this->children[] = new VigiloTest('RAM');
144
        }
145
    }
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
            $ethport = new NetworkPortEthernet();
159
            $port->getFromDB($np['id']);
160
            if ($port->getName() == 'lo') {
161
                continue;
162
            }
163

    
164
            $args   = array();
165
            $label  = isset($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
166
            $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
            $args[] = new VigiloArg('label', $label);
174
            $args[] = new VigiloArg('ifname', $port->getName());
175
            $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
                    if ($addr->getFromDB($ip['id'])) {
185
                        $this->addresses[] = $addr;
186
                    }
187
                }
188
            }
189
        }
190
    }
191

    
192
    protected function monitorSoftwares()
193
    {
194
        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
    }
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
            if (!empty($total)) {
234
                $args[] = new VigiloArg('max', $total * 1024 * 1024);
235
            }
236
            $this->children[] = new VigiloTest('Partition', $args);
237
        }
238
    }
239

    
240
    public function __toString()
241
    {
242
        $outXML=new DOMdocument();
243
        $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
    }
257
}