glpi / src / plugins / vigilo / inc / abstractmonitoreditem.class.php @ ad931b2e
History | View | Annotate | Download (5.42 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 | $candidates = array( |
||
53 | 'Locations' => $location, |
||
54 | 'Entities' => $entity, |
||
55 | ); |
||
56 | |||
57 | foreach ($candidates as $type => $candidate) { |
||
58 | if ('N/A' === $candidate->getName()) { |
||
59 | 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 | $manufacturer = new Manufacturer(); |
||
71 | 166be9d2 | Francois POIROTTE | $manufacturer->getFromDB($this->item->fields["manufacturers_id"]); |
72 | ad6689da | Francois POIROTTE | if ('N/A' !== $manufacturer->getName()) { |
73 | 166be9d2 | Francois POIROTTE | $this->children[] = new VigiloGroup("/Manufacturers/" . $manufacturer->getName()); |
74 | 8b28e91d | Romain CHOLLET | } |
75 | } |
||
76 | |||
77 | protected function selectAddress() |
||
78 | { |
||
79 | static $address = null; |
||
80 | |||
81 | 166be9d2 | Francois POIROTTE | if (null === $address && $this->agent) { |
82 | 8b28e91d | Romain CHOLLET | $addresses = $this->agent->getIPs(); |
83 | if (count($addresses)) { |
||
84 | $address = current($addresses); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | 166be9d2 | Francois POIROTTE | if (null === $address) { |
89 | $address = $this->item->getName(); |
||
90 | 8b28e91d | Romain CHOLLET | foreach ($this->addresses as $addr) { |
91 | if (!$addr->is_ipv4()) { |
||
92 | continue;
|
||
93 | } |
||
94 | |||
95 | $textual = $addr->getTextual(); |
||
96 | if (is_string($textual)) { |
||
97 | $address = $textual; |
||
98 | break;
|
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | return $address; |
||
104 | } |
||
105 | |||
106 | protected function monitorNetworkInterfaces() |
||
107 | { |
||
108 | global $DB; |
||
109 | 166be9d2 | Francois POIROTTE | |
110 | 8b28e91d | Romain CHOLLET | $query = NetworkPort::getSQLRequestToSearchForItem( |
111 | 166be9d2 | Francois POIROTTE | $this->item->getType(), |
112 | $this->item->getID()
|
||
113 | 8b28e91d | Romain CHOLLET | ); |
114 | |||
115 | foreach ($DB->query($query) as $np) { |
||
116 | ad6689da | Francois POIROTTE | $query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']); |
117 | $port = new NetworkPort(); |
||
118 | $ethport = new NetworkPortEthernet(); |
||
119 | 166be9d2 | Francois POIROTTE | |
120 | 8b28e91d | Romain CHOLLET | $port->getFromDB($np['id']); |
121 | if ($port->getName() == 'lo') { |
||
122 | continue;
|
||
123 | } |
||
124 | |||
125 | 166be9d2 | Francois POIROTTE | $label = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName(); |
126 | |||
127 | $this->children[] =
|
||
128 | $test = new VigiloTest('Interface', $args); |
||
129 | $test['label'] = $label; |
||
130 | $test['ifname'] = $port->getName(); |
||
131 | |||
132 | ad6689da | Francois POIROTTE | $ethport = $ethport->find('networkports_id=' . $np['id']); |
133 | 8b28e91d | Romain CHOLLET | foreach ($ethport as $rowEthPort) { |
134 | if ($rowEthPort['speed']) { |
||
135 | ad931b2e | Francois POIROTTE | // La bande passante de l'interface est exprimée
|
136 | // en Mbit/s dans GLPI et on la veut en bit/s dans Vigilo.
|
||
137 | $test['max'] = $rowEthPort['speed'] << 20; |
||
138 | 8b28e91d | Romain CHOLLET | break;
|
139 | } |
||
140 | } |
||
141 | |||
142 | 166be9d2 | Francois POIROTTE | // Récupère la liste de toutes les adresses IP pour l'interface.
|
143 | // Elles serviront plus tard dans selectAddress() pour choisir
|
||
144 | // l'adresse IP la plus appropriée pour interroger ce réseau.
|
||
145 | 8b28e91d | Romain CHOLLET | foreach ($DB->query($query2) as $nn) { |
146 | $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']); |
||
147 | foreach ($DB->query($query3) as $ip) { |
||
148 | $addr = new IPAddress(); |
||
149 | if ($addr->getFromDB($ip['id'])) { |
||
150 | $this->addresses[] = $addr; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | public function __toString() |
||
158 | { |
||
159 | ad6689da | Francois POIROTTE | $outXML = new DOMDocument(); |
160 | $outXML->preserveWhiteSpace = false; |
||
161 | $outXML->formatOutput = true; |
||
162 | 8b28e91d | Romain CHOLLET | $outXML->loadXML(
|
163 | self::sprintf( |
||
164 | '<?xml version="1.0"?>' .
|
||
165 | '<host name="%s" address="%s" ventilation="%s">%s</host>',
|
||
166 | 166be9d2 | Francois POIROTTE | $this->item->getName(),
|
167 | 8b28e91d | Romain CHOLLET | $this->selectAddress(),
|
168 | "Servers",
|
||
169 | $this->children
|
||
170 | ) |
||
171 | ); |
||
172 | return $outXML->saveXML(); |
||
173 | } |
||
174 | } |