Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

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 static function escapeRegex($regex)
50
    {
51
        $res = preg_quote($regex);
52
        $res = preg_replace("/[\x80-\xFF]+/",'.+', $res);
53
        return $res;
54
    }
55

    
56
    protected function selectTemplates()
57
    {
58
        $template = isset($this->item->fields['vigilo_template']) ?
59
                    $this->item->fields['vigilo_template'] : null;
60
        if (null !== $template) {
61
            $this->children[] = new VigiloTemplate($template);
62
        }
63
    }
64

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

    
70
        $entity = new Entity();
71
        $entity->getFromDB($this->item->fields["entities_id"]);
72

    
73
        // Association de l'objet à son emplacement et à son entité.
74
        $candidates = array(
75
            'Locations' => $location,
76
            'Entities'  => $entity,
77
        );
78
        foreach ($candidates as $type => $candidate) {
79
            if (NOT_AVAILABLE === $candidate->getName()) {
80
                continue;
81
            }
82

    
83
            $completeName       = explode(" > ", $candidate->getField("completename"));
84
            // Ajout de "/" et de l'origine pour avoir le chemin complet.
85
            array_unshift($completeName, $type);
86
            array_unshift($completeName, "");
87
            $groupName          = implode("/", $completeName);
88
            $this->children[]   = new VigiloGroup($groupName);
89
        }
90

    
91
        // Association de l'objet à son équipementier.
92
        $manufacturer = new Manufacturer();
93
        $manufacturer->getFromDB($this->item->fields["manufacturers_id"]);
94
        if (NOT_AVAILABLE !== $manufacturer->getName()) {
95
            $this->children[] = new VigiloGroup("/Manufacturers/" . $manufacturer->getName());
96
        }
97

    
98
        // Association de l'objet à son technicien.
99
        $technician = new User();
100
        $technician->getFromDB($this->item->fields["users_id_tech"]);
101
        if (NOT_AVAILABLE !== $technician->getName()) {
102
            $this->children[] = new VigiloGroup("/Technicians/" . $technician->getName());
103
        }
104
    }
105

    
106
    protected function selectAddress()
107
    {
108
        if ($this->agent) {
109
            $addresses = $this->agent->getIPs();
110
            if (count($addresses)) {
111
                return current($addresses);
112
            }
113
        }
114

    
115
        if (count($this->addresses)) {
116
            return current($this->addresses);
117
        }
118

    
119
        return $this->item->getName();
120
    }
121

    
122
    protected function monitorNetworkInterfaces()
123
    {
124
        global $DB;
125

    
126
        $query = NetworkPort::getSQLRequestToSearchForItem(
127
            $this->item->getType(),
128
            $this->item->getID()
129
        );
130

    
131
        foreach ($DB->query($query) as $np) {
132
            $query2     = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
133
            $port       = new NetworkPort();
134
            $ethport    = new NetworkPortEthernet();
135

    
136
            $port->getFromDB($np['id']);
137
            if ($port->getName() == 'lo') {
138
                continue;
139
            }
140

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

    
143
            $this->children[] =
144
                        $test = new VigiloTest('Interface');
145
            $test['label']  = $label;
146
            $test['ifname'] = self::escapeRegex($port->getName());
147

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

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

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