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