function update_host($hostid, $host, $port, $status, $useip, $dns, $ip, $proxy_hostid, $templates, $useipmi, $ipmi_ip, $ipmi_port, $ipmi_authtype, $ipmi_privilege, $ipmi_username, $ipmi_password, $newgroup, $groups) { $old_templates = get_templates_by_hostid($hostid); $unlinked_templates = array_diff($old_templates, $templates); foreach ($unlinked_templates as $id => $name) { unlink_template($hostid, $id); } $new_templates = array_diff($templates, $old_templates); $result = (bool) db_save_host($host, $port, $status, $useip, $dns, $ip, $proxy_hostid, $new_templates, $useipmi, $ipmi_ip, $ipmi_port, $ipmi_authtype, $ipmi_privilege, $ipmi_username, $ipmi_password, $hostid); if (!$result) { return $result; } update_host_groups($hostid, $groups); add_group_to_host($hostid, $newgroup); if (count($new_templates) > 0) { sync_host_with_templates($hostid, array_keys($new_templates)); } return $result; }
function update_host($hostid, $host, $port, $status, $useip, $dns, $ip, $proxy_hostid, $templates, $useipmi, $ipmi_ip, $ipmi_port, $ipmi_authtype, $ipmi_privilege, $ipmi_username, $ipmi_password, $newgroup, $groups) { if (zbx_empty($newgroup) && count($groups) == 0) { info('Host "' . $host . '" must be linked to at least one host group'); return false; } if (is_null($templates)) { $new_templates = array(); } else { $old_templates = get_templates_by_hostid($hostid); $new_templates = array_diff($templates, $old_templates); $unlinked_templates = array_diff($old_templates, $templates); foreach ($unlinked_templates as $id => $name) { unlink_template($hostid, $id); } } $result = (bool) db_save_host($host, $port, $status, $useip, $dns, $ip, $proxy_hostid, $new_templates, $useipmi, $ipmi_ip, $ipmi_port, $ipmi_authtype, $ipmi_privilege, $ipmi_username, $ipmi_password, $hostid); if (!$result) { return $result; } if (!is_null($groups)) { update_host_groups($hostid, $groups); } add_group_to_host($hostid, $newgroup); if (count($new_templates) > 0) { sync_host_with_templates($hostid, array_keys($new_templates)); } return $result; }
private static function link($templateids, $targetids) { if (empty($templateids)) { return true; } // check if any templates linked to targets have more than one unique item key\application {{{ foreach ($targetids as $targetid) { $linkedTpls = self::get(array('nopermissions' => 1, 'output' => API_OUTPUT_SHORTEN, 'hostids' => $targetid)); $allids = array_merge($templateids, zbx_objectValues($linkedTpls, 'templateid')); $sql = 'SELECT key_, count(*) as cnt ' . ' FROM items ' . ' WHERE ' . DBcondition('hostid', $allids) . ' GROUP BY key_ ' . ' HAVING count(*) > 1'; $res = DBselect($sql); if ($db_cnt = DBfetch($res)) { self::exception(ZBX_API_ERROR_PARAMETERS, S_TEMPLATE_WITH_ITEM_KEY . ' [' . htmlspecialchars($db_cnt['key_']) . '] ' . S_ALREADY_LINKED_TO_HOST_SMALL); } $sql = 'SELECT name, count(*) as cnt ' . ' FROM applications ' . ' WHERE ' . DBcondition('hostid', $allids) . ' GROUP BY name ' . ' HAVING count(*) > 1'; $res = DBselect($sql); if ($db_cnt = DBfetch($res)) { self::exception(ZBX_API_ERROR_PARAMETERS, S_TEMPLATE_WITH_APPLICATION . ' [' . htmlspecialchars($db_cnt['name']) . '] ' . S_ALREADY_LINKED_TO_HOST_SMALL); } } // }}} check if any templates linked to targets have more than one unique item key\application // CHECK TEMPLATE TRIGGERS DEPENDENCIES {{{ foreach ($templateids as $tnum => $templateid) { $triggerids = array(); $db_triggers = get_triggers_by_hostid($templateid); while ($trigger = DBfetch($db_triggers)) { $triggerids[$trigger['triggerid']] = $trigger['triggerid']; } $sql = 'SELECT DISTINCT h.host ' . ' FROM trigger_depends td, functions f, items i, hosts h ' . ' WHERE (' . DBcondition('td.triggerid_down', $triggerids) . ' AND f.triggerid=td.triggerid_up) ' . ' AND i.itemid=f.itemid ' . ' AND h.hostid=i.hostid ' . ' AND ' . DBcondition('h.hostid', $templateids, true) . ' AND h.status=' . HOST_STATUS_TEMPLATE; if ($db_dephost = DBfetch(DBselect($sql))) { $options = array('templateids' => $templateid, 'output' => API_OUTPUT_EXTEND); $tmp_tpls = self::get($options); $tmp_tpl = reset($tmp_tpls); self::exception(ZBX_API_ERROR_PARAMETERS, 'Trigger in template [ ' . $tmp_tpl['host'] . ' ] has dependency with trigger in template [ ' . $db_dephost['host'] . ' ]'); } } // }}} CHECK TEMPLATE TRIGGERS DEPENDENCIES $linked = array(); $sql = 'SELECT hostid, templateid ' . ' FROM hosts_templates ' . ' WHERE ' . DBcondition('hostid', $targetids) . ' AND ' . DBcondition('templateid', $templateids); $linked_db = DBselect($sql); while ($pair = DBfetch($linked_db)) { $linked[] = array($pair['hostid'] => $pair['templateid']); } // add template linkages, if problems rollback later foreach ($targetids as $targetid) { foreach ($templateids as $tnum => $templateid) { foreach ($linked as $lnum => $link) { if (isset($link[$targetid]) && $link[$targetid] == $templateid) { continue 2; } } $values = array(get_dbid('hosts_templates', 'hosttemplateid'), $targetid, $templateid); $sql = 'INSERT INTO hosts_templates VALUES (' . implode(', ', $values) . ')'; $result = DBexecute($sql); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'DBError'); } } } // CHECK CIRCULAR LINKAGE {{{ // get template linkage graph $graph = array(); $sql = 'SELECT ht.hostid, ht.templateid' . ' FROM hosts_templates ht, hosts h' . ' WHERE ht.hostid=h.hostid' . ' AND h.status=' . HOST_STATUS_TEMPLATE; $db_graph = DBselect($sql); while ($branch = DBfetch($db_graph)) { if (!isset($graph[$branch['hostid']])) { $graph[$branch['hostid']] = array(); } $graph[$branch['hostid']][$branch['templateid']] = $branch['templateid']; } // get points that have more than one parent templates $start_points = array(); $sql = 'SELECT max(ht.hostid) as hostid, ht.templateid' . ' FROM(' . ' SELECT count(htt.templateid) as ccc, htt.hostid' . ' FROM hosts_templates htt' . ' WHERE htt.hostid NOT IN ( SELECT httt.templateid FROM hosts_templates httt )' . ' GROUP BY htt.hostid' . ' ) ggg, hosts_templates ht' . ' WHERE ggg.ccc>1' . ' AND ht.hostid=ggg.hostid' . ' GROUP BY ht.templateid'; $db_start_points = DBselect($sql); while ($start_point = DBfetch($db_start_points)) { $start_points[] = $start_point['hostid']; $graph[$start_point['hostid']][$start_point['templateid']] = $start_point['templateid']; } // add to the start points also points which we add current templates $start_points = array_merge($start_points, $targetids); $start_points = array_unique($start_points); foreach ($start_points as $spnum => $start) { $path = array(); if (!self::checkCircularLink($graph, $start, $path)) { self::exception(ZBX_API_ERROR_PARAMETERS, S_CIRCULAR_LINK_CANNOT_BE_CREATED); } } // }}} CHECK CIRCULAR LINKAGE foreach ($targetids as $targetid) { foreach ($templateids as $tnum => $templateid) { foreach ($linked as $lnum => $link) { if (isset($link[$targetid]) && $link[$targetid] == $templateid) { unset($linked[$lnum]); continue 2; } } if (!sync_host_with_templates($targetid, $templateid)) { self::exception(ZBX_API_ERROR_PARAMETERS, S_CANNOT_SYNC_TEMPLATE); } } } return true; }
/** * Link Host to Templates * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @param array $data * @param string $data['hostid'] * @param array $data['templateids'] * @return boolean */ public static function linkTemplates($data) { $result = false; $error = ''; $hostid = $data['hostid']; $templateids = $data['templateids']; DBstart(false); foreach ($templateids as $templateid) { $hosttemplateid = get_dbid('hosts_templates', 'hosttemplateid'); if (!($result = DBexecute('INSERT INTO hosts_templates VALUES (' . $hosttemplateid . ',' . $hostid . ',' . $templateid . ')'))) { break; } } if ($result) { foreach ($templateids as $templateid) { $result = sync_host_with_templates($hostid, $templateid); if (!$result) { break; } } } $result = DBend($result); if ($result) { return true; } else { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => $error); return false; } }