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
|
<?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 CLldRuleHelper extends CItemGeneralHelper {
/**
* @param string $src_templateid
* @param array $dst_host
*
* @return bool
*/
public static function cloneTemplateItems(string $src_templateid, array $dst_host): bool {
$src_items = self::getSourceLldRules([
'templateids' => $src_templateid,
'inherited' => false
]);
$dst_hosts = [$dst_host['templateid'] => $dst_host + ['status' => HOST_STATUS_TEMPLATE]];
return !$src_items || self::copy($src_items, $dst_hosts);
}
/**
* @param string $src_hostid
* @param array $dst_host
*
* @return bool
*/
public static function cloneHostItems(string $src_hostid, array $dst_host): bool {
$src_items = self::getSourceLldRules([
'hostids' => $src_hostid,
'inherited' => false
]);
$dst_hosts = [$dst_host['hostid'] => $dst_host];
return !$src_items || self::copy($src_items, $dst_hosts);
}
/**
* @param array $src_items
* @param array $dst_hosts
*
* @return bool
*/
private static function copy(array $src_items, array $dst_hosts): bool {
try {
$dst_interfaceids = self::getDestinationHostInterfaces($src_items, $dst_hosts);
$dst_master_itemids = self::getDestinationMasterItems($src_items, $dst_hosts);
}
catch (Exception $e) {
return false;
}
$dst_items = [];
foreach ($dst_hosts as $dst_hostid => $dst_host) {
foreach ($src_items as $src_item) {
$dst_item = array_diff_key($src_item, array_flip(['itemid', 'hosts']));
if (array_key_exists($src_item['itemid'], $dst_interfaceids)) {
$dst_item['interfaceid'] = $dst_interfaceids[$src_item['itemid']][$dst_hostid];
}
if (array_key_exists($src_item['itemid'], $dst_master_itemids)) {
$dst_item['master_itemid'] = $dst_master_itemids[$src_item['itemid']][$dst_hostid];
}
$dst_items[] = ['hostid' => $dst_hostid] + getSanitizedItemFields([
'templateid' => 0,
'flags' => ZBX_FLAG_DISCOVERY_RULE,
'hosts' => [$dst_host]
] + $dst_item);
}
}
$response = API::DiscoveryRule()->create($dst_items);
if ($response === false) {
return false;
}
$dst_itemids = [];
foreach ($dst_hosts as $dst_hostid => $foo) {
foreach ($src_items as $src_item) {
$dst_itemids[$src_item['itemid']][$dst_hostid] = array_shift($response['itemids']);
}
}
$src_options = ['discoveryids' => array_keys($src_items)];
$dst_options = reset($dst_hosts)['status'] == HOST_STATUS_TEMPLATE
? ['templateids' => array_keys($dst_hosts)]
: ['hostids' => array_keys($dst_hosts)];
return CItemPrototypeHelper::copy($dst_itemids, $dst_hosts)
&& CTriggerPrototypeHelper::copy($src_options, $dst_options)
&& CGraphPrototypeHelper::copy($src_options, $dst_options)
&& CHostPrototypeHelper::copy($src_options, $dst_options, $dst_itemids);
}
/**
* @param array $src_options
*
* @return array
*/
private static function getSourceLldRules(array $src_options): array {
$src_items = API::DiscoveryRule()->get([
'output' => ['itemid', 'name', 'type', 'key_', 'lifetime_type', 'lifetime', 'enabled_lifetime_type',
'enabled_lifetime', 'description', 'status',
// Type fields.
// The fields used for multiple item types.
'interfaceid', 'authtype', 'username', 'password', 'params', 'timeout', 'delay', 'trapper_hosts',
// Dependent item type specific fields.
'master_itemid',
// HTTP Agent item type specific fields.
'url', 'query_fields', 'request_method', 'post_type', 'posts',
'headers', 'status_codes', 'follow_redirects', 'retrieve_mode', 'output_format', 'http_proxy',
'verify_peer', 'verify_host', 'ssl_cert_file', 'ssl_key_file', 'ssl_key_password', 'allow_traps',
// IPMI item type specific fields.
'ipmi_sensor',
// JMX item type specific fields.
'jmx_endpoint',
// Script item type specific fields.
'parameters',
// SNMP item type specific fields.
'snmp_oid',
// SSH item type specific fields.
'publickey', 'privatekey'
],
'selectPreprocessing' => ['type', 'params', 'error_handler', 'error_handler_params'],
'selectLLDMacroPaths' => ['lld_macro', 'path'],
'selectFilter' => ['evaltype', 'formula', 'conditions'],
'selectOverrides' => ['name', 'step', 'stop', 'filter', 'operations'],
'selectHosts' => ['status'],
'preservekeys' => true
] + $src_options);
foreach ($src_items as &$src_item) {
foreach ($src_item['filter']['conditions'] as &$condition) {
if ($src_item['filter']['evaltype'] != CONDITION_EVAL_TYPE_EXPRESSION) {
unset($condition['formulaid']);
}
}
unset($condition);
foreach ($src_item['overrides'] as &$override) {
unset($override['filter']['eval_formula']);
foreach ($override['filter']['conditions'] as &$condition) {
if ($override['filter']['evaltype'] != CONDITION_EVAL_TYPE_EXPRESSION) {
unset($condition['formulaid']);
}
}
unset($condition);
}
unset($override);
}
unset($src_item);
return $src_items;
}
}
|