glpi / src / plugins / vigilo / inc / abstractmonitoreditem.class.php @ 77f0ba6f
History | View | Annotate | Download (5.94 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 | 8b28e91d | Romain CHOLLET | protected function selectTemplates() |
50 | { |
||
51 | 166be9d2 | Francois POIROTTE | $template = $this->item->fields['vigilo_template']; |
52 | if (null !== $template) { |
||
53 | $this->children[] = new VigiloTemplate($template); |
||
54 | 8b28e91d | Romain CHOLLET | } |
55 | } |
||
56 | |||
57 | protected function selectGroups() |
||
58 | { |
||
59 | $location = new Location(); |
||
60 | 166be9d2 | Francois POIROTTE | $location->getFromDB($this->item->fields["locations_id"]); |
61 | 8b28e91d | Romain CHOLLET | |
62 | $entity = new Entity(); |
||
63 | 166be9d2 | Francois POIROTTE | $entity->getFromDB($this->item->fields["entities_id"]); |
64 | |||
65 | e1c378c0 | Francois POIROTTE | // Association de l'objet à son emplacement et à son entité.
|
66 | 166be9d2 | Francois POIROTTE | $candidates = array( |
67 | 'Locations' => $location, |
||
68 | 'Entities' => $entity, |
||
69 | ); |
||
70 | foreach ($candidates as $type => $candidate) { |
||
71 | e1c378c0 | Francois POIROTTE | if (NOT_AVAILABLE === $candidate->getName()) { |
72 | 166be9d2 | Francois POIROTTE | continue;
|
73 | } |
||
74 | |||
75 | $completeName = explode(" > ", $candidate->getField("completename")); |
||
76 | // Ajout de "/" et de l'origine pour avoir le chemin complet.
|
||
77 | array_unshift($completeName, $type); |
||
78 | array_unshift($completeName, ""); |
||
79 | $groupName = implode("/", $completeName); |
||
80 | $this->children[] = new VigiloGroup($groupName); |
||
81 | 8b28e91d | Romain CHOLLET | } |
82 | |||
83 | e1c378c0 | Francois POIROTTE | // Association de l'objet à son équipementier.
|
84 | 8b28e91d | Romain CHOLLET | $manufacturer = new Manufacturer(); |
85 | 166be9d2 | Francois POIROTTE | $manufacturer->getFromDB($this->item->fields["manufacturers_id"]); |
86 | e1c378c0 | Francois POIROTTE | if (NOT_AVAILABLE !== $manufacturer->getName()) { |
87 | 166be9d2 | Francois POIROTTE | $this->children[] = new VigiloGroup("/Manufacturers/" . $manufacturer->getName()); |
88 | 8b28e91d | Romain CHOLLET | } |
89 | e1c378c0 | Francois POIROTTE | |
90 | // Association de l'objet à son technicien.
|
||
91 | $technician = new User(); |
||
92 | $technician->getFromDB($this->item->fields["users_id_tech"]); |
||
93 | if (NOT_AVAILABLE !== $technician->getName()) { |
||
94 | $this->children[] = new VigiloGroup("/Technicians/" . $technician->getName()); |
||
95 | } |
||
96 | 8b28e91d | Romain CHOLLET | } |
97 | |||
98 | protected function selectAddress() |
||
99 | { |
||
100 | static $address = null; |
||
101 | |||
102 | 166be9d2 | Francois POIROTTE | if (null === $address && $this->agent) { |
103 | 8b28e91d | Romain CHOLLET | $addresses = $this->agent->getIPs(); |
104 | if (count($addresses)) { |
||
105 | $address = current($addresses); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | 166be9d2 | Francois POIROTTE | if (null === $address) { |
110 | $address = $this->item->getName(); |
||
111 | 8b28e91d | Romain CHOLLET | foreach ($this->addresses as $addr) { |
112 | if (!$addr->is_ipv4()) { |
||
113 | continue;
|
||
114 | } |
||
115 | |||
116 | $textual = $addr->getTextual(); |
||
117 | if (is_string($textual)) { |
||
118 | $address = $textual; |
||
119 | break;
|
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | return $address; |
||
125 | } |
||
126 | |||
127 | protected function monitorNetworkInterfaces() |
||
128 | { |
||
129 | global $DB; |
||
130 | 166be9d2 | Francois POIROTTE | |
131 | 8b28e91d | Romain CHOLLET | $query = NetworkPort::getSQLRequestToSearchForItem( |
132 | 166be9d2 | Francois POIROTTE | $this->item->getType(), |
133 | $this->item->getID()
|
||
134 | 8b28e91d | Romain CHOLLET | ); |
135 | |||
136 | foreach ($DB->query($query) as $np) { |
||
137 | ad6689da | Francois POIROTTE | $query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']); |
138 | $port = new NetworkPort(); |
||
139 | $ethport = new NetworkPortEthernet(); |
||
140 | 166be9d2 | Francois POIROTTE | |
141 | 8b28e91d | Romain CHOLLET | $port->getFromDB($np['id']); |
142 | if ($port->getName() == 'lo') { |
||
143 | continue;
|
||
144 | } |
||
145 | |||
146 | 166be9d2 | Francois POIROTTE | $label = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName(); |
147 | |||
148 | $this->children[] =
|
||
149 | c8bad135 | Francois POIROTTE | $test = new VigiloTest('Interface'); |
150 | 166be9d2 | Francois POIROTTE | $test['label'] = $label; |
151 | $test['ifname'] = $port->getName(); |
||
152 | |||
153 | ad6689da | Francois POIROTTE | $ethport = $ethport->find('networkports_id=' . $np['id']); |
154 | 8b28e91d | Romain CHOLLET | foreach ($ethport as $rowEthPort) { |
155 | if ($rowEthPort['speed']) { |
||
156 | ad931b2e | Francois POIROTTE | // La bande passante de l'interface est exprimée
|
157 | // en Mbit/s dans GLPI et on la veut en bit/s dans Vigilo.
|
||
158 | $test['max'] = $rowEthPort['speed'] << 20; |
||
159 | 8b28e91d | Romain CHOLLET | break;
|
160 | } |
||
161 | } |
||
162 | |||
163 | 166be9d2 | Francois POIROTTE | // Récupère la liste de toutes les adresses IP pour l'interface.
|
164 | // Elles serviront plus tard dans selectAddress() pour choisir
|
||
165 | // l'adresse IP la plus appropriée pour interroger ce réseau.
|
||
166 | 8b28e91d | Romain CHOLLET | foreach ($DB->query($query2) as $nn) { |
167 | $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']); |
||
168 | foreach ($DB->query($query3) as $ip) { |
||
169 | $addr = new IPAddress(); |
||
170 | if ($addr->getFromDB($ip['id'])) { |
||
171 | $this->addresses[] = $addr; |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
178 | public function __toString() |
||
179 | { |
||
180 | 92b19eed | Francois POIROTTE | return self::sprintf( |
181 | '<?xml version="1.0"?>' .
|
||
182 | '<host name="%s" address="%s" ventilation="%s">%s</host>',
|
||
183 | $this->item->getName(),
|
||
184 | $this->selectAddress(),
|
||
185 | "Servers",
|
||
186 | $this->children
|
||
187 | 8b28e91d | Romain CHOLLET | ); |
188 | } |
||
189 | } |