show_error_message(_('Cannot update macros')); } } /* * Display */ $form = new CForm(); $form->cleanItems(); $cmbConf = new CComboBox('configDropDown', 'adm.macros.php', 'redirect(this.options[this.selectedIndex].value);'); $cmbConf->addItems(array('adm.gui.php' => _('GUI'), 'adm.housekeeper.php' => _('Housekeeping'), 'adm.images.php' => _('Images'), 'adm.iconmapping.php' => _('Icon mapping'), 'adm.regexps.php' => _('Regular expressions'), 'adm.macros.php' => _('Macros'), 'adm.valuemapping.php' => _('Value mapping'), 'adm.workingtime.php' => _('Working time'), 'adm.triggerseverities.php' => _('Trigger severities'), 'adm.triggerdisplayoptions.php' => _('Trigger displaying options'), 'adm.other.php' => _('Other'))); $form->addItem($cmbConf); $cnf_wdgt = new CWidget(); $cnf_wdgt->addPageHeader(_('CONFIGURATION OF MACROS'), $form); $data = array(); $data['form_refresh'] = get_request('form_refresh', 0); $data['macros'] = array(); if ($data['form_refresh']) { $data['macros'] = get_request('macros', array()); } else { $data['macros'] = API::UserMacro()->get(array('output' => API_OUTPUT_EXTEND, 'globalmacro' => 1)); } if (empty($data['macros'])) { $data['macros'] = array(0 => array('macro' => '', 'value' => '')); } if ($result) { $data['macros'] = order_macros($data['macros'], 'macro'); } $macrosForm = new CView('administration.general.macros.edit', $data); $cnf_wdgt->addItem($macrosForm->render()); $cnf_wdgt->show(); require_once dirname(__FILE__) . '/include/page_footer.php';
$frmHost->addVar('form', get_request('form', 1)); $frmHost->addVar('groupid', $_REQUEST['groupid']); if ($templateid) { $frmHost->addVar('templateid', $templateid); } if ($templateid > 0 && !hasRequest('form_refresh')) { $host = $this->data['dbTemplate']['host']; $visiblename = $this->data['dbTemplate']['name']; // display empty visible name if equal to host name if ($visiblename === $host) { $visiblename = ''; } // get template groups from db $groups = $this->data['dbTemplate']['groups']; $groups = zbx_objectValues($groups, 'groupid'); $macros = order_macros($this->data['dbTemplate']['macros'], 'macro'); // get template hosts from db $hosts_linked_to = API::Host()->get(array('output' => array('hostid'), 'templateids' => $templateid, 'templated_hosts' => true)); $hosts_linked_to = zbx_objectValues($hosts_linked_to, 'hostid'); $hosts_linked_to = zbx_toHash($hosts_linked_to, 'hostid'); $templateIds = $this->data['original_templates']; } else { $groups = get_request('groups', array()); if (isset($_REQUEST['groupid']) && $_REQUEST['groupid'] > 0 && !uint_in_array($_REQUEST['groupid'], $groups)) { array_push($groups, $_REQUEST['groupid']); } $hosts_linked_to = get_request('hosts', array()); } $clear_templates = array_intersect($clear_templates, array_keys($this->data['original_templates'])); $clear_templates = array_diff($clear_templates, array_keys($templateIds)); natcasesort($templateIds);
// load data from the DB when opening the full clone form for the first time $cloneFormOpened = in_array(getRequest('form'), array('clone', 'full_clone')) && getRequest('form_refresh') == 1; if (getRequest('hostid') && (!hasRequest('form_refresh') || $cloneFormOpened)) { $proxy_hostid = $dbHost['proxy_hostid']; $host = $dbHost['host']; $visiblename = $dbHost['name']; if ($visiblename == $host) { // display empty visible name if equal to host name $visiblename = ''; } $status = $dbHost['status']; $ipmi_authtype = $dbHost['ipmi_authtype']; $ipmi_privilege = $dbHost['ipmi_privilege']; $ipmi_username = $dbHost['ipmi_username']; $ipmi_password = $dbHost['ipmi_password']; $macros = order_macros($dbHost['macros'], 'macro'); $host_groups = zbx_objectValues($dbHost['groups'], 'groupid'); $host_inventory = $dbHost['inventory']; $inventory_mode = empty($host_inventory) ? HOST_INVENTORY_DISABLED : $dbHost['inventory']['inventory_mode']; $templateIds = array(); foreach ($original_templates as $tpl) { $templateIds[$tpl['templateid']] = $tpl['templateid']; } $interfaces = $dbHost['interfaces']; foreach ($interfaces as $hinum => $interface) { $interfaces[$hinum]['items'] = 0; $interfaces[$hinum]['items'] = count($dbHost['interfaces'][$interface['interfaceid']]['items']); // check if interface has items that require specific interface type, if so type cannot be changed $locked = 0; foreach ($dbHost['interfaces'][$interface['interfaceid']]['items'] as $item) { $itemInterfaceType = itemTypeInterface($item['type']);
/** * Format macros. * * @param array $macros * * @return array */ protected function formatMacros(array $macros) { $result = array(); $macros = order_macros($macros, 'macro'); foreach ($macros as $macro) { $result[] = array('macro' => $macro['macro'], 'value' => $macro['value']); } return $result; }
$data['proxies'] = API::Proxy()->get(['output' => ['host'], 'proxyids' => [$data['proxy_hostid']], 'preservekeys' => true]); } else { $data['proxies'] = []; } } else { $data['proxies'] = API::Proxy()->get(['output' => ['host'], 'preservekeys' => true]); order_result($data['proxies'], 'host'); } foreach ($data['proxies'] as &$proxy) { $proxy = $proxy['host']; } unset($proxy); if ($data['show_inherited_macros']) { $data['macros'] = mergeInheritedMacros($data['macros'], getInheritedMacros($data['templates'])); } $data['macros'] = array_values(order_macros($data['macros'], 'macro')); if (!$data['macros'] && $data['flags'] != ZBX_FLAG_DISCOVERY_CREATED) { $macro = ['macro' => '', 'value' => '']; if ($data['show_inherited_macros']) { $macro['type'] = MACRO_TYPE_HOSTMACRO; } $data['macros'][] = $macro; } // groups with RW permissions $data['groupsAllowed'] = API::HostGroup()->get(['output' => [], 'editable' => true, 'preservekeys' => true]); // all available groups $data['groupsAll'] = API::HostGroup()->get(['output' => ['groupid', 'name']]); CArrayHelper::sort($data['groupsAll'], ['name']); if ($data['templates']) { $data['linked_templates'] = API::Template()->get(['output' => ['templateid', 'name'], 'templateids' => $data['templates']]); CArrayHelper::sort($data['linked_templates'], ['name']);
$frmHost->addVar('clear_templates', $clear_templates); $frmHost->addVar('groupid', $_REQUEST['groupid']); if ($templateid) { $frmHost->addVar('templateid', $templateid); } if ($templateid > 0 && !isset($_REQUEST['form_refresh'])) { $host = $dbTemplate['host']; $visiblename = $dbTemplate['name']; // display empry visible nam if equal to host name if ($visiblename == $host) { $visiblename = ''; } // get template groups from db $groups = $dbTemplate['groups']; $groups = zbx_objectValues($groups, 'groupid'); $macros = order_macros($dbTemplate['macros'], 'macro'); // get template hosts from db $hosts_linked_to = API::Host()->get(array('templateids' => $templateid, 'editable' => 1, 'templated_hosts' => 1)); $hosts_linked_to = zbx_objectValues($hosts_linked_to, 'hostid'); $hosts_linked_to = zbx_toHash($hosts_linked_to, 'hostid'); $templates = $original_templates; } else { $groups = get_request('groups', array()); if (isset($_REQUEST['groupid']) && $_REQUEST['groupid'] > 0 && !uint_in_array($_REQUEST['groupid'], $groups)) { array_push($groups, $_REQUEST['groupid']); } $hosts_linked_to = get_request('hosts', array()); } $clear_templates = array_intersect($clear_templates, array_keys($original_templates)); $clear_templates = array_diff($clear_templates, array_keys($templates)); natcasesort($templates);
protected function addRelatedObjects(array $options, array $result) { $result = parent::addRelatedObjects($options, $result); $itemIds = array_keys($result); // adding items if (!is_null($options['selectItems'])) { if ($options['selectItems'] != API_OUTPUT_COUNT) { $relationMap = $this->createRelationMap($result, 'parent_itemid', 'itemid', 'item_discovery'); $items = API::ItemPrototype()->get(array('output' => $options['selectItems'], 'itemids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true)); $result = $relationMap->mapMany($result, $items, 'items', $options['limitSelects']); } else { $items = API::ItemPrototype()->get(array('discoveryids' => $itemIds, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true)); $items = zbx_toHash($items, 'parent_itemid'); foreach ($result as $itemid => $item) { $result[$itemid]['items'] = isset($items[$itemid]) ? $items[$itemid]['rowscount'] : 0; } } } // adding triggers if (!is_null($options['selectTriggers'])) { if ($options['selectTriggers'] != API_OUTPUT_COUNT) { $relationMap = new CRelationMap(); $res = DBselect('SELECT id.parent_itemid,f.triggerid' . ' FROM item_discovery id,items i,functions f' . ' WHERE ' . dbConditionInt('id.parent_itemid', $itemIds) . ' AND id.itemid=i.itemid' . ' AND i.itemid=f.itemid'); while ($relation = DBfetch($res)) { $relationMap->addRelation($relation['parent_itemid'], $relation['triggerid']); } $triggers = API::TriggerPrototype()->get(array('output' => $options['selectTriggers'], 'triggerids' => $relationMap->getRelatedIds(), 'preservekeys' => true)); $result = $relationMap->mapMany($result, $triggers, 'triggers', $options['limitSelects']); } else { $triggers = API::TriggerPrototype()->get(array('discoveryids' => $itemIds, 'countOutput' => true, 'groupCount' => true)); $triggers = zbx_toHash($triggers, 'parent_itemid'); foreach ($result as $itemid => $item) { $result[$itemid]['triggers'] = isset($triggers[$itemid]) ? $triggers[$itemid]['rowscount'] : 0; } } } // adding graphs if (!is_null($options['selectGraphs'])) { if ($options['selectGraphs'] != API_OUTPUT_COUNT) { $relationMap = new CRelationMap(); $res = DBselect('SELECT id.parent_itemid,gi.graphid' . ' FROM item_discovery id,items i,graphs_items gi' . ' WHERE ' . dbConditionInt('id.parent_itemid', $itemIds) . ' AND id.itemid=i.itemid' . ' AND i.itemid=gi.itemid'); while ($relation = DBfetch($res)) { $relationMap->addRelation($relation['parent_itemid'], $relation['graphid']); } $graphs = API::GraphPrototype()->get(array('output' => $options['selectGraphs'], 'graphids' => $relationMap->getRelatedIds(), 'preservekeys' => true)); $result = $relationMap->mapMany($result, $graphs, 'graphs', $options['limitSelects']); } else { $graphs = API::GraphPrototype()->get(array('discoveryids' => $itemIds, 'countOutput' => true, 'groupCount' => true)); $graphs = zbx_toHash($graphs, 'parent_itemid'); foreach ($result as $itemid => $item) { $result[$itemid]['graphs'] = isset($graphs[$itemid]) ? $graphs[$itemid]['rowscount'] : 0; } } } // adding hosts if ($options['selectHostPrototypes'] !== null) { if ($options['selectHostPrototypes'] != API_OUTPUT_COUNT) { $relationMap = $this->createRelationMap($result, 'parent_itemid', 'hostid', 'host_discovery'); $hostPrototypes = API::HostPrototype()->get(array('output' => $options['selectHostPrototypes'], 'hostids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true)); $result = $relationMap->mapMany($result, $hostPrototypes, 'hostPrototypes', $options['limitSelects']); } else { $hostPrototypes = API::HostPrototype()->get(array('discoveryids' => $itemIds, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true)); $hostPrototypes = zbx_toHash($hostPrototypes, 'parent_itemid'); foreach ($result as $itemid => $item) { $result[$itemid]['hostPrototypes'] = isset($hostPrototypes[$itemid]) ? $hostPrototypes[$itemid]['rowscount'] : 0; } } } if ($options['selectFilter'] !== null) { $formulaRequested = $this->outputIsRequested('formula', $options['selectFilter']); $evalFormulaRequested = $this->outputIsRequested('eval_formula', $options['selectFilter']); $conditionsRequested = $this->outputIsRequested('conditions', $options['selectFilter']); $filters = array(); foreach ($result as $rule) { $filters[$rule['itemid']] = array('evaltype' => $rule['evaltype'], 'formula' => isset($rule['formula']) ? $rule['formula'] : ''); } // adding conditions if ($formulaRequested || $evalFormulaRequested || $conditionsRequested) { $conditions = API::getApiService()->select('item_condition', array('output' => array('item_conditionid', 'macro', 'value', 'itemid', 'operator'), 'filter' => array('itemid' => $itemIds), 'preservekeys' => true, 'sortfield' => 'item_conditionid')); $relationMap = $this->createRelationMap($conditions, 'itemid', 'item_conditionid'); $filters = $relationMap->mapMany($filters, $conditions, 'conditions'); foreach ($filters as &$filter) { // in case of a custom expression - use the given formula if ($filter['evaltype'] == CONDITION_EVAL_TYPE_EXPRESSION) { $formula = $filter['formula']; } else { // sort the conditions by macro before generating the formula $conditions = zbx_toHash($filter['conditions'], 'item_conditionid'); $conditions = order_macros($conditions, 'macro'); $formulaConditions = array(); foreach ($conditions as $condition) { $formulaConditions[$condition['item_conditionid']] = $condition['macro']; } $formula = CConditionHelper::getFormula($formulaConditions, $filter['evaltype']); } // generate formulaids from the effective formula $formulaIds = CConditionHelper::getFormulaIds($formula); foreach ($filter['conditions'] as &$condition) { $condition['formulaid'] = $formulaIds[$condition['item_conditionid']]; } unset($condition); // generated a letter based formula only for rules with custom expressions if ($formulaRequested && $filter['evaltype'] == CONDITION_EVAL_TYPE_EXPRESSION) { $filter['formula'] = CConditionHelper::replaceNumericIds($formula, $formulaIds); } if ($evalFormulaRequested) { $filter['eval_formula'] = CConditionHelper::replaceNumericIds($formula, $formulaIds); } } unset($filter); } // add filters to the result foreach ($result as &$rule) { $rule['filter'] = $filters[$rule['itemid']]; } unset($rule); } return $result; }
$frmHost->addVar('templateid', $data['templateId']); } if ($data['templateId'] != 0 && !hasRequest('form_refresh')) { $host = $this->data['dbTemplate']['host']; $visiblename = $this->data['dbTemplate']['name']; // display empty visible name if equal to host name if ($visiblename === $host) { $visiblename = ''; } $macros = $this->data['dbTemplate']['macros']; $templateIds = $this->data['original_templates']; } if ($data['show_inherited_macros']) { $macros = mergeInheritedMacros($macros, getInheritedMacros($templateIds)); } $macros = array_values(order_macros($macros, 'macro')); $clear_templates = array_intersect($clear_templates, array_keys($this->data['original_templates'])); $clear_templates = array_diff($clear_templates, array_keys($templateIds)); natcasesort($templateIds); $frmHost->addVar('clear_templates', $clear_templates); // TEMPLATE WIDGET { $templateList = (new CFormList('hostlist'))->addRow(_('Template name'), (new CTextBox('template_name', $host, false, 128))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)->setAttribute('autofocus', 'autofocus'))->addRow(_('Visible name'), (new CTextBox('visiblename', $visiblename, false, 128))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)); $groupsTB = new CTweenBox($frmHost, 'groups', $data['groupIds'], 10); if ($data['form'] === 'update') { // Add existing template groups to list and, depending on permissions show name as enabled or disabled. $groupsInList = []; foreach ($data['groupsAll'] as $group) { if (isset($data['groupIds'][$group['groupid']])) { $groupsTB->addItem($group['groupid'], $group['name'], true, isset($data['groupsAllowed'][$group['groupid']])); $groupsInList[] = $group['groupid']; }