Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.01 KB)

1 8b28e91d Romain CHOLLET
<?php
2
3 166be9d2 Francois POIROTTE
abstract class PluginVigiloAbstractMonitoredItem extends VigiloXml
4 8b28e91d Romain CHOLLET
{
5 166be9d2 Francois POIROTTE
    protected $item;
6 8b28e91d Romain CHOLLET
    protected $addresses;
7
    protected $ventilation;
8
    protected $children;
9
    protected $agent;
10
11 166be9d2 Francois POIROTTE
    public function __construct(CommonDBTM $item)
12 8b28e91d Romain CHOLLET
    {
13
        $this->agent        = null;
14 166be9d2 Francois POIROTTE
        $this->item         = $item;
15 8b28e91d Romain CHOLLET
        $this->ventilation  = "Servers";
16
        $this->addresses    = array();
17
        $this->children     = array();
18
19
        if (class_exists('PluginFusioninventoryAgent')) {
20
            $agent = new PluginFusioninventoryAgent();
21 166be9d2 Francois POIROTTE
            if ($agent->getAgentWithComputerid($item->getID()) !== false) {
22 8b28e91d Romain CHOLLET
                $this->agent = $agent;
23
            }
24
        }
25
26
        $this->selectTemplates();
27
        $this->selectGroups();
28
        $this->monitorNetworkInterfaces();
29
    }
30
31
    public function getName()
32
    {
33 166be9d2 Francois POIROTTE
        return $this->item->getName();
34 8b28e91d Romain CHOLLET
    }
35
36 ea2027b6 Francois POIROTTE
    public function filterTests($value)
37 92b19eed Francois POIROTTE
    {
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 8ef154dd Francois POIROTTE
    protected static function escapeRegex($regex)
50
    {
51
        $res = preg_quote($regex);
52
        $res = preg_replace("/[\x80-\xFF]+/",'.+', $res);
53
        return $res;
54
    }
55
56 8b28e91d Romain CHOLLET
    protected function selectTemplates()
57
    {
58 e878cf6c Francois POIROTTE
        $template = isset($this->item->fields['vigilo_template']) ?
59
                    $this->item->fields['vigilo_template'] : null;
60 166be9d2 Francois POIROTTE
        if (null !== $template) {
61
            $this->children[] = new VigiloTemplate($template);
62 8b28e91d Romain CHOLLET
        }
63
    }
64
65
    protected function selectGroups()
66
    {
67
        $location = new Location();
68 166be9d2 Francois POIROTTE
        $location->getFromDB($this->item->fields["locations_id"]);
69 8b28e91d Romain CHOLLET
70
        $entity = new Entity();
71 166be9d2 Francois POIROTTE
        $entity->getFromDB($this->item->fields["entities_id"]);
72
73 e1c378c0 Francois POIROTTE
        // Association de l'objet à son emplacement et à son entité.
74 166be9d2 Francois POIROTTE
        $candidates = array(
75
            'Locations' => $location,
76
            'Entities'  => $entity,
77
        );
78
        foreach ($candidates as $type => $candidate) {
79 e1c378c0 Francois POIROTTE
            if (NOT_AVAILABLE === $candidate->getName()) {
80 166be9d2 Francois POIROTTE
                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 8b28e91d Romain CHOLLET
        }
90
91 e1c378c0 Francois POIROTTE
        // Association de l'objet à son équipementier.
92 8b28e91d Romain CHOLLET
        $manufacturer = new Manufacturer();
93 166be9d2 Francois POIROTTE
        $manufacturer->getFromDB($this->item->fields["manufacturers_id"]);
94 e1c378c0 Francois POIROTTE
        if (NOT_AVAILABLE !== $manufacturer->getName()) {
95 166be9d2 Francois POIROTTE
            $this->children[] = new VigiloGroup("/Manufacturers/" . $manufacturer->getName());
96 8b28e91d Romain CHOLLET
        }
97 e1c378c0 Francois POIROTTE
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 8b28e91d Romain CHOLLET
    }
105
106
    protected function selectAddress()
107
    {
108 420b5990 Francois POIROTTE
        if ($this->agent) {
109 8b28e91d Romain CHOLLET
            $addresses = $this->agent->getIPs();
110
            if (count($addresses)) {
111 420b5990 Francois POIROTTE
                return current($addresses);
112 8b28e91d Romain CHOLLET
            }
113
        }
114
115 420b5990 Francois POIROTTE
        if (count($this->addresses)) {
116
            return current($this->addresses);
117 8b28e91d Romain CHOLLET
        }
118
119 420b5990 Francois POIROTTE
        return $this->item->getName();
120 8b28e91d Romain CHOLLET
    }
121
122
    protected function monitorNetworkInterfaces()
123
    {
124
        global $DB;
125 166be9d2 Francois POIROTTE
126 8b28e91d Romain CHOLLET
        $query = NetworkPort::getSQLRequestToSearchForItem(
127 166be9d2 Francois POIROTTE
            $this->item->getType(),
128
            $this->item->getID()
129 8b28e91d Romain CHOLLET
        );
130
131
        foreach ($DB->query($query) as $np) {
132 ad6689da Francois POIROTTE
            $query2     = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
133
            $port       = new NetworkPort();
134
            $ethport    = new NetworkPortEthernet();
135 166be9d2 Francois POIROTTE
136 8b28e91d Romain CHOLLET
            $port->getFromDB($np['id']);
137
            if ($port->getName() == 'lo') {
138
                continue;
139
            }
140
141 166be9d2 Francois POIROTTE
            $label = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
142
143
            $this->children[] =
144 c8bad135 Francois POIROTTE
                        $test = new VigiloTest('Interface');
145 166be9d2 Francois POIROTTE
            $test['label']  = $label;
146 8ef154dd Francois POIROTTE
            $test['ifname'] = self::escapeRegex($port->getName());
147 166be9d2 Francois POIROTTE
148 ad6689da Francois POIROTTE
            $ethport    = $ethport->find('networkports_id=' . $np['id']);
149 8b28e91d Romain CHOLLET
            foreach ($ethport as $rowEthPort) {
150
                if ($rowEthPort['speed']) {
151 ad931b2e Francois POIROTTE
                    // 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 8b28e91d Romain CHOLLET
                    break;
155
                }
156
            }
157
158 420b5990 Francois POIROTTE
            // Récupère la liste de toutes les adresses IPv4 pour l'interface.
159 166be9d2 Francois POIROTTE
            // Elles serviront plus tard dans selectAddress() pour choisir
160
            // l'adresse IP la plus appropriée pour interroger ce réseau.
161 8b28e91d Romain CHOLLET
            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 420b5990 Francois POIROTTE
                    if ($addr->getFromDB($ip['id']) && $addr->is_ipv4()) {
166
                        $textual = $addr->getTextual();
167
                        if (is_string($textual)) {
168
                            $this->addresses[] = $textual;
169
                        }
170 8b28e91d Romain CHOLLET
                    }
171
                }
172
            }
173
        }
174
    }
175
176
    public function __toString()
177
    {
178 92b19eed Francois POIROTTE
        return self::sprintf(
179 420b5990 Francois POIROTTE
            '<' . '?xml version="1.0"?' . '>' .
180 92b19eed Francois POIROTTE
            '<host name="%s" address="%s" ventilation="%s">%s</host>',
181
            $this->item->getName(),
182
            $this->selectAddress(),
183
            "Servers",
184
            $this->children
185 8b28e91d Romain CHOLLET
        );
186
    }
187
}