Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.96 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 c413e820 Francois POIROTTE
        $res = preg_replace("/[\x80-\xFF]+/", '.+', $res);
53 8ef154dd Francois POIROTTE
        return $res;
54
    }
55
56 8b28e91d Romain CHOLLET
    protected function selectTemplates()
57
    {
58 925171f8 Francois POIROTTE
        $template = PluginVigiloTemplate::getTemplateNameForItem($this->item);
59 166be9d2 Francois POIROTTE
        if (null !== $template) {
60
            $this->children[] = new VigiloTemplate($template);
61 8b28e91d Romain CHOLLET
        }
62
    }
63
64
    protected function selectGroups()
65
    {
66
        $location = new Location();
67 166be9d2 Francois POIROTTE
        $location->getFromDB($this->item->fields["locations_id"]);
68 8b28e91d Romain CHOLLET
69
        $entity = new Entity();
70 166be9d2 Francois POIROTTE
        $entity->getFromDB($this->item->fields["entities_id"]);
71
72 e1c378c0 Francois POIROTTE
        // Association de l'objet à son emplacement et à son entité.
73 166be9d2 Francois POIROTTE
        $candidates = array(
74
            'Locations' => $location,
75
            'Entities'  => $entity,
76
        );
77
        foreach ($candidates as $type => $candidate) {
78 e1c378c0 Francois POIROTTE
            if (NOT_AVAILABLE === $candidate->getName()) {
79 166be9d2 Francois POIROTTE
                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 8b28e91d Romain CHOLLET
        }
89
90 e1c378c0 Francois POIROTTE
        // Association de l'objet à son équipementier.
91 8b28e91d Romain CHOLLET
        $manufacturer = new Manufacturer();
92 166be9d2 Francois POIROTTE
        $manufacturer->getFromDB($this->item->fields["manufacturers_id"]);
93 e1c378c0 Francois POIROTTE
        if (NOT_AVAILABLE !== $manufacturer->getName()) {
94 166be9d2 Francois POIROTTE
            $this->children[] = new VigiloGroup("/Manufacturers/" . $manufacturer->getName());
95 8b28e91d Romain CHOLLET
        }
96 e1c378c0 Francois POIROTTE
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 8b28e91d Romain CHOLLET
    }
104
105
    protected function selectAddress()
106
    {
107 420b5990 Francois POIROTTE
        if ($this->agent) {
108 8b28e91d Romain CHOLLET
            $addresses = $this->agent->getIPs();
109
            if (count($addresses)) {
110 420b5990 Francois POIROTTE
                return current($addresses);
111 8b28e91d Romain CHOLLET
            }
112
        }
113
114 420b5990 Francois POIROTTE
        if (count($this->addresses)) {
115
            return current($this->addresses);
116 8b28e91d Romain CHOLLET
        }
117
118 420b5990 Francois POIROTTE
        return $this->item->getName();
119 8b28e91d Romain CHOLLET
    }
120
121
    protected function monitorNetworkInterfaces()
122
    {
123
        global $DB;
124 166be9d2 Francois POIROTTE
125 8b28e91d Romain CHOLLET
        $query = NetworkPort::getSQLRequestToSearchForItem(
126 166be9d2 Francois POIROTTE
            $this->item->getType(),
127
            $this->item->getID()
128 8b28e91d Romain CHOLLET
        );
129
130
        foreach ($DB->query($query) as $np) {
131 ad6689da Francois POIROTTE
            $query2     = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
132
            $port       = new NetworkPort();
133
            $ethport    = new NetworkPortEthernet();
134 166be9d2 Francois POIROTTE
135 8b28e91d Romain CHOLLET
            $port->getFromDB($np['id']);
136
            if ($port->getName() == 'lo') {
137
                continue;
138
            }
139
140 166be9d2 Francois POIROTTE
            $label = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
141
142
            $this->children[] =
143 c8bad135 Francois POIROTTE
                        $test = new VigiloTest('Interface');
144 166be9d2 Francois POIROTTE
            $test['label']  = $label;
145 8ef154dd Francois POIROTTE
            $test['ifname'] = self::escapeRegex($port->getName());
146 166be9d2 Francois POIROTTE
147 ad6689da Francois POIROTTE
            $ethport    = $ethport->find('networkports_id=' . $np['id']);
148 8b28e91d Romain CHOLLET
            foreach ($ethport as $rowEthPort) {
149
                if ($rowEthPort['speed']) {
150 ad931b2e Francois POIROTTE
                    // 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 8b28e91d Romain CHOLLET
                    break;
154
                }
155
            }
156
157 420b5990 Francois POIROTTE
            // Récupère la liste de toutes les adresses IPv4 pour l'interface.
158 166be9d2 Francois POIROTTE
            // Elles serviront plus tard dans selectAddress() pour choisir
159
            // l'adresse IP la plus appropriée pour interroger ce réseau.
160 8b28e91d Romain CHOLLET
            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 420b5990 Francois POIROTTE
                    if ($addr->getFromDB($ip['id']) && $addr->is_ipv4()) {
165
                        $textual = $addr->getTextual();
166
                        if (is_string($textual)) {
167
                            $this->addresses[] = $textual;
168
                        }
169 8b28e91d Romain CHOLLET
                    }
170
                }
171
            }
172
        }
173
    }
174
175
    public function __toString()
176
    {
177 92b19eed Francois POIROTTE
        return self::sprintf(
178 420b5990 Francois POIROTTE
            '<' . '?xml version="1.0"?' . '>' .
179 92b19eed Francois POIROTTE
            '<host name="%s" address="%s" ventilation="%s">%s</host>',
180
            $this->item->getName(),
181
            $this->selectAddress(),
182
            "Servers",
183
            $this->children
184 8b28e91d Romain CHOLLET
        );
185
    }
186
}