Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / plugins / vigilo / inc / abstractmonitoreditem.class.php @ 0548a1f2

History | View | Annotate | Download (6.01 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
    public 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 = isset($this->item->fields['vigilo_template']) ?
52
                    $this->item->fields['vigilo_template'] : null;
53
        if (null !== $template) {
54
            $this->children[] = new VigiloTemplate($template);
55
        }
56
    }
57

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

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

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

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

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

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

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

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

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

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

    
125
        return $address;
126
    }
127

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

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

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

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

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

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

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

    
164
            // Récupère la liste de toutes les adresses IP pour l'interface.
165
            // Elles serviront plus tard dans selectAddress() pour choisir
166
            // l'adresse IP la plus appropriée pour interroger ce réseau.
167
            foreach ($DB->query($query2) as $nn) {
168
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
169
                foreach ($DB->query($query3) as $ip) {
170
                    $addr = new IPAddress();
171
                    if ($addr->getFromDB($ip['id'])) {
172
                        $this->addresses[] = $addr;
173
                    }
174
                }
175
            }
176
        }
177
    }
178

    
179
    public function __toString()
180
    {
181
        return self::sprintf(
182
            '<?xml version="1.0"?>' .
183
            '<host name="%s" address="%s" ventilation="%s">%s</host>',
184
            $this->item->getName(),
185
            $this->selectAddress(),
186
            "Servers",
187
            $this->children
188
        );
189
    }
190
}