/** * Returns the farthest host prototype ancestor for each given host prototype. * * @param array $hostPrototypeIds * @param array $templateHostPrototypeIds array with parent host prototype IDs as keys and arrays of child host * prototype IDs as values * * @return array an array of child ID - ancestor ID pairs */ function getHostPrototypeSourceParentIds(array $hostPrototypeIds, array $templateHostPrototypeIds = array()) { $query = DBSelect('SELECT h.hostid,h.templateid' . ' FROM hosts h' . ' WHERE ' . dbConditionInt('h.hostid', $hostPrototypeIds) . ' AND h.templateid>0'); $hostPrototypeIds = array(); while ($hostPrototype = DBfetch($query)) { // check if we already have host prototype inherited from the current host prototype // if we do - move all of its child prototypes to the parent template if (isset($templateHostPrototypeIds[$hostPrototype['hostid']])) { $templateHostPrototypeIds[$hostPrototype['templateid']] = $templateHostPrototypeIds[$hostPrototype['hostid']]; unset($templateHostPrototypeIds[$hostPrototype['hostid']]); } else { $templateHostPrototypeIds[$hostPrototype['templateid']][] = $hostPrototype['hostid']; $hostPrototypeIds[] = $hostPrototype['templateid']; } } // continue while we still have new host prototypes to check if ($hostPrototypeIds) { return getHostPrototypeSourceParentIds($hostPrototypeIds, $templateHostPrototypeIds); } else { // return an inverse hash with prototype IDs as keys and parent prototype IDs as values $result = array(); foreach ($templateHostPrototypeIds as $templateId => $hostIds) { foreach ($hostIds as $hostId) { $result[$hostId] = $templateId; } } return $result; } }
CProfile::update('web.' . $page['file'] . '.sortorder', $sortOrder, PROFILE_TYPE_STR); $data = array('form' => getRequest('form'), 'parent_discoveryid' => getRequest('parent_discoveryid'), 'discovery_rule' => $discoveryRule, 'sort' => $sortField, 'sortorder' => $sortOrder); // get items $data['hostPrototypes'] = API::HostPrototype()->get(array('discoveryids' => $data['parent_discoveryid'], 'output' => API_OUTPUT_EXTEND, 'selectTemplates' => array('templateid', 'name'), 'editable' => true, 'sortfield' => $sortField, 'limit' => $config['search_limit'] + 1)); order_result($data['hostPrototypes'], $sortField, $sortOrder); $data['paging'] = getPagingLine($data['hostPrototypes'], $sortOrder); // fetch templates linked to the prototypes $templateIds = array(); foreach ($data['hostPrototypes'] as $hostPrototype) { $templateIds = array_merge($templateIds, zbx_objectValues($hostPrototype['templates'], 'templateid')); } $templateIds = array_unique($templateIds); $linkedTemplates = API::Template()->get(array('output' => array('templateid', 'name'), 'templateids' => $templateIds, 'selectParentTemplates' => array('hostid', 'name'))); $data['linkedTemplates'] = zbx_toHash($linkedTemplates, 'templateid'); // fetch source templates and LLD rules $hostPrototypeSourceIds = getHostPrototypeSourceParentIds(zbx_objectValues($data['hostPrototypes'], 'hostid')); if ($hostPrototypeSourceIds) { $hostPrototypeSourceTemplates = DBfetchArrayAssoc(DBSelect('SELECT h.hostid,h2.name,h2.hostid AS parent_hostid' . ' FROM hosts h,host_discovery hd,items i,hosts h2' . ' WHERE h.hostid=hd.hostid' . ' AND hd.parent_itemid=i.itemid' . ' AND i.hostid=h2.hostid' . ' AND ' . dbConditionInt('h.hostid', $hostPrototypeSourceIds)), 'hostid'); foreach ($data['hostPrototypes'] as &$hostPrototype) { if ($hostPrototype['templateid']) { $sourceTemplate = $hostPrototypeSourceTemplates[$hostPrototypeSourceIds[$hostPrototype['hostid']]]; $hostPrototype['sourceTemplate'] = array('hostid' => $sourceTemplate['parent_hostid'], 'name' => $sourceTemplate['name']); $sourceDiscoveryRuleId = get_realrule_by_itemid_and_hostid($discoveryRule['itemid'], $sourceTemplate['hostid']); $hostPrototype['sourceDiscoveryRuleId'] = $sourceDiscoveryRuleId; } } unset($hostPrototype); } // render view $itemView = new CView('configuration.host.prototype.list', $data); $itemView->render();