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 * Converter for converting import data from 4.0 to 4.2 24 */ 25class C40ImportConverter extends CConverter { 26 27 public function convert($data) { 28 $data['zabbix_export']['version'] = '4.2'; 29 30 if (array_key_exists('hosts', $data['zabbix_export'])) { 31 $data['zabbix_export']['hosts'] = $this->convertHosts($data['zabbix_export']['hosts']); 32 } 33 34 if (array_key_exists('templates', $data['zabbix_export'])) { 35 $data['zabbix_export']['templates'] = $this->convertHosts($data['zabbix_export']['templates']); 36 } 37 38 return $data; 39 } 40 41 /** 42 * Convert hosts. 43 * 44 * @param array $hosts 45 * 46 * @return array 47 */ 48 protected function convertHosts(array $hosts) { 49 foreach ($hosts as &$host) { 50 $host['tags'] = []; 51 52 if (array_key_exists('items', $host)) { 53 $host['items'] = $this->convertItems($host['items']); 54 } 55 56 if (array_key_exists('discovery_rules', $host)) { 57 $host['discovery_rules'] = $this->convertDiscoveryRules($host['discovery_rules']); 58 } 59 } 60 unset($host); 61 62 return $hosts; 63 } 64 65 /** 66 * Convert item elements. 67 * 68 * @param array $items 69 * 70 * @return array 71 */ 72 protected function convertItems(array $items) { 73 foreach ($items as &$item) { 74 if (array_key_exists('preprocessing', $item)) { 75 $item['preprocessing'] = $this->convertItemPreprocessingSteps($item['preprocessing']); 76 } 77 } 78 unset($item); 79 80 return $items; 81 } 82 83 /** 84 * Convert discovery rule elements. 85 * 86 * @param array $discovery_rules 87 * 88 * @return array 89 */ 90 protected function convertDiscoveryRules(array $discovery_rules) { 91 $default = $this->getDiscoveryRuleDefaultFields(); 92 93 foreach ($discovery_rules as &$discovery_rule) { 94 $discovery_rule += $default; 95 $discovery_rule['item_prototypes'] = $this->convertItems($discovery_rule['item_prototypes']); 96 $discovery_rule['master_item'] = []; 97 } 98 unset($discovery_rule); 99 100 return $discovery_rules; 101 } 102 103 /** 104 * Convert item preprocessing step elements. 105 * 106 * @param array $preprocessing_steps 107 * 108 * @return array 109 */ 110 protected function convertItemPreprocessingSteps(array $preprocessing_steps) { 111 $default = [ 112 'error_handler' => DB::getDefault('item_preproc', 'error_handler'), 113 'error_handler_params' => DB::getDefault('item_preproc', 'error_handler_params') 114 ]; 115 116 foreach ($preprocessing_steps as &$preprocessing_step) { 117 $preprocessing_step += $default; 118 } 119 unset($preprocessing_step); 120 121 return $preprocessing_steps; 122 } 123 124 /** 125 * Return associative array of LLD rule default fields. 126 * 127 * @return array 128 */ 129 protected function getDiscoveryRuleDefaultFields() { 130 return [ 131 'lld_macro_paths' => [], 132 'preprocessing' => [] 133 ]; 134 } 135} 136