} if (!copyItems($clone_templateid, $templateid)) { throw new Exception(); } // clone triggers $triggers = API::Trigger()->get(array('output' => API_OUTPUT_SHORTEN, 'hostids' => $clone_templateid, 'inherited' => false)); if ($triggers) { if (!copyTriggersToHosts(zbx_objectValues($triggers, 'triggerid'), $templateid, $clone_templateid)) { throw new Exception(); } } // Host graphs $db_graphs = API::Graph()->get(array('hostids' => $clone_templateid, 'inherited' => false, 'output' => API_OUTPUT_REFER)); $result = true; foreach ($db_graphs as $db_graph) { $result &= (bool) copy_graph_to_host($db_graph['graphid'], $templateid); } if (!$result) { throw new Exception(); } // clone discovery rules $discoveryRules = API::DiscoveryRule()->get(array('hostids' => $clone_templateid, 'inherited' => false)); if ($discoveryRules) { $copyDiscoveryRules = API::DiscoveryRule()->copy(array('discoveryids' => zbx_objectValues($discoveryRules, 'itemid'), 'hostids' => array($templateid))); if (!$copyDiscoveryRules) { throw new Exception(); } } // clone screens $screens = API::TemplateScreen()->get(array('templateids' => $clone_templateid, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => true, 'inherited' => false)); if ($screens) {
while ($db_item = DBfetch($res)) { $result &= copy_item_to_host($db_item['itemid'], $hostid, true); } // Host triggers $available_triggers = get_accessible_triggers(PERM_READ_ONLY, array($clone_hostid), PERM_RES_IDS_ARRAY); $sql = 'SELECT DISTINCT t.triggerid, t.description ' . ' FROM triggers t, items i, functions f' . ' WHERE i.hostid=' . $clone_hostid . ' AND f.itemid=i.itemid ' . ' AND t.triggerid=f.triggerid ' . ' AND ' . DBcondition('t.triggerid', $available_triggers) . ' AND t.templateid=0 ' . ' ORDER BY t.description'; $res = DBselect($sql); while ($db_trig = DBfetch($res)) { $result &= copy_trigger_to_host($db_trig['triggerid'], $hostid, true); } // Host graphs $available_graphs = get_accessible_graphs(PERM_READ_ONLY, array($clone_hostid), PERM_RES_IDS_ARRAY); $sql = 'SELECT DISTINCT g.graphid, g.name ' . ' FROM graphs g, graphs_items gi,items i ' . ' WHERE ' . DBcondition('g.graphid', $available_graphs) . ' AND gi.graphid=g.graphid ' . ' AND g.templateid=0 ' . ' AND i.itemid=gi.itemid ' . ' AND i.hostid=' . $clone_hostid . ' ORDER BY g.name'; $res = DBselect($sql); while ($db_graph = DBfetch($res)) { $result &= copy_graph_to_host($db_graph['graphid'], $hostid, true); } $_REQUEST['hostid'] = $clone_hostid; } $result = DBend($result); if ($result) { update_profile('HOST_PORT', $_REQUEST['port'], PROFILE_TYPE_INT); DBstart(); delete_host_profile($hostid); if (get_request('useprofile', 'no') == 'yes') { add_host_profile($hostid, $_REQUEST['devicetype'], $_REQUEST['name'], $_REQUEST['os'], $_REQUEST['serialno'], $_REQUEST['tag'], $_REQUEST['macaddress'], $_REQUEST['hardware'], $_REQUEST['software'], $_REQUEST['contact'], $_REQUEST['location'], $_REQUEST['notes']); } $result = DBend($result); } //HOSTS PROFILE EXTANDED Section if ($result) {
function copy_template_graphs($hostid, $templateid = null, $copy_mode = false) { if ($templateid == null) { $templateid = get_templates_by_hostid($hostid); $templateid = array_keys($templateid); } if (is_array($templateid)) { foreach ($templateid as $key => $id) { copy_template_graphs($hostid, $id, $copy_mode); } // attention recursion return; } $db_graphs = get_graphs_by_hostid($templateid); if ($copy_mode) { while ($db_graph = DBfetch($db_graphs)) { copy_graph_to_host($db_graph["graphid"], $hostid, $copy_mode); } } else { while ($db_graph = DBfetch($db_graphs)) { $gitems = CGraphItem::get(array('graphids' => $db_graph['graphid'], 'output' => API_OUTPUT_EXTEND)); $filter = array('name' => $db_graph['name'], 'hostids' => $hostid); if (CGraph::exists($filter)) { $db_graph['gitems'] = $gitems; $res = CGraph::update($db_graph); } else { $db_graph['templateid'] = $db_graph['graphid']; $db_graph['gitems'] = get_same_graphitems_for_host($gitems, $hostid); $res = CGraph::create($db_graph); } if ($res === false) { return false; } } } return true; }
function copy_template_graphs($hostid, $templateid = null, $copy_mode = false) { if ($templateid == null) { $templateid = get_templates_by_hostid($hostid); $templateid = array_keys($templateid); } if (is_array($templateid)) { foreach ($templateid as $key => $id) { copy_template_graphs($hostid, $id, $copy_mode); } // attention recursion return; } $db_graphs = get_graphs_by_hostid($templateid); while ($db_graph = DBfetch($db_graphs)) { copy_graph_to_host($db_graph["graphid"], $hostid, $copy_mode); } }
zbx_value2array($_REQUEST['copy_targetid']); $opt = array('groupids' => $_REQUEST['copy_targetid'], 'editable' => 1, 'nodes' => get_current_nodeid(true)); $db_groups = CHostGroup::get($opt); $db_groups = zbx_toHash($db_groups, 'groupid'); foreach ($_REQUEST['copy_targetid'] as $gnum => $groupid) { if (!isset($db_groups[$groupid])) { access_deny(); } } $options['groupids'] = $_REQUEST['copy_targetid']; } $db_hosts = CHost::get($options); DBstart(); foreach ($_REQUEST['group_graphid'] as $gnum => $graph_id) { foreach ($db_hosts as $hnum => $host) { $go_result &= (bool) copy_graph_to_host($graph_id, $host['hostid'], true); } } $go_result = DBend($go_result); show_messages($go_result, S_GRAPHS_COPIED, S_CANNOT_COPY_GRAPHS); $_REQUEST['go'] = 'none2'; } else { error(S_NO_TARGET_SELECTED); } show_messages(); } } } } } }
if (0 == $_REQUEST['copy_type']) { /* hosts */ $hosts_ids = $_REQUEST['copy_targetid']; } else { /* groups */ $hosts_ids = array(); $sql = 'SELECT DISTINCT h.hostid ' . ' FROM hosts h, hosts_groups hg' . ' WHERE h.hostid=hg.hostid ' . ' AND ' . DBcondition('hg.groupid', $_REQUEST['copy_targetid']) . ' AND ' . DBcondition('h.hostid', $available_hosts_all_nodes); $db_hosts = DBselect($sql); while ($db_host = DBfetch($db_hosts)) { array_push($hosts_ids, $db_host['hostid']); } } DBstart(); foreach ($_REQUEST['group_graphid'] as $graph_id) { foreach ($hosts_ids as $host_id) { copy_graph_to_host($graph_id, $host_id, true); } } $result = DBend(); unset($_REQUEST['form_copy_to']); } else { error('No target selection.'); } show_messages(); } else { if (isset($_REQUEST['delete_item']) && isset($_REQUEST['group_gid'])) { foreach ($_REQUEST['items'] as $gid => $data) { if (!isset($_REQUEST['group_gid'][$gid])) { continue; } unset($_REQUEST['items'][$gid]);
$discoveryRules = API::DiscoveryRule()->get(array('output' => API_OUTPUT_SHORTEN, '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' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND, 'inherited' => false, 'selectHosts' => API_OUTPUT_REFER, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL))); foreach ($graphs as $graph) { if (count($graph['hosts']) > 1) { continue; } if (httpItemExists($graph['items'])) { continue; } if (!copy_graph_to_host($graph['graphid'], $hostid)) { throw new Exception(); } } } $result = DBend(true); show_messages($result, $msg_ok, $msg_fail); unset($_REQUEST['form'], $_REQUEST['hostid']); } catch (Exception $e) { DBend(false); show_messages(false, $msg_ok, $msg_fail); } unset($_REQUEST['save']); } elseif (isset($_REQUEST['delete']) && isset($_REQUEST['hostid'])) { DBstart(); $result = API::Host()->delete(array('hostid' => $_REQUEST['hostid']));
} else { zbx_value2array($_REQUEST['copy_targetid']); $dbGroups = API::HostGroup()->get(array('groupids' => $_REQUEST['copy_targetid'], 'nodes' => get_current_nodeid(true), 'editable' => true)); $dbGroups = zbx_toHash($dbGroups, 'groupid'); foreach ($_REQUEST['copy_targetid'] as $groupid) { if (!isset($dbGroups[$groupid])) { access_deny(); } } $options['groupids'] = $_REQUEST['copy_targetid']; } $dbHosts = API::Host()->get($options); DBstart(); foreach ($_REQUEST['group_graphid'] as $graphid) { foreach ($dbHosts as $host) { $go_result &= (bool) copy_graph_to_host($graphid, $host['hostid']); } } $go_result = DBend($go_result); show_messages($go_result, _('Graphs copied'), _('Cannot copy graphs')); $_REQUEST['go'] = 'none2'; } else { error(_('No target selected.')); } show_messages(); } if ($_REQUEST['go'] != 'none' && isset($go_result) && $go_result) { $url = new CUrl(); $path = $url->getPath(); insert_js('cookie.eraseArray("' . $path . '")'); }
while ($db_item = DBfetch($res)) { $result &= copy_item_to_host($db_item['itemid'], $templateid, true); } // Host triggers $available_triggers = get_accessible_triggers(PERM_READ_ONLY, array($clone_templateid), PERM_RES_IDS_ARRAY); $sql = 'SELECT DISTINCT t.triggerid, t.description ' . ' FROM triggers t, items i, functions f' . ' WHERE i.hostid=' . $clone_templateid . ' AND f.itemid=i.itemid ' . ' AND t.triggerid=f.triggerid ' . ' AND ' . DBcondition('t.triggerid', $available_triggers) . ' AND t.templateid=0 ' . ' ORDER BY t.description'; $res = DBselect($sql); while ($db_trig = DBfetch($res)) { $result &= copy_trigger_to_host($db_trig['triggerid'], $templateid, true); } // Host graphs $available_graphs = get_accessible_graphs(PERM_READ_ONLY, array($clone_templateid), PERM_RES_IDS_ARRAY); $sql = 'SELECT DISTINCT g.graphid, g.name ' . ' FROM graphs g, graphs_items gi,items i ' . ' WHERE ' . DBcondition('g.graphid', $available_graphs) . ' AND gi.graphid=g.graphid ' . ' AND g.templateid=0 ' . ' AND i.itemid=gi.itemid ' . ' AND i.hostid=' . $clone_templateid . ' ORDER BY g.name'; $res = DBselect($sql); while ($db_graph = DBfetch($res)) { $result &= copy_graph_to_host($db_graph['graphid'], $templateid, true); } } // --->>> <<<--- // <<<--- LINK/UNLINK HOSTS --->>> if ($result) { $hosts = array_intersect($hosts, $available_hosts); //-- unlink -- $linked_hosts = array(); $db_childs = get_hosts_by_templateid($templateid); while ($db_child = DBfetch($db_childs)) { $linked_hosts[$db_child['hostid']] = $db_child['hostid']; } $unlink_hosts = array_diff($linked_hosts, $hosts); foreach ($unlink_hosts as $id => $value) { $result &= unlink_template($value, $templateid, false);