File: CTriggerPrototypeHelper.php

package info (click to toggle)
zabbix 1%3A7.0.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272,688 kB
  • sloc: sql: 946,050; ansic: 389,440; php: 292,698; javascript: 83,388; sh: 5,680; makefile: 3,285; java: 1,420; cpp: 694; perl: 64; xml: 56
file content (180 lines) | stat: -rw-r--r-- 5,900 bytes parent folder | download | duplicates (2)
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
<?php declare(strict_types = 0);
/*
** 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/>.
**/


class CTriggerPrototypeHelper extends CTriggerGeneralHelper {

	/**
	 * @param array $src_options
	 * @param array $dst_options
	 *
	 * @return bool
	 */
	public static function copy(array $src_options, array $dst_options): bool {
		$src_triggers = self::getSourceTriggerPrototypes($src_options);

		if (!$src_triggers) {
			return true;
		}

		$src_triggerids = array_fill_keys(array_keys($src_triggers), true);
		$src_dep_triggers = [];
		$src_master_dep_triggers = [];
		$src_hosts = [];

		foreach ($src_triggers as $src_trigger) {
			foreach ($src_trigger['dependencies'] as $master_trigger) {
				if (array_key_exists($master_trigger['triggerid'], $src_triggerids)) {
					$src_dep_triggers[$src_trigger['triggerid']] = $src_trigger;
					$src_master_dep_triggers[$master_trigger['triggerid']][$src_trigger['triggerid']] = true;

					unset($src_triggers[$src_trigger['triggerid']]);
				}
				else {
					$src_host = $src_trigger['hosts'][$src_trigger['discoveryRule']['hostid']];

					$src_hosts[$master_trigger['triggerid']][$src_trigger['triggerid']] = $src_host;
				}
			}
		}

		$dst_hosts = array_key_exists('templateids', $dst_options)
			? API::Template()->get([
				'output' => ['host'],
				'preservekeys' => true
			] + $dst_options)
			: API::Host()->get([
				'output' => ['host', 'status'],
				'preservekeys' => true
			] + $dst_options);

		$dst_master_triggerids = self::getDestinationMasterTriggers($src_hosts, $dst_hosts);

		do {
			$dst_triggers = [];

			foreach ($dst_hosts as $dst_hostid => $dst_host) {
				foreach ($src_triggers as $src_trigger) {
					$dst_trigger = array_diff_key($src_trigger, array_flip(['triggerid', 'hosts', 'discoveryRule']));

					$src_host = $src_trigger['hosts'][$src_trigger['discoveryRule']['hostid']];

					$dst_trigger['expression'] = self::getExpressionWithReplacedHost(
						$src_trigger['expression'], $src_host['host'], $dst_host['host']
					);

					if ($src_trigger['recovery_mode'] == ZBX_RECOVERY_MODE_RECOVERY_EXPRESSION) {
						$dst_trigger['recovery_expression'] = self::getExpressionWithReplacedHost(
							$src_trigger['recovery_expression'], $src_host['host'], $dst_host['host']
						);
					}

					foreach ($dst_trigger['dependencies'] as &$master_trigger) {
						if (array_key_exists($master_trigger['triggerid'], $dst_master_triggerids)) {
							$dst_triggerid = $dst_master_triggerids[$master_trigger['triggerid']][$dst_hostid];

							$master_trigger['triggerid'] = is_array($dst_triggerid)
								? $dst_triggerid[$src_host['hostid']]
								: $dst_triggerid;
						}
					}
					unset($master_trigger);

					$dst_triggers[] = $dst_trigger;
				}
			}

			$response = API::TriggerPrototype()->create($dst_triggers);

			if ($response === false) {
				return false;
			}

			$_src_triggers = [];

			if ($src_dep_triggers) {
				foreach ($dst_hosts as $dst_hostid => $foo) {
					foreach ($src_triggers as $src_trigger) {
						$dst_triggerid = array_shift($response['triggerids']);

						if (array_key_exists($src_trigger['triggerid'], $src_master_dep_triggers)) {
							$dst_master_triggerids[$src_trigger['triggerid']][$dst_hostid] = $dst_triggerid;

							foreach ($src_master_dep_triggers[$src_trigger['triggerid']] as $src_dep_triggerid => $f) {
								unset($src_master_dep_triggers[$src_trigger['triggerid']][$src_dep_triggerid]);

								if (!$src_master_dep_triggers[$src_trigger['triggerid']]) {
									unset($src_master_dep_triggers[$src_trigger['triggerid']]);
								}

								foreach ($src_dep_triggers[$src_dep_triggerid]['dependencies'] as $master_trigger) {
									if (bccomp($master_trigger['triggerid'], $src_trigger['triggerid']) == 0) {
										continue;
									}

									if (array_key_exists($master_trigger['triggerid'], $src_master_dep_triggers)
											&& array_key_exists($src_dep_triggerid, $src_master_dep_triggers[$master_trigger['triggerid']])) {
										continue 3;
									}
								}

								$_src_triggers[] = $src_dep_triggers[$src_dep_triggerid];
								unset($src_dep_triggers[$src_dep_triggerid]);
							}
						}
					}
				}
			}

			$src_triggers = $_src_triggers;
		} while ($src_triggers);

		return true;
	}

	/**
	 * @param array  $src_options
	 *
	 * @return array
	 */
	private static function getSourceTriggerPrototypes(array $src_options): array {
		$src_triggers = API::TriggerPrototype()->get([
			'output' => ['triggerid', 'expression', 'description', 'url_name', 'url', 'status', 'priority', 'comments',
				'type', 'recovery_mode', 'recovery_expression', 'correlation_mode', 'correlation_tag', 'manual_close',
				'opdata', 'event_name', 'discover'
			],
			'selectDependencies' => ['triggerid'],
			'selectTags' => ['tag', 'value'],
			'selectHosts' => ['hostid', 'host'],
			'selectDiscoveryRule' => ['itemid', 'hostid'],
			'preservekeys' => true
		] + $src_options);

		if (!$src_triggers) {
			return [];
		}

		$src_triggers = CMacrosResolverHelper::resolveTriggerExpressions($src_triggers,
			['sources' => ['expression', 'recovery_expression']]
		);

		foreach ($src_triggers as &$src_trigger) {
			$src_trigger['hosts'] = array_column($src_trigger['hosts'], null, 'hostid');
		}
		unset($src_trigger);

		return $src_triggers;
	}
}