Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / plugins / vigilo / inc / abstractmonitoreditem.class.php @ 8d255750

History | View | Annotate | Download (5.94 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 filterTests($value)
37
    {
38
        return is_object($value) && ($value instanceof VigiloTest);
39
    }
40

    
41
    public function getTests()
42
    {
43
        return new CallbackFilterIterator(
44
            new ArrayIterator($this->children),
45
            array($this, 'filterTests')
46
        );
47
    }
48

    
49
    protected function selectTemplates()
50
    {
51
        $template = $this->item->fields['vigilo_template'];
52
        if (null !== $template) {
53
            $this->children[] = new VigiloTemplate($template);
54
        }
55
    }
56

    
57
    protected function selectGroups()
58
    {
59
        $location = new Location();
60
        $location->getFromDB($this->item->fields["locations_id"]);
61

    
62
        $entity = new Entity();
63
        $entity->getFromDB($this->item->fields["entities_id"]);
64

    
65
        // Association de l'objet à son emplacement et à son entité.
66
        $candidates = array(
67
            'Locations' => $location,
68
            'Entities'  => $entity,
69
        );
70
        foreach ($candidates as $type => $candidate) {
71
            if (NOT_AVAILABLE === $candidate->getName()) {
72
                continue;
73
            }
74

    
75
            $completeName       = explode(" > ", $candidate->getField("completename"));
76
            // Ajout de "/" et de l'origine pour avoir le chemin complet.
77
            array_unshift($completeName, $type);
78
            array_unshift($completeName, "");
79
            $groupName          = implode("/", $completeName);
80
            $this->children[]   = new VigiloGroup($groupName);
81
        }
82

    
83
        // Association de l'objet à son équipementier.
84
        $manufacturer = new Manufacturer();
85
        $manufacturer->getFromDB($this->item->fields["manufacturers_id"]);
86
        if (NOT_AVAILABLE !== $manufacturer->getName()) {
87
            $this->children[] = new VigiloGroup("/Manufacturers/" . $manufacturer->getName());
88
        }
89

    
90
        // Association de l'objet à son technicien.
91
        $technician = new User();
92
        $technician->getFromDB($this->item->fields["users_id_tech"]);
93
        if (NOT_AVAILABLE !== $technician->getName()) {
94
            $this->children[] = new VigiloGroup("/Technicians/" . $technician->getName());
95
        }
96
    }
97

    
98
    protected function selectAddress()
99
    {
100
        static $address = null;
101

    
102
        if (null === $address && $this->agent) {
103
            $addresses = $this->agent->getIPs();
104
            if (count($addresses)) {
105
                $address = current($addresses);
106
            }
107
        }
108

    
109
        if (null === $address) {
110
            $address = $this->item->getName();
111
            foreach ($this->addresses as $addr) {
112
                if (!$addr->is_ipv4()) {
113
                    continue;
114
                }
115

    
116
                $textual = $addr->getTextual();
117
                if (is_string($textual)) {
118
                    $address = $textual;
119
                    break;
120
                }
121
            }
122
        }
123

    
124
        return $address;
125
    }
126

    
127
    protected function monitorNetworkInterfaces()
128
    {
129
        global $DB;
130

    
131
        $query = NetworkPort::getSQLRequestToSearchForItem(
132
            $this->item->getType(),
133
            $this->item->getID()
134
        );
135

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

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

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

    
148
            $this->children[] =
149
                        $test = new VigiloTest('Interface');
150
            $test['label']  = $label;
151
            $test['ifname'] = $port->getName();
152

    
153
            $ethport    = $ethport->find('networkports_id=' . $np['id']);
154
            foreach ($ethport as $rowEthPort) {
155
                if ($rowEthPort['speed']) {
156
                    // La bande passante de l'interface est exprimée
157
                    // en Mbit/s dans GLPI et on la veut en bit/s dans Vigilo.
158
                    $test['max'] = $rowEthPort['speed'] << 20;
159
                    break;
160
                }
161
            }
162

    
163
            // Récupère la liste de toutes les adresses IP pour l'interface.
164
            // Elles serviront plus tard dans selectAddress() pour choisir
165
            // l'adresse IP la plus appropriée pour interroger ce réseau.
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
    public function __toString()
179
    {
180
        return self::sprintf(
181
            '<?xml version="1.0"?>' .
182
            '<host name="%s" address="%s" ventilation="%s">%s</host>',
183
            $this->item->getName(),
184
            $this->selectAddress(),
185
            "Servers",
186
            $this->children
187
        );
188
    }
189
}