Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / Vigilo / VigiloHost.php @ e809466b

History | View | Annotate | Download (8.11 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 && "N/A" !== $template_name) {
44
            $this->children[] = new VigiloHostTemplate($this->computer->getField("template_name"));
45
        }
46

    
47
        $template_number = $this->computer->getField("vigilo_template");
48
        if ('0' !== $template_number && 'N/A' !== $template_number) {
49
            $common_dbtm = new CommonDBTM();
50
            $template_name = PluginVigiloVigiloTemplate::getVigiloTemplateNameByID($template_number);
51
            $this->children[] = new VigiloHostTemplate($template_name);
52
        } elseif (empty($this->children)) {
53
            $template_name = "default";
54
            $this->children[] = new VigiloHostTemplate($template_name);
55
        }
56
    }
57

    
58
    protected function selectGroups()
59
    {
60
        $location = new Location();
61
        $location->getFromDB($this->computer->fields["locations_id"]);
62
        if ('N/A' != $location->getName()) {
63
            $locationCompleteName   = explode(" > ", $location->getField("completename"));
64
            $locationRealName       = implode("/", $locationCompleteName);
65
            $this->children[]       = new VigiloGroup($locationRealName);
66
        }
67

    
68
        $entity = new Entity();
69
        $entity->getFromDB($this->computer->fields["entities_id"]);
70
        if ('N/A' != $entity->getName()) {
71
            $entityCompleteName = explode(" > ", $entity->getField("completename"));
72
            $entityRealName     = implode("/", $entityCompleteName);
73
            $this->children[]   = new VigiloGroup($entityRealName);
74
        }
75

    
76
        $manufacturer = new Manufacturer();
77
        $manufacturer->getFromDB($this->computer->fields["manufacturers_id"]);
78
        if ('N/A' != $manufacturer->getName()) {
79
            $this->children[] = new VigiloGroup($manufacturer->getName());
80
        }
81
    }
82

    
83
    protected function selectAddress()
84
    {
85
        static $address = null;
86

    
87
        if (null !== $address && $this->agent) {
88
            $addresses = $this->agent->getIPs();
89
            if (count($addresses)) {
90
                $address = current($addresses);
91
            }
92
        }
93

    
94
        if (null !== $address) {
95
            $address = $this->computer->getName();
96
            foreach ($this->addresses as $addr) {
97
                if (!$addr->is_ipv4()) {
98
                    continue;
99
                }
100

    
101
                $textual = $addr->getTextual();
102
                if (is_string($textual)) {
103
                    $address = $textual;
104
                    break;
105
                }
106
            }
107
        }
108

    
109
        return $address;
110
    }
111

    
112
    protected function monitorMemory()
113
    {
114
        global $DB;
115

    
116
        $total = 0;
117
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
118
            $this->computer->getType(),
119
            $this->computer->getID()
120
        );
121

    
122
        foreach ($DB->query($query) as $mem) {
123
            $memory = new Item_DeviceMemory();
124
            $memory->getFromDB($mem['id']);
125
            $total += $memory->fields['size'] * 1024 * 1024;
126
        }
127

    
128
        if ($total > 0) {
129
            $this->children[] = new VigiloTest('RAM');
130
        }
131
    }
132

    
133
    protected function monitorNetworkInterfaces()
134
    {
135
        global $DB;
136
        $query = NetworkPort::getSQLRequestToSearchForItem(
137
            $this->computer->getType(),
138
            $this->computer->getID()
139
        );
140

    
141
        foreach ($DB->query($query) as $np) {
142
            $query2     = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
143
            $port       = new NetworkPort();
144
            $ethport    = new NetworkPortEthernet();
145
            $port->getFromDB($np['id']);
146
            if ('lo' == $port->getName()) {
147
                continue;
148
            }
149

    
150
            $args       = array();
151
            $label      = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
152
            $ethport    = $ethport->find('networkports_id=' . $np['id']);
153
            foreach ($ethport as $rowEthPort) {
154
                if ($rowEthPort['speed']) {
155
                    $args[] = new VigiloArg('max', $rowEthPort['speed']);
156
                    break;
157
                }
158
            }
159
            $args[] = new VigiloArg('label', $label);
160
            $args[] = new VigiloArg('ifname', $port->getName());
161
            $this->children[] = new VigiloTest('Interface', $args);
162

    
163
            // Retrieve all IP addresses associated with this interface.
164
            // This will be used later in selectAddress() to select
165
            // the most appropriate IP address to query this computer.
166
            foreach ($DB->query($query2) as $nn) {
167
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
168
                foreach ($DB->query($query3) as $ip) {
169
                    $addr = new IPAddress();
170
                    if ($addr->getFromDB($ip['id'])) {
171
                        $this->addresses[] = $addr;
172
                    }
173
                }
174
            }
175
        }
176
    }
177

    
178
    protected function monitorSoftwares()
179
    {
180
        $listOfTest = new VigiloTestSoftware($this->computer);
181
        $computerSoftwareVersion = new Computer_SoftwareVersion();
182
        $ids = $computerSoftwareVersion->find('computers_id=' . $this->computer->getID());
183
        foreach ($ids as $id) {
184
            if ($id['softwareversions_id']) {
185
                $softwareVersion = new SoftwareVersion();
186
                $ids2 = $softwareVersion->find('id=' . $id['softwareversions_id']);
187
                foreach ($ids2 as $id2) {
188
                    if ($id2['softwares_id']) {
189
                        $software = new Software();
190
                        $software->getFromDB($id2['softwares_id']);
191
                        $listOfTest->addRelevantTestWith($software->getName());
192
                    }
193
                }
194
            }
195
        }
196
        foreach ($listOfTest->getTable() as $test) {
197
             $this->children[] = $test;
198
        }
199
    }
200

    
201
    protected function monitorPartitions()
202
    {
203
        global $DB;
204

    
205
        $query = ComputerDisk::getSQLRequestToSearchForItem(
206
            $this->computer->getType(),
207
            $this->computer->getID()
208
        );
209

    
210
        foreach ($DB->query($query) as $cd) {
211
            $disk = new ComputerDisk();
212
            $disk->getFromDB($cd['id']);
213

    
214
            $args = array();
215
            $args[] = new VigiloArg('label', $disk->getName());
216
            $args[] = new VigiloArg('partname', $disk->fields['mountpoint']);
217
            $total = $disk->fields['totalsize'];
218
            if (!empty($total)) {
219
                $args[] = new VigiloArg('max', $total * 1024 * 1024);
220
            }
221
            $this->children[] = new VigiloTest('Partition', $args);
222
        }
223
    }
224

    
225
    public function __toString()
226
    {
227
        $outXML = new DOMDocument();
228
        $outXML->preserveWhiteSpace = false;
229
        $outXML->formatOutput       = true;
230
        $outXML->loadXML(
231
            self::sprintf(
232
                '<?xml version="1.0"?>' .
233
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
234
                $this->computer->getName(),
235
                $this->selectAddress(),
236
                "Servers",
237
                $this->children
238
            )
239
        );
240
        return $outXML->saveXML();
241
    }
242
}