Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / Vigilo / VigiloHost.php @ bef4271a

History | View | Annotate | Download (6.2 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
        $this->selectTemplates();
26
        $this->selectGroups();
27
        $this->monitorProcessor();
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
        $refs = array(
42
            "glpi_operatingsystems" => "operatingsystems_id",
43
            "glpi_operatingsystemversions" => "operatingsystemversions_id",
44
            "glpi_operatingsystemservicepacks" => "operatingsystemservicepacks_id",
45
        );
46

    
47
        $model = array();
48
        foreach ($refs as $table => $field) {
49
            $id = $this->computer->fields[$field];
50
            $value = Dropdown::getDropdownName($table, $id);
51
            if ($value !== "" && $value !== null && $value !== "&nbsp;" &&
52
                $value !== false && $value !== "-----") {
53
                $model[] = $value;
54
            }
55
        }
56

    
57
        if (!count($model))
58
            $model = "default";
59
        else
60
            $model = implode(" - ", $model);
61

    
62
        $this->children[] = new VigiloHostTemplate($model);
63
    }
64

    
65
    protected function selectGroups()
66
    {
67
        $location = new Location();
68
        $location->getFromDB($this->computer->fields["locations_id"]);
69
        $location = $location->getName();
70

    
71
        if (!$location)
72
            $location = "Servers";
73

    
74
        $this->children[] = new VigiloGroup($location);
75
    }
76

    
77
    protected function selectAddress()
78
    {
79
        static $address = null;
80

    
81
        if ($address === null && $this->agent) {
82
            $addresses = $this->agent->getIPs();
83
            if (count($addresses))
84
                $address = current($addresses);
85
        }
86

    
87
        if ($address === null) {
88
            $address = $this->computer->getName();
89
            foreach ($this->addresses as $addr) {
90
                if (!$addr->is_ipv4())
91
                    continue;
92

    
93
                $textual = $addr->getTextual();
94
                if (is_string($textual)) {
95
                    $address = $textual;
96
                    break;
97
                }
98
            }
99
        }
100

    
101
        return $address;
102
    }
103

    
104
    protected function monitorProcessor()
105
    {
106
        $this->children[] = new VigiloTest('CPU');
107
    }
108

    
109
    protected function monitorMemory()
110
    {
111
        global $DB;
112

    
113
        $total = 0;
114
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
115
            $this->computer->getType(),
116
            $this->computer->getID()
117
        );
118

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

    
125
        if ($total > 0)
126
            $this->children[] = new VigiloTest('RAM');
127
    }
128

    
129
    protected function monitorNetworkInterfaces()
130
    {
131
        global $DB;
132

    
133
        $query = NetworkPort::getSQLRequestToSearchForItem(
134
            $this->computer->getType(),
135
            $this->computer->getID()
136
        );
137

    
138
        foreach ($DB->query($query) as $np) {
139
            $query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
140

    
141
            $port = new NetworkPort();
142
            $port->getFromDB($np['id']);
143
            if ($port->getName() == 'lo')
144
                continue;
145

    
146
            $args   = array();
147
            $label  = isset($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
148
            $args[] = new VigiloArg('label', $label);
149
            $args[] = new VigiloArg('name', $port->getName());
150
            // TODO: retrieve interface speed (from glpi_networkportethernets)
151
            $this->children[] = new VigiloTest('Interface', $args);
152

    
153
            // Retrieve all IP addresses associated with this interface.
154
            // This will be used later in selectAddress() to select
155
            // the most appropriate IP address to query this computer.
156
            foreach ($DB->query($query2) as $nn) {
157
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
158
                foreach ($DB->query($query3) as $ip) {
159
                    $addr = new IPAddress();
160
                    if ($addr->getFromDB($ip['id']))
161
                        $this->addresses[] = $addr;
162
                }
163
            }
164
        }
165
    }
166

    
167
    protected function monitorSoftwares()
168
    {
169
        
170
    }
171

    
172
    protected function monitorPartitions()
173
    {
174
        global $DB;
175

    
176
        $query = ComputerDisk::getSQLRequestToSearchForItem(
177
            $this->computer->getType(),
178
            $this->computer->getID()
179
        );
180

    
181
        foreach ($DB->query($query) as $cd) {
182
            $disk = new ComputerDisk();
183
            $disk->getFromDB($cd['id']);
184

    
185
            $args = array();
186
            $args[] = new VigiloArg('label', $disk->getName());
187
            $args[] = new VigiloArg('partname', $disk->fields['mountpoint']);
188
            $total = $disk->fields['totalsize'];
189
            if (!empty($total))
190
                $args[] = new VigiloArg('max', $total * 1024 * 1024);
191
            $this->children[] = new VigiloTest('Partition', $args);
192
        }
193
    }
194

    
195
    public function __toString()
196
    {
197
        $outXML=new DOMdocument();
198
              $outXML->preserveWhiteSpace=false;
199
             $outXML->formatOutput=true;
200
             $outXML->loadXML(self::sprintf(
201
            '<?xml version="1.0"?>' .
202
            '<host name="%s" address="%s" ventilation="%s">%s</host>',
203
            $this->computer->getName(),
204
            $this->selectAddress(),
205
            "Servers",
206
            $this->children
207
            ));
208
        return $outXML->saveXML();        
209
        
210
    }
211
}