| 12
 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
 
 | <?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
 * Converter for converting import data from 4.0 to 4.2
 */
class C40ImportConverter extends CConverter {
	public function convert($data) {
		$data['zabbix_export']['version'] = '4.2';
		if (array_key_exists('hosts', $data['zabbix_export'])) {
			$data['zabbix_export']['hosts'] = $this->convertHosts($data['zabbix_export']['hosts']);
		}
		if (array_key_exists('templates', $data['zabbix_export'])) {
			$data['zabbix_export']['templates'] = $this->convertHosts($data['zabbix_export']['templates']);
		}
		return $data;
	}
	/**
	 * Convert hosts.
	 *
	 * @param array $hosts
	 *
	 * @return array
	 */
	protected function convertHosts(array $hosts) {
		foreach ($hosts as &$host) {
			$host['tags'] = [];
			if (array_key_exists('items', $host)) {
				$host['items'] = $this->convertItems($host['items']);
			}
			if (array_key_exists('discovery_rules', $host)) {
				$host['discovery_rules'] = $this->convertDiscoveryRules($host['discovery_rules']);
			}
		}
		unset($host);
		return $hosts;
	}
	/**
	 * Convert item elements.
	 *
	 * @param array $items
	 *
	 * @return array
	 */
	protected function convertItems(array $items) {
		foreach ($items as &$item) {
			if (array_key_exists('preprocessing', $item)) {
				$item['preprocessing'] = $this->convertItemPreprocessingSteps($item['preprocessing']);
			}
		}
		unset($item);
		return $items;
	}
	/**
	 * Convert discovery rule elements.
	 *
	 * @param array $discovery_rules
	 *
	 * @return array
	 */
	protected function convertDiscoveryRules(array $discovery_rules) {
		$default = $this->getDiscoveryRuleDefaultFields();
		foreach ($discovery_rules as &$discovery_rule) {
			$discovery_rule += $default;
			$discovery_rule['item_prototypes'] = $this->convertItems($discovery_rule['item_prototypes']);
			$discovery_rule['master_item'] = [];
		}
		unset($discovery_rule);
		return $discovery_rules;
	}
	/**
	 * Convert item preprocessing step elements.
	 *
	 * @param array $preprocessing_steps
	 *
	 * @return array
	 */
	protected function convertItemPreprocessingSteps(array $preprocessing_steps) {
		$default = [
			'error_handler' => DB::getDefault('item_preproc', 'error_handler'),
			'error_handler_params' => DB::getDefault('item_preproc', 'error_handler_params')
		];
		foreach ($preprocessing_steps as &$preprocessing_step) {
			$preprocessing_step += $default;
		}
		unset($preprocessing_step);
		return $preprocessing_steps;
	}
	/**
	 * Return associative array of LLD rule default fields.
	 *
	 * @return array
	 */
	protected function getDiscoveryRuleDefaultFields() {
		return [
			'lld_macro_paths' => [],
			'preprocessing' => []
		];
	}
}
 |