Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.96 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 = PluginVigiloTemplate::getTemplateNameForItem($this->item);
59
        if (null !== $template) {
60
            $this->children[] = new VigiloTemplate($template);
61
        }
62
    }
63

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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