/** * Mass update hosts * * @param _array $hosts multidimensional array with Hosts data * @param array $hosts['hosts'] Array of Host objects to update * @param string $hosts['fields']['host'] Host name. * @param array $hosts['fields']['groupids'] HostGroup IDs add Host to. * @param int $hosts['fields']['port'] Port. OPTIONAL * @param int $hosts['fields']['status'] Host Status. OPTIONAL * @param int $hosts['fields']['useip'] Use IP. OPTIONAL * @param string $hosts['fields']['dns'] DNS. OPTIONAL * @param string $hosts['fields']['ip'] IP. OPTIONAL * @param int $hosts['fields']['proxy_hostid'] Proxy Host ID. OPTIONAL * @param int $hosts['fields']['useipmi'] Use IPMI. OPTIONAL * @param string $hosts['fields']['ipmi_ip'] IPMAI IP. OPTIONAL * @param int $hosts['fields']['ipmi_port'] IPMI port. OPTIONAL * @param int $hosts['fields']['ipmi_authtype'] IPMI authentication type. OPTIONAL * @param int $hosts['fields']['ipmi_privilege'] IPMI privilege. OPTIONAL * @param string $hosts['fields']['ipmi_username'] IPMI username. OPTIONAL * @param string $hosts['fields']['ipmi_password'] IPMI password. OPTIONAL * @return boolean */ public static function massUpdate($data) { $hosts = zbx_toArray($data['hosts']); $hostids = zbx_objectValues($hosts, 'hostid'); try { self::BeginTransaction(__METHOD__); $options = array('hostids' => $hostids, 'editable' => 1, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => 1); $upd_hosts = self::get($options); foreach ($hosts as $hnum => $host) { if (!isset($upd_hosts[$host['hostid']])) { self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION); } } // CHECK IF HOSTS HAVE AT LEAST 1 GROUP {{{ if (isset($data['groups']) && empty($data['groups'])) { self::exception(ZBX_API_ERROR_PARAMETERS, 'No groups for hosts'); } // }}} CHECK IF HOSTS HAVE AT LEAST 1 GROUP // UPDATE HOSTS PROPERTIES {{{ if (isset($data['host'])) { if (count($hosts) > 1) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot mass update host name'); } $cur_host = reset($hosts); $options = array('filter' => array('host' => $cur_host['host']), 'output' => API_OUTPUT_SHORTEN, 'editable' => 1, 'nopermissions' => 1); $host_exists = self::get($options); $host_exist = reset($host_exists); if ($host_exist && $host_exist['hostid'] != $cur_host['hostid']) { self::exception(ZBX_API_ERROR_PARAMETERS, S_HOST . ' [ ' . $data['host'] . ' ] ' . S_ALREADY_EXISTS_SMALL); } //can't add host with the same name as existing template if (CTemplate::exists(array('host' => $cur_host['host']))) { self::exception(ZBX_API_ERROR_PARAMETERS, S_TEMPLATE . ' [ ' . $cur_host['host'] . ' ] ' . S_ALREADY_EXISTS_SMALL); } } if (isset($data['host']) && !preg_match('/^' . ZBX_PREG_HOST_FORMAT . '$/i', $data['host'])) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Incorrect characters used for Hostname [ ' . $data['host'] . ' ]'); } if (isset($data['dns']) && !empty($data['dns']) && !preg_match('/^' . ZBX_PREG_DNS_FORMAT . '$/i', $data['dns'])) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Incorrect characters used for DNS [ ' . $data['dns'] . ' ]'); } $sql_set = array(); if (isset($data['proxy_hostid'])) { $sql_set[] = 'proxy_hostid=' . $data['proxy_hostid']; } if (isset($data['host'])) { $sql_set[] = 'host=' . zbx_dbstr($data['host']); } if (isset($data['port'])) { $sql_set[] = 'port=' . $data['port']; } if (isset($data['status'])) { $sql_set[] = 'status=' . $data['status']; } if (isset($data['useip'])) { $sql_set[] = 'useip=' . $data['useip']; } if (isset($data['dns'])) { $sql_set[] = 'dns=' . zbx_dbstr($data['dns']); } if (isset($data['ip'])) { $sql_set[] = 'ip=' . zbx_dbstr($data['ip']); } if (isset($data['useipmi'])) { $sql_set[] = 'useipmi=' . $data['useipmi']; } if (isset($data['ipmi_port'])) { $sql_set[] = 'ipmi_port=' . $data['ipmi_port']; } if (isset($data['ipmi_authtype'])) { $sql_set[] = 'ipmi_authtype=' . $data['ipmi_authtype']; } if (isset($data['ipmi_privilege'])) { $sql_set[] = 'ipmi_privilege=' . $data['ipmi_privilege']; } if (isset($data['ipmi_username'])) { $sql_set[] = 'ipmi_username='******'ipmi_username']); } if (isset($data['ipmi_password'])) { $sql_set[] = 'ipmi_password='******'ipmi_password']); } if (isset($data['ipmi_ip'])) { $sql_set[] = 'ipmi_ip=' . zbx_dbstr($data['ipmi_ip']); } if (!empty($sql_set)) { $sql = 'UPDATE hosts SET ' . implode(', ', $sql_set) . ' WHERE ' . DBcondition('hostid', $hostids); $result = DBexecute($sql); if (isset($data['status'])) { update_host_status($hostids, $data['status']); } } // }}} UPDATE HOSTS PROPERTIES // UPDATE HOSTGROUPS LINKAGE {{{ if (isset($data['groups']) && !is_null($data['groups'])) { $data['groups'] = zbx_toArray($data['groups']); $host_groups = CHostGroup::get(array('hostids' => $hostids)); $host_groupids = zbx_objectValues($host_groups, 'groupid'); $new_groupids = zbx_objectValues($data['groups'], 'groupid'); $groups_to_add = array_diff($new_groupids, $host_groupids); if (!empty($groups_to_add)) { $result = self::massAdd(array('hosts' => $hosts, 'groups' => zbx_toObject($groups_to_add, 'groupid'))); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t add group'); } } $groupids_to_del = array_diff($host_groupids, $new_groupids); if (!empty($groupids_to_del)) { $result = self::massRemove(array('hostids' => $hostids, 'groupids' => $groupids_to_del)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t remove group'); } } } // }}} UPDATE HOSTGROUPS LINKAGE $data['templates_clear'] = isset($data['templates_clear']) ? zbx_toArray($data['templates_clear']) : array(); $cleared_templateids = array(); foreach ($hostids as $hostid) { foreach ($data['templates_clear'] as $tpl) { $result = unlink_template($hostid, $tpl['templateid'], false); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot unlink template [ ' . $tpl['templateid'] . ' ]'); } $cleared_templateids[] = $tpl['templateid']; } } // UPDATE TEMPLATE LINKAGE {{{ if (isset($data['templates']) && !is_null($data['templates'])) { $opt = array('hostids' => $hostids, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => true); $host_templates = CTemplate::get($opt); $host_templateids = array_keys($host_templates); $new_templateids = zbx_objectValues($data['templates'], 'templateid'); $templates_to_del = array_diff($host_templateids, $new_templateids); $templates_to_del = array_diff($templates_to_del, $cleared_templateids); if (!empty($templates_to_del)) { $result = self::massRemove(array('hostids' => $hostids, 'templateids' => $templates_to_del)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, S_CANNOT_UNLINK_TEMPLATE); } } $result = self::massAdd(array('hosts' => $hosts, 'templates' => $data['templates'])); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, S_CANNOT_LINK_TEMPLATE); } } // }}} UPDATE TEMPLATE LINKAGE // UPDATE MACROS {{{ if (isset($data['macros']) && !is_null($data['macros'])) { $macrosToAdd = zbx_toHash($data['macros'], 'macro'); $hostMacros = CUserMacro::get(array('hostids' => $hostids, 'output' => API_OUTPUT_EXTEND)); $hostMacros = zbx_toHash($hostMacros, 'macro'); // Delete $macrosToDelete = array(); foreach ($hostMacros as $hmnum => $hmacro) { if (!isset($macrosToAdd[$hmacro['macro']])) { $macrosToDelete[] = $hmacro['macro']; } } // Update $macrosToUpdate = array(); foreach ($macrosToAdd as $nhmnum => $nhmacro) { if (isset($hostMacros[$nhmacro['macro']])) { $macrosToUpdate[] = $nhmacro; unset($macrosToAdd[$nhmnum]); } } //---- if (!empty($macrosToDelete)) { $result = self::massRemove(array('hostids' => $hostids, 'macros' => $macrosToDelete)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t remove macro'); } } if (!empty($macrosToUpdate)) { $result = CUsermacro::massUpdate(array('hosts' => $hosts, 'macros' => $macrosToUpdate)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot update macro'); } } if (!empty($macrosToAdd)) { $result = self::massAdd(array('hosts' => $hosts, 'macros' => $macrosToAdd)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot add macro'); } } } // }}} UPDATE MACROS // PROFILE {{{ if (isset($data['profile']) && !is_null($data['profile'])) { if (empty($data['profile'])) { $sql = 'DELETE FROM hosts_profiles WHERE ' . DBcondition('hostid', $hostids); if (!DBexecute($sql)) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot delete profile'); } } else { $existing_profiles = array(); $existing_profiles_db = DBselect('SELECT hostid FROM hosts_profiles WHERE ' . DBcondition('hostid', $hostids)); while ($existing_profile = DBfetch($existing_profiles_db)) { $existing_profiles[] = $existing_profile['hostid']; } $hostids_without_profile = array_diff($hostids, $existing_profiles); $fields = array_keys($data['profile']); $fields = implode(', ', $fields); $values = array_map('zbx_dbstr', $data['profile']); $values = implode(', ', $values); foreach ($hostids_without_profile as $hostid) { $sql = 'INSERT INTO hosts_profiles (hostid, ' . $fields . ') VALUES (' . $hostid . ', ' . $values . ')'; if (!DBexecute($sql)) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot create profile'); } } if (!empty($existing_profiles)) { $host_profile_fields = array('devicetype', 'name', 'os', 'serialno', 'tag', 'macaddress', 'hardware', 'software', 'contact', 'location', 'notes'); $sql_set = array(); foreach ($host_profile_fields as $field) { if (isset($data['profile'][$field])) { $sql_set[] = $field . '=' . zbx_dbstr($data['profile'][$field]); } } $sql = 'UPDATE hosts_profiles SET ' . implode(', ', $sql_set) . ' WHERE ' . DBcondition('hostid', $existing_profiles); if (!DBexecute($sql)) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot update profile'); } } } } // }}} PROFILE // EXTENDED PROFILE {{{ if (isset($data['extendedProfile']) && !is_null($data['extendedProfile'])) { if (empty($data['extendedProfile'])) { $sql = 'DELETE FROM hosts_profiles_ext WHERE ' . DBcondition('hostid', $hostids); if (!DBexecute($sql)) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot delete extended profile'); } } else { $existing_profiles = array(); $existing_profiles_db = DBselect('SELECT hostid FROM hosts_profiles_ext WHERE ' . DBcondition('hostid', $hostids)); while ($existing_profile = DBfetch($existing_profiles_db)) { $existing_profiles[] = $existing_profile['hostid']; } $hostids_without_profile = array_diff($hostids, $existing_profiles); $fields = array_keys($data['extendedProfile']); $fields = implode(', ', $fields); $values = array_map('zbx_dbstr', $data['extendedProfile']); $values = implode(', ', $values); foreach ($hostids_without_profile as $hostid) { $sql = 'INSERT INTO hosts_profiles_ext (hostid, ' . $fields . ') VALUES (' . $hostid . ', ' . $values . ')'; if (!DBexecute($sql)) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot create extended profile'); } } if (!empty($existing_profiles)) { $host_profile_ext_fields = array('device_alias', 'device_type', 'device_chassis', 'device_os', 'device_os_short', 'device_hw_arch', 'device_serial', 'device_model', 'device_tag', 'device_vendor', 'device_contract', 'device_who', 'device_status', 'device_app_01', 'device_app_02', 'device_app_03', 'device_app_04', 'device_app_05', 'device_url_1', 'device_url_2', 'device_url_3', 'device_networks', 'device_notes', 'device_hardware', 'device_software', 'ip_subnet_mask', 'ip_router', 'ip_macaddress', 'oob_ip', 'oob_subnet_mask', 'oob_router', 'date_hw_buy', 'date_hw_install', 'date_hw_expiry', 'date_hw_decomm', 'site_street_1', 'site_street_2', 'site_street_3', 'site_city', 'site_state', 'site_country', 'site_zip', 'site_rack', 'site_notes', 'poc_1_name', 'poc_1_email', 'poc_1_phone_1', 'poc_1_phone_2', 'poc_1_cell', 'poc_1_screen', 'poc_1_notes', 'poc_2_name', 'poc_2_email', 'poc_2_phone_1', 'poc_2_phone_2', 'poc_2_cell', 'poc_2_screen', 'poc_2_notes'); $sql_set = array(); foreach ($host_profile_ext_fields as $field) { if (isset($data['extendedProfile'][$field])) { $sql_set[] = $field . '=' . zbx_dbstr($data['extendedProfile'][$field]); } } $sql = 'UPDATE hosts_profiles_ext SET ' . implode(', ', $sql_set) . ' WHERE ' . DBcondition('hostid', $existing_profiles); if (!DBexecute($sql)) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot update extended profile'); } } } } // }}} EXTENDED PROFILE self::EndTransaction(true, __METHOD__); return array('hostids' => $hostids); } catch (APIException $e) { self::EndTransaction(false, __METHOD__); $error = $e->getErrors(); $error = reset($error); self::setError(__METHOD__, $e->getCode(), $error); return false; } }
// $dscvry_wdgt->addHeader(array(S_FOUND.': ',$numrows), $r_form); $numrows = new CDiv(); $numrows->setAttribute('name', 'numrows'); $dscvry_wdgt->addHeader(S_DISCOVERY_RULES_BIG, $r_form); // $dscvry_wdgt->addHeader($numrows); //------------- $sortfield = getPageSortField('ip'); $sortorder = getPageSortOrder(); $options = array('selectHosts' => array('hostid', 'host', 'status'), 'output' => API_OUTPUT_EXTEND, 'sortfield' => $sortfield, 'sortorder' => $sortorder, 'limitSelects' => 1); if ($druleid > 0) { $options['druleids'] = $druleid; } else { $options['druleids'] = zbx_objectValues($drules, 'druleid'); } $dservices = CDService::get($options); $gMacros = CUserMacro::get(array('output' => API_OUTPUT_EXTEND, 'globalmacro' => 1)); $gMacros = zbx_toHash($gMacros, 'macro'); $services = array(); foreach ($dservices as $dsnum => $dservice) { $key_ = $dservice['key_']; if (!zbx_empty($key_)) { if (isset($gMacros[$key_])) { $key_ = $gMacros[$key_]['value']; } $key_ = ': ' . $key_; } $service_name = discovery_check_type2str($dservice['type']) . discovery_port2str($dservice['type'], $dservice['port']) . $key_; $services[$service_name] = 1; } ksort($services); $header = array(is_show_all_nodes() ? new CCol(S_NODE, 'left') : null, make_sorting_header(S_DISCOVERED_DEVICE, 'ip'), new CCol(S_MONITORED_HOST, 'left'), new CCol(array(S_UPTIME . '/', S_DOWNTIME), 'left'));
function get_macros_widget($hostid = null) { if (isset($_REQUEST['form_refresh'])) { $macros = get_request('macros', array()); } else { if ($hostid > 0) { $macros = CUserMacro::get(array('output' => API_OUTPUT_EXTEND, 'hostids' => $hostid)); order_result($macros, 'macro'); } else { if ($hostid === null) { $macros = CUserMacro::get(array('output' => API_OUTPUT_EXTEND, 'globalmacro' => 1)); order_result($macros, 'macro'); } else { $macros = array(); } } } if (empty($macros)) { $macros = array(0 => array('macro' => '', 'value' => '')); } $macros_tbl = new CTable(SPACE, 'formElementTable'); $macros_tbl->setAttribute('id', 'tbl_macros'); $macros_tbl->addRow(array(SPACE, S_MACRO, SPACE, S_VALUE)); insert_js(' function addMacroRow(){ if(typeof(addMacroRow.macro_count) == "undefined"){ addMacroRow.macro_count = ' . count($macros) . '; } var tr = document.createElement("tr"); tr.className = (addMacroRow.macro_count % 2) ? "form_even_row" : "form_odd_row"; var td1 = document.createElement("td"); tr.appendChild(td1); var cb = document.createElement("input"); cb.setAttribute("type", "checkbox"); cb.className = "checkbox"; td1.appendChild(cb); td1.appendChild(document.createTextNode(" ")); var td2 = document.createElement("td"); tr.appendChild(td2); var text1 = document.createElement("input"); text1.setAttribute("type", "text"); text1.setAttribute("name", "macros["+addMacroRow.macro_count+"][macro]"); text1.className = "biginput"; text1.setAttribute("size",30); text1.setAttribute("placeholder","{$MACRO}"); td2.appendChild(text1); td2.appendChild(document.createTextNode(" ")); var td3 = document.createElement("td"); tr.appendChild(td3); var span = document.createElement("span"); span.innerHTML = "⇒"; span.setAttribute("style", "vertical-align:top;"); td3.appendChild(span); var td4 = document.createElement("td"); tr.appendChild(td4); var text2 = document.createElement("input"); text2.setAttribute("type", "text"); text2.setAttribute("placeholder","<' . S_VALUE . '>"); text2.setAttribute("name","macros["+addMacroRow.macro_count+"][value]"); text2.className = "biginput"; text2.setAttribute("size",40); td4.appendChild(text2); var sd = $("row_new_macro").insert({before : tr}); addMacroRow.macro_count++; } '); $macros = array_values($macros); foreach ($macros as $macroid => $macro) { $text1 = new CTextBox('macros[' . $macroid . '][macro]', $macro['macro'], 30); $text1->setAttribute('placeholder', '{$MACRO}'); $text2 = new CTextBox('macros[' . $macroid . '][value]', $macro['value'], 40); $text2->setAttribute('placeholder', '<' . S_VALUE . '>'); $span = new CSpan(RARR); $span->addStyle('vertical-align:top;'); $macros_tbl->addRow(array(new CCheckBox(), $text1, $span, $text2)); } $script = '$$("#tbl_macros input:checked").each(function(obj){ $(obj.parentNode.parentNode).remove(); if (typeof(deleted_macro_cnt) == \'undefined\') deleted_macro_cnt=1; else deleted_macro_cnt++; });'; $delete_btn = new CButton('macros_del', S_DELETE_SELECTED, $script); $delete_btn->setType('button'); $add_button = new CButton('macro_add', S_ADD, 'javascript: addMacroRow()'); $add_button->setType('button'); $buttonRow = new CRow(); $buttonRow->setAttribute('id', 'row_new_macro'); $col = new CCol(array($add_button, SPACE, $delete_btn)); $col->setAttribute('colspan', 4); $buttonRow->addItem($col); $macros_tbl->addRow($buttonRow); $footer = null; if ($hostid === null) { $footer = array(new CButton('save', S_SAVE, "if (deleted_macro_cnt > 0) return confirm('" . S_ARE_YOU_SURE_YOU_WANT_TO_DELETE . " '+deleted_macro_cnt+' " . S_MACROS_ES . "?');")); } return new CFormElement(S_MACROS, $macros_tbl, $footer); }
// SELECT GRAPHS $params = array('hostids' => $hostids, 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND); $graphs = CGraph::get($params); // SELECT GRAPH ITEMS $graphids = zbx_objectValues($graphs, 'graphid'); $params = array('graphids' => $graphids, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => 1, 'expandData' => 1); $gitems = CGraphItem::get($params); foreach ($gitems as $gnum => $gitem) { $gitems[$gitem['gitemid']]['host_key_'] = $gitem['host'] . ':' . $gitem['key_']; } // SELECT TEMPLATES $params = array('hostids' => $hostids, 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND); $templates = CTemplate::get($params); // SELECT MACROS $params = array('hostids' => $hostids, 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND); $macros = CUserMacro::get($params); // SELECT ITEMS $params = array('hostids' => $hostids, 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND); $items = CItem::get($params); // SELECT APPLICATIONS $itemids = zbx_objectValues($items, 'itemid'); //sdii($itemids); $params = array('itemids' => $itemids, 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND); $applications = Capplication::get($params); //sdii($applications); // SELECT TRIGGERS $params = array('hostids' => $hostids, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => 1, 'select_dependencies' => API_OUTPUT_EXTEND, 'expandData' => 1); $triggers = CTrigger::get($params); foreach ($triggers as $tnum => $trigger) { $triggers[$trigger['triggerid']]['expression'] = explode_exp($trigger['expression'], false); }
/** * Mass update hosts * * @param _array $hosts multidimensional array with Hosts data * @param array $hosts['hosts'] Array of Host objects to update * @param string $hosts['fields']['host'] Host name. * @param array $hosts['fields']['groupids'] HostGroup IDs add Host to. * @param int $hosts['fields']['port'] Port. OPTIONAL * @param int $hosts['fields']['status'] Host Status. OPTIONAL * @param int $hosts['fields']['useip'] Use IP. OPTIONAL * @param string $hosts['fields']['dns'] DNS. OPTIONAL * @param string $hosts['fields']['ip'] IP. OPTIONAL * @param int $hosts['fields']['proxy_hostid'] Proxy Host ID. OPTIONAL * @param int $hosts['fields']['useipmi'] Use IPMI. OPTIONAL * @param string $hosts['fields']['ipmi_ip'] IPMAI IP. OPTIONAL * @param int $hosts['fields']['ipmi_port'] IPMI port. OPTIONAL * @param int $hosts['fields']['ipmi_authtype'] IPMI authentication type. OPTIONAL * @param int $hosts['fields']['ipmi_privilege'] IPMI privilege. OPTIONAL * @param string $hosts['fields']['ipmi_username'] IPMI username. OPTIONAL * @param string $hosts['fields']['ipmi_password'] IPMI password. OPTIONAL * @return boolean */ public static function massUpdate($data) { $transaction = false; $templates = zbx_toArray($data['templates']); $templateids = zbx_objectValues($templates, 'templateid'); try { $options = array('templateids' => $templateids, 'editable' => 1, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => 1); $upd_templates = self::get($options); foreach ($templates as $tnum => $template) { if (!isset($upd_templates[$template['templateid']])) { self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION); } } // CHECK IF TEMPLATES HAVE AT LEAST 1 GROUP {{{ if (isset($data['groups']) && empty($data['groups'])) { self::exception(ZBX_API_ERROR_PARAMETERS, 'No groups for template'); } $data['groups'] = zbx_toArray($data['groups']); // }}} CHECK IF TEMPLATES HAVE AT LEAST 1 GROUP $transaction = self::BeginTransaction(__METHOD__); // UPDATE TEMPLATES PROPERTIES {{{ if (isset($data['host'])) { if (count($templates) > 1) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot mass update template name'); } $cur_template = reset($templates); $options = array('filter' => array('host' => $cur_template['host']), 'output' => API_OUTPUT_SHORTEN, 'editable' => 1, 'nopermissions' => 1); $template_exists = self::get($options); $template_exist = reset($template_exists); if ($template_exist && $template_exist['templateid'] != $cur_template['templateid']) { self::exception(ZBX_API_ERROR_PARAMETERS, S_TEMPLATE . ' [ ' . $data['host'] . ' ] ' . S_ALREADY_EXISTS_SMALL); } //can't set the same name as existing host if (CHost::exists(array('host' => $cur_template['host']))) { self::exception(ZBX_API_ERROR_PARAMETERS, S_HOST . ' [ ' . $template['host'] . ' ] ' . S_ALREADY_EXISTS_SMALL); } } if (isset($data['host']) && !preg_match('/^' . ZBX_PREG_HOST_FORMAT . '$/i', $data['host'])) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Incorrect characters used for Hostname [ ' . $data['host'] . ' ]'); } $sql_set = array(); if (isset($data['host'])) { $sql_set[] = 'host=' . zbx_dbstr($data['host']); } if (!empty($sql_set)) { $sql = 'UPDATE hosts SET ' . implode(', ', $sql_set) . ' WHERE ' . DBcondition('hostid', $templateids); $result = DBexecute($sql); } // }}} UPDATE TEMPLATES PROPERTIES // UPDATE HOSTGROUPS LINKAGE {{{ if (isset($data['groups']) && !is_null($data['groups'])) { $template_groups = CHostGroup::get(array('hostids' => $templateids)); $template_groupids = zbx_objectValues($template_groups, 'groupid'); $new_groupids = zbx_objectValues($data['groups'], 'groupid'); $groups_to_add = array_diff($new_groupids, $template_groupids); if (!empty($groups_to_add)) { $result = self::massAdd(array('templates' => $templates, 'groups' => zbx_toObject($groups_to_add, 'groupid'))); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t add group'); } } $groupids_to_del = array_diff($template_groupids, $new_groupids); if (!empty($groupids_to_del)) { $result = self::massRemove(array('templateids' => $templateids, 'groupids' => $groupids_to_del)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t remove group'); } } } // }}} UPDATE HOSTGROUPS LINKAGE $data['templates_clear'] = isset($data['templates_clear']) ? zbx_toArray($data['templates_clear']) : array(); $cleared_templateids = array(); foreach ($templateids as $templateid) { foreach ($data['templates_clear'] as $tpl) { $result = unlink_template($templateid, $tpl['templateid'], false); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot unlink template [ ' . $tpl['templateid'] . ' ]'); } $cleared_templateids[] = $tpl['templateid']; } } // UPDATE TEMPLATE LINKAGE {{{ // firstly need to unlink all things, to correctly check circulars if (isset($data['hosts']) && !is_null($data['hosts'])) { $template_hosts = CHost::get(array('templateids' => $templateids, 'templated_hosts' => 1)); $template_hostids = zbx_objectValues($template_hosts, 'hostid'); $new_hostids = zbx_objectValues($data['hosts'], 'hostid'); $hosts_to_del = array_diff($template_hostids, $new_hostids); $hostids_to_del = array_diff($hosts_to_del, $cleared_templateids); if (!empty($hostids_to_del)) { $result = self::massRemove(array('hostids' => $hostids_to_del, 'templateids' => $templateids)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t unlink template'); } } } if (isset($data['templates_link']) && !is_null($data['templates_link'])) { $template_templates = CTemplate::get(array('hostids' => $templateids)); $template_templateids = zbx_objectValues($template_templates, 'templateid'); $new_templateids = zbx_objectValues($data['templates_link'], 'templateid'); $templates_to_del = array_diff($template_templateids, $new_templateids); $templateids_to_del = array_diff($templates_to_del, $cleared_templateids); if (!empty($templateids_to_del)) { $result = self::massRemove(array('templateids' => $templateids, 'templateids_link' => $templateids_to_del)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t unlink template'); } } } if (isset($data['hosts']) && !is_null($data['hosts'])) { $hosts_to_add = array_diff($new_hostids, $template_hostids); if (!empty($hosts_to_add)) { $result = self::massAdd(array('templates' => $templates, 'hosts' => $hosts_to_add)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t link template'); } } } if (isset($data['templates_link']) && !is_null($data['templates_link'])) { $templates_to_add = array_diff($new_templateids, $template_templateids); if (!empty($templates_to_add)) { $result = self::massAdd(array('templates' => $templates, 'templates_link' => $templates_to_add)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t link template'); } } } // }}} UPDATE TEMPLATE LINKAGE // UPDATE MACROS {{{ if (isset($data['macros']) && !is_null($data['macros'])) { $macrosToAdd = zbx_toHash($data['macros'], 'macro'); $templateMacros = CUserMacro::get(array('hostids' => $templateids, 'output' => API_OUTPUT_EXTEND)); $templateMacros = zbx_toHash($templateMacros, 'macro'); // Delete $macrosToDelete = array(); foreach ($templateMacros as $hmnum => $hmacro) { if (!isset($macrosToAdd[$hmacro['macro']])) { $macrosToDelete[] = $hmacro['macro']; } } // Update $macrosToUpdate = array(); foreach ($macrosToAdd as $nhmnum => $nhmacro) { if (isset($templateMacros[$nhmacro['macro']])) { $macrosToUpdate[] = $nhmacro; unset($macrosToAdd[$nhmnum]); } } //---- if (!empty($macrosToDelete)) { $result = self::massRemove(array('templateids' => $templateids, 'macros' => $macrosToDelete)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t remove macro'); } } if (!empty($macrosToUpdate)) { $result = CUsermacro::massUpdate(array('templates' => $templates, 'macros' => $macrosToUpdate)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot update macro'); } } if (!empty($macrosToAdd)) { $result = self::massAdd(array('templates' => $templates, 'macros' => $macrosToAdd)); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot add macro'); } } } // }}} UPDATE MACROS self::EndTransaction(true, __METHOD__); return array('templateids' => $templateids); } catch (APIException $e) { if ($transaction) { self::EndTransaction(false, __METHOD__); } $error = $e->getErrors(); $error = reset($error); self::setError(__METHOD__, $e->getCode(), $error); return false; } }
} } if (!empty($macrosToUpdate)) { if (!CUsermacro::updateGlobal($macrosToUpdate)) { throw new Exception(S_CANNOT_UPDATE_MACRO); } } if (!empty($newMacros)) { $macrosToAdd = array_values($newMacros); $new_macroids = CUsermacro::createGlobal($macrosToAdd); if (!$new_macroids) { throw new Exception(S_CANNOT_ADD_MACRO); } } if (!empty($macrosToAdd)) { $new_macros = CUserMacro::get(array('globalmacroids' => $new_macroids['globalmacroids'], 'globalmacro' => 1, 'output' => API_OUTPUT_EXTEND)); $new_macros = zbx_toHash($new_macros, 'globalmacroid'); foreach ($macrosToDelete as $delm) { add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_MACRO, $delm['globalmacroid'], $global_macros[$delm['globalmacroid']]['macro'], null, null, null); } foreach ($new_macroids['globalmacroids'] as $newid) { add_audit_ext(AUDIT_ACTION_ADD, AUDIT_RESOURCE_MACRO, $newid, $new_macros[$newid]['macro'], null, null, null); } } DBend(true); show_messages(true, S_MACROS_UPDATED, S_CANNOT_UPDATE_MACROS); } catch (Exception $e) { DBend(false); error($e->getMessage()); show_messages(false, S_MACROS_UPDATED, S_CANNOT_UPDATE_MACROS); }