Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / Vigilo / VigiloHost.php @ 79397eb3

History | View | Annotate | Download (8.09 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
        }
53
        else {
54
           if (empty($this->children)) {
55
               $template_name = "default";
56
               $this->children[] = new VigiloHostTemplate($template_name);
57
           }
58
        }
59
    }
60

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

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

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

    
86
    protected function selectAddress()
87
    {
88
        static $address = null;
89

    
90
        if ($address === null && $this->agent) {
91
            $addresses = $this->agent->getIPs();
92
            if (count($addresses)) {
93
                $address = current($addresses);
94
            }
95
        }
96

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

    
104
                $textual = $addr->getTextual();
105
                if (is_string($textual)) {
106
                    $address = $textual;
107
                    break;
108
                }
109
            }
110
        }
111

    
112
        return $address;
113
    }
114

    
115
    protected function monitorMemory()
116
    {
117
        global $DB;
118

    
119
        $total = 0;
120
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
121
            $this->computer->getType(),
122
            $this->computer->getID()
123
        );
124

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

    
131
        if ($total > 0) {
132
            $this->children[] = new VigiloTest('RAM');
133
        }
134
    }
135

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

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

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

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

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

    
205
    protected function monitorPartitions()
206
    {
207
        global $DB;
208

    
209
        $query = ComputerDisk::getSQLRequestToSearchForItem(
210
            $this->computer->getType(),
211
            $this->computer->getID()
212
        );
213

    
214
        foreach ($DB->query($query) as $cd) {
215
            $disk = new ComputerDisk();
216
            $disk->getFromDB($cd['id']);
217

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

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