Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.19 KB)

1
<?php
2

    
3
class VigiloNetworkEquipment extends VigiloXml
4
{
5
    protected $network;
6
    protected $addresses;
7
    protected $ventilation;
8
    protected $children;
9
    protected $agent;
10

    
11
    public function __construct($network)
12
    {
13
        $this->agent        = null;
14
        $this->ventilation  = "Servers";
15
        $this->network      = $network;
16
        $this->addresses    = array();
17
        $this->children     = array();
18

    
19
        if (class_exists('PluginFusioninventoryAgent')) {
20
            $agent = new PluginFusioninventoryAgent();
21
            if ($agent->getAgentWithComputerid($this->network->getID()) !== false) {
22
                $this->agent = $agent;
23
            }
24
        }
25

    
26
        $this->selectTemplates();
27
        $this->selectGroups();
28
        $this->monitorNetworkInterfaces();
29
    }
30

    
31
    public function getName()
32
    {
33
        return $this->network->getName();
34
    }
35

    
36
    protected function selectTemplates()
37
    {
38
        $template_name = $this->network->getField("template_name");
39

    
40
        if ($template_name !== "N/A") {
41
            $this->children[] = new VigiloHostTemplate($this->network->getField("template_name"));
42
        }
43
    }
44

    
45
    protected function selectGroups()
46
    {
47
        $location = new Location();
48
        $location->getFromDB($this->network->fields["locations_id"]);
49
        if (!($location->getName()=='N/A')) {
50
            $locationCompleteName=explode(" > ", $location->getField("completename"));
51
            $locationRealName=implode("/", $locationCompleteName);
52
            $this->children[] = new VigiloGroup($locationRealName);
53
        }
54

    
55
        $entity = new Entity();
56
        $entity->getFromDB($this->network->fields["entities_id"]);
57
        if (!($entity->getName()=='N/A')) {
58
            $entityCompleteName=explode(" > ", $entity->getField("completename"));
59
            $entityRealName=implode("/", $entityCompleteName);
60
            $this->children[] = new VigiloGroup($entityRealName);
61
        }
62

    
63
        $manufacturer = new Manufacturer();
64
        $manufacturer->getFromDB($this->network->fields["manufacturers_id"]);
65
        if (!($manufacturer->getName()=='N/A')) {
66
            $this->children[] = new VigiloGroup($manufacturer->getName());
67
        }
68
    }
69

    
70
    protected function selectAddress()
71
    {
72
        static $address = null;
73

    
74
        if ($address === null && $this->agent) {
75
            $addresses = $this->agent->getIPs();
76
            if (count($addresses)) {
77
                $address = current($addresses);
78
            }
79
        }
80

    
81
        if ($address === null) {
82
            $address = $this->network->getName();
83
            foreach ($this->addresses as $addr) {
84
                if (!$addr->is_ipv4()) {
85
                    continue;
86
                }
87

    
88
                $textual = $addr->getTextual();
89
                if (is_string($textual)) {
90
                    $address = $textual;
91
                    break;
92
                }
93
            }
94
        }
95

    
96
        return $address;
97
    }
98

    
99
    protected function monitorNetworkInterfaces()
100
    {
101
        global $DB;
102
        $query = NetworkPort::getSQLRequestToSearchForItem(
103
            $this->network->getType(),
104
            $this->network->getID()
105
        );
106

    
107
        foreach ($DB->query($query) as $np) {
108
            $query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
109
            $port = new NetworkPort();
110
            $ethport = new NetworkPortEthernet();
111
            $port->getFromDB($np['id']);
112
            if ($port->getName() == 'lo') {
113
                continue;
114
            }
115

    
116
            $args   = array();
117
            $label  = isset($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
118
            $ethport = $ethport->find('networkports_id=' . $np['id']);
119
            foreach ($ethport as $rowEthPort) {
120
                if ($rowEthPort['speed']) {
121
                    $args[] = new VigiloArg('max', $rowEthPort['speed']);
122
                    break;
123
                }
124
            }
125
            $args[] = new VigiloArg('label', $label);
126
            $args[] = new VigiloArg('ifname', $port->getName());
127
            $this->children[] = new VigiloTest('Interface', $args);
128

    
129
            // Retrieve all IP addresses associated with this interface.
130
            // This will be used later in selectAddress() to select
131
            // the most appropriate IP address to query this network.
132
            foreach ($DB->query($query2) as $nn) {
133
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
134
                foreach ($DB->query($query3) as $ip) {
135
                    $addr = new IPAddress();
136
                    if ($addr->getFromDB($ip['id'])) {
137
                        $this->addresses[] = $addr;
138
                    }
139
                }
140
            }
141
        }
142
    }
143

    
144
    public function __toString()
145
    {
146
        $outXML=new DOMdocument();
147
        $outXML->preserveWhiteSpace=false;
148
        $outXML->formatOutput=true;
149
        $outXML->loadXML(
150
            self::sprintf(
151
                '<?xml version="1.0"?>' .
152
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
153
                $this->network->getName(),
154
                $this->selectAddress(),
155
                "Servers",
156
                $this->children
157
            )
158
        );
159
        return $outXML->saveXML();
160
    }
161
}