Esempio n. 1
0
 protected function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $hostids = array_keys($result);
     // adding groups
     if ($options['selectGroups'] !== null) {
         $relationMap = $this->createRelationMap($result, 'hostid', 'groupid', 'hosts_groups');
         $groups = API::HostGroup()->get(array('output' => $options['selectGroups'], 'groupids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
         $result = $relationMap->mapMany($result, $groups, 'groups');
     }
     // adding templates
     if ($options['selectParentTemplates'] !== null) {
         if ($options['selectParentTemplates'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'hostid', 'templateid', 'hosts_templates');
             $templates = API::Template()->get(array('output' => $options['selectParentTemplates'], 'templateids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
             if (!is_null($options['limitSelects'])) {
                 order_result($templates, 'host');
             }
             $result = $relationMap->mapMany($result, $templates, 'parentTemplates', $options['limitSelects']);
         } else {
             $templates = API::Template()->get(array('hostids' => $hostids, 'countOutput' => true, 'groupCount' => true));
             $templates = zbx_toHash($templates, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['parentTemplates'] = isset($templates[$hostid]) ? $templates[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding items
     if ($options['selectItems'] !== null) {
         if ($options['selectItems'] != API_OUTPUT_COUNT) {
             $items = API::Item()->get(array('output' => $this->outputExtend($options['selectItems'], array('hostid', 'itemid')), 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true));
             if (!is_null($options['limitSelects'])) {
                 order_result($items, 'name');
             }
             $relationMap = $this->createRelationMap($items, 'hostid', 'itemid');
             $items = $this->unsetExtraFields($items, array('hostid', 'itemid'), $options['selectItems']);
             $result = $relationMap->mapMany($result, $items, 'items', $options['limitSelects']);
         } else {
             $items = API::Item()->get(array('hostids' => $hostids, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
             $items = zbx_toHash($items, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['items'] = isset($items[$hostid]) ? $items[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding discoveries
     if ($options['selectDiscoveries'] !== null) {
         if ($options['selectDiscoveries'] != API_OUTPUT_COUNT) {
             $items = API::DiscoveryRule()->get(array('output' => $this->outputExtend($options['selectDiscoveries'], array('hostid', 'itemid')), 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true));
             if (!is_null($options['limitSelects'])) {
                 order_result($items, 'name');
             }
             $relationMap = $this->createRelationMap($items, 'hostid', 'itemid');
             $items = $this->unsetExtraFields($items, array('hostid', 'itemid'), $options['selectDiscoveries']);
             $result = $relationMap->mapMany($result, $items, 'discoveries', $options['limitSelects']);
         } else {
             $items = API::DiscoveryRule()->get(array('hostids' => $hostids, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
             $items = zbx_toHash($items, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['discoveries'] = isset($items[$hostid]) ? $items[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding triggers
     if ($options['selectTriggers'] !== null) {
         if ($options['selectTriggers'] != API_OUTPUT_COUNT) {
             // discovered items
             $res = DBselect('SELECT i.hostid,f.triggerid' . ' FROM items i,functions f' . ' WHERE ' . dbConditionInt('i.hostid', $hostids) . ' AND i.itemid=f.itemid');
             $relationMap = new CRelationMap();
             while ($relation = DBfetch($res)) {
                 $relationMap->addRelation($relation['hostid'], $relation['triggerid']);
             }
             $triggers = API::Trigger()->get(array('output' => $options['selectTriggers'], 'triggerids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
             if (!is_null($options['limitSelects'])) {
                 order_result($triggers, 'description');
             }
             $result = $relationMap->mapMany($result, $triggers, 'triggers', $options['limitSelects']);
         } else {
             $triggers = API::Trigger()->get(array('hostids' => $hostids, 'countOutput' => true, 'groupCount' => true));
             $triggers = zbx_toHash($triggers, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['triggers'] = isset($triggers[$hostid]) ? $triggers[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding graphs
     if ($options['selectGraphs'] !== null) {
         if ($options['selectGraphs'] != API_OUTPUT_COUNT) {
             // discovered items
             $res = DBselect('SELECT i.hostid,gi.graphid' . ' FROM items i,graphs_items gi' . ' WHERE ' . dbConditionInt('i.hostid', $hostids) . ' AND i.itemid=gi.itemid');
             $relationMap = new CRelationMap();
             while ($relation = DBfetch($res)) {
                 $relationMap->addRelation($relation['hostid'], $relation['graphid']);
             }
             $graphs = API::Graph()->get(array('output' => $options['selectGraphs'], 'graphids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
             if (!is_null($options['limitSelects'])) {
                 order_result($graphs, 'name');
             }
             $result = $relationMap->mapMany($result, $graphs, 'graphs', $options['limitSelects']);
         } else {
             $graphs = API::Graph()->get(array('hostids' => $hostids, 'countOutput' => true, 'groupCount' => true));
             $graphs = zbx_toHash($graphs, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['graphs'] = isset($graphs[$hostid]) ? $graphs[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding http tests
     if ($options['selectHttpTests'] !== null) {
         if ($options['selectHttpTests'] != API_OUTPUT_COUNT) {
             $httpTests = API::HttpTest()->get(array('output' => $this->outputExtend($options['selectHttpTests'], array('hostid', 'httptestid')), 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true));
             if (!is_null($options['limitSelects'])) {
                 order_result($httpTests, 'name');
             }
             $relationMap = $this->createRelationMap($httpTests, 'hostid', 'httptestid');
             $httpTests = $this->unsetExtraFields($httpTests, array('hostid', 'httptestid'), $options['selectHttpTests']);
             $result = $relationMap->mapMany($result, $httpTests, 'httpTests', $options['limitSelects']);
         } else {
             $httpTests = API::HttpTest()->get(array('hostids' => $hostids, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
             $httpTests = zbx_toHash($httpTests, 'hostid');
             foreach ($result as $hostId => $host) {
                 $result[$hostId]['httpTests'] = isset($httpTests[$hostId]) ? $httpTests[$hostId]['rowscount'] : 0;
             }
         }
     }
     // adding applications
     if ($options['selectApplications'] !== null) {
         if ($options['selectApplications'] != API_OUTPUT_COUNT) {
             $applications = API::Application()->get(array('output' => $this->outputExtend($options['selectApplications'], array('hostid', 'applicationid')), 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true));
             if (!is_null($options['limitSelects'])) {
                 order_result($applications, 'name');
             }
             $relationMap = $this->createRelationMap($applications, 'hostid', 'applicationid');
             $applications = $this->unsetExtraFields($applications, array('hostid', 'applicationid'), $options['selectApplications']);
             $result = $relationMap->mapMany($result, $applications, 'applications', $options['limitSelects']);
         } else {
             $applications = API::Application()->get(array('output' => $options['selectApplications'], 'hostids' => $hostids, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
             $applications = zbx_toHash($applications, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['applications'] = isset($applications[$hostid]) ? $applications[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding macros
     if ($options['selectMacros'] !== null && $options['selectMacros'] != API_OUTPUT_COUNT) {
         $macros = API::UserMacro()->get(array('output' => $this->outputExtend($options['selectMacros'], array('hostid', 'hostmacroid')), 'hostids' => $hostids, 'preservekeys' => true));
         $relationMap = $this->createRelationMap($macros, 'hostid', 'hostmacroid');
         $macros = $this->unsetExtraFields($macros, array('hostid', 'hostmacroid'), $options['selectMacros']);
         $result = $relationMap->mapMany($result, $macros, 'macros', $options['limitSelects']);
     }
     return $result;
 }
Esempio n. 2
0
 protected function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $hostPrototypeIds = array_keys($result);
     // adding discovery rule
     if ($options['selectDiscoveryRule'] !== null && $options['selectDiscoveryRule'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'hostid', 'parent_itemid', 'host_discovery');
         $discoveryRules = API::DiscoveryRule()->get(['output' => $options['selectDiscoveryRule'], 'itemids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true]);
         $result = $relationMap->mapOne($result, $discoveryRules, 'discoveryRule');
     }
     // adding group links
     if ($options['selectGroupLinks'] !== null && $options['selectGroupLinks'] != API_OUTPUT_COUNT) {
         $groupPrototypes = DBFetchArray(DBselect('SELECT hg.group_prototypeid,hg.hostid' . ' FROM group_prototype hg' . ' WHERE ' . dbConditionInt('hg.hostid', $hostPrototypeIds) . ' AND hg.groupid IS NOT NULL'));
         $relationMap = $this->createRelationMap($groupPrototypes, 'hostid', 'group_prototypeid');
         $groupPrototypes = API::getApiService()->select('group_prototype', ['output' => $options['selectGroupLinks'], 'group_prototypeids' => $relationMap->getRelatedIds(), 'preservekeys' => true]);
         foreach ($groupPrototypes as &$groupPrototype) {
             unset($groupPrototype['name']);
         }
         unset($groupPrototype);
         $result = $relationMap->mapMany($result, $groupPrototypes, 'groupLinks');
     }
     // adding group prototypes
     if ($options['selectGroupPrototypes'] !== null && $options['selectGroupPrototypes'] != API_OUTPUT_COUNT) {
         $groupPrototypes = DBFetchArray(DBselect('SELECT hg.group_prototypeid,hg.hostid' . ' FROM group_prototype hg' . ' WHERE ' . dbConditionInt('hg.hostid', $hostPrototypeIds) . ' AND hg.groupid IS NULL'));
         $relationMap = $this->createRelationMap($groupPrototypes, 'hostid', 'group_prototypeid');
         $groupPrototypes = API::getApiService()->select('group_prototype', ['output' => $options['selectGroupPrototypes'], 'group_prototypeids' => $relationMap->getRelatedIds(), 'preservekeys' => true]);
         foreach ($groupPrototypes as &$groupPrototype) {
             unset($groupPrototype['groupid']);
         }
         unset($groupPrototype);
         $result = $relationMap->mapMany($result, $groupPrototypes, 'groupPrototypes');
     }
     // adding host
     if ($options['selectParentHost'] !== null && $options['selectParentHost'] != API_OUTPUT_COUNT) {
         $relationMap = new CRelationMap();
         $dbRules = DBselect('SELECT hd.hostid,i.hostid AS parent_hostid' . ' FROM host_discovery hd,items i' . ' WHERE ' . dbConditionInt('hd.hostid', $hostPrototypeIds) . ' AND hd.parent_itemid=i.itemid');
         while ($relation = DBfetch($dbRules)) {
             $relationMap->addRelation($relation['hostid'], $relation['parent_hostid']);
         }
         $hosts = API::Host()->get(['output' => $options['selectParentHost'], 'hostids' => $relationMap->getRelatedIds(), 'templated_hosts' => true, 'nopermissions' => true, 'preservekeys' => true]);
         $result = $relationMap->mapOne($result, $hosts, 'parentHost');
     }
     // adding templates
     if ($options['selectTemplates'] !== null) {
         if ($options['selectTemplates'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'hostid', 'templateid', 'hosts_templates');
             $templates = API::Template()->get(['output' => $options['selectTemplates'], 'templateids' => $relationMap->getRelatedIds(), 'preservekeys' => true]);
             $result = $relationMap->mapMany($result, $templates, 'templates');
         } else {
             $templates = API::Template()->get(['hostids' => $hostPrototypeIds, 'countOutput' => true, 'groupCount' => true]);
             $templates = zbx_toHash($templates, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['templates'] = isset($templates[$hostid]) ? $templates[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding inventory
     if ($options['selectInventory'] !== null) {
         $relationMap = $this->createRelationMap($result, 'hostid', 'hostid');
         // only allow to retrieve the hostid and inventory_mode fields
         $output = [];
         if ($this->outputIsRequested('hostid', $options['selectInventory'])) {
             $output[] = 'hostid';
         }
         if ($this->outputIsRequested('inventory_mode', $options['selectInventory'])) {
             $output[] = 'inventory_mode';
         }
         $inventory = API::getApiService()->select('host_inventory', ['output' => $output, 'filter' => ['hostid' => $hostPrototypeIds]]);
         $result = $relationMap->mapOne($result, zbx_toHash($inventory, 'hostid'), 'inventory');
     }
     return $result;
 }