1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2016 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
class CMapImporter extends CImporter {
/**
* Import maps.
*
* @param array $maps
*/
public function import(array $maps) {
$maps = zbx_toHash($maps, 'name');
$this->checkCircularMapReferences($maps);
$maps = $this->resolveMapElementReferences($maps);
/*
* Get all importable maps with removed elements and links. First import maps and then update maps with
* elements and links from import file. This way we make sure we are able to resolve any references
* between maps and links that are imported.
*/
$mapsWithoutElements = $this->getMapsWithoutElements($maps);
$mapsToProcess = ['createMissing' => [], 'updateExisting' => []];
foreach ($mapsWithoutElements as $mapName => $mapWithoutElements) {
$mapId = $this->referencer->resolveMap($mapWithoutElements['name']);
if ($mapId) {
// Update sysmapid in source map too.
$mapWithoutElements['sysmapid'] = $mapId;
$maps[$mapName]['sysmapid'] = $mapId;
$mapsToProcess['updateExisting'][] = $mapWithoutElements;
}
else {
$mapsToProcess['createMissing'][] = $mapWithoutElements;
}
}
if ($this->options['maps']['createMissing'] && $mapsToProcess['createMissing']) {
$newMapIds = API::Map()->create($mapsToProcess['createMissing']);
foreach ($mapsToProcess['createMissing'] as $num => $map) {
$mapId = $newMapIds['sysmapids'][$num];
$this->referencer->addMapRef($map['name'], $mapId);
$maps[$map['name']]['sysmapid'] = $mapId;
}
}
if ($this->options['maps']['updateExisting'] && $mapsToProcess['updateExisting']) {
API::Map()->update($mapsToProcess['updateExisting']);
}
// Form an array of maps that need to be updated with elements and links, respecting the create/update options.
$mapsToUpdate = [];
foreach ($mapsToProcess as $mapActionKey => $mapArray) {
if ($this->options['maps'][$mapActionKey] && $mapsToProcess[$mapActionKey]) {
foreach ($mapArray as $mapItem) {
$map = [
'sysmapid' => $maps[$mapItem['name']]['sysmapid'],
'name' => $mapItem['name'],
'selements' => $maps[$mapItem['name']]['selements'],
'links' => $maps[$mapItem['name']]['links']
];
$map = $this->resolveMapReferences($map);
// Remove the map name so API does not make an update query to the database.
unset($map['name']);
$mapsToUpdate[] = $map;
}
}
}
if ($mapsToUpdate) {
API::Map()->update($mapsToUpdate);
}
}
/**
* Check if map elements have circular references.
* Circular references can be only in map elements that represent another map.
*
* @see checkCircularRecursive
*
* @throws Exception if circular reference found.
*
* @param array $maps
*/
protected function checkCircularMapReferences(array $maps) {
foreach ($maps as $mapName => $map) {
if (empty($map['selements'])) {
continue;
}
foreach ($map['selements'] as $selement) {
$checked = [$mapName];
if ($circMaps = $this->checkCircularRecursive($selement, $maps, $checked)) {
throw new Exception(_s('Circular reference in maps: %1$s.', implode(' - ', $circMaps)));
}
}
}
}
/**
* Recursive function for searching for circular map references.
* If circular reference exist it return array with map elements with circular reference.
*
* @param array $element Map element to inspect on current recursive loop.
* @param array $maps All maps where circular references should be searched.
* @param array $checked Map names that already were processed, should contain unique values
* if no circular references exist.
*
* @return array|bool
*/
protected function checkCircularRecursive(array $element, array $maps, array $checked) {
// If element is not a map element, recursive reference cannot happen.
if ($element['elementtype'] != SYSMAP_ELEMENT_TYPE_MAP) {
return false;
}
$elementMapName = $element['element']['name'];
// If current element map name is already in list of checked map names, circular reference exists.
if (in_array($elementMapName, $checked)) {
/*
* To have a nice result containing only maps that have circular reference,
* remove everything that was added before repeated map name.
*/
$checked = array_slice($checked, array_search($elementMapName, $checked));
// Add repeated name to have nice loop like m1->m2->m3->m1.
$checked[] = $elementMapName;
return $checked;
}
else {
$checked[] = $elementMapName;
}
// Find maps that reference the current element, and if one has selements, check all of them recursively.
if (!empty($maps[$elementMapName]['selements'])) {
foreach ($maps[$elementMapName]['selements'] as $selement) {
return $this->checkCircularRecursive($selement, $maps, $checked);
}
}
return false;
}
/**
* Return maps without their elements.
*
* @param array $maps
*
* @return array
*/
protected function getMapsWithoutElements(array $maps) {
foreach ($maps as &$map) {
if (array_key_exists('selements', $map)) {
unset($map['selements']);
}
if (array_key_exists('links', $map)) {
unset($map['links']);
}
}
unset($map);
return $maps;
}
/**
* Change all references in map to database ids.
*
* @throws Exception
*
* @param array $map
*
* @return array
*/
protected function resolveMapReferences(array $map) {
if (isset($map['selements'])) {
foreach ($map['selements'] as &$selement) {
switch ($selement['elementtype']) {
case SYSMAP_ELEMENT_TYPE_MAP:
$selement['elementid'] = $this->referencer->resolveMap($selement['element']['name']);
if (!$selement['elementid']) {
throw new Exception(_s('Cannot find map "%1$s" used in map "%2$s".',
$selement['element']['name'], $map['name']));
}
break;
case SYSMAP_ELEMENT_TYPE_HOST_GROUP:
$selement['elementid'] = $this->referencer->resolveGroup($selement['element']['name']);
if (!$selement['elementid']) {
throw new Exception(_s('Cannot find group "%1$s" used in map "%2$s".',
$selement['element']['name'], $map['name']));
}
break;
case SYSMAP_ELEMENT_TYPE_HOST:
$selement['elementid'] = $this->referencer->resolveHost($selement['element']['host']);
if (!$selement['elementid']) {
throw new Exception(_s('Cannot find host "%1$s" used in map "%2$s".',
$selement['element']['host'], $map['name']));
}
break;
case SYSMAP_ELEMENT_TYPE_TRIGGER:
$el = $selement['element'];
$selement['elementid'] = $this->referencer->resolveTrigger($el['description'], $el['expression']);
if (!$selement['elementid']) {
throw new Exception(_s(
'Cannot find trigger "%1$s" used in map "%2$s".',
$selement['element']['description'],
$map['name']
));
}
break;
case SYSMAP_ELEMENT_TYPE_IMAGE:
$selement['elementid'] = 0;
break;
}
$icons = [
'icon_off' => 'iconid_off',
'icon_on' => 'iconid_on',
'icon_disabled' => 'iconid_disabled',
'icon_maintenance' => 'iconid_maintenance',
];
foreach ($icons as $element => $field) {
if (!empty($selement[$element])) {
$image = getImageByIdent($selement[$element]);
if (!$image) {
throw new Exception(_s('Cannot find icon "%1$s" used in map "%2$s".',
$selement[$element]['name'], $map['name']));
}
$selement[$field] = $image['imageid'];
}
}
}
unset($selement);
}
if (isset($map['links'])) {
foreach ($map['links'] as &$link) {
if (!$link['linktriggers']) {
unset($link['linktriggers']);
continue;
}
foreach ($link['linktriggers'] as &$linkTrigger) {
$trigger = $linkTrigger['trigger'];
$triggerId = $this->referencer->resolveTrigger($trigger['description'], $trigger['expression']);
if (!$triggerId) {
throw new Exception(_s(
'Cannot find trigger "%1$s" used in map "%2$s".',
$trigger['description'],
$map['name']
));
}
$linkTrigger['triggerid'] = $triggerId;
}
unset($linkTrigger);
}
unset($link);
}
return $map;
}
/**
* Resolves the iconmap and background images for the maps.
*
* @throws Exception if icon map or background image is not found.
*
* @param array $maps
*
* @return array
*/
protected function resolveMapElementReferences(array $maps) {
foreach ($maps as &$map) {
if (isset($map['iconmap']) && $map['iconmap']) {
$map['iconmapid'] = $this->referencer->resolveIconMap($map['iconmap']['name']);
if (!$map['iconmapid']) {
throw new Exception(_s('Cannot find icon map "%1$s" used in map "%2$s".',
$map['iconmap']['name'], $map['name']
));
}
}
if (isset($map['background']) && $map['background']) {
$image = getImageByIdent($map['background']);
if (!$image) {
throw new Exception(_s('Cannot find background image "%1$s" used in map "%2$s".',
$map['background']['name'], $map['name']
));
}
$map['backgroundid'] = $image['imageid'];
}
}
unset($map);
return $maps;
}
}
|