Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / Vigilo / VigiloHost.php @ 077e4de7

History | View | Annotate | Download (8.06 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

    
47
        $template_number = $this->computer->getField("vigilo_template");
48
        if ($template_number !== '0' && $template_number !== 'N/A') {
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 (!($location->getName() == 'N/A')) {
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 (!($entity->getName() == 'N/A')) {
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 (!($manufacturer->getName() == 'N/A')) {
79
            $this->children[] = new VigiloGroup($manufacturer->getName());
80
        }
81
    }
82

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

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

    
94
        if ($address === null) {
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 ($port->getName() == 'lo') {
147
                continue;
148
            }
149

    
150
            $args   = array();
151
            $label  = isset($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
        global $DB;
181
        $listOfTest=new VigiloTestSoftware($this->computer);
182
        $computerSoftwareVersion=new Computer_SoftwareVersion();
183
        $ids=$computerSoftwareVersion->find('computers_id=' . $this->computer->getID());
184
        foreach ($ids as $id) {
185
            if ($id['softwareversions_id']) {
186
                $softwareVersion=new SoftwareVersion();
187
                $ids2=$softwareVersion->find('id=' . $id['softwareversions_id']);
188
                foreach ($ids2 as $id2) {
189
                    if ($id2['softwares_id']) {
190
                        $software=new Software();
191
                        $software->getFromDB($id2['softwares_id']);
192
                        $listOfTest->addRelevantTestWith($software->getName());
193
                    }
194
                }
195
            }
196
        }
197
        foreach ($listOfTest->getTable() as $test) {
198
             $this->children[]=$test;
199
        }
200
    }
201

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

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

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

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

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