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
|
<?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 CTriggerHelper extends CTriggerGeneralHelper {
/**
* @param string $src_templateid
* @param string $dst_templateid
*
* @return bool
*/
public static function cloneTemplateTriggers(string $src_templateid, string $dst_templateid): bool {
$src_options = [
'hostids' => $src_templateid,
'inherited' => false
];
$dst_options = ['templateids' => [$dst_templateid]];
return self::copy($src_options, $dst_options);
}
/**
* @param string $src_hostid
* @param string $dst_hostid
*
* @return bool
*/
public static function cloneHostTriggers(string $src_hostid, string $dst_hostid): bool {
$src_options = [
'hostids' => $src_hostid,
'inherited' => false,
'filter' => ['flags' => ZBX_FLAG_DISCOVERY_NORMAL]
];
$dst_options = ['hostids' => [$dst_hostid]];
return self::copy($src_options, $dst_options);
}
/**
* @param array $src_options
* @param array $dst_options
*
* @return bool
*/
public static function copy(array $src_options, array $dst_options): bool {
$src_triggers = self::getSourceTriggers($src_options);
if (!$src_triggers) {
return true;
}
elseif (!array_key_exists('hostids', $src_options)) {
foreach ($src_triggers as $src_trigger) {
if (count($src_trigger['hosts']) > 1) {
$error = reset($src_trigger['hosts'])['status'] == HOST_STATUS_TEMPLATE
? _('Cannot copy trigger "%1$s", because it has multiple templates in the expression.')
: _('Cannot copy trigger "%1$s", because it has multiple hosts in the expression.');
error(sprintf($error, $src_trigger['description']));
return false;
}
}
}
$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']]);
}
elseif (array_key_exists('triggerids', $src_options)) {
$src_host = array_key_exists('hostids', $src_options)
? $src_trigger['hosts'][$src_options['hostids']]
: reset($src_trigger['hosts']);
$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);
try {
$dst_master_triggerids = self::getDestinationMasterTriggers($src_hosts, $dst_hosts);
}
catch (Exception $e) {
return false;
}
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']));
$src_host = array_key_exists('hostids', $src_options)
? $src_trigger['hosts'][$src_options['hostids']]
: reset($src_trigger['hosts']);
$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::Trigger()->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_triggers as $src_trigger) {
if (array_key_exists($src_trigger['triggerid'], $src_master_dep_triggers)) {
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 2;
}
}
$_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 getSourceTriggers(array $src_options): array {
$src_triggers = API::Trigger()->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'
],
'selectDependencies' => ['triggerid'],
'selectTags' => ['tag', 'value'],
'selectHosts' => ['hostid', 'host', 'status'],
'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;
}
}
|