foreach ($hostItemPrototypes as $itemPrototype) { $prototypeList[$itemPrototype['itemid']] = itemName($itemPrototype); } order_result($prototypeList); $listBox = new CListBox('itemsPrototypes', null, 8); $listBox->setAttribute('disabled', 'disabled'); $listBox->addItems($prototypeList); $hostList->addRow(_('Item prototypes'), $listBox); } // Trigger prototypes $hostTriggerPrototypes = API::TriggerPrototype()->get(array('hostids' => $_REQUEST['hostid'], 'discoveryids' => $hostDiscoveryRuleids, 'inherited' => false, 'output' => array('triggerid', 'description'), 'selectItems' => array('type'))); if (!empty($hostTriggerPrototypes)) { $prototypeList = array(); foreach ($hostTriggerPrototypes as $triggerPrototype) { // skip trigger prototypes with web items if (httpItemExists($triggerPrototype['items'])) { continue; } $prototypeList[$triggerPrototype['triggerid']] = $triggerPrototype['description']; } if ($prototypeList) { order_result($prototypeList); $listBox = new CListBox('triggerprototypes', null, 8); $listBox->setAttribute('disabled', 'disabled'); $listBox->addItems($prototypeList); $hostList->addRow(_('Trigger prototypes'), $listBox); } } // Graph prototypes $hostGraphPrototypes = API::GraphPrototype()->get(array('hostids' => $_REQUEST['hostid'], 'discoveryids' => $hostDiscoveryRuleids, 'inherited' => false, 'selectHosts' => array('hostid'), 'output' => array('graphid', 'name'))); if (!empty($hostGraphPrototypes)) {
} } // clone discovery rules $discoveryRules = API::DiscoveryRule()->get(array('output' => array('itemid'), 'hostids' => $srcHostId, 'inherited' => false)); if ($discoveryRules) { $copyDiscoveryRules = API::DiscoveryRule()->copy(array('discoveryids' => zbx_objectValues($discoveryRules, 'itemid'), 'hostids' => array($hostId))); if (!$copyDiscoveryRules) { throw new Exception(); } } $graphs = API::Graph()->get(array('hostids' => $srcHostId, 'selectItems' => array('type'), 'output' => API_OUTPUT_EXTEND, 'inherited' => false, 'selectHosts' => array('hostid'), 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL))); foreach ($graphs as $graph) { if (count($graph['hosts']) > 1) { continue; } if (httpItemExists($graph['items'])) { continue; } if (!copyGraphToHost($graph['graphid'], $hostId)) { throw new Exception(); } } } $result = DBend(true); show_messages($result, $msgOk, $msgFail); clearCookies($result); unset($_REQUEST['form'], $_REQUEST['hostid']); } catch (Exception $e) { DBend(false); show_messages(false, $msgOk, $msgFail); }
/** * Copies all of the graphs from the source discovery to the target discovery rule. * * @throws APIException if graph saving fails * * @param array $srcDiscovery The source discovery rule to copy from * @param array $dstDiscovery The target discovery rule to copy to * * @return array */ protected function copyGraphPrototypes(array $srcDiscovery, array $dstDiscovery) { // fetch source graphs $srcGraphs = API::GraphPrototype()->get(array('discoveryids' => $srcDiscovery['itemid'], 'output' => API_OUTPUT_EXTEND, 'selectGraphItems' => API_OUTPUT_EXTEND, 'selectHosts' => API_OUTPUT_REFER, 'preservekeys' => true)); if (!$srcGraphs) { return array(); } $srcItemIds = array(); foreach ($srcGraphs as $key => $graph) { // skip graphs with items from multiple hosts if (count($graph['hosts']) > 1) { unset($srcGraphs[$key]); continue; } // skip graphs with http items if (httpItemExists($graph['gitems'])) { unset($srcGraphs[$key]); continue; } // save all used item ids to map them to the new items foreach ($graph['gitems'] as $item) { $srcItemIds[$item['itemid']] = $item['itemid']; } if ($graph['ymin_itemid']) { $srcItemIds[$graph['ymin_itemid']] = $graph['ymin_itemid']; } if ($graph['ymax_itemid']) { $srcItemIds[$graph['ymax_itemid']] = $graph['ymax_itemid']; } } // fetch source items $items = API::Item()->get(array('itemids' => $srcItemIds, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true, 'filter' => array('flags' => null))); $srcItems = array(); $itemKeys = array(); foreach ($items as $item) { $srcItems[$item['itemid']] = $item; $itemKeys[$item['key_']] = $item['key_']; } // fetch newly cloned items $newItems = API::Item()->get(array('hostids' => $dstDiscovery['hostid'], 'filter' => array('key_' => $itemKeys, 'flags' => null), 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true)); $items = array_merge($dstDiscovery['items'], $newItems); $dstItems = array(); foreach ($items as $item) { $dstItems[$item['key_']] = $item; } $dstGraphs = $srcGraphs; foreach ($dstGraphs as &$graph) { unset($graph['graphid']); unset($graph['templateid']); foreach ($graph['gitems'] as &$gitem) { // replace the old item with the new one with the same key $item = $srcItems[$gitem['itemid']]; $gitem['itemid'] = $dstItems[$item['key_']]['itemid']; unset($gitem['gitemid'], $gitem['graphid']); } // replace the old axis items with the new one with the same key if ($graph['ymin_itemid']) { $yMinSrcItem = $srcItems[$graph['ymin_itemid']]; $graph['ymin_itemid'] = $dstItems[$yMinSrcItem['key_']]['itemid']; } if ($graph['ymax_itemid']) { $yMaxSrcItem = $srcItems[$graph['ymax_itemid']]; $graph['ymax_itemid'] = $dstItems[$yMaxSrcItem['key_']]['itemid']; } } // save graphs $rs = API::Graph()->create($dstGraphs); if (!$rs) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot clone graph prototypes.')); } return $rs; }
} // copy discovery rules $dbDiscoveryRules = API::DiscoveryRule()->get(['output' => ['itemid'], 'hostids' => $srcHostId, 'inherited' => false]); if ($dbDiscoveryRules) { $copyDiscoveryRules = API::DiscoveryRule()->copy(['discoveryids' => zbx_objectValues($dbDiscoveryRules, 'itemid'), 'hostids' => [$hostId]]); if (!$copyDiscoveryRules) { throw new Exception(); } } // copy graphs $dbGraphs = API::Graph()->get(['output' => API_OUTPUT_EXTEND, 'selectHosts' => ['hostid'], 'selectItems' => ['type'], 'hostids' => $srcHostId, 'filter' => ['flags' => ZBX_FLAG_DISCOVERY_NORMAL], 'inherited' => false]); foreach ($dbGraphs as $dbGraph) { if (count($dbGraph['hosts']) > 1) { continue; } if (httpItemExists($dbGraph['items'])) { continue; } if (!copyGraphToHost($dbGraph['graphid'], $hostId)) { throw new Exception(); } } } $result = DBend(true); if ($result) { uncheckTableRows(); } show_messages($result, $msgOk, $msgFail); unset($_REQUEST['form'], $_REQUEST['hostid']); } catch (Exception $e) { DBend(false);