1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22/** 23 * Class that handles associations for zabbix elements unique fields and their database ids. 24 * The purpose is to gather all elements that need ids from database and resolve them with one query. 25 */ 26class CImportReferencer { 27 28 /** 29 * @var array with references to interfaceid (hostid -> reference_name -> interfaceid) 30 */ 31 public $interfacesCache = []; 32 protected $groups = []; 33 protected $templates = []; 34 protected $hosts = []; 35 protected $applications = []; 36 protected $items = []; 37 protected $valueMaps = []; 38 protected $triggers = []; 39 protected $graphs = []; 40 protected $iconMaps = []; 41 protected $maps = []; 42 protected $screens = []; 43 protected $templateScreens = []; 44 protected $macros = []; 45 protected $proxies = []; 46 protected $hostPrototypes = []; 47 protected $groupsRefs; 48 protected $templatesRefs; 49 protected $hostsRefs; 50 protected $applicationsRefs; 51 protected $itemsRefs; 52 protected $valueMapsRefs; 53 protected $triggersRefs; 54 protected $graphsRefs; 55 protected $iconMapsRefs; 56 protected $mapsRefs; 57 protected $screensRefs; 58 protected $templateScreensRefs; 59 protected $macrosRefs; 60 protected $proxiesRefs; 61 protected $hostPrototypesRefs; 62 63 64 /** 65 * Get group id by name. 66 * 67 * @param string $name 68 * 69 * @return string|bool 70 */ 71 public function resolveGroup($name) { 72 if ($this->groupsRefs === null) { 73 $this->selectGroups(); 74 } 75 76 return isset($this->groupsRefs[$name]) ? $this->groupsRefs[$name] : false; 77 } 78 79 /** 80 * Get host id by host. 81 * 82 * @param string $host 83 * 84 * @return string|bool 85 */ 86 public function resolveHost($host) { 87 if ($this->hostsRefs === null) { 88 $this->selectHosts(); 89 } 90 91 return isset($this->hostsRefs[$host]) ? $this->hostsRefs[$host] : false; 92 } 93 94 /** 95 * Get template id by host. 96 * 97 * @param string $host 98 * 99 * @return string|bool 100 */ 101 public function resolveTemplate($host) { 102 if ($this->templatesRefs === null) { 103 $this->selectTemplates(); 104 } 105 106 return isset($this->templatesRefs[$host]) ? $this->templatesRefs[$host] : false; 107 } 108 109 /** 110 * Get host or template id by host. 111 * 112 * @param string $host 113 * 114 * @return string|bool 115 */ 116 public function resolveHostOrTemplate($host) { 117 if ($this->templatesRefs === null) { 118 $this->selectTemplates(); 119 } 120 if ($this->hostsRefs === null) { 121 $this->selectHosts(); 122 } 123 124 if (isset($this->templatesRefs[$host])) { 125 return $this->templatesRefs[$host]; 126 } 127 elseif (isset($this->hostsRefs[$host])) { 128 return $this->hostsRefs[$host]; 129 } 130 else { 131 return false; 132 } 133 } 134 135 /** 136 * Get application id by host id and application name. 137 * 138 * @param string $hostid 139 * @param string $name 140 * 141 * @return string|bool 142 */ 143 public function resolveApplication($hostid, $name) { 144 if ($this->applicationsRefs === null) { 145 $this->selectApplications(); 146 } 147 148 return isset($this->applicationsRefs[$hostid][$name]) ? $this->applicationsRefs[$hostid][$name] : false; 149 } 150 151 /** 152 * Get item id by host id and item key_. 153 * 154 * @param string $hostid 155 * @param string $key 156 * 157 * @return string|bool 158 */ 159 public function resolveItem($hostid, $key) { 160 if ($this->itemsRefs === null) { 161 $this->selectItems(); 162 } 163 164 return isset($this->itemsRefs[$hostid][$key]) ? $this->itemsRefs[$hostid][$key] : false; 165 } 166 167 /** 168 * Get value map id by vale map name. 169 * 170 * @param string $name 171 * 172 * @return string|bool 173 */ 174 public function resolveValueMap($name) { 175 if ($this->valueMapsRefs === null) { 176 $this->selectValueMaps(); 177 } 178 179 return isset($this->valueMapsRefs[$name]) ? $this->valueMapsRefs[$name] : false; 180 } 181 182 /** 183 * Get trigger ID by trigger name and expression. 184 * 185 * @param string $name 186 * @param string $expression 187 * 188 * @return string|bool 189 */ 190 public function resolveTrigger($name, $expression) { 191 if ($this->triggersRefs === null) { 192 $this->selectTriggers(); 193 } 194 195 return isset($this->triggersRefs[$name][$expression]) ? $this->triggersRefs[$name][$expression] : false; 196 } 197 198 /** 199 * Get graph ID by host ID and graph name. 200 * 201 * @param string $hostId 202 * @param string $name 203 * 204 * @return string|bool 205 */ 206 public function resolveGraph($hostId, $name) { 207 if ($this->graphsRefs === null) { 208 $this->selectGraphs(); 209 } 210 211 return isset($this->graphsRefs[$hostId][$name]) ? $this->graphsRefs[$hostId][$name] : false; 212 } 213 214 /** 215 * Get icon map id by name. 216 * 217 * @param string $name 218 * 219 * @return string|bool 220 */ 221 public function resolveIconMap($name) { 222 if ($this->iconMapsRefs === null) { 223 $this->selectIconMaps(); 224 } 225 226 return isset($this->iconMapsRefs[$name]) ? $this->iconMapsRefs[$name] : false; 227 } 228 229 /** 230 * Get map id by name. 231 * 232 * @param string $name 233 * 234 * @return string|bool 235 */ 236 public function resolveMap($name) { 237 if ($this->mapsRefs === null) { 238 $this->selectMaps(); 239 } 240 241 return isset($this->mapsRefs[$name]) ? $this->mapsRefs[$name] : false; 242 } 243 244 /** 245 * Get screen id by name. 246 * 247 * @param string $name 248 * 249 * @return string|bool 250 */ 251 public function resolveScreen($name) { 252 if ($this->screensRefs === null) { 253 $this->selectScreens(); 254 } 255 256 return isset($this->screensRefs[$name]) ? $this->screensRefs[$name] : false; 257 } 258 259 /** 260 * Get templated screen ID by template ID and screen name. 261 * 262 * @param string $templateId 263 * @param string $screenName 264 * 265 * @return string|bool 266 */ 267 public function resolveTemplateScreen($templateId, $screenName) { 268 if ($this->templateScreensRefs === null) { 269 $this->selectTemplateScreens(); 270 } 271 272 return isset($this->templateScreensRefs[$templateId][$screenName]) 273 ? $this->templateScreensRefs[$templateId][$screenName] 274 : false; 275 } 276 277 /** 278 * Get macro ID by host ID and macro name. 279 * 280 * @param string $hostid 281 * @param string $name 282 * 283 * @return string|bool 284 */ 285 public function resolveMacro($hostid, $name) { 286 if ($this->macrosRefs === null) { 287 $this->selectMacros(); 288 } 289 290 return isset($this->macrosRefs[$hostid][$name]) ? $this->macrosRefs[$hostid][$name] : false; 291 } 292 293 /** 294 * Get proxy id by name. 295 * 296 * @param string $name 297 * 298 * @return string|bool 299 */ 300 public function resolveProxy($name) { 301 if ($this->proxiesRefs === null) { 302 $this->selectProxyes(); 303 } 304 305 return isset($this->proxiesRefs[$name]) ? $this->proxiesRefs[$name] : false; 306 } 307 308 /** 309 * Get proxy id by name. 310 * 311 * @param string $hostId 312 * @param string $discoveryRuleId 313 * @param string $hostPrototype 314 * 315 * @return string|bool 316 */ 317 public function resolveHostPrototype($hostId, $discoveryRuleId, $hostPrototype) { 318 if ($this->hostPrototypesRefs === null) { 319 $this->selectHostPrototypes(); 320 } 321 322 if (isset($this->hostPrototypesRefs[$hostId][$discoveryRuleId][$hostPrototype])) { 323 return $this->hostPrototypesRefs[$hostId][$discoveryRuleId][$hostPrototype]; 324 } 325 else { 326 return false; 327 } 328 } 329 330 /** 331 * Add group names that need association with a database group id. 332 * 333 * @param array $groups 334 */ 335 public function addGroups(array $groups) { 336 $this->groups = array_unique(array_merge($this->groups, $groups)); 337 } 338 339 /** 340 * Add group name association with group id. 341 * 342 * @param string $name 343 * @param string $id 344 */ 345 public function addGroupRef($name, $id) { 346 $this->groupsRefs[$name] = $id; 347 } 348 349 /** 350 * Add templates names that need association with a database template id. 351 * 352 * @param array $templates 353 */ 354 public function addTemplates(array $templates) { 355 $this->templates = array_unique(array_merge($this->templates, $templates)); 356 } 357 358 /** 359 * Add template name association with template id. 360 * 361 * @param string $name 362 * @param string $id 363 */ 364 public function addTemplateRef($name, $id) { 365 $this->templatesRefs[$name] = $id; 366 } 367 368 /** 369 * Add hosts names that need association with a database host id. 370 * 371 * @param array $hosts 372 */ 373 public function addHosts(array $hosts) { 374 $this->hosts = array_unique(array_merge($this->hosts, $hosts)); 375 } 376 377 /** 378 * Add host name association with host id. 379 * 380 * @param string $host 381 * @param string $id 382 */ 383 public function addHostRef($host, $id) { 384 $this->hostsRefs[$host] = $id; 385 } 386 387 /** 388 * Add application names that need association with a database application id. 389 * Input array has format: 390 * array('hostname1' => array('appname1', 'appname2'), 'hostname2' => array('appname1'), ...) 391 * 392 * @param array $applications 393 */ 394 public function addApplications(array $applications) { 395 foreach ($applications as $host => $apps) { 396 if (!isset($this->applications[$host])) { 397 $this->applications[$host] = []; 398 } 399 $this->applications[$host] = array_unique(array_merge($this->applications[$host], $apps)); 400 } 401 } 402 403 /** 404 * Add item keys that need association with a database item id. 405 * Input array has format: 406 * array('hostname1' => array('itemkey1', 'itemkey2'), 'hostname2' => array('itemkey1'), ...) 407 * 408 * @param array $items 409 */ 410 public function addItems(array $items) { 411 foreach ($items as $host => $keys) { 412 if (!isset($this->items[$host])) { 413 $this->items[$host] = []; 414 } 415 $this->items[$host] = array_unique(array_merge($this->items[$host], $keys)); 416 } 417 } 418 419 /** 420 * Add item key association with item id. 421 * 422 * @param string $hostId 423 * @param string $key 424 * @param string $itemId 425 */ 426 public function addItemRef($hostId, $key, $itemId) { 427 $this->itemsRefs[$hostId][$key] = $itemId; 428 } 429 430 /** 431 * Add value map association with valuemap ID. 432 * 433 * @param string $name 434 * @param string $valuemapid 435 */ 436 public function addValueMapRef($name, $valuemapid) { 437 $this->valueMapsRefs[$name] = $valuemapid; 438 } 439 440 /** 441 * Add value map names that need association with a database value map ID. 442 * 443 * @param array $valueMaps 444 */ 445 public function addValueMaps(array $valueMaps) { 446 $this->valueMaps = array_unique(array_merge($this->valueMaps, $valueMaps)); 447 } 448 449 /** 450 * Add trigger names/expressions that need association with a database trigger id. 451 * Input array has format: 452 * array('triggername1' => array('expr1', 'expr2'), 'triggername2' => array('expr1'), ...) 453 * 454 * @param array $triggers 455 */ 456 public function addTriggers(array $triggers) { 457 foreach ($triggers as $name => $expressions) { 458 if (!isset($this->triggers[$name])) { 459 $this->triggers[$name] = []; 460 } 461 $this->triggers[$name] = array_unique(array_merge($this->triggers[$name], $expressions)); 462 } 463 } 464 465 /** 466 * Add graph names that need association with a database graph ID. 467 * Input array has format: 468 * array('hostname1' => array('graphname1', 'graphname2'), 'hostname2' => array('graphname1'), ...) 469 * 470 * @param array $graphs 471 */ 472 public function addGraphs(array $graphs) { 473 foreach ($graphs as $host => $hostGraphs) { 474 if (!isset($this->graphs[$host])) { 475 $this->graphs[$host] = []; 476 } 477 $this->graphs[$host] = array_unique(array_merge($this->graphs[$host], $hostGraphs)); 478 } 479 } 480 481 /** 482 * Add trigger name/expression association with trigger id. 483 * 484 * @param string $name 485 * @param string $expression 486 * @param string $triggerId 487 */ 488 public function addTriggerRef($name, $expression, $triggerId) { 489 $this->triggersRefs[$name][$expression] = $triggerId; 490 } 491 492 /** 493 * Add icon map names that need association with a database icon map id. 494 * 495 * @param array $iconMaps 496 */ 497 public function addIconMaps(array $iconMaps) { 498 $this->iconMaps = array_unique(array_merge($this->iconMaps, $iconMaps)); 499 } 500 501 /** 502 * Add map names that need association with a database map id. 503 * 504 * @param array $maps 505 */ 506 public function addMaps(array $maps) { 507 $this->maps = array_unique(array_merge($this->maps, $maps)); 508 } 509 510 /** 511 * Add map name association with map id. 512 * 513 * @param string $name 514 * @param string $mapId 515 */ 516 public function addMapRef($name, $mapId) { 517 $this->mapsRefs[$name] = $mapId; 518 } 519 520 /** 521 * Add screens names that need association with a database screen id. 522 * 523 * @param array $screens 524 */ 525 public function addScreens(array $screens) { 526 $this->screens = array_unique(array_merge($this->screens, $screens)); 527 } 528 529 /** 530 * Add templated screen names that need association with a database screen id. 531 * 532 * @param array $screens 533 */ 534 public function addTemplateScreens(array $screens) { 535 $this->templateScreens = array_unique(array_merge($this->templateScreens, $screens)); 536 } 537 538 /** 539 * Add screen name association with screen id. 540 * 541 * @param string $name 542 * @param string $screenId 543 */ 544 public function addScreenRef($name, $screenId) { 545 $this->screensRefs[$name] = $screenId; 546 } 547 548 /** 549 * Add template screen name association with template screen ID. 550 * 551 * @param string $screenName 552 * @param string $templateScreenId 553 */ 554 public function addTemplateScreenRef($screenName, $templateScreenId) { 555 $this->templateScreensRefs[$screenName] = $templateScreenId; 556 } 557 558 /** 559 * Add macros names that need association with a database macro id. 560 * 561 * @param array $macros 562 */ 563 public function addMacros(array $macros) { 564 foreach ($macros as $host => $ms) { 565 if (!isset($this->macros[$host])) { 566 $this->macros[$host] = []; 567 } 568 $this->macros[$host] = array_unique(array_merge($this->macros[$host], $ms)); 569 } 570 } 571 572 /** 573 * Add macro name association with macro id. 574 * 575 * @param string $hostId 576 * @param string $macro 577 * @param string $macroId 578 */ 579 public function addMacroRef($hostId, $macro, $macroId) { 580 $this->macrosRefs[$hostId][$macro] = $macroId; 581 } 582 583 /** 584 * Add proxy names that need association with a database proxy id. 585 * 586 * @param array $proxies 587 */ 588 public function addProxies(array $proxies) { 589 $this->proxies = array_unique(array_merge($this->proxies, $proxies)); 590 } 591 592 /** 593 * Add proxy name association with proxy id. 594 * 595 * @param string $name 596 * @param string $proxyId 597 */ 598 public function addProxyRef($name, $proxyId) { 599 $this->proxiesRefs[$name] = $proxyId; 600 } 601 602 /** 603 * Add host prototypes that need association with a database host prototype id. 604 * 605 * @param array $hostPrototypes 606 */ 607 public function addHostPrototypes(array $hostPrototypes) { 608 foreach ($hostPrototypes as $host => $discoveryRule) { 609 if (!isset($this->hostPrototypes[$host])) { 610 $this->hostPrototypes[$host] = []; 611 } 612 foreach ($discoveryRule as $discoveryRuleKey => $hostPrototypes) { 613 if (!isset($this->hostPrototypes[$host][$discoveryRuleKey])) { 614 $this->hostPrototypes[$host][$discoveryRuleKey] = []; 615 } 616 $this->hostPrototypes[$host][$discoveryRuleKey] = array_unique( 617 array_merge($this->hostPrototypes[$host][$discoveryRuleKey], $hostPrototypes) 618 ); 619 } 620 } 621 } 622 623 /** 624 * Select group ids for previously added group names. 625 */ 626 protected function selectGroups() { 627 if (!empty($this->groups)) { 628 $this->groupsRefs = []; 629 $dbGroups = API::HostGroup()->get([ 630 'filter' => ['name' => $this->groups], 631 'output' => ['groupid', 'name'], 632 'preservekeys' => true 633 ]); 634 foreach ($dbGroups as $group) { 635 $this->groupsRefs[$group['name']] = $group['groupid']; 636 } 637 638 $this->groups = []; 639 } 640 } 641 642 /** 643 * Select template ids for previously added template names. 644 */ 645 protected function selectTemplates() { 646 if (!empty($this->templates)) { 647 $this->templatesRefs = []; 648 $dbTemplates = API::Template()->get([ 649 'output' => ['host', 'templateid'], 650 'preservekeys' => true, 651 'editable' => true, 652 'filter' => ['host' => $this->templates] 653 ]); 654 foreach ($dbTemplates as $template) { 655 $this->templatesRefs[$template['host']] = $template['templateid']; 656 } 657 658 $this->templates = []; 659 } 660 } 661 662 /** 663 * Select host ids for previously added host names. 664 */ 665 protected function selectHosts() { 666 if (!empty($this->hosts)) { 667 $this->hostsRefs = []; 668 // fetch only normal hosts, discovered hosts must not be imported 669 $dbHosts = API::Host()->get([ 670 'filter' => ['host' => $this->hosts], 671 'output' => ['hostid', 'host'], 672 'preservekeys' => true, 673 'templated_hosts' => true 674 ]); 675 foreach ($dbHosts as $host) { 676 $this->hostsRefs[$host['host']] = $host['hostid']; 677 } 678 679 $this->hosts = []; 680 } 681 } 682 683 /** 684 * Select application ids for previously added application names. 685 */ 686 protected function selectApplications() { 687 if (!empty($this->applications)) { 688 $this->applicationsRefs = []; 689 $sqlWhere = []; 690 691 foreach ($this->applications as $host => $applications) { 692 $hostId = $this->resolveHostOrTemplate($host); 693 if ($hostId) { 694 $sqlWhere[] = '(a.hostid='.zbx_dbstr($hostId).' AND '. 695 dbConditionString('a.name', $applications).')'; 696 } 697 } 698 699 if ($sqlWhere) { 700 $dbApplications = DBselect( 701 'SELECT a.applicationid,a.hostid,a.name'. 702 ' FROM applications a'. 703 ' WHERE '.implode(' OR ', $sqlWhere). 704 ' AND a.flags='.ZBX_FLAG_DISCOVERY_NORMAL 705 ); 706 while ($dbApplication = DBfetch($dbApplications)) { 707 $this->applicationsRefs[$dbApplication['hostid']][$dbApplication['name']] = $dbApplication['applicationid']; 708 } 709 } 710 } 711 } 712 713 /** 714 * Unset application refs to make referencer select them from db again. 715 */ 716 public function refreshApplications() { 717 $this->applicationsRefs = null; 718 } 719 720 /** 721 * Select item ids for previously added item keys. 722 */ 723 protected function selectItems() { 724 if (!empty($this->items)) { 725 $this->itemsRefs = []; 726 727 $sqlWhere = []; 728 foreach ($this->items as $host => $keys) { 729 $hostId = $this->resolveHostOrTemplate($host); 730 if ($hostId) { 731 $sqlWhere[] = '(i.hostid='.zbx_dbstr($hostId).' AND '.dbConditionString('i.key_', $keys).')'; 732 } 733 } 734 735 if ($sqlWhere) { 736 $dbItems = DBselect('SELECT i.itemid,i.hostid,i.key_ FROM items i WHERE '.implode(' OR ', $sqlWhere)); 737 while ($dbItem = DBfetch($dbItems)) { 738 $this->itemsRefs[$dbItem['hostid']][$dbItem['key_']] = $dbItem['itemid']; 739 } 740 } 741 } 742 } 743 744 /** 745 * Unset item refs to make referencer select them from db again. 746 */ 747 public function refreshItems() { 748 $this->itemsRefs = null; 749 } 750 751 /** 752 * Select value map IDs for previously added value map names. 753 */ 754 protected function selectValueMaps() { 755 if ($this->valueMaps) { 756 $this->valueMapsRefs = []; 757 758 $valuemaps = API::ValueMap()->get([ 759 'output' => ['valeumapid', 'name'], 760 'filter' => ['name' => $this->valueMaps] 761 ]); 762 763 foreach ($valuemaps as $valuemap) { 764 $this->valueMapsRefs[$valuemap['name']] = $valuemap['valuemapid']; 765 } 766 767 $this->valueMaps = []; 768 } 769 } 770 771 /** 772 * Select trigger ids for previously added trigger names/expressions. 773 */ 774 protected function selectTriggers() { 775 if (!empty($this->triggers)) { 776 $this->triggersRefs = []; 777 778 $dbTriggers = API::Trigger()->get([ 779 'output' => ['triggerid', 'expression', 'description'], 780 'filter' => [ 781 'description' => array_keys($this->triggers), 782 'flags' => [ 783 ZBX_FLAG_DISCOVERY_NORMAL, 784 ZBX_FLAG_DISCOVERY_PROTOTYPE, 785 ZBX_FLAG_DISCOVERY_CREATED 786 ] 787 ] 788 ]); 789 790 $dbTriggers = CMacrosResolverHelper::resolveTriggerExpressions($dbTriggers); 791 792 foreach ($dbTriggers as $dbTrigger) { 793 if (isset($this->triggers[$dbTrigger['description']][$dbTrigger['expression']])) { 794 $this->triggersRefs[$dbTrigger['description']][$dbTrigger['expression']] = $dbTrigger['triggerid']; 795 } 796 } 797 } 798 } 799 800 /** 801 * Select graph IDs for previously added graph names. 802 */ 803 protected function selectGraphs() { 804 if ($this->graphs) { 805 $this->graphsRefs = []; 806 807 $graphNames = []; 808 809 foreach ($this->graphs as $graphs) { 810 foreach ($graphs as $graph) { 811 $graphNames[$graph] = $graph; 812 } 813 } 814 815 $dbGraphs = API::Graph()->get([ 816 'output' => ['graphid', 'name'], 817 'selectHosts' => ['hostid'], 818 'filter' => [ 819 'name' => $graphNames, 820 'flags' => null 821 ] 822 ]); 823 824 foreach ($dbGraphs as $dbGraph) { 825 foreach ($dbGraph['hosts'] as $host) { 826 $this->graphsRefs[$host['hostid']][$dbGraph['name']] = $dbGraph['graphid']; 827 } 828 } 829 } 830 } 831 832 /** 833 * Unset trigger refs to make referencer select them from db again. 834 */ 835 public function refreshTriggers() { 836 $this->triggersRefs = null; 837 } 838 839 /** 840 * Unset graph refs to make referencer select them from DB again. 841 */ 842 public function refreshGraphs() { 843 $this->graphsRefs = null; 844 } 845 846 /** 847 * Select icon map ids for previously added icon maps names. 848 */ 849 protected function selectIconMaps() { 850 if (!empty($this->iconMaps)) { 851 $this->iconMapsRefs = []; 852 $dbIconMaps = API::IconMap()->get([ 853 'filter' => ['name' => $this->iconMaps], 854 'output' => ['iconmapid', 'name'], 855 'preservekeys' => true 856 ]); 857 foreach ($dbIconMaps as $iconMap) { 858 $this->iconMapsRefs[$iconMap['name']] = $iconMap['iconmapid']; 859 } 860 861 $this->iconMaps = []; 862 } 863 } 864 865 /** 866 * Select map ids for previously added maps names. 867 */ 868 protected function selectMaps() { 869 if (!empty($this->maps)) { 870 $this->mapsRefs = []; 871 $dbMaps = API::Map()->get([ 872 'filter' => ['name' => $this->maps], 873 'output' => ['sysmapid', 'name'], 874 'preservekeys' => true 875 ]); 876 foreach ($dbMaps as $dbMap) { 877 $this->mapsRefs[$dbMap['name']] = $dbMap['sysmapid']; 878 } 879 880 $this->maps = []; 881 } 882 } 883 884 /** 885 * Select screen ids for previously added screen names. 886 */ 887 protected function selectScreens() { 888 if (!empty($this->screens)) { 889 $this->screensRefs = []; 890 891 $db_screens = API::Screen()->get([ 892 'filter' => ['name' => $this->screens], 893 'output' => ['screenid', 'name'] 894 ]); 895 foreach ($db_screens as $db_screen) { 896 $this->screensRefs[$db_screen['name']] = $db_screen['screenid']; 897 } 898 899 $this->screens = []; 900 } 901 } 902 903 /** 904 * Select template screen IDs for previously added screen names and template IDs. 905 */ 906 protected function selectTemplateScreens() { 907 if ($this->templateScreens) { 908 $this->templateScreensRefs = []; 909 910 $db_template_screens = API::TemplateScreen()->get([ 911 'filter' => ['name' => $this->templateScreens], 912 'output' => ['screenid', 'name', 'templateid'] 913 ]); 914 foreach ($db_template_screens as $screen) { 915 $this->templateScreensRefs[$screen['templateid']][$screen['name']] = $screen['screenid']; 916 } 917 918 $this->templateScreens = []; 919 } 920 } 921 922 /** 923 * Select macro ids for previously added macro names. 924 */ 925 protected function selectMacros() { 926 if (!empty($this->macros)) { 927 $this->macrosRefs = []; 928 $sqlWhere = []; 929 foreach ($this->macros as $host => $macros) { 930 $hostId = $this->resolveHostOrTemplate($host); 931 if ($hostId) { 932 $sqlWhere[] = '(hm.hostid='.zbx_dbstr($hostId).' AND '.dbConditionString('hm.macro', $macros).')'; 933 } 934 } 935 936 if ($sqlWhere) { 937 $dbMacros = DBselect('SELECT hm.hostmacroid,hm.hostid,hm.macro FROM hostmacro hm WHERE '.implode(' OR ', $sqlWhere)); 938 while ($dbMacro = DBfetch($dbMacros)) { 939 $this->macrosRefs[$dbMacro['hostid']][$dbMacro['macro']] = $dbMacro['hostmacroid']; 940 } 941 } 942 943 $this->macros = []; 944 } 945 } 946 947 /** 948 * Select proxy ids for previously added proxy names. 949 */ 950 protected function selectProxyes() { 951 if (!empty($this->proxies)) { 952 $this->proxiesRefs = []; 953 $dbProxy = API::Proxy()->get([ 954 'filter' => ['host' => $this->proxies], 955 'output' => ['hostid', 'host'], 956 'preservekeys' => true 957 ]); 958 foreach ($dbProxy as $proxy) { 959 $this->proxiesRefs[$proxy['host']] = $proxy['proxyid']; 960 } 961 962 $this->proxies = []; 963 } 964 } 965 966 /** 967 * Select host prototype ids for previously added host prototypes names. 968 */ 969 protected function selectHostPrototypes() { 970 if (!empty($this->hostPrototypes)) { 971 $this->hostPrototypesRefs = []; 972 $sqlWhere = []; 973 foreach ($this->hostPrototypes as $host => $discoveryRule) { 974 $hostId = $this->resolveHostOrTemplate($host); 975 976 foreach ($discoveryRule as $discoveryRuleKey => $hostPrototypes) { 977 $discoveryRuleId = $this->resolveItem($hostId, $discoveryRuleKey); 978 if ($hostId) { 979 $sqlWhere[] = '(hd.parent_itemid='.zbx_dbstr($discoveryRuleId).' AND '.dbConditionString('h.host', $hostPrototypes).')'; 980 } 981 } 982 } 983 984 if ($sqlWhere) { 985 $query = DBselect( 986 'SELECT h.host,h.hostid,hd.parent_itemid,i.hostid AS parent_hostid '. 987 ' FROM hosts h,host_discovery hd,items i'. 988 ' WHERE h.hostid=hd.hostid'. 989 ' AND hd.parent_itemid=i.itemid'. 990 ' AND '.implode(' OR ', $sqlWhere) 991 ); 992 while ($data = DBfetch($query)) { 993 $this->hostPrototypesRefs[$data['parent_hostid']][$data['parent_itemid']][$data['host']] = $data['hostid']; 994 } 995 } 996 } 997 } 998} 999