Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / plugins / vigilo / inc / abstractmonitoreditem.class.php @ e1c378c0

History | View | Annotate | Download (5.86 KB)

1
<?php
2

    
3
abstract class PluginVigiloAbstractMonitoredItem extends VigiloXml
4
{
5
    protected $item;
6
    protected $addresses;
7
    protected $ventilation;
8
    protected $children;
9
    protected $agent;
10

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

    
19
        if (class_exists('PluginFusioninventoryAgent')) {
20
            $agent = new PluginFusioninventoryAgent();
21
            if ($agent->getAgentWithComputerid($item->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->item->getName();
34
    }
35

    
36
    protected function selectTemplates()
37
    {
38
        $template = $this->item->fields['vigilo_template'];
39
        if (null !== $template) {
40
            $this->children[] = new VigiloTemplate($template);
41
        }
42
    }
43

    
44
    protected function selectGroups()
45
    {
46
        $location = new Location();
47
        $location->getFromDB($this->item->fields["locations_id"]);
48

    
49
        $entity = new Entity();
50
        $entity->getFromDB($this->item->fields["entities_id"]);
51

    
52
        // Association de l'objet à son emplacement et à son entité.
53
        $candidates = array(
54
            'Locations' => $location,
55
            'Entities'  => $entity,
56
        );
57
        foreach ($candidates as $type => $candidate) {
58
            if (NOT_AVAILABLE === $candidate->getName()) {
59
                continue;
60
            }
61

    
62
            $completeName       = explode(" > ", $candidate->getField("completename"));
63
            // Ajout de "/" et de l'origine pour avoir le chemin complet.
64
            array_unshift($completeName, $type);
65
            array_unshift($completeName, "");
66
            $groupName          = implode("/", $completeName);
67
            $this->children[]   = new VigiloGroup($groupName);
68
        }
69

    
70
        // Association de l'objet à son équipementier.
71
        $manufacturer = new Manufacturer();
72
        $manufacturer->getFromDB($this->item->fields["manufacturers_id"]);
73
        if (NOT_AVAILABLE !== $manufacturer->getName()) {
74
            $this->children[] = new VigiloGroup("/Manufacturers/" . $manufacturer->getName());
75
        }
76

    
77
        // Association de l'objet à son technicien.
78
        $technician = new User();
79
        $technician->getFromDB($this->item->fields["users_id_tech"]);
80
        if (NOT_AVAILABLE !== $technician->getName()) {
81
            $this->children[] = new VigiloGroup("/Technicians/" . $technician->getName());
82
        }
83
    }
84

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

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

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

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

    
111
        return $address;
112
    }
113

    
114
    protected function monitorNetworkInterfaces()
115
    {
116
        global $DB;
117

    
118
        $query = NetworkPort::getSQLRequestToSearchForItem(
119
            $this->item->getType(),
120
            $this->item->getID()
121
        );
122

    
123
        foreach ($DB->query($query) as $np) {
124
            $query2     = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
125
            $port       = new NetworkPort();
126
            $ethport    = new NetworkPortEthernet();
127

    
128
            $port->getFromDB($np['id']);
129
            if ($port->getName() == 'lo') {
130
                continue;
131
            }
132

    
133
            $label = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
134

    
135
            $this->children[] =
136
                        $test = new VigiloTest('Interface');
137
            $test['label']  = $label;
138
            $test['ifname'] = $port->getName();
139

    
140
            $ethport    = $ethport->find('networkports_id=' . $np['id']);
141
            foreach ($ethport as $rowEthPort) {
142
                if ($rowEthPort['speed']) {
143
                    // La bande passante de l'interface est exprimée
144
                    // en Mbit/s dans GLPI et on la veut en bit/s dans Vigilo.
145
                    $test['max'] = $rowEthPort['speed'] << 20;
146
                    break;
147
                }
148
            }
149

    
150
            // Récupère la liste de toutes les adresses IP pour l'interface.
151
            // Elles serviront plus tard dans selectAddress() pour choisir
152
            // l'adresse IP la plus appropriée pour interroger ce réseau.
153
            foreach ($DB->query($query2) as $nn) {
154
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
155
                foreach ($DB->query($query3) as $ip) {
156
                    $addr = new IPAddress();
157
                    if ($addr->getFromDB($ip['id'])) {
158
                        $this->addresses[] = $addr;
159
                    }
160
                }
161
            }
162
        }
163
    }
164

    
165
    public function __toString()
166
    {
167
        $outXML = new DOMDocument();
168
        $outXML->preserveWhiteSpace = false;
169
        $outXML->formatOutput       = true;
170
        $outXML->loadXML(
171
            self::sprintf(
172
                '<?xml version="1.0"?>' .
173
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
174
                $this->item->getName(),
175
                $this->selectAddress(),
176
                "Servers",
177
                $this->children
178
            )
179
        );
180
        return $outXML->saveXML();
181
    }
182
}