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
|
<?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 CGraphGeneralHelper {
/**
* @param array $src_graphs
* @param array $dst_options
* @param array $src_options
*
* @return array
*
* @throws Exception
*/
protected static function getDestinationItems(array $src_graphs, array $dst_options, array $src_options): array {
$dst_hostids = reset($dst_options);
$src_item_graphs = [];
foreach ($src_graphs as $src_graph) {
if ($src_graph['ymin_itemid'] != 0) {
$src_item_graphs[$src_graph['ymin_itemid']][$src_graph['graphid']] = true;
}
if ($src_graph['ymax_itemid'] != 0) {
$src_item_graphs[$src_graph['ymax_itemid']][$src_graph['graphid']] = true;
}
foreach ($src_graph['gitems'] as $gitem) {
$src_item_graphs[$gitem['itemid']][$src_graph['graphid']] = true;
}
}
$src_items = API::Item()->get([
'output' => ['itemid', 'hostid', 'key_'],
'webitems' => true,
'itemids' => array_keys($src_item_graphs)
]);
if (array_key_exists('hostids', $src_options) && $src_options['hostids'] != 0) {
foreach ($src_items as $i => $src_item) {
if (bccomp($src_item['hostid'], $src_options['hostids']) != 0) {
unset($src_items[$i]);
}
}
}
if (!$src_items) {
return [];
}
$dst_items = API::Item()->get([
'output' => ['itemid', 'hostid', 'key_'],
'webitems' => true,
'filter' => ['key_' => array_unique(array_column($src_items, 'key_'))]
] + $dst_options);
$_dst_itemids = [];
foreach ($dst_items as $dst_item) {
$_dst_itemids[$dst_item['key_']][$dst_item['hostid']] = $dst_item['itemid'];
}
$dst_itemids = [];
foreach ($src_items as $src_item) {
foreach ($dst_hostids as $dst_hostid) {
if (!array_key_exists($src_item['key_'], $_dst_itemids)
|| !array_key_exists($dst_hostid, $_dst_itemids[$src_item['key_']])) {
$src_graph = $src_graphs[key($src_item_graphs[$src_item['itemid']])];
if (array_key_exists('flags', $src_graph)) {
$error = array_key_exists('hostids', $dst_options)
? _('Cannot copy graph "%1$s", because the item with key "%2$s" does not exist on the host "%3$s".')
: _('Cannot copy graph "%1$s", because the item with key "%2$s" does not exist on the template "%3$s".');
}
else {
$error = array_key_exists('hostids', $dst_options)
? _('Cannot copy graph prototype "%1$s", because the item with key "%2$s" does not exist on the host "%3$s".')
: _('Cannot copy graph prototype "%1$s", because the item with key "%2$s" does not exist on the template "%3$s".');
}
$dst_hosts = array_key_exists('hostids', $dst_options)
? API::Host()->get([
'output' => ['host'],
'hostids' => $dst_hostid
])
: API::Template()->get([
'output' => ['host'],
'templateids' => $dst_hostid
]);
error(sprintf($error, $src_graph['name'], $src_item['key_'], $dst_hosts[0]['host']));
throw new Exception();
}
$dst_itemids[$src_item['itemid']][$dst_hostid] = $_dst_itemids[$src_item['key_']][$dst_hostid];
}
}
return $dst_itemids;
}
}
|