Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / plugins / vigilo / inc / abstractmonitoreditem.class.php @ 420b5990

History | View | Annotate | Download (5.82 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
        if ($this->agent) {
102
            $addresses = $this->agent->getIPs();
103
            if (count($addresses)) {
104
                return current($addresses);
105
            }
106
        }
107

    
108
        if (count($this->addresses)) {
109
            return current($this->addresses);
110
        }
111

    
112
        return $this->item->getName();
113
    }
114

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

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

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

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

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

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

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

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

    
169
    public function __toString()
170
    {
171
        return 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
}