/**
  * Process screen.
  *
  * @return CDiv (screen inside container)
  */
 public function get()
 {
     // fetch hosts
     $hosts = API::Host()->get(['output' => ['hostid', 'status'], 'selectGraphs' => $this->screenitem['style'] == STYLE_LEFT ? API_OUTPUT_COUNT : null, 'selectScreens' => $this->screenitem['style'] == STYLE_LEFT ? API_OUTPUT_COUNT : null, 'groupids' => $this->screenitem['resourceid'], 'preservekeys' => true]);
     $hostids = array_keys($hosts);
     $options = ['output' => ['triggerid', 'expression', 'description', 'url', 'value', 'priority', 'lastchange', 'flags'], 'selectHosts' => ['hostid', 'name', 'status'], 'selectItems' => ['itemid', 'hostid', 'name', 'key_', 'value_type'], 'hostids' => $hostids, 'monitored' => true, 'skipDependent' => true, 'sortfield' => 'description', 'preservekeys' => true];
     // application filter
     if ($this->screenitem['application'] !== '') {
         $applications = API::Application()->get(['output' => [], 'hostids' => $hostids, 'search' => ['name' => $this->screenitem['application']], 'preservekeys' => true]);
         $options['applicationids'] = array_keys($applications);
     }
     $triggers = API::Trigger()->get($options);
     $triggers = CMacrosResolverHelper::resolveTriggerUrls($triggers);
     /*
      * Each screen cell with "Triggers overview" depends on one specific group which in this case is 'resourceid'.
      * Pass it as 'groupid' to menu pop-up "Events" link.
      */
     foreach ($triggers as &$trigger) {
         $trigger['groupid'] = $this->screenitem['resourceid'];
     }
     unset($trigger);
     $groups = API::HostGroup()->get(['output' => ['name'], 'groupids' => [$this->screenitem['resourceid']]]);
     $header = (new CDiv([new CTag('h4', true, _('Triggers overview')), (new CList())->addItem([_('Group'), ':', SPACE, $groups[0]['name']])]))->addClass(ZBX_STYLE_DASHBRD_WIDGET_HEAD);
     $table = getTriggersOverview($hosts, $triggers, $this->pageFile, $this->screenitem['style'], $this->screenid);
     $footer = (new CList())->addItem(_s('Updated: %s', zbx_date2str(TIME_FORMAT_SECONDS)))->addClass(ZBX_STYLE_DASHBRD_WIDGET_FOOT);
     return $this->getOutput(new CUiWidget(uniqid(), [$header, $table, $footer]));
 }
 /**
  * Process screen.
  *
  * @return CDiv (screen inside container)
  */
 public function get()
 {
     // fetch hosts
     $hosts = API::Host()->get(array('output' => array('hostid', 'status'), 'selectGraphs' => $this->screenitem['style'] == STYLE_LEFT ? API_OUTPUT_COUNT : null, 'selectScreens' => $this->screenitem['style'] == STYLE_LEFT ? API_OUTPUT_COUNT : null, 'groupids' => $this->screenitem['resourceid'], 'preservekeys' => true));
     $hostIds = array_keys($hosts);
     $options = array('output' => array('description', 'expression', 'priority', 'url', 'value', 'triggerid', 'lastchange', 'flags'), 'selectHosts' => array('hostid', 'name', 'status'), 'hostids' => $hostIds, 'monitored' => true, 'skipDependent' => true, 'sortfield' => 'description');
     // application filter
     if ($this->screenitem['application'] !== '') {
         $applications = API::Application()->get(array('output' => array('applicationid'), 'hostids' => $hostIds, 'search' => array('name' => $this->screenitem['application'])));
         $options['applicationids'] = zbx_objectValues($applications, 'applicationid');
     }
     $triggers = API::Trigger()->get($options);
     return $this->getOutput(getTriggersOverview($hosts, $triggers, $this->pageFile, $this->screenitem['style'], $this->screenid));
 }
 /**
  * Process screen.
  *
  * @return CDiv (screen inside container)
  */
 public function get()
 {
     $hostids = array();
     $dbHostGroups = DBselect('SELECT DISTINCT hg.hostid FROM hosts_groups hg WHERE hg.groupid=' . zbx_dbstr($this->screenitem['resourceid']));
     while ($dbHostGroup = DBfetch($dbHostGroups)) {
         $hostids[$dbHostGroup['hostid']] = $dbHostGroup['hostid'];
     }
     // application filter
     $applicationIds = null;
     if ($this->screenitem['application'] !== '') {
         $applications = API::Application()->get(array('output' => array('applicationid'), 'hostids' => $hostids, 'search' => array('name' => $this->screenitem['application'])));
         $applicationIds = zbx_objectValues($applications, 'applicationid');
     }
     return $this->getOutput(getItemsDataOverview($hostids, $applicationIds, $this->screenitem['style']));
 }
 /**
  * Process screen.
  *
  * @return CDiv (screen inside container)
  */
 public function get()
 {
     $hostids = [];
     $dbHostGroups = DBselect('SELECT DISTINCT hg.hostid FROM hosts_groups hg WHERE hg.groupid=' . zbx_dbstr($this->screenitem['resourceid']));
     while ($dbHostGroup = DBfetch($dbHostGroups)) {
         $hostids[$dbHostGroup['hostid']] = $dbHostGroup['hostid'];
     }
     // application filter
     $applicationIds = null;
     if ($this->screenitem['application'] !== '') {
         $applications = API::Application()->get(['output' => ['applicationid'], 'hostids' => $hostids, 'search' => ['name' => $this->screenitem['application']]]);
         $applicationIds = zbx_objectValues($applications, 'applicationid');
     }
     $groups = API::HostGroup()->get(['output' => ['name'], 'groupids' => [$this->screenitem['resourceid']]]);
     $header = (new CDiv([new CTag('h4', true, _('Data overview')), (new CList())->addItem([_('Group'), ':', SPACE, $groups[0]['name']])]))->addClass(ZBX_STYLE_DASHBRD_WIDGET_HEAD);
     $table = getItemsDataOverview($hostids, $applicationIds, $this->screenitem['style']);
     $footer = (new CList())->addItem(_s('Updated: %s', zbx_date2str(TIME_FORMAT_SECONDS)))->addClass(ZBX_STYLE_DASHBRD_WIDGET_FOOT);
     return $this->getOutput(new CUiWidget(uniqid(), [$header, $table, $footer]));
 }
 /**
  * Process screen.
  *
  * @return CDiv (screen inside container)
  */
 public function get()
 {
     // fetch hosts
     $hosts = API::Host()->get(array('output' => array('hostid', 'status'), 'selectGraphs' => $this->screenitem['style'] == STYLE_LEFT ? API_OUTPUT_COUNT : null, 'selectScreens' => $this->screenitem['style'] == STYLE_LEFT ? API_OUTPUT_COUNT : null, 'groupids' => $this->screenitem['resourceid'], 'preservekeys' => true));
     $hostIds = array_keys($hosts);
     $options = array('output' => array('description', 'expression', 'priority', 'url', 'value', 'triggerid', 'lastchange', 'flags'), 'selectHosts' => array('hostid', 'name', 'status'), 'selectItems' => array('itemid', 'hostid', 'name', 'key_', 'value_type'), 'hostids' => $hostIds, 'monitored' => true, 'skipDependent' => true, 'sortfield' => 'description');
     // application filter
     if ($this->screenitem['application'] !== '') {
         $applications = API::Application()->get(array('output' => array('applicationid'), 'hostids' => $hostIds, 'search' => array('name' => $this->screenitem['application'])));
         $options['applicationids'] = zbx_objectValues($applications, 'applicationid');
     }
     $triggers = API::Trigger()->get($options);
     /*
      * Each screen cell with "Triggers overview" depends on one specific group which in this case is 'resourceid'.
      * Pass it as 'groupid' to menu pop-up "Events" link.
      */
     foreach ($triggers as &$trigger) {
         $trigger['groupid'] = $this->screenitem['resourceid'];
     }
     unset($trigger);
     return $this->getOutput(getTriggersOverview($hosts, $triggers, $this->pageFile, $this->screenitem['style'], $this->screenid));
 }
Beispiel #6
0
 public function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $itemids = array_keys($result);
     // adding applications
     if ($options['selectApplications'] !== null && $options['selectApplications'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'itemid', 'applicationid', 'items_applications');
         $applications = API::Application()->get(array('output' => $options['selectApplications'], 'applicationids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
         $result = $relationMap->mapMany($result, $applications, 'applications');
     }
     // adding interfaces
     if ($options['selectInterfaces'] !== null && $options['selectInterfaces'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'itemid', 'interfaceid');
         $interfaces = API::HostInterface()->get(array('output' => $options['selectInterfaces'], 'interfaceids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true));
         $result = $relationMap->mapMany($result, $interfaces, 'interfaces');
     }
     // adding triggers
     if (!is_null($options['selectTriggers'])) {
         if ($options['selectTriggers'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'itemid', 'triggerid', 'functions');
             $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('countOutput' => true, 'groupCount' => true, 'itemids' => $itemids));
             $triggers = zbx_toHash($triggers, 'itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($triggers[$itemid])) {
                     $result[$itemid]['triggers'] = $triggers[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['triggers'] = 0;
                 }
             }
         }
     }
     // adding graphs
     if (!is_null($options['selectGraphs'])) {
         if ($options['selectGraphs'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'itemid', 'graphid', 'graphs_items');
             $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('countOutput' => true, 'groupCount' => true, 'itemids' => $itemids));
             $graphs = zbx_toHash($graphs, 'itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($graphs[$itemid])) {
                     $result[$itemid]['graphs'] = $graphs[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['graphs'] = 0;
                 }
             }
         }
     }
     // adding discoveryrule
     if ($options['selectDiscoveryRule'] !== null && $options['selectDiscoveryRule'] != API_OUTPUT_COUNT) {
         $relationMap = new CRelationMap();
         // discovered items
         $dbRules = DBselect('SELECT id1.itemid,id2.parent_itemid' . ' FROM item_discovery id1,item_discovery id2,items i' . ' WHERE ' . dbConditionInt('id1.itemid', $itemids) . ' AND id1.parent_itemid=id2.itemid' . ' AND i.itemid=id1.itemid' . ' AND i.flags=' . ZBX_FLAG_DISCOVERY_CREATED);
         while ($rule = DBfetch($dbRules)) {
             $relationMap->addRelation($rule['itemid'], $rule['parent_itemid']);
         }
         // item prototypes
         // TODO: this should not be in the item API
         $dbRules = DBselect('SELECT id.parent_itemid,id.itemid' . ' FROM item_discovery id,items i' . ' WHERE ' . dbConditionInt('id.itemid', $itemids) . ' AND i.itemid=id.itemid' . ' AND i.flags=' . ZBX_FLAG_DISCOVERY_PROTOTYPE);
         while ($rule = DBfetch($dbRules)) {
             $relationMap->addRelation($rule['itemid'], $rule['parent_itemid']);
         }
         $discoveryRules = API::DiscoveryRule()->get(array('output' => $options['selectDiscoveryRule'], 'itemids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true));
         $result = $relationMap->mapOne($result, $discoveryRules, 'discoveryRule');
     }
     // adding item discovery
     if ($options['selectItemDiscovery'] !== null) {
         $itemDiscoveries = API::getApiService()->select('item_discovery', array('output' => $this->outputExtend($options['selectItemDiscovery'], array('itemdiscoveryid', 'itemid')), 'filter' => array('itemid' => array_keys($result)), 'preservekeys' => true));
         $relationMap = $this->createRelationMap($itemDiscoveries, 'itemid', 'itemdiscoveryid');
         $itemDiscoveries = $this->unsetExtraFields($itemDiscoveries, array('itemid', 'itemdiscoveryid'), $options['selectItemDiscovery']);
         $result = $relationMap->mapOne($result, $itemDiscoveries, 'itemDiscovery');
     }
     // adding history data
     $requestedOutput = array();
     if ($this->outputIsRequested('lastclock', $options['output'])) {
         $requestedOutput['lastclock'] = true;
     }
     if ($this->outputIsRequested('lastns', $options['output'])) {
         $requestedOutput['lastns'] = true;
     }
     if ($this->outputIsRequested('lastvalue', $options['output'])) {
         $requestedOutput['lastvalue'] = true;
     }
     if ($this->outputIsRequested('prevvalue', $options['output'])) {
         $requestedOutput['prevvalue'] = true;
     }
     if ($requestedOutput) {
         $history = Manager::History()->getLast($result, 2, ZBX_HISTORY_PERIOD);
         foreach ($result as &$item) {
             $lastHistory = isset($history[$item['itemid']][0]) ? $history[$item['itemid']][0] : null;
             $prevHistory = isset($history[$item['itemid']][1]) ? $history[$item['itemid']][1] : null;
             if (isset($requestedOutput['lastclock'])) {
                 $item['lastclock'] = $lastHistory ? $lastHistory['clock'] : '0';
             }
             if (isset($requestedOutput['lastns'])) {
                 $item['lastns'] = $lastHistory ? $lastHistory['ns'] : '0';
             }
             if (isset($requestedOutput['lastvalue'])) {
                 $item['lastvalue'] = $lastHistory ? $lastHistory['value'] : '0';
             }
             if (isset($requestedOutput['prevvalue'])) {
                 $item['prevvalue'] = $prevHistory ? $prevHistory['value'] : '0';
             }
         }
         unset($item);
     }
     return $result;
 }
     $i = 1;
     foreach ($steps as $snum => $step) {
         $steps[$snum]['no'] = $i++;
         $stepid = isset($step['httpstepid']) ? $step['httpstepid'] : null;
         if (!is_null($stepid)) {
             $steps[$snum]['webstepid'] = $stepid;
             unset($steps[$snum]['httpstepid']);
         }
     }
 }
 $httpTest = array('hostid' => $_REQUEST['hostid'], 'name' => $_REQUEST['name'], 'authentication' => $_REQUEST['authentication'], 'delay' => $_REQUEST['delay'], 'status' => $_REQUEST['status'], 'agent' => $_REQUEST['agent'], 'macros' => $_REQUEST['macros'], 'steps' => $steps);
 $db_app_result = DBselect('SELECT a.applicationid' . ' FROM applications a' . ' WHERE a.name=' . zbx_dbstr($_REQUEST['application']) . ' AND a.hostid=' . $_REQUEST['hostid']);
 if ($applicationid = DBfetch($db_app_result)) {
     $httpTest['applicationid'] = $applicationid['applicationid'];
 } else {
     $result = API::Application()->create(array('name' => $_REQUEST['application'], 'hostid' => $_REQUEST['hostid']));
     if (!$result) {
         throw new Exception(_('Cannot add new application.') . ' [ ' . $application . ' ]');
     } else {
         $httpTest['applicationid'] = reset($result['applicationids']);
     }
 }
 if ($_REQUEST['authentication'] != HTTPTEST_AUTH_NONE) {
     $httpTest['http_user'] = $_REQUEST['http_user'];
     $httpTest['http_password'] = $_REQUEST['http_password'];
 } else {
     $httpTest['http_user'] = '';
     $httpTest['http_password'] = '';
 }
 if (isset($_REQUEST['httptestid'])) {
     $httpTest['httptestid'] = $httptestid = $_REQUEST['httptestid'];
 /**
  * Import applications.
  */
 protected function processApplications()
 {
     $allApplciations = $this->getFormattedApplications();
     if (empty($allApplciations)) {
         return;
     }
     $applicationsToCreate = array();
     foreach ($allApplciations as $host => $applications) {
         if (!$this->referencer->isProcessedHost($host)) {
             continue;
         }
         $hostid = $this->referencer->resolveHostOrTemplate($host);
         foreach ($applications as $application) {
             $application['hostid'] = $hostid;
             $appId = $this->referencer->resolveApplication($hostid, $application['name']);
             if (!$appId) {
                 $applicationsToCreate[] = $application;
             }
         }
     }
     // create the applications and create a hash hostid->name->applicationid
     if (!empty($applicationsToCreate)) {
         $newApplicationsIds = API::Application()->create($applicationsToCreate);
         foreach ($newApplicationsIds['applicationids'] as $anum => $applicationId) {
             $application = $applicationsToCreate[$anum];
             $this->referencer->addApplicationRef($application['hostid'], $application['name'], $applicationId);
         }
     }
     // refresh applications because templated ones can be inherited to host and used in items
     $this->referencer->refreshApplications();
 }
    $applicationView = new CView('configuration.application.edit', $data);
    $applicationView->render();
    $applicationView->show();
} else {
    $sortField = getRequest('sort', CProfile::get('web.' . $page['file'] . '.sort', 'name'));
    $sortOrder = getRequest('sortorder', CProfile::get('web.' . $page['file'] . '.sortorder', ZBX_SORT_UP));
    CProfile::update('web.' . $page['file'] . '.sort', $sortField, PROFILE_TYPE_STR);
    CProfile::update('web.' . $page['file'] . '.sortorder', $sortOrder, PROFILE_TYPE_STR);
    $pageFilter = new CPageFilter(array('groups' => array('editable' => true, 'with_hosts_and_templates' => true), 'hosts' => array('editable' => true, 'templated_hosts' => true), 'hostid' => getRequest('hostid'), 'groupid' => getRequest('groupid')));
    $data = array('pageFilter' => $pageFilter, 'sort' => $sortField, 'sortorder' => $sortOrder, 'hostid' => $pageFilter->hostid, 'groupid' => $pageFilter->groupid);
    if ($pageFilter->hostsSelected) {
        // get application ids
        $applications = API::Application()->get(array('hostids' => $pageFilter->hostid > 0 ? $pageFilter->hostid : null, 'groupids' => $pageFilter->groupid > 0 ? $pageFilter->groupid : null, 'output' => array('applicationid'), 'editable' => true, 'sortfield' => $sortField, 'limit' => $config['search_limit'] + 1));
        $applicationIds = zbx_objectValues($applications, 'applicationid');
        // get applications
        $data['applications'] = API::Application()->get(array('applicationids' => $applicationIds, 'output' => API_OUTPUT_EXTEND, 'selectItems' => array('itemid'), 'selectHost' => array('hostid', 'name')));
        order_result($data['applications'], $sortField, $sortOrder);
        // fetch template application source parents
        $applicationSourceParentIds = getApplicationSourceParentIds($applicationIds);
        $parentAppIds = array();
        foreach ($applicationSourceParentIds as $applicationParentIds) {
            foreach ($applicationParentIds as $parentId) {
                $parentAppIds[$parentId] = $parentId;
            }
        }
        if ($parentAppIds) {
            $parentTemplates = DBfetchArrayAssoc(DBselect('SELECT a.applicationid,h.hostid,h.name' . ' FROM applications a,hosts h' . ' WHERE a.hostid=h.hostid' . ' AND ' . dbConditionInt('a.applicationid', $parentAppIds)), 'applicationid');
            foreach ($data['applications'] as &$application) {
                if ($application['templateids'] && isset($applicationSourceParentIds[$application['applicationid']])) {
                    foreach ($applicationSourceParentIds[$application['applicationid']] as $parentAppId) {
                        $application['sourceTemplates'][] = $parentTemplates[$parentAppId];
} elseif (isset($_REQUEST['save']) && $_REQUEST['form_hostid'] > 0) {
    $delay_flex = get_request('delay_flex', array());
    $db_delay_flex = '';
    foreach ($delay_flex as $value) {
        $db_delay_flex .= $value['delay'] . '/' . $value['period'] . ';';
    }
    $db_delay_flex = trim($db_delay_flex, ';');
    $applications = get_request('applications', array());
    $application = reset($applications);
    if (empty($application)) {
        array_shift($applications);
    }
    DBstart();
    $result = true;
    if (!zbx_empty($_REQUEST['new_application'])) {
        $new_appid = API::Application()->create(array('name' => $_REQUEST['new_application'], 'hostid' => $_REQUEST['form_hostid']));
        if ($new_appid) {
            $new_appid = reset($new_appid['applicationids']);
            $applications[$new_appid] = $new_appid;
        } else {
            $result = false;
        }
    }
    if ($result) {
        $item = array('name' => get_request('name'), 'description' => get_request('description'), 'key_' => get_request('key'), 'hostid' => get_request('form_hostid'), 'interfaceid' => get_request('interfaceid', 0), 'delay' => get_request('delay'), 'history' => get_request('history'), 'status' => get_request('status'), 'type' => get_request('type'), 'snmp_community' => get_request('snmp_community'), 'snmp_oid' => get_request('snmp_oid'), 'value_type' => get_request('value_type'), 'trapper_hosts' => get_request('trapper_hosts'), 'port' => get_request('port'), 'units' => get_request('units'), 'multiplier' => get_request('multiplier', 0), 'delta' => get_request('delta'), 'snmpv3_securityname' => get_request('snmpv3_securityname'), 'snmpv3_securitylevel' => get_request('snmpv3_securitylevel'), 'snmpv3_authpassphrase' => get_request('snmpv3_authpassphrase'), 'snmpv3_privpassphrase' => get_request('snmpv3_privpassphrase'), 'formula' => get_request('formula'), 'trends' => get_request('trends'), 'logtimefmt' => get_request('logtimefmt'), 'valuemapid' => get_request('valuemapid'), 'delay_flex' => $db_delay_flex, 'authtype' => get_request('authtype'), 'username' => get_request('username'), 'password' => get_request('password'), 'publickey' => get_request('publickey'), 'privatekey' => get_request('privatekey'), 'params' => get_request('params'), 'ipmi_sensor' => get_request('ipmi_sensor'), 'data_type' => get_request('data_type'), 'applications' => $applications, 'inventory_link' => get_request('inventory_link'));
        if (isset($_REQUEST['itemid'])) {
            $db_item = get_item_by_itemid_limited($_REQUEST['itemid']);
            $db_item['applications'] = get_applications_by_itemid($_REQUEST['itemid']);
            foreach ($item as $field => $value) {
                if ($item[$field] == $db_item[$field]) {
                    unset($item[$field]);
Beispiel #11
0
 public static function parseMain($rules)
 {
     $triggerExpressionConverter = new C24TriggerConverter(new CFunctionMacroParser(), new CMacroParser('#'));
     $triggersForDependencies = array();
     if ($rules['hosts']['updateExisting'] || $rules['hosts']['createMissing'] || $rules['templates']['createMissing'] || $rules['templates']['updateExisting']) {
         $xpath = new DOMXPath(self::$xml);
         $hosts = $xpath->query('hosts/host');
         // stores parsed host and template IDs
         $processedHostIds = array();
         // stores converted trigger expressions for each host
         $triggerExpressions = array();
         // stores converted item keys for each host
         $itemKeys = array();
         // process hosts
         foreach ($hosts as $host) {
             $host_db = self::mapXML2arr($host, XML_TAG_HOST);
             if (!isset($host_db['status'])) {
                 $host_db['status'] = HOST_STATUS_TEMPLATE;
             }
             if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                 $current_host = API::Template()->get(array('output' => array('templateid'), 'filter' => array('host' => $host_db['host']), 'nopermissions' => true, 'limit' => 1));
             } else {
                 $current_host = API::Host()->get(array('output' => array('hostid'), 'filter' => array('host' => $host_db['host']), 'nopermissions' => true, 'limit' => 1));
             }
             if (!$current_host && ($host_db['status'] == HOST_STATUS_TEMPLATE && !$rules['templates']['createMissing'] || $host_db['status'] != HOST_STATUS_TEMPLATE && !$rules['hosts']['createMissing'])) {
                 continue;
             }
             if ($current_host && ($host_db['status'] == HOST_STATUS_TEMPLATE && !$rules['templates']['updateExisting'] || $host_db['status'] != HOST_STATUS_TEMPLATE && !$rules['hosts']['updateExisting'])) {
                 continue;
             }
             // there were no host visible names in 1.8
             if (!isset($host_db['name'])) {
                 $host_db['name'] = $host_db['host'];
             }
             // host will have no interfaces - we will be creating them separately
             $host_db['interfaces'] = null;
             // it is possible, that data is imported from 1.8, where there was only one network interface per host
             /**
              * @todo when new XML format will be introduced, this check should be changed to XML version check
              */
             $oldVersionInput = $host_db['status'] != HOST_STATUS_TEMPLATE;
             $interfaces = array();
             // rearranging host structure, so it would look more like 2.0 host
             if ($oldVersionInput) {
                 // the main interface is always "agent" type
                 if (!is_null($host_db['ip'])) {
                     $interfaces[] = array('main' => INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_AGENT, 'useip' => $host_db['useip'], 'ip' => $host_db['ip'], 'dns' => $host_db['dns'], 'port' => $host_db['port']);
                 }
                 // now we need to check if host had SNMP items. If it had, we need an SNMP interface for every different port.
                 $items = $xpath->query('items/item', $host);
                 $snmp_interface_ports_created = array();
                 foreach ($items as $item) {
                     $item_db = self::mapXML2arr($item, XML_TAG_ITEM);
                     if (($item_db['type'] == ITEM_TYPE_SNMPV1 || $item_db['type'] == ITEM_TYPE_SNMPV2C || $item_db['type'] == ITEM_TYPE_SNMPV3) && !isset($snmp_interface_ports_created[$item_db['snmp_port']])) {
                         $interfaces[] = array('main' => $snmp_interface_ports_created ? INTERFACE_SECONDARY : INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_SNMP, 'useip' => $host_db['useip'], 'ip' => $host_db['ip'], 'dns' => $host_db['dns'], 'port' => $item_db['snmp_port']);
                         $snmp_interface_ports_created[$item_db['snmp_port']] = 1;
                     }
                 }
                 unset($snmp_interface_ports_created);
                 // it was a temporary variable
                 // we need to add ipmi interface if at least one ipmi item exists
                 foreach ($items as $item) {
                     $item_db = self::mapXML2arr($item, XML_TAG_ITEM);
                     if ($item_db['type'] == ITEM_TYPE_IPMI) {
                         // when saving a host in 1.8, it's possible to set useipmi=1 and not to fill an IP address
                         // we were not really sure what to do with this host,
                         // and decided to take host IP address instead and show info message about this
                         if ($host_db['ipmi_ip'] === '') {
                             $ipmi_ip = $host_db['ip'];
                             info(_s('Host "%s" has "useipmi" parameter checked, but has no "ipmi_ip" parameter! Using host IP address as an address for IPMI interface.', $host_db['host']));
                         } else {
                             $ipmi_ip = $host_db['ipmi_ip'];
                         }
                         $interfaces[] = array('main' => INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_IPMI, 'useip' => INTERFACE_USE_IP, 'ip' => $ipmi_ip, 'dns' => '', 'port' => $host_db['ipmi_port']);
                         // we need only one ipmi interface
                         break;
                     }
                 }
             }
             if ($current_host) {
                 $options = array('filter' => array('host' => $host_db['host']), 'output' => API_OUTPUT_EXTEND, 'editable' => true, 'selectInterfaces' => API_OUTPUT_EXTEND);
                 if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                     $current_host = API::Template()->get($options);
                 } else {
                     $current_host = API::Host()->get($options);
                 }
                 if (empty($current_host)) {
                     throw new Exception(_s('No permission for host "%1$s".', $host_db['host']));
                 } else {
                     $current_host = reset($current_host);
                 }
                 // checking if host already exists - then some of the interfaces may not need to be created
                 if ($host_db['status'] != HOST_STATUS_TEMPLATE) {
                     $currentMainInterfaces = array();
                     $currentInterfacesByType = array();
                     // group existing main interfaces by interface type into $currentMainInterfaces
                     // and group interfaces by type into $currentInterfacesByType
                     foreach ($current_host['interfaces'] as $currentInterface) {
                         if ($currentInterface['main'] == INTERFACE_PRIMARY) {
                             $currentMainInterfaces[$currentInterface['type']] = $currentInterface;
                         }
                         $currentInterfacesByType[$currentInterface['type']][] = $currentInterface;
                     }
                     // loop through all interfaces we got from XML
                     foreach ($interfaces as &$interfaceXml) {
                         $interfaceXmlType = $interfaceXml['type'];
                         // if this is the primary interface of some type and we have default interface of same type
                         // in current (target) host, re-use "interfaceid" of the matching default interface
                         // in current host
                         if ($interfaceXml['main'] == INTERFACE_PRIMARY && isset($currentMainInterfaces[$interfaceXmlType])) {
                             $interfaceXml['interfaceid'] = $currentMainInterfaces[$interfaceXmlType]['interfaceid'];
                         } else {
                             // otherwise, loop through all current (target) host interfaces with type of current
                             // imported interface and re-use "interfaceid" in case if all interface parameters match
                             if (isset($currentInterfacesByType[$interfaceXmlType])) {
                                 foreach ($currentInterfacesByType[$interfaceXmlType] as $currentInterface) {
                                     if ($currentInterface['ip'] == $interfaceXml['ip'] && $currentInterface['dns'] == $interfaceXml['dns'] && $currentInterface['port'] == $interfaceXml['port'] && $currentInterface['useip'] == $interfaceXml['useip']) {
                                         $interfaceXml['interfaceid'] = $currentInterface['interfaceid'];
                                         break;
                                     }
                                 }
                             }
                         }
                     }
                     unset($interfaceXml);
                     $host_db['interfaces'] = $interfaces;
                 }
             } elseif ($host_db['status'] != HOST_STATUS_TEMPLATE) {
                 $host_db['interfaces'] = $interfaces;
             }
             // HOST GROUPS {{{
             $groups = $xpath->query('groups/group', $host);
             $host_db['groups'] = array();
             $groups_to_parse = array();
             foreach ($groups as $group) {
                 $groups_to_parse[] = array('name' => $group->nodeValue);
             }
             if (empty($groups_to_parse)) {
                 $groups_to_parse[] = array('name' => ZBX_DEFAULT_IMPORT_HOST_GROUP);
             }
             foreach ($groups_to_parse as $group) {
                 $hostGroup = API::HostGroup()->get(array('output' => API_OUTPUT_EXTEND, 'filter' => $group, 'editable' => true, 'limit' => 1));
                 if ($hostGroup) {
                     $host_db['groups'][] = reset($hostGroup);
                 } else {
                     if ($rules['groups']['createMissing']) {
                         $result = API::HostGroup()->create($group);
                         if ($result) {
                             $newHostGroup = API::HostGroup()->get(array('output' => API_OUTPUT_EXTEND, 'groupids' => $result['groupids'], 'limit' => 1));
                             $host_db['groups'][] = reset($newHostGroup);
                         }
                     } else {
                         throw new Exception(_s('No permissions for host group "%1$s".', $group['name']));
                     }
                 }
             }
             // }}} HOST GROUPS
             // MACROS
             $macros = $xpath->query('macros/macro', $host);
             if ($macros->length > 0) {
                 $host_db['macros'] = array();
                 foreach ($macros as $macro) {
                     $host_db['macros'][] = self::mapXML2arr($macro, XML_TAG_MACRO);
                 }
             }
             // }}} MACROS
             // host inventory
             if ($oldVersionInput) {
                 if (!isset($host_db['inventory'])) {
                     $host_db['inventory'] = array();
                 }
                 $inventoryNode = $xpath->query('host_profile/*', $host);
                 if ($inventoryNode->length > 0) {
                     foreach ($inventoryNode as $field) {
                         $newInventoryName = self::mapInventoryName($field->nodeName);
                         $host_db['inventory'][$newInventoryName] = $field->nodeValue;
                     }
                 }
                 $inventoryNodeExt = $xpath->query('host_profiles_ext/*', $host);
                 if ($inventoryNodeExt->length > 0) {
                     foreach ($inventoryNodeExt as $field) {
                         $newInventoryName = self::mapInventoryName($field->nodeName);
                         if (isset($host_db['inventory'][$newInventoryName]) && $field->nodeValue !== '') {
                             $host_db['inventory'][$newInventoryName] .= "\r\n\r\n";
                             $host_db['inventory'][$newInventoryName] .= $field->nodeValue;
                         } else {
                             $host_db['inventory'][$newInventoryName] = $field->nodeValue;
                         }
                     }
                 }
                 $host_db['inventory_mode'] = isset($host_db['inventory']) ? HOST_INVENTORY_MANUAL : HOST_INVENTORY_DISABLED;
             }
             if (isset($host_db['proxy_hostid'])) {
                 $proxy_exists = API::Proxy()->get(array('output' => array('proxyid'), 'proxyids' => $host_db['proxy_hostid']));
                 if (empty($proxy_exists)) {
                     $host_db['proxy_hostid'] = 0;
                 }
             }
             if ($current_host && ($rules['hosts']['updateExisting'] || $rules['templates']['updateExisting'])) {
                 if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                     $host_db['templateid'] = $current_host['templateid'];
                     $result = API::Template()->update($host_db);
                     $current_hostid = $current_host['templateid'];
                 } else {
                     $host_db['hostid'] = $current_host['hostid'];
                     $result = API::Host()->update($host_db);
                     $current_hostid = $current_host['hostid'];
                 }
             }
             if (!$current_host && ($rules['hosts']['createMissing'] || $rules['templates']['createMissing'])) {
                 if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                     $result = API::Template()->create($host_db);
                     $current_hostid = reset($result['templateids']);
                 } else {
                     $result = API::Host()->create($host_db);
                     $current_hostid = reset($result['hostids']);
                 }
             }
             // store parsed host IDs
             $processedHostIds[$host_db['host']] = $current_hostid;
         }
         // gather triggers and convert old expressions
         $triggersXML = array();
         // cycle each host and gather trigger descriptions and expressions
         foreach ($hosts as $host) {
             $host_db = self::mapXML2arr($host, XML_TAG_HOST);
             $current_hostid = isset($processedHostIds[$host_db['host']]) ? $processedHostIds[$host_db['host']] : false;
             if ($current_hostid) {
                 $triggersXML[$current_hostid] = array();
                 $triggerExpressions[$host_db['host']] = array();
                 $oldVersionInput = $host_db['status'] != HOST_STATUS_TEMPLATE;
                 $triggers = $xpath->query('triggers/trigger', $host);
                 foreach ($triggers as $trigger) {
                     $trigger_db = self::mapXML2arr($trigger, XML_TAG_TRIGGER);
                     $oldExpression = $trigger_db['expression'];
                     if (!isset($triggerExpressions[$host_db['host']][$trigger_db['description']])) {
                         $triggerExpressions[$host_db['host']][$trigger_db['description']] = array();
                     }
                     if ($oldVersionInput) {
                         $expressionPart = explode(':', $trigger_db['expression']);
                         $keyName = explode(',', $expressionPart[1], 2);
                         if (count($keyName) == 2) {
                             $keyValue = explode('.', $keyName[1], 2);
                             $key = $keyName[0] . "," . $keyValue[0];
                             if (in_array($keyName[0], self::$oldKeys) || in_array($keyName[0], self::$oldKeysPref)) {
                                 $trigger_db['expression'] = str_replace($key, self::convertOldSimpleKey($key), $trigger_db['expression']);
                             }
                         }
                     }
                     // {HOSTNAME} is here for backward compatibility
                     $trigger_db['expression'] = str_replace('{{HOSTNAME}:', '{' . $host_db['host'] . ':', $trigger_db['expression']);
                     $trigger_db['expression'] = str_replace('{{HOST.HOST}:', '{' . $host_db['host'] . ':', $trigger_db['expression']);
                     $trigger_db['expression'] = $triggerExpressionConverter->convert($trigger_db['expression']);
                     $triggersXML[$current_hostid][$trigger_db['description']][$trigger_db['expression']] = $trigger_db['expression'];
                     $triggerExpressions[$host_db['host']][$trigger_db['description']][$oldExpression] = $trigger_db['expression'];
                 }
             }
         }
         // delete missing triggers
         if ($rules['triggers']['deleteMissing']) {
             // select triggers from parsed hosts
             $dbTriggers = API::Trigger()->get(array('output' => array('triggerid', 'description', 'expression'), 'expandExpression' => true, 'hostids' => $processedHostIds, 'selectHosts' => array('hostid'), 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL)));
             // find corresponding trigger ID by description and expression
             $triggerIdsXML = array();
             foreach ($dbTriggers as $dbTrigger) {
                 $hostId = reset($dbTrigger['hosts']);
                 if (isset($triggersXML[$hostId['hostid']][$dbTrigger['description']][$dbTrigger['expression']])) {
                     $triggerIdsXML[$dbTrigger['triggerid']] = $dbTrigger['triggerid'];
                 }
             }
             $triggersToDelete = array_diff_key($dbTriggers, $triggerIdsXML);
             $triggerIdsToDelete = array();
             // check that potentially deletable trigger belongs to same hosts that are in XML
             // if some triggers belong to more hosts than current XML contains, don't delete them
             foreach ($triggersToDelete as $triggerId => $trigger) {
                 $triggerHostIds = array_flip(zbx_objectValues($trigger['hosts'], 'hostid'));
                 if (!array_diff_key($triggerHostIds, array_flip($processedHostIds))) {
                     $triggerIdsToDelete[] = $triggerId;
                 }
             }
             if ($triggerIdsToDelete) {
                 API::Trigger()->delete($triggerIdsToDelete);
             }
         }
         // delete missing graphs
         if ($rules['graphs']['deleteMissing']) {
             $graphsXML = array();
             // cycle each host and gather all graph names
             foreach ($hosts as $host) {
                 $host_db = self::mapXML2arr($host, XML_TAG_HOST);
                 $current_hostid = isset($processedHostIds[$host_db['host']]) ? $processedHostIds[$host_db['host']] : false;
                 if ($current_hostid) {
                     $graphsXML[$current_hostid] = array();
                     $graphs = $xpath->query('graphs/graph', $host);
                     foreach ($graphs as $graph) {
                         $graph_db = self::mapXML2arr($graph, XML_TAG_GRAPH);
                         $graphsXML[$current_hostid][$graph_db['name']] = $graph_db['name'];
                     }
                 }
             }
             // select graphs from already parsed hosts
             $dbGraphs = API::Graph()->get(array('output' => array('graphid', 'name'), 'hostids' => $processedHostIds, 'selectHosts' => array('hostid'), 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL)));
             $graphIdsXML = array();
             foreach ($dbGraphs as $dbGraph) {
                 $hostId = reset($dbGraph['hosts']);
                 if (isset($graphsXML[$hostId['hostid']][$dbGraph['name']])) {
                     $graphIdsXML[$dbGraph['graphid']] = $dbGraph['graphid'];
                 }
             }
             $graphsToDelete = array_diff_key($dbGraphs, $graphIdsXML);
             $graphsIdsToDelete = array();
             // check that potentially deletable graph belongs to same hosts that are in XML
             // if some graphs belong to more hosts than current XML contains, don't delete them
             foreach ($graphsToDelete as $graphId => $graph) {
                 $graphHostIds = array_flip(zbx_objectValues($graph['hosts'], 'hostid'));
                 if (!array_diff_key($graphHostIds, array_flip($processedHostIds))) {
                     $graphsIdsToDelete[] = $graphId;
                 }
             }
             if ($graphsIdsToDelete) {
                 API::Graph()->delete($graphsIdsToDelete);
             }
         }
         // gather items and convert old keys
         $itemsXML = array();
         foreach ($hosts as $host) {
             $host_db = self::mapXML2arr($host, XML_TAG_HOST);
             $current_hostid = isset($processedHostIds[$host_db['host']]) ? $processedHostIds[$host_db['host']] : false;
             if ($current_hostid) {
                 $itemsXML[$current_hostid] = array();
                 $itemKeys[$host_db['host']] = array();
                 $oldVersionInput = $host_db['status'] != HOST_STATUS_TEMPLATE;
                 $items = $xpath->query('items/item', $host);
                 foreach ($items as $item) {
                     $item_db = self::mapXML2arr($item, XML_TAG_ITEM);
                     if ($oldVersionInput) {
                         $oldKey = $item_db['key_'];
                         $item_db['key_'] = self::convertOldSimpleKey($item_db['key_']);
                         $itemKeys[$host_db['host']][$oldKey] = $item_db['key_'];
                     }
                     $itemsXML[$current_hostid][$item_db['key_']] = $item_db['key_'];
                 }
             }
         }
         // delete missing items
         if ($rules['items']['deleteMissing']) {
             $dbItems = API::Item()->get(array('output' => array('itemid', 'key_', 'hostid'), 'hostids' => $processedHostIds, 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL)));
             $itemIdsXML = array();
             foreach ($dbItems as $dbItem) {
                 if (isset($itemsXML[$dbItem['hostid']][$dbItem['key_']])) {
                     $itemIdsXML[$dbItem['itemid']] = $dbItem['itemid'];
                 }
             }
             $itemsToDelete = array_diff_key($dbItems, $itemIdsXML);
             if ($itemsToDelete) {
                 API::Item()->delete(array_keys($itemsToDelete));
             }
         }
         // delete missing applications
         if ($rules['applications']['deleteMissing']) {
             $applicationsXML = array();
             foreach ($hosts as $host) {
                 $host_db = self::mapXML2arr($host, XML_TAG_HOST);
                 $current_hostid = isset($processedHostIds[$host_db['host']]) ? $processedHostIds[$host_db['host']] : false;
                 if ($current_hostid) {
                     $items = $xpath->query('items/item', $host);
                     foreach ($items as $item) {
                         $applications = $xpath->query('applications/application', $item);
                         foreach ($applications as $application) {
                             $applicationsXML[$current_hostid][$application->nodeValue] = $application->nodeValue;
                         }
                     }
                 }
             }
             $dbApplications = API::Application()->get(array('output' => array('applicationid', 'hostid', 'name'), 'hostids' => $processedHostIds, 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false));
             $applicationsIdsXML = array();
             foreach ($dbApplications as $dbApplication) {
                 if (isset($applicationsXML[$dbApplication['hostid']][$dbApplication['name']])) {
                     $applicationsIdsXML[$dbApplication['applicationid']] = $dbApplication['applicationid'];
                 }
             }
             $applicationsToDelete = array_diff_key($dbApplications, $applicationsIdsXML);
             if ($applicationsToDelete) {
                 API::Application()->delete(array_keys($applicationsToDelete));
             }
         }
         // cycle each host again and create/update other objects
         foreach ($hosts as $host) {
             $host_db = self::mapXML2arr($host, XML_TAG_HOST);
             if (!isset($host_db['status'])) {
                 $host_db['status'] = HOST_STATUS_TEMPLATE;
             }
             $current_hostid = isset($processedHostIds[$host_db['host']]) ? $processedHostIds[$host_db['host']] : false;
             if (!$current_hostid) {
                 continue;
             }
             $oldVersionInput = $host_db['status'] != HOST_STATUS_TEMPLATE;
             // TEMPLATES {{{
             if (!empty($rules['templateLinkage']['createMissing'])) {
                 $templates = $xpath->query('templates/template', $host);
                 $templateLinkage = array();
                 foreach ($templates as $template) {
                     $options = array('filter' => array('host' => $template->nodeValue), 'output' => array('templateid'), 'editable' => true);
                     $current_template = API::Template()->get($options);
                     if (empty($current_template)) {
                         throw new Exception(_s('No permission for template "%1$s".', $template->nodeValue));
                     }
                     $current_template = reset($current_template);
                     $templateLinkage[] = $current_template;
                 }
                 if ($templateLinkage) {
                     $result = API::Template()->massAdd(array('hosts' => array('hostid' => $current_hostid), 'templates' => $templateLinkage));
                     if (!$result) {
                         throw new Exception();
                     }
                 }
             }
             // }}} TEMPLATES
             // ITEMS {{{
             if ($rules['items']['updateExisting'] || $rules['items']['createMissing'] || $rules['applications']['createMissing']) {
                 // applications are located under items in version 1.8,
                 // so we need to get item list in any of these cases
                 $items = $xpath->query('items/item', $host);
                 if ($oldVersionInput) {
                     $interfaces = API::HostInterface()->get(array('hostids' => $current_hostid, 'output' => API_OUTPUT_EXTEND));
                     // we must know interface ids to assign them to items
                     $agent_interface_id = null;
                     $ipmi_interface_id = null;
                     $snmp_interfaces = array();
                     // hash 'port' => 'interfaceid'
                     foreach ($interfaces as $interface) {
                         switch ($interface['type']) {
                             case INTERFACE_TYPE_AGENT:
                                 $agent_interface_id = $interface['interfaceid'];
                                 break;
                             case INTERFACE_TYPE_IPMI:
                                 $ipmi_interface_id = $interface['interfaceid'];
                                 break;
                             case INTERFACE_TYPE_SNMP:
                                 $snmp_interfaces[$interface['port']] = $interface['interfaceid'];
                                 break;
                         }
                     }
                 }
                 // if this is an export from 1.8, we need to make some adjustments to items
                 // cycle each XML item
                 foreach ($items as $item) {
                     if ($rules['items']['updateExisting'] || $rules['items']['createMissing']) {
                         $item_db = self::mapXML2arr($item, XML_TAG_ITEM);
                         $item_db['hostid'] = $current_hostid;
                         // item needs interfaces
                         if ($oldVersionInput) {
                             // 'snmp_port' column was renamed to 'port'
                             if ($item_db['snmp_port'] != 0) {
                                 // zabbix agent items have no ports
                                 $item_db['port'] = $item_db['snmp_port'];
                             }
                             unset($item_db['snmp_port']);
                             // assigning appropriate interface depending on item type
                             switch ($item_db['type']) {
                                 // zabbix agent interface
                                 case ITEM_TYPE_ZABBIX:
                                 case ITEM_TYPE_SIMPLE:
                                 case ITEM_TYPE_EXTERNAL:
                                 case ITEM_TYPE_SSH:
                                 case ITEM_TYPE_TELNET:
                                     $item_db['interfaceid'] = $agent_interface_id;
                                     break;
                                     // snmp interface
                                 // snmp interface
                                 case ITEM_TYPE_SNMPV1:
                                 case ITEM_TYPE_SNMPV2C:
                                 case ITEM_TYPE_SNMPV3:
                                     // for an item with different port - different interface
                                     $item_db['interfaceid'] = $snmp_interfaces[$item_db['port']];
                                     break;
                                 case ITEM_TYPE_IPMI:
                                     $item_db['interfaceid'] = $ipmi_interface_id;
                                     break;
                                     // no interfaces required for these item types
                                 // no interfaces required for these item types
                                 case ITEM_TYPE_HTTPTEST:
                                 case ITEM_TYPE_CALCULATED:
                                 case ITEM_TYPE_AGGREGATE:
                                 case ITEM_TYPE_INTERNAL:
                                 case ITEM_TYPE_ZABBIX_ACTIVE:
                                 case ITEM_TYPE_TRAPPER:
                                 case ITEM_TYPE_DB_MONITOR:
                                     $item_db['interfaceid'] = null;
                                     break;
                             }
                             $item_db['key_'] = $itemKeys[$host_db['host']][$item_db['key_']];
                         }
                         $current_item = API::Item()->get(array('filter' => array('hostid' => $item_db['hostid'], 'key_' => $item_db['key_']), 'webitems' => true, 'editable' => true, 'output' => array('itemid')));
                         $current_item = reset($current_item);
                     }
                     // create applications independently of create or update item options
                     // in case we update items, we need to assign items to applications,
                     // so we also gather application IDs independetly of selected application options
                     $applications = $xpath->query('applications/application', $item);
                     $itemApplications = array();
                     $applicationsToAdd = array();
                     $applicationsIds = array();
                     foreach ($applications as $application) {
                         $application_db = array('name' => $application->nodeValue, 'hostid' => $current_hostid);
                         $current_application = API::Application()->get(array('filter' => $application_db, 'output' => API_OUTPUT_EXTEND));
                         $applicationValue = reset($current_application);
                         if ($current_application) {
                             if (!$itemApplications) {
                                 $itemApplications = $current_application;
                             } elseif (!in_array($applicationValue['applicationid'], $applicationsIds)) {
                                 $itemApplications = array_merge($itemApplications, $current_application);
                             }
                             $applicationsIds[] = $applicationValue['applicationid'];
                         } else {
                             $applicationsToAdd[] = $application_db;
                         }
                     }
                     if ($applicationsToAdd && $rules['applications']['createMissing']) {
                         $result = API::Application()->create($applicationsToAdd);
                         $newApplications = API::Application()->get(array('applicationids' => $result['applicationids'], 'output' => API_OUTPUT_EXTEND));
                         $itemApplications = array_merge($itemApplications, $newApplications);
                     }
                     if ($rules['items']['updateExisting'] || $rules['items']['createMissing']) {
                         // if item does not exist and there is no need to create it, skip item creation
                         if (!$current_item && !$rules['items']['createMissing']) {
                             info(_s('Item "%1$s" skipped - user rule.', $item_db['key_']));
                             continue;
                         }
                         // if item exists, but there there is no need for update, skip item update
                         if ($current_item && !$rules['items']['updateExisting']) {
                             info(_s('Item "%1$s" skipped - user rule.', $item_db['key_']));
                             continue;
                         }
                         if ($current_item && $rules['items']['updateExisting']) {
                             $item_db['itemid'] = $current_item['itemid'];
                             $result = API::Item()->update($item_db);
                             $current_item = API::Item()->get(array('itemids' => $result['itemids'], 'webitems' => true, 'output' => array('itemid')));
                         }
                         if (!$current_item && $rules['items']['createMissing']) {
                             $result = API::Item()->create($item_db);
                             $current_item = API::Item()->get(array('itemids' => $result['itemids'], 'webitems' => true, 'output' => array('itemid')));
                         }
                         // after items are created or updated, see to if items need to assigned to applications
                         if (isset($itemApplications) && $itemApplications) {
                             API::Application()->massAdd(array('applications' => $itemApplications, 'items' => $current_item));
                         }
                     }
                 }
             }
             // }}} ITEMS
             // TRIGGERS {{{
             if ($rules['triggers']['updateExisting'] || $rules['triggers']['createMissing']) {
                 $triggers = $xpath->query('triggers/trigger', $host);
                 $triggersToCreate = array();
                 $triggersToUpdate = array();
                 foreach ($triggers as $trigger) {
                     $trigger_db = self::mapXML2arr($trigger, XML_TAG_TRIGGER);
                     $trigger_db['expression'] = $triggerExpressions[$host_db['host']][$trigger_db['description']][$trigger_db['expression']];
                     $trigger_db['hostid'] = $current_hostid;
                     $currentTrigger = API::Trigger()->get(array('output' => array('triggerid'), 'filter' => array('description' => $trigger_db['description']), 'hostids' => array($current_hostid), 'selectHosts' => array('hostid'), 'nopermissions' => true, 'limit' => 1));
                     $currentTrigger = reset($currentTrigger);
                     if ($currentTrigger) {
                         $dbTriggers = API::Trigger()->get(array('output' => API_OUTPUT_EXTEND, 'filter' => array('description' => $trigger_db['description']), 'hostids' => array($current_hostid), 'editable' => true));
                         foreach ($dbTriggers as $dbTrigger) {
                             $expression = explode_exp($dbTrigger['expression']);
                             if (strcmp($trigger_db['expression'], $expression) == 0) {
                                 $currentTrigger = $dbTrigger;
                                 break;
                             }
                             if (!$currentTrigger) {
                                 throw new Exception(_s('No permission for trigger "%1$s".', $trigger_db['description']));
                             }
                         }
                     }
                     unset($trigger_db['hostid']);
                     if (!$currentTrigger && !$rules['triggers']['createMissing']) {
                         info(_s('Trigger "%1$s" skipped - user rule.', $trigger_db['description']));
                         continue;
                     }
                     if ($currentTrigger && !$rules['triggers']['updateExisting']) {
                         info(_s('Trigger "%1$s" skipped - user rule.', $trigger_db['description']));
                         continue;
                     }
                     if (!$currentTrigger && $rules['triggers']['createMissing']) {
                         $triggersToCreate[] = $trigger_db;
                     }
                     if ($currentTrigger && $rules['triggers']['updateExisting']) {
                         $trigger_db['triggerid'] = $currentTrigger['triggerid'];
                         $triggersToUpdate[] = $trigger_db;
                     }
                 }
                 if ($triggersToUpdate) {
                     $result = API::Trigger()->update($triggersToUpdate);
                     $triggersUpdated = API::Trigger()->get(array('output' => API_OUTPUT_EXTEND, 'triggerids' => $result['triggerids']));
                     $triggersForDependencies = array_merge($triggersForDependencies, $triggersUpdated);
                 }
                 if ($triggersToCreate) {
                     $result = API::Trigger()->create($triggersToCreate);
                     $triggersCreated = API::Trigger()->get(array('output' => API_OUTPUT_EXTEND, 'triggerids' => $result['triggerids']));
                     $triggersForDependencies = array_merge($triggersForDependencies, $triggersCreated);
                 }
             }
             // }}} TRIGGERS
             // GRAPHS {{{
             if ($rules['graphs']['updateExisting'] || $rules['graphs']['createMissing']) {
                 $graphs = $xpath->query('graphs/graph', $host);
                 $graphs_to_add = array();
                 $graphs_to_upd = array();
                 foreach ($graphs as $graph) {
                     // GRAPH ITEMS {{{
                     $gitems = $xpath->query('graph_elements/graph_element', $graph);
                     $graph_hostids = array();
                     $graph_items = array();
                     foreach ($gitems as $gitem) {
                         $gitem_db = self::mapXML2arr($gitem, XML_TAG_GRAPH_ELEMENT);
                         $data = explode(':', $gitem_db['host_key_']);
                         $gitem_host = array_shift($data);
                         // {HOSTNAME} is here for backward compatibility
                         $gitem_db['host'] = $gitem_host == '{HOSTNAME}' ? $host_db['host'] : $gitem_host;
                         $gitem_db['host'] = $gitem_host == '{HOST.HOST}' ? $host_db['host'] : $gitem_host;
                         if ($oldVersionInput) {
                             $data[0] = self::convertOldSimpleKey($data[0]);
                         }
                         $gitem_db['key_'] = implode(':', $data);
                         $itemExists = API::Item()->get(array('output' => array('itemid'), 'filter' => array('key_' => $gitem_db['key_']), 'webitems' => true, 'nopermissions' => true, 'limit' => 1));
                         if ($itemExists) {
                             $current_item = API::Item()->get(array('filter' => array('key_' => $gitem_db['key_']), 'webitems' => true, 'editable' => true, 'host' => $gitem_db['host'], 'output' => array('itemid', 'hostid')));
                             if (empty($current_item)) {
                                 throw new Exception(_s('No permission for item "%1$s".', $gitem_db['key_']));
                             }
                             $current_item = reset($current_item);
                             $graph_hostids[] = $current_item['hostid'];
                             $gitem_db['itemid'] = $current_item['itemid'];
                             $graph_items[] = $gitem_db;
                         } else {
                             throw new Exception(_s('Item "%1$s" does not exist.', $gitem_db['host_key_']));
                         }
                     }
                     // }}} GRAPH ITEMS
                     $graph_db = self::mapXML2arr($graph, XML_TAG_GRAPH);
                     $graph_db['hostids'] = $graph_hostids;
                     // do we need to show the graph legend, after it is imported?
                     // in 1.8, this setting was present only for pie and exploded graphs
                     // for other graph types we are always showing the legend
                     if ($graph_db['graphtype'] != GRAPH_TYPE_PIE && $graph_db['graphtype'] != GRAPH_TYPE_EXPLODED) {
                         $graph_db['show_legend'] = 1;
                     }
                     $current_graph = API::Graph()->get(array('output' => array('graphid'), 'selectHosts' => array('hostid', 'host'), 'hostids' => $graph_db['hostids'], 'filter' => array('name' => $graph_db['name']), 'nopermissions' => true, 'limit' => 1));
                     if ($current_graph) {
                         $current_graph = API::Graph()->get(array('output' => API_OUTPUT_EXTEND, 'hostids' => $graph_db['hostids'], 'filter' => array('name' => $graph_db['name']), 'editable' => true));
                         if (empty($current_graph)) {
                             throw new Exception(_s('No permission for graph "%1$s".', $graph_db['name']));
                         }
                         $current_graph = reset($current_graph);
                     }
                     if (!$current_graph && empty($rules['graphs']['createMissing'])) {
                         info(_s('Graph "%1$s" skipped - user rule.', $graph_db['name']));
                         continue;
                         // break if not update updateExisting
                     }
                     if ($current_graph && empty($rules['graphs']['updateExisting'])) {
                         info(_s('Graph "%1$s" skipped - user rule.', $graph_db['name']));
                         continue;
                         // break if not update updateExisting
                     }
                     if (!isset($graph_db['ymin_type'])) {
                         throw new Exception(_s('No "ymin_type" field for graph "%s".', $graph_db['name']));
                     }
                     if (!isset($graph_db['ymax_type'])) {
                         throw new Exception(_s('No "ymax_type" field for graph "%s".', $graph_db['name']));
                     }
                     if ($graph_db['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
                         $item_data = explode(':', $graph_db['ymin_item_key'], 2);
                         if (count($item_data) < 2) {
                             throw new Exception(_s('Incorrect y min item for graph "%1$s".', $graph_db['name']));
                         }
                         if (!($item = get_item_by_key($item_data[1], $item_data[0]))) {
                             throw new Exception(_s('Missing item "%1$s" for host "%2$s".', $graph_db['ymin_item_key'], $host_db['host']));
                         }
                         $graph_db['ymin_itemid'] = $item['itemid'];
                     }
                     if ($graph_db['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
                         $item_data = explode(':', $graph_db['ymax_item_key'], 2);
                         if (count($item_data) < 2) {
                             throw new Exception(_s('Incorrect y max item for graph "%1$s".', $graph_db['name']));
                         }
                         if (!($item = get_item_by_key($item_data[1], $item_data[0]))) {
                             throw new Exception(_s('Missing item "%1$s" for host "%2$s".', $graph_db['ymax_item_key'], $host_db['host']));
                         }
                         $graph_db['ymax_itemid'] = $item['itemid'];
                     }
                     $graph_db['gitems'] = $graph_items;
                     if ($current_graph) {
                         $graph_db['graphid'] = $current_graph['graphid'];
                         $graphs_to_upd[] = $graph_db;
                     } else {
                         $graphs_to_add[] = $graph_db;
                     }
                 }
                 if (!empty($graphs_to_add)) {
                     API::Graph()->create($graphs_to_add);
                 }
                 if (!empty($graphs_to_upd)) {
                     API::Graph()->update($graphs_to_upd);
                 }
             }
         }
         // DEPENDENCIES
         $dependencies = $xpath->query('dependencies/dependency');
         if ($dependencies->length > 0) {
             $triggersForDependencies = zbx_objectValues($triggersForDependencies, 'triggerid');
             $triggersForDependencies = array_flip($triggersForDependencies);
             $triggerDependencies = array();
             foreach ($dependencies as $dependency) {
                 $triggerDescription = $dependency->getAttribute('description');
                 $currentTrigger = get_trigger_by_description($triggerDescription);
                 if ($currentTrigger && isset($triggersForDependencies[$currentTrigger['triggerid']])) {
                     $dependsOnList = $xpath->query('depends', $dependency);
                     foreach ($dependsOnList as $dependsOn) {
                         $depTrigger = get_trigger_by_description($dependsOn->nodeValue);
                         if ($depTrigger) {
                             if (!isset($triggerDependencies[$currentTrigger['triggerid']])) {
                                 $triggerDependencies[$currentTrigger['triggerid']] = array('triggerid' => $currentTrigger['triggerid'], 'dependencies' => array());
                             }
                             $triggerDependencies[$currentTrigger['triggerid']]['dependencies'][] = array('triggerid' => $depTrigger['triggerid']);
                         }
                     }
                 }
             }
             if ($triggerDependencies) {
                 API::Trigger()->update($triggerDependencies);
             }
         }
     }
 }
Beispiel #12
0
     }
     break;
 case 'templates':
     $templates = API::Template()->get(array('editable' => isset($data['editable']) ? $data['editable'] : null, 'output' => array('templateid', 'name'), 'search' => isset($data['search']) ? array('name' => $data['search']) : null, 'limit' => $config['search_limit']));
     if ($templates) {
         CArrayHelper::sort($templates, array(array('field' => 'name', 'order' => ZBX_SORT_UP)));
         if (isset($data['limit'])) {
             $templates = array_slice($templates, 0, $data['limit']);
         }
         foreach ($templates as $template) {
             $result[] = array('id' => $template['templateid'], 'name' => $template['name']);
         }
     }
     break;
 case 'applications':
     $applications = API::Application()->get(array('hostids' => zbx_toArray($data['hostid']), 'output' => array('applicationid', 'name'), 'search' => isset($data['search']) ? array('name' => $data['search']) : null, 'limit' => $config['search_limit']));
     if ($applications) {
         CArrayHelper::sort($applications, array(array('field' => 'name', 'order' => ZBX_SORT_UP)));
         if (isset($data['limit'])) {
             $applications = array_slice($applications, 0, $data['limit']);
         }
         foreach ($applications as $application) {
             $result[] = array('id' => $application['applicationid'], 'name' => $application['name']);
         }
     }
     break;
 case 'triggers':
     $triggers = API::Trigger()->get(array('editable' => isset($data['editable']) ? $data['editable'] : null, 'output' => array('triggerid', 'description'), 'selectHosts' => array('name'), 'search' => isset($data['search']) ? array('description' => $data['search']) : null, 'limit' => $config['search_limit']));
     if ($triggers) {
         CArrayHelper::sort($triggers, array(array('field' => 'description', 'order' => ZBX_SORT_UP)));
         if (isset($data['limit'])) {
Beispiel #13
0
 $form = new CForm();
 $form->setName('applicationform');
 $form->setAttribute('id', 'applications');
 $table = new CTableInfo(_('No applications found.'));
 $table->setHeader(array($multiselect ? new CCheckBox('allApplications', null, "javascript: checkAll('" . $form->getName() . "', 'allApplications', 'applications');") : null, _('Name')));
 $options = array('output' => array('applicationid', 'name'), 'nodeids' => $nodeId, 'hostids' => $hostid, 'expandData' => true);
 if (is_null($hostid)) {
     $options['groupids'] = $groupid;
 }
 if (!is_null($writeonly)) {
     $options['editable'] = true;
 }
 if (!is_null($templated)) {
     $options['templated'] = $templated;
 }
 $apps = API::Application()->get($options);
 CArrayHelper::sort($apps, array('name'));
 $data = array();
 $parentId = $dstfld1 ? zbx_jsvalue($dstfld1) : 'null';
 foreach ($apps as &$app) {
     $name = new CSpan($app['name'], 'link');
     $name->attr('id', 'spanid' . $app['applicationid']);
     $jsAction = 'javascript: addValue(' . zbx_jsvalue($reference) . ', ' . zbx_jsvalue($app['applicationid']) . ', ' . $parentId . ');';
     if ($multiselect) {
         $checkBox = new CCheckBox('applications[' . zbx_jsValue('applicationid') . ']', null, null, $app['applicationid']);
     }
     $name->setAttribute('onclick', $jsAction . ' jQuery(this).removeAttr("onclick");');
     $data[$app['applicationid']] = array('id' => $app['applicationid'], 'name' => $app['name'], 'prefix' => get_node_name_by_elid($app['applicationid'], null, NAME_DELIMITER));
     $table->addRow(array($multiselect ? $checkBox : null, $name));
 }
 unset($app);
 /**
  * Get items data.
  *
  * @param array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['triggerids']
  * @param array $options['applicationids']
  * @param boolean $options['status']
  * @param boolean $options['templated_items']
  * @param boolean $options['editable']
  * @param boolean $options['count']
  * @param string $options['pattern']
  * @param int $options['limit']
  * @param string $options['order']
  *
  * @return array|int item data as array or false if error
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     // allowed columns for sorting
     $sortColumns = array('itemid', 'name', 'key_', 'delay', 'history', 'trends', 'type', 'status');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     $sqlParts = array('select' => array('items' => 'i.itemid'), 'from' => array('items' => 'items i'), 'where' => array('webtype' => 'i.type<>' . ITEM_TYPE_HTTPTEST, 'flags' => 'i.flags IN (' . ZBX_FLAG_DISCOVERY_NORMAL . ',' . ZBX_FLAG_DISCOVERY_CREATED . ')'), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'proxyids' => null, 'itemids' => null, 'interfaceids' => null, 'graphids' => null, 'triggerids' => null, 'applicationids' => null, 'webitems' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'editable' => null, 'nopermissions' => null, 'group' => null, 'host' => null, 'application' => null, 'with_triggers' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectHosts' => null, 'selectInterfaces' => null, 'selectTriggers' => null, 'selectGraphs' => null, 'selectApplications' => null, 'selectDiscoveryRule' => null, 'selectItemDiscovery' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($defOptions, $options);
     if (is_array($options['output'])) {
         unset($sqlParts['select']['items']);
         $dbTable = DB::getSchema('items');
         $sqlParts['select']['itemid'] = 'i.itemid';
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 'i.' . $field;
             }
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + permission check
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $userGroups = getUserGroupsByUserId($userid);
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE i.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>=' . $permission . ')';
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         $sqlParts['where']['itemid'] = dbConditionInt('i.itemid', $options['itemids']);
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sqlParts['select']['hostid'] = 'i.hostid';
         }
         $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['hostids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.hostid';
         }
     }
     // interfaceids
     if (!is_null($options['interfaceids'])) {
         zbx_value2array($options['interfaceids']);
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sqlParts['select']['interfaceid'] = 'i.interfaceid';
         }
         $sqlParts['where']['interfaceid'] = dbConditionInt('i.interfaceid', $options['interfaceids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.interfaceid';
         }
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['groupid'] = 'hg.groupid';
         }
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']);
         $sqlParts['where'][] = 'hg.hostid=i.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['hg'] = 'hg.groupid';
         }
     }
     // proxyids
     if (!is_null($options['proxyids'])) {
         zbx_value2array($options['proxyids']);
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sqlParts['select']['proxyid'] = 'h.proxy_hostid';
         }
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where'][] = dbConditionInt('h.proxy_hostid', $options['proxyids']);
         $sqlParts['where'][] = 'h.hostid=i.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['h'] = 'h.proxy_hostid';
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['triggerid'] = 'f.triggerid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['where'][] = dbConditionInt('f.triggerid', $options['triggerids']);
         $sqlParts['where']['if'] = 'i.itemid=f.itemid';
     }
     // applicationids
     if (!is_null($options['applicationids'])) {
         zbx_value2array($options['applicationids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['applicationid'] = 'ia.applicationid';
         }
         $sqlParts['from']['items_applications'] = 'items_applications ia';
         $sqlParts['where'][] = dbConditionInt('ia.applicationid', $options['applicationids']);
         $sqlParts['where']['ia'] = 'ia.itemid=i.itemid';
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['graphid'] = 'gi.graphid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['where'][] = dbConditionInt('gi.graphid', $options['graphids']);
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
     }
     // webitems
     if (!is_null($options['webitems'])) {
         unset($sqlParts['where']['webtype']);
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 'i.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 'i.templateid IS NULL';
         }
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // monitored
     if (!is_null($options['monitored'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['monitored']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_MONITORED;
             $sqlParts['where'][] = 'i.status=' . ITEM_STATUS_ACTIVE;
         } else {
             $sqlParts['where'][] = '(h.status<>' . HOST_STATUS_MONITORED . ' OR i.status<>' . ITEM_STATUS_ACTIVE . ')';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('items i', $options, $sqlParts);
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('items i', $options, $sqlParts);
         if (isset($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sqlParts['from']['hosts'] = 'hosts h';
             $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
             $sqlParts['where']['h'] = dbConditionString('h.host', $options['filter']['host'], false, true);
         }
         if (array_key_exists('flags', $options['filter']) && ($options['filter']['flags'] === null || !zbx_empty($options['filter']['flags']))) {
             unset($sqlParts['where']['flags']);
         }
     }
     // group
     if (!is_null($options['group'])) {
         $sqlParts['from']['groups'] = 'groups g';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where']['ghg'] = 'g.groupid=hg.groupid';
         $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sqlParts['where'][] = ' g.name=' . zbx_dbstr($options['group']);
     }
     // host
     if (!is_null($options['host'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['host'] = 'h.host';
         }
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         $sqlParts['where'][] = ' h.host=' . zbx_dbstr($options['host']);
     }
     // application
     if (!is_null($options['application'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['application'] = 'a.name as application';
         }
         $sqlParts['from']['applications'] = 'applications a';
         $sqlParts['from']['items_applications'] = 'items_applications ia';
         $sqlParts['where']['aia'] = 'a.applicationid = ia.applicationid';
         $sqlParts['where']['iai'] = 'ia.itemid=i.itemid';
         $sqlParts['where'][] = ' a.name=' . zbx_dbstr($options['application']);
     }
     // with_triggers
     if (!is_null($options['with_triggers'])) {
         if ($options['with_triggers'] == 1) {
             $sqlParts['where'][] = ' EXISTS (SELECT NULL FROM functions ff WHERE ff.itemid=i.itemid)';
         } else {
             $sqlParts['where'][] = 'NOT EXISTS (SELECT NULL FROM functions ff WHERE ff.itemid=i.itemid)';
         }
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['items'] = 'i.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('COUNT(DISTINCT i.itemid) AS rowscount');
         // groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sqlParts['group'] as $key => $fields) {
                 $sqlParts['select'][$key] = $fields;
             }
         }
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 'i');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $itemids = array();
     $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
     while ($item = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $item;
             } else {
                 $result = $item['rowscount'];
             }
         } else {
             $itemids[$item['itemid']] = $item['itemid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$item['itemid']] = array('itemid' => $item['itemid']);
             } else {
                 if (!isset($result[$item['itemid']])) {
                     $result[$item['itemid']] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$item['itemid']]['hosts'])) {
                     $result[$item['itemid']]['hosts'] = array();
                 }
                 if (!is_null($options['selectTriggers']) && !isset($result[$item['itemid']]['triggers'])) {
                     $result[$item['itemid']]['triggers'] = array();
                 }
                 if (!is_null($options['selectGraphs']) && !isset($result[$item['itemid']]['graphs'])) {
                     $result[$item['itemid']]['graphs'] = array();
                 }
                 if (!is_null($options['selectApplications']) && !isset($result[$item['itemid']]['applications'])) {
                     $result[$item['itemid']]['applications'] = array();
                 }
                 if (!is_null($options['selectDiscoveryRule']) && !isset($result[$item['itemid']]['discoveryRule'])) {
                     $result[$item['itemid']]['discoveryRule'] = array();
                 }
                 if (!is_null($options['selectInterfaces']) && !isset($result[$item['itemid']]['interfaces'])) {
                     $result[$item['itemid']]['interfaces'] = array();
                 }
                 // triggerids
                 if (isset($item['triggerid']) && is_null($options['selectTriggers'])) {
                     if (!isset($result[$item['itemid']]['triggers'])) {
                         $result[$item['itemid']]['triggers'] = array();
                     }
                     $result[$item['itemid']]['triggers'][] = array('triggerid' => $item['triggerid']);
                     unset($item['triggerid']);
                 }
                 // graphids
                 if (isset($item['graphid']) && is_null($options['selectGraphs'])) {
                     if (!isset($result[$item['itemid']]['graphs'])) {
                         $result[$item['itemid']]['graphs'] = array();
                     }
                     $result[$item['itemid']]['graphs'][] = array('graphid' => $item['graphid']);
                     unset($item['graphid']);
                 }
                 // applicationids
                 if (isset($item['applicationid']) && is_null($options['selectApplications'])) {
                     if (!isset($result[$item['itemid']]['applications'])) {
                         $result[$item['itemid']]['applications'] = array();
                     }
                     $result[$item['itemid']]['applications'][] = array('applicationid' => $item['applicationid']);
                     unset($item['applicationid']);
                 }
                 $result[$item['itemid']] += $item;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     /*
      * Adding objects
      */
     // adding hosts
     if (!is_null($options['selectHosts'])) {
         if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) {
             $objParams = array('nodeids' => $options['nodeids'], 'itemids' => $itemids, 'templated_hosts' => true, 'output' => $options['selectHosts'], 'nopermissions' => true, 'preservekeys' => true);
             $hosts = API::Host()->get($objParams);
             foreach ($hosts as $host) {
                 $hitems = $host['items'];
                 unset($host['items']);
                 foreach ($hitems as $item) {
                     $result[$item['itemid']]['hosts'][] = $host;
                 }
             }
             $templates = API::Template()->get($objParams);
             foreach ($templates as $template) {
                 $titems = $template['items'];
                 unset($template['items']);
                 foreach ($titems as $item) {
                     $result[$item['itemid']]['hosts'][] = $template;
                 }
             }
         }
     }
     // adding interfaces
     if (!is_null($options['selectInterfaces'])) {
         if (is_array($options['selectInterfaces']) || str_in_array($options['selectInterfaces'], $subselectsAllowedOutputs)) {
             $interfaces = API::HostInterface()->get(array('nodeids' => $options['nodeids'], 'itemids' => $itemids, 'output' => $options['selectInterfaces'], 'nopermissions' => true, 'preservekeys' => true));
             foreach ($interfaces as $interface) {
                 $hitems = $interface['items'];
                 unset($interface['items']);
                 foreach ($hitems as $item) {
                     $result[$item['itemid']]['interfaces'][] = $interface;
                 }
             }
         }
     }
     // adding triggers
     if (!is_null($options['selectTriggers'])) {
         $objParams = array('nodeids' => $options['nodeids'], 'itemids' => $itemids, 'preservekeys' => true);
         if (in_array($options['selectTriggers'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectTriggers'];
             $triggers = API::Trigger()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($triggers, 'description');
             }
             foreach ($triggers as $triggerid => $trigger) {
                 unset($triggers[$triggerid]['items']);
                 $count = array();
                 foreach ($trigger['items'] as $item) {
                     // skip trigger assignment for unwanted items from other hosts
                     if (!isset($result[$item['itemid']])) {
                         continue;
                     }
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$item['itemid']])) {
                             $count[$item['itemid']] = 0;
                         }
                         $count[$item['itemid']]++;
                         if ($count[$item['itemid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$item['itemid']]['triggers'][] =& $triggers[$triggerid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectTriggers']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $triggers = API::Trigger()->get($objParams);
             $triggers = zbx_toHash($triggers, 'itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($triggers[$itemid])) {
                     $result[$itemid]['triggers'] = $triggers[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['triggers'] = 0;
                 }
             }
         }
     }
     // adding graphs
     if (!is_null($options['selectGraphs'])) {
         $objParams = array('nodeids' => $options['nodeids'], 'itemids' => $itemids, 'preservekeys' => true);
         if (in_array($options['selectGraphs'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectGraphs'];
             $graphs = API::Graph()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($graphs, 'name');
             }
             foreach ($graphs as $graphid => $graph) {
                 unset($graphs[$graphid]['items']);
                 $count = array();
                 foreach ($graph['items'] as $item) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$item['itemid']])) {
                             $count[$item['itemid']] = 0;
                         }
                         $count[$item['itemid']]++;
                         if ($count[$item['itemid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$item['itemid']]['graphs'][] =& $graphs[$graphid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectGraphs']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $graphs = API::Graph()->get($objParams);
             $graphs = zbx_toHash($graphs, 'itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($graphs[$itemid])) {
                     $result[$itemid]['graphs'] = $graphs[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['graphs'] = 0;
                 }
             }
         }
     }
     // adding applications
     if (!is_null($options['selectApplications']) && str_in_array($options['selectApplications'], $subselectsAllowedOutputs)) {
         $applications = API::Application()->get(array('nodeids' => $options['nodeids'], 'output' => $options['selectApplications'], 'itemids' => $itemids, 'preservekeys' => true));
         foreach ($applications as $application) {
             $aitems = $application['items'];
             unset($application['items']);
             foreach ($aitems as $item) {
                 $result[$item['itemid']]['applications'][] = $application;
             }
         }
     }
     // adding discoveryrule
     if (!is_null($options['selectDiscoveryRule'])) {
         $ruleids = $ruleMap = array();
         $dbRules = DBselect('SELECT id1.itemid,id2.parent_itemid' . ' FROM item_discovery id1,item_discovery id2,items i' . ' WHERE ' . dbConditionInt('id1.itemid', $itemids) . ' AND id1.parent_itemid=id2.itemid' . ' AND i.itemid=id1.itemid' . ' AND i.flags=' . ZBX_FLAG_DISCOVERY_CREATED);
         while ($rule = DBfetch($dbRules)) {
             $ruleids[$rule['parent_itemid']] = $rule['parent_itemid'];
             $ruleMap[$rule['itemid']] = $rule['parent_itemid'];
         }
         $dbRules = DBselect('SELECT id.parent_itemid,id.itemid' . ' FROM item_discovery id,items i' . ' WHERE ' . dbConditionInt('id.itemid', $itemids) . ' AND i.itemid=id.itemid' . ' AND i.flags=' . ZBX_FLAG_DISCOVERY_CHILD);
         while ($rule = DBfetch($dbRules)) {
             $ruleids[$rule['parent_itemid']] = $rule['parent_itemid'];
             $ruleMap[$rule['itemid']] = $rule['parent_itemid'];
         }
         $objParams = array('nodeids' => $options['nodeids'], 'itemids' => $ruleids, 'filter' => array('flags' => null), 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectDiscoveryRule']) || str_in_array($options['selectDiscoveryRule'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectDiscoveryRule'];
             $discoveryRules = $this->get($objParams);
             foreach ($result as $itemid => $item) {
                 if (isset($ruleMap[$itemid]) && isset($discoveryRules[$ruleMap[$itemid]])) {
                     $result[$itemid]['discoveryRule'] = $discoveryRules[$ruleMap[$itemid]];
                 }
             }
         }
     }
     // add other related objects
     if ($result) {
         $result = $this->addRelatedObjects($options, $result);
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
    $hostList->addRow(_('IPMI interfaces'), (new CDiv($ifTab))->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)->setAttribute('data-type', 'ipmi')->setWidth(ZBX_HOST_INTERFACE_WIDTH));
}
$hostList->addRow(_('Description'), (new CTextArea('description', $data['description']))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH));
// Proxy
if ($data['flags'] != ZBX_FLAG_DISCOVERY_CREATED) {
    $proxy = new CComboBox('proxy_hostid', $data['proxy_hostid'], null, [0 => _('(no proxy)')] + $data['proxies']);
    $proxy->setEnabled($data['flags'] != ZBX_FLAG_DISCOVERY_CREATED);
} else {
    $proxy = (new CTextBox(null, $data['proxy_hostid'] != 0 ? $data['proxies'][$data['proxy_hostid']] : _('(no proxy)'), true))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH);
    $hostList->addVar('proxy_hostid', $data['proxy_hostid']);
}
$hostList->addRow(_('Monitored by proxy'), $proxy);
$hostList->addRow(_('Enabled'), (new CCheckBox('status', HOST_STATUS_MONITORED))->setChecked($data['status'] == HOST_STATUS_MONITORED));
if ($data['clone_hostid'] != 0) {
    // host applications
    $hostApps = API::Application()->get(['output' => ['name'], 'hostids' => [$data['clone_hostid']], 'inherited' => false, 'preservekeys' => true, 'filter' => ['flags' => ZBX_FLAG_DISCOVERY_NORMAL]]);
    if ($hostApps) {
        $applicationsList = [];
        foreach ($hostApps as $hostAppId => $hostApp) {
            $applicationsList[$hostAppId] = $hostApp['name'];
        }
        order_result($applicationsList);
        $listBox = new CListBox('applications', null, 8);
        $listBox->setAttribute('disabled', 'disabled');
        $listBox->addItems($applicationsList);
        $hostList->addRow(_('Applications'), $listBox);
    }
    // host items
    $hostItems = API::Item()->get(['output' => ['itemid', 'hostid', 'key_', 'name'], 'hostids' => [$data['clone_hostid']], 'inherited' => false, 'filter' => ['flags' => ZBX_FLAG_DISCOVERY_NORMAL]]);
    if ($hostItems) {
        $hostItems = CMacrosResolverHelper::resolveItemNames($hostItems);
 /**
  * Get Host data
  *
  * @param array         $options
  * @param array         $options['nodeids']                  Node IDs
  * @param array         $options['groupids']                 HostGroup IDs
  * @param array         $options['hostids']                  Host IDs
  * @param boolean       $options['monitored_hosts']          only monitored Hosts
  * @param boolean       $options['templated_hosts']          include templates in result
  * @param boolean       $options['with_items']               only with items
  * @param boolean       $options['with_monitored_items']     only with monitored items
  * @param boolean       $options['with_historical_items']    only with historical items
  * @param boolean       $options['with_triggers']            only with triggers
  * @param boolean       $options['with_monitored_triggers']  only with monitored triggers
  * @param boolean       $options['with_httptests']           only with http tests
  * @param boolean       $options['with_monitored_httptests'] only with monitored http tests
  * @param boolean       $options['with_graphs']              only with graphs
  * @param boolean       $options['editable']                 only with read-write permission. Ignored for SuperAdmins
  * @param boolean       $options['selectGroups']             select HostGroups
  * @param boolean       $options['selectItems']              select Items
  * @param boolean       $options['selectTriggers']           select Triggers
  * @param boolean       $options['selectGraphs']             select Graphs
  * @param boolean       $options['selectApplications']       select Applications
  * @param boolean       $options['selectMacros']             select Macros
  * @param boolean|array $options['selectInventory']          select Inventory
  * @param boolean       $options['withInventory']            select only hosts with inventory
  * @param int           $options['count']                    count Hosts, returned column name is rowscount
  * @param string        $options['pattern']                  search hosts by pattern in Host name
  * @param string        $options['extendPattern']            search hosts by pattern in Host name, ip and DNS
  * @param int           $options['limit']                    limit selection
  * @param string        $options['sortfield']                field to sort by
  * @param string        $options['sortorder']                sort order
  *
  * @return array|boolean Host data as array or false if error
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     // allowed columns for sorting
     $sortColumns = array('hostid', 'host', 'name', 'status');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     $sqlParts = array('select' => array('hosts' => 'h.hostid'), 'from' => array('hosts' => 'hosts h'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'proxyids' => null, 'templateids' => null, 'interfaceids' => null, 'itemids' => null, 'triggerids' => null, 'maintenanceids' => null, 'graphids' => null, 'applicationids' => null, 'dhostids' => null, 'dserviceids' => null, 'httptestids' => null, 'monitored_hosts' => null, 'templated_hosts' => null, 'proxy_hosts' => null, 'with_items' => null, 'with_monitored_items' => null, 'with_historical_items' => null, 'with_simple_graph_items' => null, 'with_triggers' => null, 'with_monitored_triggers' => null, 'with_httptests' => null, 'with_monitored_httptests' => null, 'with_graphs' => null, 'with_applications' => null, 'withInventory' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectGroups' => null, 'selectParentTemplates' => null, 'selectItems' => null, 'selectDiscoveries' => null, 'selectTriggers' => null, 'selectGraphs' => null, 'selectDHosts' => null, 'selectDServices' => null, 'selectApplications' => null, 'selectMacros' => null, 'selectScreens' => null, 'selectInterfaces' => null, 'selectInventory' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($defOptions, $options);
     if (is_array($options['output'])) {
         unset($sqlParts['select']['hosts']);
         $dbTable = DB::getSchema('hosts');
         $sqlParts['select']['hostid'] = 'h.hostid';
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 'h.' . $field;
             }
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $userGroups = getUserGroupsByUserId($userid);
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE h.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>=' . $permission . ')';
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         $sqlParts['where']['hostid'] = dbConditionInt('h.hostid', $options['hostids']);
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['groupid'] = 'hg.groupid';
         }
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']);
         $sqlParts['where']['hgh'] = 'hg.hostid=h.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['groupid'] = 'hg.groupid';
         }
     }
     // proxyids
     if (!is_null($options['proxyids'])) {
         zbx_value2array($options['proxyids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['proxy_hostid'] = 'h.proxy_hostid';
         }
         $sqlParts['where'][] = dbConditionInt('h.proxy_hostid', $options['proxyids']);
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['templateid'] = 'ht.templateid';
         }
         $sqlParts['from']['hosts_templates'] = 'hosts_templates ht';
         $sqlParts['where'][] = dbConditionInt('ht.templateid', $options['templateids']);
         $sqlParts['where']['hht'] = 'h.hostid=ht.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['templateid'] = 'ht.templateid';
         }
     }
     // interfaceids
     if (!is_null($options['interfaceids'])) {
         zbx_value2array($options['interfaceids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['interfaceid'] = 'hi.interfaceid';
         }
         $sqlParts['from']['interface'] = 'interface hi';
         $sqlParts['where'][] = dbConditionInt('hi.interfaceid', $options['interfaceids']);
         $sqlParts['where']['hi'] = 'h.hostid=hi.hostid';
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['itemid'] = 'i.itemid';
         }
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['where'][] = dbConditionInt('i.itemid', $options['itemids']);
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['triggerid'] = 'f.triggerid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['where'][] = dbConditionInt('f.triggerid', $options['triggerids']);
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
     }
     // httptestids
     if (!is_null($options['httptestids'])) {
         zbx_value2array($options['httptestids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['httptestid'] = 'ht.httptestid';
         }
         $sqlParts['from']['applications'] = 'applications a';
         $sqlParts['from']['httptest'] = 'httptest ht';
         $sqlParts['where'][] = dbConditionInt('ht.httptestid', $options['httptestids']);
         $sqlParts['where']['aht'] = 'a.applicationid=ht.applicationid';
         $sqlParts['where']['ah'] = 'a.hostid=h.hostid';
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['graphid'] = 'gi.graphid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['where'][] = dbConditionInt('gi.graphid', $options['graphids']);
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
     }
     // applicationids
     if (!is_null($options['applicationids'])) {
         zbx_value2array($options['applicationids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['applicationid'] = 'a.applicationid';
         }
         $sqlParts['from']['applications'] = 'applications a';
         $sqlParts['where'][] = dbConditionInt('a.applicationid', $options['applicationids']);
         $sqlParts['where']['ah'] = 'a.hostid=h.hostid';
     }
     // dhostids
     if (!is_null($options['dhostids'])) {
         zbx_value2array($options['dhostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['dhostid'] = 'ds.dhostid';
         }
         $sqlParts['from']['dservices'] = 'dservices ds';
         $sqlParts['where'][] = dbConditionInt('ds.dhostid', $options['dhostids']);
         $sqlParts['where']['dsh'] = 'ds.ip=h.ip';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['dhostid'] = 'ds.dhostid';
         }
     }
     // dserviceids
     if (!is_null($options['dserviceids'])) {
         zbx_value2array($options['dserviceids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['dserviceid'] = 'ds.dserviceid';
         }
         $sqlParts['from']['dservices'] = 'dservices ds';
         $sqlParts['from']['interface'] = 'interface i';
         $sqlParts['where'][] = dbConditionInt('ds.dserviceid', $options['dserviceids']);
         $sqlParts['where']['dsh'] = 'ds.ip=i.ip';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['dserviceid'] = 'ds.dserviceid';
         }
     }
     // maintenanceids
     if (!is_null($options['maintenanceids'])) {
         zbx_value2array($options['maintenanceids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['maintenanceid'] = 'mh.maintenanceid';
         }
         $sqlParts['from']['maintenances_hosts'] = 'maintenances_hosts mh';
         $sqlParts['where'][] = dbConditionInt('mh.maintenanceid', $options['maintenanceids']);
         $sqlParts['where']['hmh'] = 'h.hostid=mh.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['maintenanceid'] = 'mh.maintenanceid';
         }
     }
     // monitored_hosts, templated_hosts
     if (!is_null($options['monitored_hosts'])) {
         $sqlParts['where']['status'] = 'h.status=' . HOST_STATUS_MONITORED;
     } elseif (!is_null($options['templated_hosts'])) {
         $sqlParts['where']['status'] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ',' . HOST_STATUS_TEMPLATE . ')';
     } elseif (!is_null($options['proxy_hosts'])) {
         $sqlParts['where']['status'] = 'h.status IN (' . HOST_STATUS_PROXY_ACTIVE . ',' . HOST_STATUS_PROXY_PASSIVE . ')';
     } else {
         $sqlParts['where']['status'] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')';
     }
     // with_items, with_monitored_items, with_historical_items, with_simple_graph_items
     if (!is_null($options['with_items'])) {
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM items i' . ' WHERE h.hostid=i.hostid' . ')';
     } elseif (!is_null($options['with_monitored_items'])) {
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM items i' . ' WHERE h.hostid=i.hostid' . ' AND i.status=' . ITEM_STATUS_ACTIVE . ')';
     } elseif (!is_null($options['with_historical_items'])) {
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM items i' . ' WHERE h.hostid=i.hostid' . ' AND i.status IN (' . ITEM_STATUS_ACTIVE . ',' . ITEM_STATUS_NOTSUPPORTED . ')' . ' AND i.lastvalue IS NOT NULL' . ')';
     } elseif (!is_null($options['with_simple_graph_items'])) {
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM items i' . ' WHERE h.hostid=i.hostid' . ' AND i.value_type IN (' . ITEM_VALUE_TYPE_FLOAT . ',' . ITEM_VALUE_TYPE_UINT64 . ')' . ' AND i.status=' . ITEM_STATUS_ACTIVE . ' AND i.flags IN (' . ZBX_FLAG_DISCOVERY_NORMAL . ',' . ZBX_FLAG_DISCOVERY_CREATED . ')' . ')';
     }
     // with_triggers, with_monitored_triggers
     if (!is_null($options['with_triggers'])) {
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM items i,functions f,triggers t' . ' WHERE h.hostid=i.hostid' . ' AND i.itemid=f.itemid' . ' AND f.triggerid=t.triggerid' . ')';
     } elseif (!is_null($options['with_monitored_triggers'])) {
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM items i,functions f,triggers t' . ' WHERE h.hostid=i.hostid' . ' AND i.itemid=f.itemid' . ' AND f.triggerid=t.triggerid' . ' AND i.status=' . ITEM_STATUS_ACTIVE . ' AND t.status=' . TRIGGER_STATUS_ENABLED . ')';
     }
     // with_httptests, with_monitored_httptests
     if (!is_null($options['with_httptests'])) {
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM applications a,httptest ht' . ' WHERE h.hostid=a.hostid' . ' AND a.applicationid=ht.applicationid' . ')';
     } elseif (!is_null($options['with_monitored_httptests'])) {
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM applications a,httptest ht' . ' WHERE h.hostid=a.hostid' . ' AND a.applicationid=ht.applicationid' . ' AND ht.status=' . HTTPTEST_STATUS_ACTIVE . ')';
     }
     // with_graphs
     if (!is_null($options['with_graphs'])) {
         $sqlParts['where'][] = 'EXISTS (' . ' SELECT 1' . ' FROM items i,graphs_items gi' . ' WHERE i.hostid=h.hostid' . ' AND i.itemid=gi.itemid ' . zbx_limit(1) . ')';
     }
     // with applications
     if (!is_null($options['with_applications'])) {
         $sqlParts['from']['applications'] = 'applications a';
         $sqlParts['where'][] = 'a.hostid=h.hostid';
     }
     // withInventory
     if (!is_null($options['withInventory']) && $options['withInventory']) {
         $sqlParts['where'][] = ' h.hostid IN (' . ' SELECT hin.hostid' . ' FROM host_inventory hin)';
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('hosts h', $options, $sqlParts);
         if (zbx_db_search('interface hi', $options, $sqlParts)) {
             $sqlParts['from']['interface'] = 'interface hi';
             $sqlParts['where']['hi'] = 'h.hostid=hi.hostid';
         }
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('hosts h', $options, $sqlParts);
         if ($this->dbFilter('interface hi', $options, $sqlParts)) {
             $sqlParts['from']['interface'] = 'interface hi';
             $sqlParts['where']['hi'] = 'h.hostid=hi.hostid';
         }
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['hosts'] = 'h.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('COUNT(DISTINCT h.hostid) AS rowscount');
         // groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sqlParts['group'] as $key => $fields) {
                 $sqlParts['select'][$key] = $fields;
             }
         }
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 'h');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $hostids = array();
     $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
     while ($host = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $host;
             } else {
                 $result = $host['rowscount'];
             }
         } else {
             $hostids[$host['hostid']] = $host['hostid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$host['hostid']] = array('hostid' => $host['hostid']);
             } else {
                 if (!isset($result[$host['hostid']])) {
                     $result[$host['hostid']] = array();
                 }
                 if (!is_null($options['selectGroups']) && !isset($result[$host['hostid']]['groups'])) {
                     $result[$host['hostid']]['groups'] = array();
                 }
                 if (!is_null($options['selectParentTemplates']) && !isset($result[$host['hostid']]['parentTemplates'])) {
                     $result[$host['hostid']]['parentTemplates'] = array();
                 }
                 if (!is_null($options['selectItems']) && !isset($result[$host['hostid']]['items'])) {
                     $result[$host['hostid']]['items'] = array();
                 }
                 if (!is_null($options['selectDiscoveries']) && !isset($result[$host['hostid']]['discoveries'])) {
                     $result[$host['hostid']]['discoveries'] = array();
                 }
                 if (!is_null($options['selectInventory']) && !isset($result[$host['hostid']]['inventory'])) {
                     $result[$host['hostid']]['inventory'] = array();
                 }
                 if (!is_null($options['selectTriggers']) && !isset($result[$host['hostid']]['triggers'])) {
                     $result[$host['hostid']]['triggers'] = array();
                 }
                 if (!is_null($options['selectGraphs']) && !isset($result[$host['hostid']]['graphs'])) {
                     $result[$host['hostid']]['graphs'] = array();
                 }
                 if (!is_null($options['selectDHosts']) && !isset($result[$host['hostid']]['dhosts'])) {
                     $result[$host['hostid']]['dhosts'] = array();
                 }
                 if (!is_null($options['selectDServices']) && !isset($result[$host['hostid']]['dservices'])) {
                     $result[$host['hostid']]['dservices'] = array();
                 }
                 if (!is_null($options['selectApplications']) && !isset($result[$host['hostid']]['applications'])) {
                     $result[$host['hostid']]['applications'] = array();
                 }
                 if (!is_null($options['selectMacros']) && !isset($result[$host['hostid']]['macros'])) {
                     $result[$host['hostid']]['macros'] = array();
                 }
                 if (!is_null($options['selectScreens']) && !isset($result[$host['hostid']]['screens'])) {
                     $result[$host['hostid']]['screens'] = array();
                 }
                 if (!is_null($options['selectInterfaces']) && !isset($result[$host['hostid']]['interfaces'])) {
                     $result[$host['hostid']]['interfaces'] = array();
                 }
                 // groupids
                 if (isset($host['groupid']) && is_null($options['selectGroups'])) {
                     if (!isset($result[$host['hostid']]['groups'])) {
                         $result[$host['hostid']]['groups'] = array();
                     }
                     $result[$host['hostid']]['groups'][] = array('groupid' => $host['groupid']);
                     unset($host['groupid']);
                 }
                 // templateids
                 if (isset($host['templateid'])) {
                     if (!isset($result[$host['hostid']]['templates'])) {
                         $result[$host['hostid']]['templates'] = array();
                     }
                     $result[$host['hostid']]['templates'][] = array('templateid' => $host['templateid'], 'hostid' => $host['templateid']);
                     unset($host['templateid']);
                 }
                 // triggerids
                 if (isset($host['triggerid']) && is_null($options['selectTriggers'])) {
                     if (!isset($result[$host['hostid']]['triggers'])) {
                         $result[$host['hostid']]['triggers'] = array();
                     }
                     $result[$host['hostid']]['triggers'][] = array('triggerid' => $host['triggerid']);
                     unset($host['triggerid']);
                 }
                 // interfaceids
                 if (isset($host['interfaceid']) && is_null($options['selectInterfaces'])) {
                     if (!isset($result[$host['hostid']]['interfaces'])) {
                         $result[$host['hostid']]['interfaces'] = array();
                     }
                     $result[$host['hostid']]['interfaces'][] = array('interfaceid' => $host['interfaceid']);
                     unset($host['interfaceid']);
                 }
                 // itemids
                 if (isset($host['itemid']) && is_null($options['selectItems'])) {
                     if (!isset($result[$host['hostid']]['items'])) {
                         $result[$host['hostid']]['items'] = array();
                     }
                     $result[$host['hostid']]['items'][] = array('itemid' => $host['itemid']);
                     unset($host['itemid']);
                 }
                 // graphids
                 if (isset($host['graphid']) && is_null($options['selectGraphs'])) {
                     if (!isset($result[$host['hostid']]['graphs'])) {
                         $result[$host['hostid']]['graphs'] = array();
                     }
                     $result[$host['hostid']]['graphs'][] = array('graphid' => $host['graphid']);
                     unset($host['graphid']);
                 }
                 // graphids
                 if (isset($host['applicationid'])) {
                     if (!isset($result[$host['hostid']]['applications'])) {
                         $result[$host['hostid']]['applications'] = array();
                     }
                     $result[$host['hostid']]['applications'][] = array('applicationid' => $host['applicationid']);
                     unset($host['applicationid']);
                 }
                 // httptestids
                 if (isset($host['httptestid'])) {
                     if (!isset($result[$host['hostid']]['httptests'])) {
                         $result[$host['hostid']]['httptests'] = array();
                     }
                     $result[$host['hostid']]['httptests'][] = array('httptestid' => $host['httptestid']);
                     unset($host['httptestid']);
                 }
                 // dhostids
                 if (isset($host['dhostid']) && is_null($options['selectDHosts'])) {
                     if (!isset($result[$host['hostid']]['dhosts'])) {
                         $result[$host['hostid']]['dhosts'] = array();
                     }
                     $result[$host['hostid']]['dhosts'][] = array('dhostid' => $host['dhostid']);
                     unset($host['dhostid']);
                 }
                 // dserviceids
                 if (isset($host['dserviceid']) && is_null($options['selectDServices'])) {
                     if (!isset($result[$host['hostid']]['dservices'])) {
                         $result[$host['hostid']]['dservices'] = array();
                     }
                     $result[$host['hostid']]['dservices'][] = array('dserviceid' => $host['dserviceid']);
                     unset($host['dserviceid']);
                 }
                 // maintenanceids
                 if (isset($host['maintenanceid'])) {
                     if (!isset($result[$host['hostid']]['maintenances'])) {
                         $result[$host['hostid']]['maintenances'] = array();
                     }
                     if ($host['maintenanceid'] > 0) {
                         $result[$host['hostid']]['maintenances'][] = array('maintenanceid' => $host['maintenanceid']);
                     }
                 }
                 $result[$host['hostid']] += $host;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     /*
      * adding objects
      */
     // adding groups
     if (!is_null($options['selectGroups']) && str_in_array($options['selectGroups'], $subselectsAllowedOutputs)) {
         $groups = API::HostGroup()->get(array('nodeids' => $options['nodeids'], 'output' => $options['selectGroups'], 'hostids' => $hostids, 'preservekeys' => true));
         foreach ($groups as $group) {
             $ghosts = $group['hosts'];
             unset($group['hosts']);
             foreach ($ghosts as $host) {
                 $result[$host['hostid']]['groups'][] = $group;
             }
         }
     }
     // adding inventories
     if (!is_null($options['selectInventory']) && $options['selectInventory'] !== false) {
         if (is_array($options['selectInventory'])) {
             // if we are given a list of fields that needs to be fetched
             $dbTable = DB::getSchema('host_inventory');
             $selectHIn = array('hin.hostid');
             foreach ($options['selectInventory'] as $field) {
                 if (isset($dbTable['fields'][$field])) {
                     $selectHIn[] = 'hin.' . $field;
                 }
             }
         } else {
             // all fields are needed
             $selectHIn = array('hin.*');
         }
         $dbInventory = DBselect('SELECT ' . implode(', ', $selectHIn) . ' FROM host_inventory hin' . ' WHERE ' . dbConditionInt('hin.hostid', $hostids));
         while ($inventory = DBfetch($dbInventory)) {
             $result[$inventory['hostid']]['inventory'] = $inventory;
         }
     }
     // adding templates
     if (!is_null($options['selectParentTemplates'])) {
         $objParams = array('nodeids' => $options['nodeids'], 'hostids' => $hostids, 'preservekeys' => true);
         if (is_array($options['selectParentTemplates']) || str_in_array($options['selectParentTemplates'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectParentTemplates'];
             $templates = API::Template()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($templates, 'host');
             }
             foreach ($templates as $templateid => $template) {
                 unset($templates[$templateid]['hosts']);
                 $count = array();
                 foreach ($template['hosts'] as $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['parentTemplates'][] =& $templates[$templateid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectParentTemplates']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $templates = API::Template()->get($objParams);
             $templates = zbx_toHash($templates, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['templates'] = isset($templates[$hostid]) ? $templates[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding hostinterfaces
     if (!is_null($options['selectInterfaces'])) {
         $objParams = array('nodeids' => $options['nodeids'], 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectInterfaces']) || str_in_array($options['selectInterfaces'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectInterfaces'];
             $interfaces = API::HostInterface()->get($objParams);
             // we need to order interfaces for proper linkage and viewing
             order_result($interfaces, 'interfaceid', ZBX_SORT_UP);
             $count = array();
             foreach ($interfaces as $interfaceid => $interface) {
                 if (!is_null($options['limitSelects'])) {
                     if (!isset($count[$interface['hostid']])) {
                         $count[$interface['hostid']] = 0;
                     }
                     $count[$interface['hostid']]++;
                     if ($count[$interface['hostid']] > $options['limitSelects']) {
                         continue;
                     }
                 }
                 $result[$interface['hostid']]['interfaces'][$interfaceid] =& $interfaces[$interfaceid];
             }
         } elseif (API_OUTPUT_COUNT == $options['selectInterfaces']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $interfaces = API::HostInterface()->get($objParams);
             $interfaces = zbx_toHash($interfaces, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['interfaces'] = isset($interfaces[$hostid]) ? $interfaces[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding items
     if (!is_null($options['selectItems'])) {
         $objParams = array('nodeids' => $options['nodeids'], 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectItems']) || str_in_array($options['selectItems'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectItems'];
             $items = API::Item()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($items, 'name');
             }
             $count = array();
             foreach ($items as $itemid => $item) {
                 if (!is_null($options['limitSelects'])) {
                     if (!isset($count[$item['hostid']])) {
                         $count[$item['hostid']] = 0;
                     }
                     $count[$item['hostid']]++;
                     if ($count[$item['hostid']] > $options['limitSelects']) {
                         continue;
                     }
                 }
                 $result[$item['hostid']]['items'][] =& $items[$itemid];
             }
         } elseif (API_OUTPUT_COUNT == $options['selectItems']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $items = API::Item()->get($objParams);
             $items = zbx_toHash($items, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['items'] = isset($items[$hostid]) ? $items[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding discoveries
     if (!is_null($options['selectDiscoveries'])) {
         $objParams = array('nodeids' => $options['nodeids'], 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectDiscoveries']) || str_in_array($options['selectDiscoveries'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectDiscoveries'];
             $items = API::DiscoveryRule()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($items, 'name');
             }
             $count = array();
             foreach ($items as $itemid => $item) {
                 unset($items[$itemid]['hosts']);
                 foreach ($item['hosts'] as $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['discoveries'][] =& $items[$itemid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectDiscoveries']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $items = API::DiscoveryRule()->get($objParams);
             $items = zbx_toHash($items, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['discoveries'] = isset($items[$hostid]) ? $items[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding triggers
     if (!is_null($options['selectTriggers'])) {
         if (is_array($options['selectTriggers']) || str_in_array($options['selectTriggers'], $subselectsAllowedOutputs)) {
             $triggers = API::Trigger()->get(array('nodeids' => $options['nodeids'], 'hostids' => $hostids, 'preservekeys' => true, 'output' => $options['selectTriggers']));
             if (!is_null($options['limitSelects'])) {
                 order_result($triggers, 'description');
             }
             $count = array();
             foreach ($triggers as $triggerid => $trigger) {
                 unset($triggers[$triggerid]['hosts']);
                 foreach ($trigger['hosts'] as $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['triggers'][] =& $triggers[$triggerid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectTriggers']) {
             $triggers = API::Trigger()->get(array('nodeids' => $options['nodeids'], '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 (!is_null($options['selectGraphs'])) {
         if (is_array($options['selectGraphs']) || str_in_array($options['selectGraphs'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectGraphs'];
             $graphs = API::Graph()->get(array('nodeids' => $options['nodeids'], 'hostids' => $hostids, 'preservekeys' => true, 'output' => $options['selectGraphs']));
             if (!is_null($options['limitSelects'])) {
                 order_result($graphs, 'name');
             }
             $count = array();
             foreach ($graphs as $graphid => $graph) {
                 unset($graphs[$graphid]['hosts']);
                 foreach ($graph['hosts'] as $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['graphs'][] =& $graphs[$graphid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectGraphs']) {
             $graphs = API::Graph()->get(array('nodeids' => $options['nodeids'], '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 discovery hosts
     if (!is_null($options['selectDHosts'])) {
         $objParams = array('nodeids' => $options['nodeids'], 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectDHosts']) || str_in_array($options['selectDHosts'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectDHosts'];
             $dhosts = API::DHost()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($dhosts, 'dhostid');
             }
             $count = array();
             foreach ($dhosts as $dhostid => $dhost) {
                 unset($dhosts[$dhostid]['hosts']);
                 foreach ($dhost['hosts'] as $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['dhosts'][] =& $dhosts[$dhostid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectDHosts']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $dhosts = API::DHost()->get($objParams);
             $dhosts = zbx_toHash($dhosts, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['dhosts'] = isset($dhosts[$hostid]) ? $dhosts[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding applications
     if (!is_null($options['selectApplications'])) {
         $objParams = array('nodeids' => $options['nodeids'], 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectApplications']) || str_in_array($options['selectApplications'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectApplications'];
             $applications = API::Application()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($applications, 'name');
             }
             $count = array();
             foreach ($applications as $applicationid => $application) {
                 unset($applications[$applicationid]['hosts']);
                 foreach ($application['hosts'] as $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['applications'][] =& $applications[$applicationid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectApplications']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $applications = API::Application()->get($objParams);
             $applications = zbx_toHash($applications, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['applications'] = isset($applications[$hostid]) ? $applications[$hostid]['rowscount'] : 0;
             }
         }
     }
     // adding macros
     if (!is_null($options['selectMacros']) && str_in_array($options['selectMacros'], $subselectsAllowedOutputs)) {
         $objParams = array('nodeids' => $options['nodeids'], 'output' => $options['selectMacros'], 'hostids' => $hostids, 'preservekeys' => true);
         $macros = API::UserMacro()->get($objParams);
         foreach ($macros as $macroid => $macro) {
             $mhosts = $macro['hosts'];
             unset($macro['hosts']);
             foreach ($mhosts as $host) {
                 $result[$host['hostid']]['macros'][$macroid] = $macro;
             }
         }
     }
     // adding screens
     if (!is_null($options['selectScreens'])) {
         $objParams = array('nodeids' => $options['nodeids'], 'hostids' => $hostids, 'editable' => $options['editable'], 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectScreens']) || str_in_array($options['selectScreens'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectScreens'];
             $screens = API::TemplateScreen()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($screens, 'name');
             }
             foreach ($screens as $snum => $screen) {
                 if (!is_null($options['limitSelects'])) {
                     if (count($result[$screen['hostid']]['screens']) >= $options['limitSelects']) {
                         continue;
                     }
                 }
                 unset($screens[$snum]['hosts']);
                 $result[$screen['hostid']]['screens'][] =& $screens[$snum];
             }
         } elseif (API_OUTPUT_COUNT == $options['selectScreens']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $screens = API::TemplateScreen()->get($objParams);
             $screens = zbx_toHash($screens, 'hostid');
             foreach ($result as $hostid => $host) {
                 $result[$hostid]['screens'] = isset($screens[$hostid]) ? $screens[$hostid]['rowscount'] : 0;
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Beispiel #17
0
 public static function parseMain($rules)
 {
     $triggersForDependencies = array();
     if (!empty($rules['hosts']['updateExisting']) || !empty($rules['hosts']['createMissing']) || !empty($rules['templates']['createMissing']) || !empty($rules['templates']['updateExisting'])) {
         $xpath = new DOMXPath(self::$xml);
         $hosts = $xpath->query('hosts/host');
         foreach ($hosts as $host) {
             $host_db = self::mapXML2arr($host, XML_TAG_HOST);
             if (!isset($host_db['status'])) {
                 $host_db['status'] = HOST_STATUS_TEMPLATE;
             }
             $current_host = $host_db['status'] == HOST_STATUS_TEMPLATE ? API::Template()->exists($host_db) : API::Host()->exists($host_db);
             if (!$current_host && ($host_db['status'] == HOST_STATUS_TEMPLATE && empty($rules['templates']['createMissing']) || $host_db['status'] != HOST_STATUS_TEMPLATE && empty($rules['hosts']['createMissing']))) {
                 continue;
             }
             if ($current_host && ($host_db['status'] == HOST_STATUS_TEMPLATE && empty($rules['templates']['updateExisting']) || $host_db['status'] != HOST_STATUS_TEMPLATE && empty($rules['hosts']['updateExisting']))) {
                 continue;
             }
             // there were no host visible names in 1.8
             if (!isset($host_db['name'])) {
                 $host_db['name'] = $host_db['host'];
             }
             // host will have no interfaces - we will be creating them separately
             $host_db['interfaces'] = null;
             // it is possible, that data is imported from 1.8, where there was only one network interface per host
             /**
              * @todo when new XML format will be introduced, this check should be changed to XML version check
              */
             $old_version_input = $host_db['status'] != HOST_STATUS_TEMPLATE;
             if ($old_version_input) {
                 // rearranging host structure, so it would look more like 2.0 host
                 $interfaces = array();
                 // the main interface is always "agent" type
                 if (!is_null($host_db['ip'])) {
                     $interfaces[] = array('main' => INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_AGENT, 'useip' => $host_db['useip'], 'ip' => $host_db['ip'], 'dns' => $host_db['dns'], 'port' => $host_db['port']);
                 }
                 // now we need to check if host had SNMP items. If it had, we need and SNMP interface for every different port.
                 $items = $xpath->query('items/item', $host);
                 $snmp_interface_ports_created = array();
                 foreach ($items as $item) {
                     $item_db = self::mapXML2arr($item, XML_TAG_ITEM);
                     if (($item_db['type'] == ITEM_TYPE_SNMPV1 || $item_db['type'] == ITEM_TYPE_SNMPV2C || $item_db['type'] == ITEM_TYPE_SNMPV3) && !isset($snmp_interface_ports_created[$item_db['snmp_port']])) {
                         $interfaces[] = array('main' => INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_SNMP, 'useip' => $host_db['useip'], 'ip' => $host_db['ip'], 'dns' => $host_db['dns'], 'port' => $item_db['snmp_port']);
                         $snmp_interface_ports_created[$item_db['snmp_port']] = 1;
                     }
                 }
                 unset($snmp_interface_ports_created);
                 // it was a temporary variable
                 // we ned to add ipmi interface if at least one ipmi item exists
                 foreach ($items as $item) {
                     $item_db = self::mapXML2arr($item, XML_TAG_ITEM);
                     if ($item_db['type'] == ITEM_TYPE_IPMI) {
                         // when saving a host in 1.8, it's possible to set useipmi=1 and not to fill an IP address
                         // we were not really sure what to do with this host,
                         // and decided to take host IP address instead and show info message about this
                         if ($host_db['ipmi_ip'] == '') {
                             $ipmi_ip = $host_db['ip'];
                             info(_s('Host "%s" has "useipmi" parameter checked, but has no "ipmi_ip" parameter! Using host IP address as an address for IPMI interface.', $host_db['host']));
                         } else {
                             $ipmi_ip = $host_db['ipmi_ip'];
                         }
                         $interfaces[] = array('main' => INTERFACE_PRIMARY, 'type' => INTERFACE_TYPE_IPMI, 'useip' => INTERFACE_USE_DNS, 'ip' => '', 'dns' => $ipmi_ip, 'port' => $host_db['ipmi_port']);
                         // we need only one ipmi interface
                         break;
                     }
                 }
             }
             if ($current_host) {
                 $options = array('filter' => array('host' => $host_db['host']), 'output' => API_OUTPUT_EXTEND, 'editable' => 1, 'selectInterfaces' => API_OUTPUT_EXTEND);
                 if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                     $current_host = API::Template()->get($options);
                 } else {
                     $current_host = API::Host()->get($options);
                 }
                 if (empty($current_host)) {
                     throw new Exception(_s('No permission for host "%1$s".', $host_db['host']));
                 } else {
                     $current_host = reset($current_host);
                 }
                 // checking if host already exists - then some of the interfaces may not need to be created
                 if ($host_db['status'] != HOST_STATUS_TEMPLATE) {
                     // for every interface we got based on XML
                     foreach ($interfaces as $i => $interface_db) {
                         // checking every interface of current host
                         foreach ($current_host['interfaces'] as $interface) {
                             // if all parameters of interface are identical
                             if ($interface['type'] == $interface_db['type'] && $interface['ip'] == $interface_db['ip'] && $interface['dns'] == $interface_db['dns'] && $interface['port'] == $interface_db['port'] && $interface['useip'] == $interface_db['useip']) {
                                 // this interface is the same as existing one!
                                 $interfaces[$i]['interfaceid'] = $interface['interfaceid'];
                                 break;
                             }
                         }
                     }
                 }
                 $interfaces_created_with_host = false;
             } else {
                 if ($host_db['status'] != HOST_STATUS_TEMPLATE) {
                     $host_db['interfaces'] = $interfaces;
                     $interfaces_created_with_host = true;
                 }
             }
             // HOST GROUPS {{{
             $groups = $xpath->query('groups/group', $host);
             $host_db['groups'] = array();
             $groups_to_parse = array();
             foreach ($groups as $group) {
                 $groups_to_parse[] = array('name' => $group->nodeValue);
             }
             if (empty($groups_to_parse)) {
                 $groups_to_parse[] = array('name' => ZBX_DEFAULT_IMPORT_HOST_GROUP);
             }
             foreach ($groups_to_parse as $group) {
                 $current_group = API::HostGroup()->exists($group);
                 if ($current_group) {
                     $options = array('filter' => $group, 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
                     $current_group = API::HostGroup()->get($options);
                     if (empty($current_group)) {
                         throw new Exception(_s('No permissions for group "%1$s".', $group['name']));
                     }
                     $host_db['groups'][] = reset($current_group);
                 } else {
                     $result = API::HostGroup()->create($group);
                     if (!$result) {
                         throw new Exception();
                     }
                     $options = array('groupids' => $result['groupids'], 'output' => API_OUTPUT_EXTEND);
                     $new_group = API::HostGroup()->get($options);
                     $host_db['groups'][] = reset($new_group);
                 }
             }
             // }}} HOST GROUPS
             // MACROS
             $macros = $xpath->query('macros/macro', $host);
             if ($macros->length > 0) {
                 $host_db['macros'] = array();
                 foreach ($macros as $macro) {
                     $host_db['macros'][] = self::mapXML2arr($macro, XML_TAG_MACRO);
                 }
             }
             // }}} MACROS
             // host inventory
             if ($old_version_input) {
                 if (!isset($host_db['inventory'])) {
                     $host_db['inventory'] = array();
                 }
                 $inventoryNode = $xpath->query('host_profile/*', $host);
                 if ($inventoryNode->length > 0) {
                     foreach ($inventoryNode as $field) {
                         $newInventoryName = self::mapInventoryName($field->nodeName);
                         $host_db['inventory'][$newInventoryName] = $field->nodeValue;
                     }
                 }
                 $inventoryNodeExt = $xpath->query('host_profiles_ext/*', $host);
                 if ($inventoryNodeExt->length > 0) {
                     foreach ($inventoryNodeExt as $field) {
                         $newInventoryName = self::mapInventoryName($field->nodeName);
                         if (isset($host_db['inventory'][$newInventoryName]) && $field->nodeValue !== '') {
                             $host_db['inventory'][$newInventoryName] .= "\r\n\r\n";
                             $host_db['inventory'][$newInventoryName] .= $field->nodeValue;
                         } else {
                             $host_db['inventory'][$newInventoryName] = $field->nodeValue;
                         }
                     }
                 }
                 $host_db['inventory_mode'] = isset($host_db['inventory']) ? HOST_INVENTORY_MANUAL : HOST_INVENTORY_DISABLED;
             }
             // HOSTS
             if (isset($host_db['proxy_hostid'])) {
                 $proxy_exists = API::Proxy()->get(array('proxyids' => $host_db['proxy_hostid']));
                 if (empty($proxy_exists)) {
                     $host_db['proxy_hostid'] = 0;
                 }
             }
             if ($current_host && (!empty($rules['hosts']['updateExisting']) || !empty($rules['templates']['updateExisting']))) {
                 if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                     $host_db['templateid'] = $current_host['templateid'];
                     $result = API::Template()->update($host_db);
                     $current_hostid = $current_host['templateid'];
                 } else {
                     $host_db['hostid'] = $current_host['hostid'];
                     $result = API::Host()->update($host_db);
                     $current_hostid = $current_host['hostid'];
                 }
                 if (!$result) {
                     throw new Exception();
                 }
             }
             if (!$current_host && (!empty($rules['hosts']['createMissing']) || !empty($rules['templates']['createMissing']))) {
                 if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                     $result = API::Template()->create($host_db);
                     if (!$result) {
                         throw new Exception();
                     }
                     $current_hostid = reset($result['templateids']);
                 } else {
                     $result = API::Host()->create($host_db);
                     if (!$result) {
                         throw new Exception();
                     }
                     $current_hostid = reset($result['hostids']);
                 }
             }
             $current_hostname = $host_db['host'];
             // TEMPLATES {{{
             if (!empty($rules['templateLinkage']['createMissing'])) {
                 $templates = $xpath->query('templates/template', $host);
                 $templateLinkage = array();
                 foreach ($templates as $template) {
                     $options = array('filter' => array('host' => $template->nodeValue), 'output' => array('templateid'), 'editable' => true);
                     $current_template = API::Template()->get($options);
                     if (empty($current_template)) {
                         throw new Exception(_s('No permission for template "%1$s".', $template->nodeValue));
                     }
                     $current_template = reset($current_template);
                     $templateLinkage[] = $current_template;
                 }
                 if ($templateLinkage) {
                     $result = API::Template()->massAdd(array('hosts' => array('hostid' => $current_hostid), 'templates' => $templateLinkage));
                     if (!$result) {
                         throw new Exception();
                     }
                 }
             }
             // }}} TEMPLATES
             // ITEMS {{{
             if (!empty($rules['items']['updateExisting']) || !empty($rules['items']['createMissing'])) {
                 $items = $xpath->query('items/item', $host);
                 // if this is an export from 1.8, we need to make some adjustments to items
                 if ($old_version_input) {
                     if (!$interfaces_created_with_host) {
                         // if host had another interfaces, we are not touching them: they remain as is
                         foreach ($interfaces as $i => $interface) {
                             // interface was not already created
                             if (!isset($interface['interfaceid'])) {
                                 // creating interface
                                 $interface['hostid'] = $current_hostid;
                                 $ids = API::HostInterface()->create($interface);
                                 if ($ids === false) {
                                     throw new Exception();
                                 }
                                 $interfaces[$i]['interfaceid'] = reset($ids['interfaceids']);
                             }
                         }
                     } else {
                         $options = array('hostids' => $current_hostid, 'output' => API_OUTPUT_EXTEND);
                         $interfaces = API::HostInterface()->get($options);
                     }
                     // we must know interface ids to assign them to items
                     $agent_interface_id = null;
                     $ipmi_interface_id = null;
                     $snmp_interfaces = array();
                     // hash 'port' => 'iterfaceid'
                     foreach ($interfaces as $interface) {
                         switch ($interface['type']) {
                             case INTERFACE_TYPE_AGENT:
                                 $agent_interface_id = $interface['interfaceid'];
                                 break;
                             case INTERFACE_TYPE_IPMI:
                                 $ipmi_interface_id = $interface['interfaceid'];
                                 break;
                             case INTERFACE_TYPE_SNMP:
                                 $snmp_interfaces[$interface['port']] = $interface['interfaceid'];
                                 break;
                         }
                     }
                 }
                 foreach ($items as $item) {
                     $item_db = self::mapXML2arr($item, XML_TAG_ITEM);
                     $item_db['hostid'] = $current_hostid;
                     // item needs interfaces
                     if ($old_version_input) {
                         // 'snmp_port' column was renamed to 'port'
                         if ($item_db['snmp_port'] != 0) {
                             // zabbix agent items have no ports
                             $item_db['port'] = $item_db['snmp_port'];
                         }
                         unset($item_db['snmp_port']);
                         // assigning appropriate interface depending on item type
                         switch ($item_db['type']) {
                             // zabbix agent interface
                             case ITEM_TYPE_ZABBIX:
                             case ITEM_TYPE_SIMPLE:
                             case ITEM_TYPE_EXTERNAL:
                             case ITEM_TYPE_SSH:
                             case ITEM_TYPE_TELNET:
                                 $item_db['interfaceid'] = $agent_interface_id;
                                 break;
                                 // snmp interface
                             // snmp interface
                             case ITEM_TYPE_SNMPV1:
                             case ITEM_TYPE_SNMPV2C:
                             case ITEM_TYPE_SNMPV3:
                                 // for an item with different port - different interface
                                 $item_db['interfaceid'] = $snmp_interfaces[$item_db['port']];
                                 break;
                             case ITEM_TYPE_IPMI:
                                 $item_db['interfaceid'] = $ipmi_interface_id;
                                 break;
                                 // no interfaces required for these item types
                             // no interfaces required for these item types
                             case ITEM_TYPE_HTTPTEST:
                             case ITEM_TYPE_CALCULATED:
                             case ITEM_TYPE_AGGREGATE:
                             case ITEM_TYPE_INTERNAL:
                             case ITEM_TYPE_ZABBIX_ACTIVE:
                             case ITEM_TYPE_TRAPPER:
                             case ITEM_TYPE_DB_MONITOR:
                                 $item_db['interfaceid'] = null;
                                 break;
                         }
                         $item_db['key_'] = self::convertOldSimpleKey($item_db['key_']);
                     }
                     $options = array('filter' => array('hostid' => $item_db['hostid'], 'key_' => $item_db['key_']), 'webitems' => 1, 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
                     $current_item = API::Item()->get($options);
                     $current_item = reset($current_item);
                     if (!$current_item && empty($rules['items']['createMissing'])) {
                         info(_s('Item "%1$s" skipped - user rule.', $item_db['key_']));
                         continue;
                         // break if not update updateExisting
                     }
                     if ($current_item && empty($rules['items']['updateExisting'])) {
                         info(_s('Item "%1$s" skipped - user rule.', $item_db['key_']));
                         continue;
                         // break if not update updateExisting
                     }
                     // ITEM APPLICATIONS {{{
                     $applications = $xpath->query('applications/application', $item);
                     $item_applications = array();
                     $applications_to_add = array();
                     $applicationsIds = array();
                     foreach ($applications as $application) {
                         $application_db = array('name' => $application->nodeValue, 'hostid' => $current_hostid);
                         $current_application = API::Application()->get(array('filter' => $application_db, 'output' => API_OUTPUT_EXTEND));
                         $applicationValue = reset($current_application);
                         if ($current_application) {
                             if (empty($item_applications)) {
                                 $item_applications = $current_application;
                                 $applicationsIds[] = $applicationValue['applicationid'];
                             } else {
                                 if (!in_array($applicationValue['applicationid'], $applicationsIds)) {
                                     $item_applications = array_merge($item_applications, $current_application);
                                     $applicationsIds[] = $applicationValue['applicationid'];
                                 }
                             }
                         } else {
                             $applications_to_add[] = $application_db;
                         }
                     }
                     if (!empty($applications_to_add)) {
                         $result = API::Application()->create($applications_to_add);
                         if (!$result) {
                             throw new Exception();
                         }
                         $options = array('applicationids' => $result['applicationids'], 'output' => API_OUTPUT_EXTEND);
                         $new_applications = API::Application()->get($options);
                         $item_applications = array_merge($item_applications, $new_applications);
                     }
                     // }}} ITEM APPLICATIONS
                     if ($current_item && !empty($rules['items']['updateExisting'])) {
                         $item_db['itemid'] = $current_item['itemid'];
                         $result = API::Item()->update($item_db);
                         if (!$result) {
                             throw new Exception();
                         }
                         $options = array('itemids' => $result['itemids'], 'webitems' => 1, 'output' => API_OUTPUT_EXTEND);
                         $current_item = API::Item()->get($options);
                     }
                     if (!$current_item && !empty($rules['items']['createMissing'])) {
                         $result = API::Item()->create($item_db);
                         if (!$result) {
                             throw new Exception();
                         }
                         $options = array('itemids' => $result['itemids'], 'webitems' => 1, 'output' => API_OUTPUT_EXTEND);
                         $current_item = API::Item()->get($options);
                     }
                     if (!empty($item_applications)) {
                         $r = API::Application()->massAdd(array('applications' => $item_applications, 'items' => $current_item));
                         if ($r === false) {
                             throw new Exception();
                         }
                     }
                 }
             }
             // }}} ITEMS
             // TRIGGERS {{{
             if (!empty($rules['triggers']['updateExisting']) || !empty($rules['triggers']['createMissing'])) {
                 $triggers = $xpath->query('triggers/trigger', $host);
                 $triggers_to_add = array();
                 $triggers_to_upd = array();
                 foreach ($triggers as $trigger) {
                     $trigger_db = self::mapXML2arr($trigger, XML_TAG_TRIGGER);
                     if ($old_version_input) {
                         $expressionPart = explode(':', $trigger_db['expression']);
                         $keyName = explode(',', $expressionPart[1], 2);
                         if (count($keyName) == 2) {
                             $keyValue = explode('.', $keyName[1], 2);
                             $key = $keyName[0] . "," . $keyValue[0];
                             if (in_array($keyName[0], self::$oldKeys) || in_array($keyName[0], self::$oldKeysPref)) {
                                 $trigger_db['expression'] = str_replace($key, self::convertOldSimpleKey($key), $trigger_db['expression']);
                             }
                         }
                     }
                     // {HOSTNAME} is here for backward compatibility
                     $trigger_db['expression'] = str_replace('{{HOSTNAME}:', '{' . $host_db['host'] . ':', $trigger_db['expression']);
                     $trigger_db['expression'] = str_replace('{{HOST.HOST}:', '{' . $host_db['host'] . ':', $trigger_db['expression']);
                     $trigger_db['hostid'] = $current_hostid;
                     if ($current_trigger = API::Trigger()->exists($trigger_db)) {
                         $ctriggers = API::Trigger()->get(array('filter' => array('description' => $trigger_db['description']), 'hostids' => $current_hostid, 'output' => API_OUTPUT_EXTEND, 'editable' => 1));
                         $current_trigger = false;
                         foreach ($ctriggers as $ct) {
                             $tmp_exp = explode_exp($ct['expression']);
                             if (strcmp($trigger_db['expression'], $tmp_exp) == 0) {
                                 $current_trigger = $ct;
                                 break;
                             }
                         }
                         if (!$current_trigger) {
                             throw new Exception(_s('No permission for trigger "%s".', $trigger_db['description']));
                         }
                     }
                     unset($trigger_db['hostid']);
                     if (!$current_trigger && empty($rules['triggers']['createMissing'])) {
                         info(_s('Trigger "%1$s" skipped - user rule.', $trigger_db['description']));
                         continue;
                         // break if not update updateExisting
                     }
                     if ($current_trigger && empty($rules['triggers']['updateExisting'])) {
                         info(_s('Trigger "%1$s" skipped - user rule.', $trigger_db['description']));
                         continue;
                         // break if not update updateExisting
                     }
                     if ($current_trigger && !empty($rules['triggers']['updateExisting'])) {
                         $trigger_db['triggerid'] = $current_trigger['triggerid'];
                         $triggers_to_upd[] = $trigger_db;
                     }
                     if (!$current_trigger && !empty($rules['triggers']['createMissing'])) {
                         $triggers_to_add[] = $trigger_db;
                     }
                 }
                 if (!empty($triggers_to_upd)) {
                     $result = API::Trigger()->update($triggers_to_upd);
                     if (!$result) {
                         throw new Exception();
                     }
                     $options = array('triggerids' => $result['triggerids'], 'output' => API_OUTPUT_EXTEND);
                     $r = API::Trigger()->get($options);
                     $triggersForDependencies = array_merge($triggersForDependencies, $r);
                 }
                 if (!empty($triggers_to_add)) {
                     $result = API::Trigger()->create($triggers_to_add);
                     if (!$result) {
                         throw new Exception();
                     }
                     $options = array('triggerids' => $result['triggerids'], 'output' => API_OUTPUT_EXTEND);
                     $r = API::Trigger()->get($options);
                     $triggersForDependencies = array_merge($triggersForDependencies, $r);
                 }
             }
             // }}} TRIGGERS
             // GRAPHS {{{
             if (!empty($rules['graphs']['updateExisting']) || !empty($rules['graphs']['createMissing'])) {
                 $graphs = $xpath->query('graphs/graph', $host);
                 $graphs_to_add = array();
                 $graphs_to_upd = array();
                 foreach ($graphs as $graph) {
                     // GRAPH ITEMS {{{
                     $gitems = $xpath->query('graph_elements/graph_element', $graph);
                     $graph_hostids = array();
                     $graph_items = array();
                     foreach ($gitems as $gitem) {
                         $gitem_db = self::mapXML2arr($gitem, XML_TAG_GRAPH_ELEMENT);
                         $data = explode(':', $gitem_db['host_key_']);
                         $gitem_host = array_shift($data);
                         // {HOSTNAME} is here for backward compatibility
                         $gitem_db['host'] = $gitem_host == '{HOSTNAME}' ? $host_db['host'] : $gitem_host;
                         $gitem_db['host'] = $gitem_host == '{HOST.HOST}' ? $host_db['host'] : $gitem_host;
                         if ($old_version_input) {
                             $data[0] = self::convertOldSimpleKey($data[0]);
                         }
                         $gitem_db['key_'] = implode(':', $data);
                         if ($current_item = API::Item()->exists($gitem_db)) {
                             $current_item = API::Item()->get(array('filter' => array('key_' => $gitem_db['key_']), 'webitems' => 1, 'host' => $gitem_db['host'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1));
                             if (empty($current_item)) {
                                 throw new Exception(_s('No permission for item "%1$s".', $gitem_db['key_']));
                             }
                             $current_item = reset($current_item);
                             $graph_hostids[] = $current_item['hostid'];
                             $gitem_db['itemid'] = $current_item['itemid'];
                             $graph_items[] = $gitem_db;
                         } else {
                             throw new Exception(_s('Item "%1$s" does not exist.', $gitem_db['host_key_']));
                         }
                     }
                     // }}} GRAPH ITEMS
                     $graph_db = self::mapXML2arr($graph, XML_TAG_GRAPH);
                     $graph_db['hostids'] = $graph_hostids;
                     // do we need to show the graph legend, after it is imported?
                     // in 1.8, this setting was present only for pie and exploded graphs
                     // for other graph types we are always showing the legend
                     if ($graph_db['graphtype'] != GRAPH_TYPE_PIE && $graph_db['graphtype'] != GRAPH_TYPE_EXPLODED) {
                         $graph_db['show_legend'] = 1;
                     }
                     $current_graph = API::Graph()->exists($graph_db);
                     if ($current_graph) {
                         $current_graph = API::Graph()->get(array('filter' => array('name' => $graph_db['name']), 'hostids' => $graph_db['hostids'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1));
                         if (empty($current_graph)) {
                             throw new Exception(_s('No permission for graph "%1$s".', $graph_db['name']));
                         }
                         $current_graph = reset($current_graph);
                     }
                     if (!$current_graph && empty($rules['graphs']['createMissing'])) {
                         info(_s('Graph "%1$s" skipped - user rule.', $graph_db['name']));
                         continue;
                         // break if not update updateExisting
                     }
                     if ($current_graph && empty($rules['graphs']['updateExisting'])) {
                         info(_s('Graph "%1$s" skipped - user rule.', $graph_db['name']));
                         continue;
                         // break if not update updateExisting
                     }
                     if (!isset($graph_db['ymin_type'])) {
                         throw new Exception(_s('No "ymin_type" field for graph "%s".', $graph_db['name']));
                     }
                     if (!isset($graph_db['ymax_type'])) {
                         throw new Exception(_s('No "ymax_type" field for graph "%s".', $graph_db['name']));
                     }
                     if ($graph_db['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
                         $item_data = explode(':', $graph_db['ymin_item_key'], 2);
                         if (count($item_data) < 2) {
                             throw new Exception(_s('Incorrect y min item for graph "%1$s".', $graph_db['name']));
                         }
                         if (!($item = get_item_by_key($item_data[1], $item_data[0]))) {
                             throw new Exception(_s('Missing item "%1$s" for host "%2$s".', $graph_db['ymin_item_key'], $host_db['host']));
                         }
                         $graph_db['ymin_itemid'] = $item['itemid'];
                     }
                     if ($graph_db['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
                         $item_data = explode(':', $graph_db['ymax_item_key'], 2);
                         if (count($item_data) < 2) {
                             throw new Exception(_s('Incorrect y max item for graph "%1$s".', $graph_db['name']));
                         }
                         if (!($item = get_item_by_key($item_data[1], $item_data[0]))) {
                             throw new Exception(_s('Missing item "%1$s" for host "%2$s".', $graph_db['ymax_item_key'], $host_db['host']));
                         }
                         $graph_db['ymax_itemid'] = $item['itemid'];
                     }
                     $graph_db['gitems'] = $graph_items;
                     if ($current_graph) {
                         $graph_db['graphid'] = $current_graph['graphid'];
                         $graphs_to_upd[] = $graph_db;
                     } else {
                         $graphs_to_add[] = $graph_db;
                     }
                 }
                 if (!empty($graphs_to_add)) {
                     $r = API::Graph()->create($graphs_to_add);
                     if ($r === false) {
                         throw new Exception();
                     }
                 }
                 if (!empty($graphs_to_upd)) {
                     $r = API::Graph()->update($graphs_to_upd);
                     if ($r === false) {
                         throw new Exception();
                     }
                 }
             }
             // SCREENS
             if (!empty($rules['screens']['updateExisting']) || !empty($rules['screens']['createMissing'])) {
                 $screens_node = $xpath->query('screens', $host);
                 if ($screens_node->length > 0) {
                     $importScreens = self::XMLtoArray($screens_node->item(0));
                     foreach ($importScreens as $screen) {
                         $current_screen = API::TemplateScreen()->get(array('filter' => array('name' => $screen['name']), 'templateids' => $current_hostid, 'output' => API_OUTPUT_EXTEND, 'editable' => 1));
                         $current_screen = reset($current_screen);
                         if (!$current_screen && empty($rules['screens']['createMissing'])) {
                             info(_s('Screen "%1$s" skipped - user rule.', $screen['name']));
                             continue;
                         }
                         if ($current_screen && empty($rules['screens']['updateExisting'])) {
                             info(_s('Screen "%1$s" skipped - user rule.', $screen['name']));
                             continue;
                         }
                         if (isset($screen['screenitems'])) {
                             foreach ($screen['screenitems'] as &$screenitem) {
                                 $nodeCaption = isset($screenitem['resourceid']['node']) ? $screenitem['resourceid']['node'] . ':' : '';
                                 if (!isset($screenitem['resourceid'])) {
                                     $screenitem['resourceid'] = 0;
                                 }
                                 if (is_array($screenitem['resourceid'])) {
                                     switch ($screenitem['resourcetype']) {
                                         case SCREEN_RESOURCE_GRAPH:
                                             $db_graphs = API::Graph()->getObjects($screenitem['resourceid']);
                                             if (empty($db_graphs)) {
                                                 $error = _s('Cannot find graph "%1$s" used in screen "%2$s".', $nodeCaption . $screenitem['resourceid']['host'] . ':' . $screenitem['resourceid']['name'], $screen['name']);
                                                 throw new Exception($error);
                                             }
                                             $tmp = reset($db_graphs);
                                             $screenitem['resourceid'] = $tmp['graphid'];
                                             break;
                                         case SCREEN_RESOURCE_SIMPLE_GRAPH:
                                         case SCREEN_RESOURCE_PLAIN_TEXT:
                                             $db_items = API::Item()->getObjects($screenitem['resourceid']);
                                             if (empty($db_items)) {
                                                 $error = _s('Cannot find item "%1$s" used in screen "%2$s".', $nodeCaption . $screenitem['resourceid']['host'] . ':' . $screenitem['resourceid']['key_'], $screen['name']);
                                                 throw new Exception($error);
                                             }
                                             $tmp = reset($db_items);
                                             $screenitem['resourceid'] = $tmp['itemid'];
                                             break;
                                         default:
                                             $screenitem['resourceid'] = 0;
                                             break;
                                     }
                                 }
                             }
                         }
                         $screen['templateid'] = $current_hostid;
                         if ($current_screen) {
                             $screen['screenid'] = $current_screen['screenid'];
                             $result = API::TemplateScreen()->update($screen);
                             if (!$result) {
                                 throw new Exception(_('Cannot update screen.'));
                             }
                             info('[' . $current_hostname . '] ' . _s('Screen "%1$s" updated.', $screen['name']));
                         } else {
                             $result = API::TemplateScreen()->create($screen);
                             if (!$result) {
                                 throw new Exception(_('Cannot create screen.'));
                             }
                             info('[' . $current_hostname . '] ' . _s('Screen "%1$s" added.', $screen['name']));
                         }
                     }
                 }
             }
         }
         // DEPENDENCIES
         $dependencies = $xpath->query('dependencies/dependency');
         if ($dependencies->length > 0) {
             $triggersForDependencies = zbx_objectValues($triggersForDependencies, 'triggerid');
             $triggersForDependencies = array_flip($triggersForDependencies);
             $newDependencies = array();
             foreach ($dependencies as $dependency) {
                 $triggerDescription = $dependency->getAttribute('description');
                 $currentTrigger = get_trigger_by_description($triggerDescription);
                 if ($currentTrigger && isset($triggersForDependencies[$currentTrigger['triggerid']])) {
                     $dependsOnList = $xpath->query('depends', $dependency);
                     foreach ($dependsOnList as $dependsOn) {
                         $depTrigger = get_trigger_by_description($dependsOn->nodeValue);
                         if ($depTrigger['triggerid']) {
                             $newDependencies[] = array('triggerid' => $currentTrigger['triggerid'], 'dependsOnTriggerid' => $depTrigger['triggerid']);
                         }
                     }
                 }
             }
             if ($newDependencies) {
                 API::Trigger()->addDependencies($newDependencies);
             }
         }
     }
 }
 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;
 }
 /**
  * Deletes applications from DB that are missing in XML.
  *
  * @return null
  */
 protected function deleteMissingApplications()
 {
     if (!$this->options['applications']['deleteMissing']) {
         return;
     }
     $processedHostIds = $this->importedObjectContainer->getHostIds();
     $processedTemplateIds = $this->importedObjectContainer->getTemplateIds();
     $processedHostIds = array_merge($processedHostIds, $processedTemplateIds);
     // no hosts or templates have been processed
     if (!$processedHostIds) {
         return;
     }
     $applicationIdsXML = array();
     $allApplications = $this->getFormattedApplications();
     if ($allApplications) {
         foreach ($allApplications as $host => $applications) {
             $hostId = $this->referencer->resolveHostOrTemplate($host);
             foreach ($applications as $application) {
                 $applicationId = $this->referencer->resolveApplication($hostId, $application['name']);
                 if ($applicationId) {
                     $applicationIdsXML[$applicationId] = $applicationId;
                 }
             }
         }
     }
     $dbApplicationIds = API::Application()->get(array('output' => array('applicationid'), 'hostids' => $processedHostIds, 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false));
     $applicationsToDelete = array_diff_key($dbApplicationIds, $applicationIdsXML);
     if ($applicationsToDelete) {
         API::Application()->delete(array_keys($applicationsToDelete));
     }
     // refresh applications because templated ones can be inherited to host and used in items
     $this->referencer->refreshApplications();
 }
Beispiel #20
0
             } else {
                 $applications = array();
             }
         }
     }
     $item = array('interfaceid' => getRequest('interfaceid'), 'description' => getRequest('description'), 'delay' => getRequest('delay'), 'history' => getRequest('history'), 'status' => getRequest('status'), 'type' => getRequest('type'), 'snmp_community' => getRequest('snmp_community'), 'snmp_oid' => getRequest('snmp_oid'), 'value_type' => getRequest('value_type'), 'trapper_hosts' => getRequest('trapper_hosts'), 'port' => getRequest('port'), 'units' => getRequest('units'), 'multiplier' => $multiplier, 'delta' => getRequest('delta'), 'snmpv3_contextname' => getRequest('snmpv3_contextname'), 'snmpv3_securityname' => getRequest('snmpv3_securityname'), 'snmpv3_securitylevel' => getRequest('snmpv3_securitylevel'), 'snmpv3_authprotocol' => getRequest('snmpv3_authprotocol'), 'snmpv3_authpassphrase' => getRequest('snmpv3_authpassphrase'), 'snmpv3_privprotocol' => getRequest('snmpv3_privprotocol'), 'snmpv3_privpassphrase' => getRequest('snmpv3_privpassphrase'), 'formula' => $formula, 'trends' => getRequest('trends'), 'logtimefmt' => getRequest('logtimefmt'), 'valuemapid' => getRequest('valuemapid'), 'delay_flex' => $db_delay_flex, 'authtype' => getRequest('authtype'), 'username' => getRequest('username'), 'password' => getRequest('password'), 'publickey' => getRequest('publickey'), 'privatekey' => getRequest('privatekey'), 'ipmi_sensor' => getRequest('ipmi_sensor'), 'applications' => $applications, 'data_type' => getRequest('data_type'));
     // add applications
     if (!empty($existApplication) && (!isset($visible['applications']) || !isset($_REQUEST['applications']))) {
         foreach ($existApplication as $linkApp) {
             $linkApplications[] = array('applicationid' => $linkApp);
         }
         foreach (getRequest('group_itemid') as $linkItem) {
             $linkItems[] = array('itemid' => $linkItem);
         }
         $linkApp = array('applications' => $linkApplications, 'items' => $linkItems);
         API::Application()->massAdd($linkApp);
     }
     foreach ($item as $key => $field) {
         if ($field === null) {
             unset($item[$key]);
         }
     }
     foreach ($_REQUEST['group_itemid'] as $id) {
         $item['itemid'] = $id;
         if (!($result = API::Item()->update($item))) {
             break;
         }
     }
 } catch (Exception $e) {
     $result = false;
 }
    if ($dbHost['proxy_hostid']) {
        $proxy = API::Proxy()->get(array('output' => array('host', 'proxyid'), 'proxyids' => $dbHost['proxy_hostid'], 'limit' => 1));
        $proxy = reset($proxy);
        $proxyControl = new CTextBox('proxy_host', $proxy['host'], null, true);
    } else {
        $proxyControl = new CTextBox('proxy_host', _('(no proxy)'), null, true);
    }
}
$hostList->addRow(_('Monitored by proxy'), $proxyControl);
$cmbStatus = new CComboBox('status', $status);
$cmbStatus->addItem(HOST_STATUS_MONITORED, _('Monitored'));
$cmbStatus->addItem(HOST_STATUS_NOT_MONITORED, _('Not monitored'));
$hostList->addRow(_('Status'), $cmbStatus);
if ($_REQUEST['form'] == 'full_clone') {
    // host applications
    $hostApps = API::Application()->get(array('hostids' => $_REQUEST['hostid'], 'inherited' => false, 'output' => array('name'), 'preservekeys' => true));
    if (!empty($hostApps)) {
        $applicationsList = array();
        foreach ($hostApps as $hostAppId => $hostApp) {
            $applicationsList[$hostAppId] = $hostApp['name'];
        }
        order_result($applicationsList);
        $listBox = new CListBox('applications', null, 8);
        $listBox->setAttribute('disabled', 'disabled');
        $listBox->addItems($applicationsList);
        $hostList->addRow(_('Applications'), $listBox);
    }
    // host items
    $hostItems = API::Item()->get(array('hostids' => $_REQUEST['hostid'], 'inherited' => false, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL), 'output' => array('itemid', 'key_', 'name')));
    if (!empty($hostItems)) {
        $itemsList = array();
Beispiel #22
0
    unset($host);
    if (!$singleHostSelected) {
        $sortFields = $sortField === 'host' ? array(array('field' => 'name', 'order' => $sortOrder)) : array('name');
        CArrayHelper::sort($hosts, $sortFields);
    }
    $hostIds = array_keys($hosts);
    $applications = null;
    // if an application filter is set, fetch the applications and then use them to filter items
    if ($filter['application'] !== '') {
        $applications = API::Application()->get(array('output' => API_OUTPUT_EXTEND, 'hostids' => $hostIds, 'search' => array('name' => $filter['application']), 'preservekeys' => true));
    }
    $items = API::Item()->get(array('hostids' => array_keys($hosts), 'output' => array('itemid', 'name', 'type', 'value_type', 'units', 'hostid', 'state', 'valuemapid', 'status', 'error', 'trends', 'history', 'delay', 'key_', 'flags'), 'selectApplications' => array('applicationid'), 'selectItemDiscovery' => array('ts_delete'), 'applicationids' => $applications !== null ? zbx_objectValues($applications, 'applicationid') : null, 'webitems' => true, 'filter' => array('status' => array(ITEM_STATUS_ACTIVE)), 'preservekeys' => true));
    // if the applications haven't been loaded when filtering, load them based on the retrieved items to avoid
    // fetching applications from hosts that may not be displayed
    if ($applications === null) {
        $applications = API::Application()->get(array('output' => API_OUTPUT_EXTEND, 'hostids' => array_keys(array_flip(zbx_objectValues($items, 'hostid'))), 'search' => array('name' => $filter['application']), 'preservekeys' => true));
    }
}
if ($items) {
    // macros
    $items = CMacrosResolverHelper::resolveItemKeys($items);
    $items = CMacrosResolverHelper::resolveItemNames($items);
    // filter items by name
    foreach ($items as $key => $item) {
        if ($filter['select'] !== '') {
            $haystack = mb_strtolower($item['name_expanded']);
            $needle = mb_strtolower($filter['select']);
            if (mb_strpos($haystack, $needle) === false) {
                unset($items[$key]);
            }
        }
 /**
  * Get Itemprototype data
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     // allowed columns for sorting
     $sortColumns = array('itemid', 'name', 'key_', 'delay', 'history', 'trends', 'type', 'status');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     $sqlParts = array('select' => array('items' => 'i.itemid'), 'from' => array('items' => 'items i'), 'where' => array('i.flags=' . ZBX_FLAG_DISCOVERY_CHILD), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'itemids' => null, 'discoveryids' => null, 'graphids' => null, 'triggerids' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectHosts' => null, 'selectApplications' => null, 'selectTriggers' => null, 'selectGraphs' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($defOptions, $options);
     if (is_array($options['output'])) {
         unset($sqlParts['select']['items']);
         $dbTable = DB::getSchema('items');
         $sqlParts['select']['itemid'] = 'i.itemid';
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 'i.' . $field;
             }
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $userGroups = getUserGroupsByUserId($userid);
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE i.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>=' . $permission . ')';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sqlParts['select']['hostid'] = 'i.hostid';
         }
         $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['hostids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.hostid';
         }
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         $sqlParts['where']['itemid'] = dbConditionInt('i.itemid', $options['itemids']);
     }
     // discoveryids
     if (!is_null($options['discoveryids'])) {
         zbx_value2array($options['discoveryids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['discoveryid'] = 'id.parent_itemid';
         }
         $sqlParts['from']['item_discovery'] = 'item_discovery id';
         $sqlParts['where'][] = dbConditionInt('id.parent_itemid', $options['discoveryids']);
         $sqlParts['where']['idi'] = 'i.itemid=id.itemid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['id'] = 'id.parent_itemid';
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['triggerid'] = 'f.triggerid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['where'][] = dbConditionInt('f.triggerid', $options['triggerids']);
         $sqlParts['where']['if'] = 'i.itemid=f.itemid';
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['graphid'] = 'gi.graphid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['where'][] = dbConditionInt('gi.graphid', $options['graphids']);
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 'i.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 'i.templateid IS NULL';
         }
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // monitored
     if (!is_null($options['monitored'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['monitored']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_MONITORED;
             $sqlParts['where'][] = 'i.status=' . ITEM_STATUS_ACTIVE;
         } else {
             $sqlParts['where'][] = '(h.status<>' . HOST_STATUS_MONITORED . ' OR i.status<>' . ITEM_STATUS_ACTIVE . ')';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('items i', $options, $sqlParts);
     }
     // --- FILTER ---
     if (is_array($options['filter'])) {
         $this->dbFilter('items i', $options, $sqlParts);
         if (isset($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sqlParts['from']['hosts'] = 'hosts h';
             $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
             $sqlParts['where']['h'] = dbConditionString('h.host', $options['filter']['host']);
         }
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['items'] = 'i.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('count(DISTINCT i.itemid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sqlParts['group'] as $key => $fields) {
                 $sqlParts['select'][$key] = $fields;
             }
         }
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 'i');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     //----------
     $itemids = array();
     $sqlParts['select'] = array_unique($sqlParts['select']);
     $sqlParts['from'] = array_unique($sqlParts['from']);
     $sqlParts['where'] = array_unique($sqlParts['where']);
     $sqlParts['group'] = array_unique($sqlParts['group']);
     $sqlParts['order'] = array_unique($sqlParts['order']);
     $sqlSelect = '';
     $sqlFrom = '';
     $sqlWhere = '';
     $sqlGroup = '';
     $sqlOrder = '';
     if (!empty($sqlParts['select'])) {
         $sqlSelect .= implode(',', $sqlParts['select']);
     }
     if (!empty($sqlParts['from'])) {
         $sqlFrom .= implode(',', $sqlParts['from']);
     }
     if (!empty($sqlParts['where'])) {
         $sqlWhere .= ' AND ' . implode(' AND ', $sqlParts['where']);
     }
     if (!empty($sqlParts['group'])) {
         $sqlWhere .= ' GROUP BY ' . implode(',', $sqlParts['group']);
     }
     if (!empty($sqlParts['order'])) {
         $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']);
     }
     $sqlLimit = $sqlParts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . DBin_node('i.itemid', $nodeids) . $sqlWhere . $sqlGroup . $sqlOrder;
     //SDI($sql);
     $res = DBselect($sql, $sqlLimit);
     while ($item = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $item;
             } else {
                 $result = $item['rowscount'];
             }
         } else {
             $itemids[$item['itemid']] = $item['itemid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$item['itemid']] = array('itemid' => $item['itemid']);
             } else {
                 if (!isset($result[$item['itemid']])) {
                     $result[$item['itemid']] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$item['itemid']]['hosts'])) {
                     $result[$item['itemid']]['hosts'] = array();
                 }
                 if (!is_null($options['selectApplications']) && !isset($result[$item['itemid']]['applications'])) {
                     $result[$item['itemid']]['applications'] = array();
                 }
                 if (!is_null($options['selectTriggers']) && !isset($result[$item['itemid']]['triggers'])) {
                     $result[$item['itemid']]['triggers'] = array();
                 }
                 if (!is_null($options['selectGraphs']) && !isset($result[$item['itemid']]['graphs'])) {
                     $result[$item['itemid']]['graphs'] = array();
                 }
                 // hostids
                 if (isset($item['hostid']) && is_null($options['selectHosts'])) {
                     if (!isset($result[$item['itemid']]['hosts'])) {
                         $result[$item['itemid']]['hosts'] = array();
                     }
                     $result[$item['itemid']]['hosts'][] = array('hostid' => $item['hostid']);
                 }
                 // triggerids
                 if (isset($item['triggerid']) && is_null($options['selectTriggers'])) {
                     if (!isset($result[$item['itemid']]['triggers'])) {
                         $result[$item['itemid']]['triggers'] = array();
                     }
                     $result[$item['itemid']]['triggers'][] = array('triggerid' => $item['triggerid']);
                     unset($item['triggerid']);
                 }
                 // graphids
                 if (isset($item['graphid']) && is_null($options['selectGraphs'])) {
                     if (!isset($result[$item['itemid']]['graphs'])) {
                         $result[$item['itemid']]['graphs'] = array();
                     }
                     $result[$item['itemid']]['graphs'][] = array('graphid' => $item['graphid']);
                     unset($item['graphid']);
                 }
                 // discoveryids
                 if (isset($item['discoveryids'])) {
                     if (!isset($result[$item['itemid']]['discovery'])) {
                         $result[$item['itemid']]['discovery'] = array();
                     }
                     $result[$item['itemid']]['discovery'][] = array('ruleid' => $item['item_parentid']);
                     unset($item['item_parentid']);
                 }
                 $result[$item['itemid']] += $item;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     // Adding Objects
     // Adding hosts
     if (!is_null($options['selectHosts'])) {
         if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) {
             $objParams = array('nodeids' => $nodeids, 'itemids' => $itemids, 'templated_hosts' => 1, 'output' => $options['selectHosts'], 'nopermissions' => 1, 'preservekeys' => 1);
             $hosts = API::Host()->get($objParams);
             foreach ($hosts as $host) {
                 $hitems = $host['items'];
                 unset($host['items']);
                 foreach ($hitems as $inum => $item) {
                     $result[$item['itemid']]['hosts'][] = $host;
                 }
             }
             $templates = API::Template()->get($objParams);
             foreach ($templates as $template) {
                 $titems = $template['items'];
                 unset($template['items']);
                 foreach ($titems as $inum => $item) {
                     $result[$item['itemid']]['hosts'][] = $template;
                 }
             }
         }
     }
     // Adding triggers
     if (!is_null($options['selectTriggers'])) {
         $objParams = array('nodeids' => $nodeids, 'itemids' => $itemids, 'preservekeys' => true);
         if (in_array($options['selectTriggers'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectTriggers'];
             $triggers = API::TriggerPrototype()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($triggers, 'description');
             }
             $count = array();
             foreach ($triggers as $triggerid => $trigger) {
                 unset($triggers[$triggerid]['items']);
                 foreach ($trigger['items'] as $item) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$item['itemid']])) {
                             $count[$item['itemid']] = 0;
                         }
                         $count[$item['itemid']]++;
                         if ($count[$item['itemid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$item['itemid']]['triggers'][] =& $triggers[$triggerid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectTriggers']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $triggers = API::TriggerPrototype()->get($objParams);
             $triggers = zbx_toHash($triggers, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($triggers[$itemid])) {
                     $result[$itemid]['triggers'] = $triggers[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['triggers'] = 0;
                 }
             }
         }
     }
     // Adding applications
     if (!is_null($options['selectApplications']) && str_in_array($options['selectApplications'], $subselectsAllowedOutputs)) {
         $objParams = array('nodeids' => $nodeids, 'output' => $options['selectApplications'], 'itemids' => $itemids, 'preservekeys' => 1);
         $applications = API::Application()->get($objParams);
         foreach ($applications as $applicationid => $application) {
             $aitems = $application['items'];
             unset($application['items']);
             foreach ($aitems as $inum => $item) {
                 $result[$item['itemid']]['applications'][] = $application;
             }
         }
     }
     // Adding graphs
     if (!is_null($options['selectGraphs'])) {
         $objParams = array('nodeids' => $nodeids, 'itemids' => $itemids, 'preservekeys' => true);
         if (in_array($options['selectGraphs'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectGraphs'];
             $graphs = API::GraphPrototype()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($graphs, 'name');
             }
             $count = array();
             foreach ($graphs as $graphid => $graph) {
                 unset($graphs[$graphid]['items']);
                 foreach ($graph['items'] as $item) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$item['itemid']])) {
                             $count[$item['itemid']] = 0;
                         }
                         $count[$item['itemid']]++;
                         if ($count[$item['itemid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$item['itemid']]['graphs'][] =& $graphs[$graphid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectGraphs']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $graphs = API::GraphPrototype()->get($objParams);
             $graphs = zbx_toHash($graphs, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($graphs[$itemid])) {
                     $result[$itemid]['graphs'] = $graphs[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['graphs'] = 0;
                 }
             }
         }
     }
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Beispiel #24
0
function copyApplications($srcHostId, $dstHostId)
{
    $apps_to_clone = API::Application()->get(array('hostids' => $srcHostId, 'output' => API_OUTPUT_EXTEND, 'inherited' => false));
    if (empty($apps_to_clone)) {
        return true;
    }
    foreach ($apps_to_clone as &$app) {
        $app['hostid'] = $dstHostId;
        unset($app['applicationid'], $app['templateid']);
    }
    return API::Application()->create($apps_to_clone);
}
Beispiel #25
0
/**
 * Creates and returns the trigger overview table for the given hosts.
 *
 * @param array  $hostIds
 * @param string $application	name of application to filter
 * @param string $pageFile		the page where the element is displayed
 * @param int    $viewMode		table display style: either hosts on top, or host on the left side
 * @param string $screenId		the ID of the screen, that contains the trigger overview table
 *
 * @return CTableInfo
 */
function getTriggersOverview($hostIds, $application, $pageFile, $viewMode = null, $screenId = null)
{
    if (is_null($viewMode)) {
        $viewMode = CProfile::get('web.overview.view.style', STYLE_TOP);
    }
    // get application ids
    $applicationIds = null;
    if ($application !== '') {
        $dbApplications = API::Application()->get(array('hostids' => $hostIds, 'filter' => array('name' => $application), 'output' => array('applicationid')));
        $applicationIds = zbx_objectValues($dbApplications, 'applicationid');
        $hostIds = null;
    }
    // get triggers
    $dbTriggers = API::Trigger()->get(array('hostids' => $hostIds, 'applicationids' => $applicationIds, 'monitored' => true, 'skipDependent' => true, 'output' => API_OUTPUT_EXTEND, 'selectHosts' => array('hostid', 'name'), 'sortfield' => 'description'));
    // get hosts
    $hostIds = array();
    foreach ($dbTriggers as $trigger) {
        $host = reset($trigger['hosts']);
        $hostIds[$host['hostid']] = $host['hostid'];
    }
    $hosts = API::Host()->get(array('output' => array('name', 'hostid', 'status'), 'hostids' => $hostIds, 'preservekeys' => true, 'selectScreens' => $viewMode == STYLE_LEFT ? API_OUTPUT_COUNT : null));
    $triggers = array();
    $hostNames = array();
    foreach ($dbTriggers as $trigger) {
        $host = reset($trigger['hosts']);
        $host['name'] = get_node_name_by_elid($host['hostid'], null, NAME_DELIMITER) . $host['name'];
        $trigger['description'] = CMacrosResolverHelper::resolveTriggerReference($trigger['expression'], $trigger['description']);
        $hostNames[$host['hostid']] = $host['name'];
        // a little tricky check for attempt to overwrite active trigger (value=1) with
        // inactive or active trigger with lower priority.
        if (!isset($triggers[$trigger['description']][$host['name']]) || ($triggers[$trigger['description']][$host['name']]['value'] == TRIGGER_VALUE_FALSE && $trigger['value'] == TRIGGER_VALUE_TRUE || ($triggers[$trigger['description']][$host['name']]['value'] == TRIGGER_VALUE_FALSE || $trigger['value'] == TRIGGER_VALUE_TRUE) && $trigger['priority'] > $triggers[$trigger['description']][$host['name']]['priority'])) {
            $triggers[$trigger['description']][$host['name']] = array('hostid' => $host['hostid'], 'triggerid' => $trigger['triggerid'], 'value' => $trigger['value'], 'lastchange' => $trigger['lastchange'], 'priority' => $trigger['priority'], 'flags' => $trigger['flags'], 'url' => $trigger['url'], 'hosts' => array($host));
        }
    }
    $triggerTable = new CTableInfo(_('No triggers found.'));
    if (empty($hostNames)) {
        return $triggerTable;
    }
    $triggerTable->makeVerticalRotation();
    order_result($hostNames);
    if ($viewMode == STYLE_TOP) {
        // header
        $header = array(new CCol(_('Triggers'), 'center'));
        foreach ($hostNames as $hostName) {
            $header[] = new CCol($hostName, 'vertical_rotation');
        }
        $triggerTable->setHeader($header, 'vertical_header');
        // data
        foreach ($triggers as $description => $triggerHosts) {
            $columns = array(nbsp($description));
            foreach ($hostNames as $hostName) {
                $columns[] = getTriggerOverviewCells(isset($triggerHosts[$hostName]) ? $triggerHosts[$hostName] : null, $pageFile, $screenId);
            }
            $triggerTable->addRow($columns);
        }
    } else {
        // header
        $header = array(new CCol(_('Host'), 'center'));
        foreach ($triggers as $description => $triggerHosts) {
            $header[] = new CCol($description, 'vertical_rotation');
        }
        $triggerTable->setHeader($header, 'vertical_header');
        // data
        $scripts = API::Script()->getScriptsByHosts(zbx_objectValues($hosts, 'hostid'));
        foreach ($hostNames as $hostId => $hostName) {
            $name = new CSpan($hostName, 'link_menu');
            $name->setMenuPopup(getMenuPopupHost($hosts[$hostId], $scripts[$hostId]));
            $columns = array($name);
            foreach ($triggers as $triggerHosts) {
                $columns[] = getTriggerOverviewCells(isset($triggerHosts[$hostName]) ? $triggerHosts[$hostName] : null, $pageFile, $screenId);
            }
            $triggerTable->addRow($columns);
        }
    }
    return $triggerTable;
}
 /**
  * Get Hosts for export from database.
  *
  * @param array $hostIds
  */
 protected function gatherHosts(array $hostIds)
 {
     $hosts = API::Host()->get(array('hostids' => $hostIds, 'output' => array('proxy_hostid', 'host', 'status', 'ipmi_authtype', 'ipmi_privilege', 'ipmi_username', 'ipmi_password', 'name'), 'selectInventory' => true, 'selectInterfaces' => array('interfaceid', 'main', 'type', 'useip', 'ip', 'dns', 'port'), 'selectMacros' => API_OUTPUT_EXTEND, 'selectGroups' => API_OUTPUT_EXTEND, 'selectParentTemplates' => API_OUTPUT_EXTEND, 'preservekeys' => true));
     // merge host groups with all groups
     $hostGroups = array();
     foreach ($hosts as &$host) {
         $hostGroups += zbx_toHash($host['groups'], 'groupid');
         $host['applications'] = array();
         $host['discoveryRules'] = array();
         $host['items'] = array();
     }
     unset($host);
     $this->data['groups'] += $hostGroups;
     // applications
     $applications = API::Application()->get(array('hostids' => $hostIds, 'output' => API_OUTPUT_EXTEND, 'inherited' => false, 'preservekeys' => true));
     foreach ($applications as $application) {
         if (!isset($hosts[$application['hostid']]['applications'])) {
             $hosts[$application['hostid']]['applications'] = array();
         }
         $hosts[$application['hostid']]['applications'][] = $application;
     }
     // proxies
     $dbProxies = DBselect('SELECT h.hostid,h.host' . ' FROM hosts h' . ' WHERE ' . dbConditionInt('h.hostid', zbx_objectValues($hosts, 'proxy_hostid')));
     $proxies = array();
     while ($proxy = DBfetch($dbProxies)) {
         $proxies[$proxy['hostid']] = $proxy['host'];
     }
     foreach ($hosts as &$host) {
         $host['proxy'] = $host['proxy_hostid'] ? array('name' => $proxies[$host['proxy_hostid']]) : null;
     }
     unset($host);
     $this->data['hosts'] = $hosts;
     $this->gatherHostItems($hostIds);
     $this->gatherHostDiscoveryRules($hostIds);
 }
    }
    show_messages($result, _('Item prototype deleted'), _('Cannot delete item prototype'));
    unset($_REQUEST['itemid'], $_REQUEST['form']);
} elseif (isset($_REQUEST['clone']) && isset($_REQUEST['itemid'])) {
    unset($_REQUEST['itemid']);
    $_REQUEST['form'] = 'clone';
} elseif (hasRequest('add') || hasRequest('update')) {
    $applications = getRequest('applications', []);
    $application = reset($applications);
    if ($application == 0) {
        array_shift($applications);
    }
    $result = true;
    DBstart();
    if (!zbx_empty($_REQUEST['new_application'])) {
        $new_appid = API::Application()->create(['name' => $_REQUEST['new_application'], 'hostid' => $discoveryRule['hostid']]);
        if ($new_appid) {
            $new_appid = reset($new_appid['applicationids']);
            $applications[$new_appid] = $new_appid;
        } else {
            $result = false;
        }
    }
    /*
     * Intially validate "delay_flex" field one by one to make sure it does not have interval separator ";".
     * Skip empty fields and convert "delay_flex" array to string glued with ";" which is later validated through API.
     */
    $delay_flex = '';
    $intervals = [];
    if (getRequest('delay_flex')) {
        foreach (getRequest('delay_flex') as $interval) {
 /**
  * Delete Template
  *
  * @param array $templateids
  * @param array $templateids['templateids']
  * @return boolean
  */
 public function delete($templateids)
 {
     if (empty($templateids)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.'));
     }
     $templateids = zbx_toArray($templateids);
     $options = array('templateids' => $templateids, 'editable' => true, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true);
     $delTemplates = $this->get($options);
     foreach ($templateids as $templateid) {
         if (!isset($delTemplates[$templateid])) {
             self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
         }
     }
     API::Template()->unlink($templateids, null, true);
     // delete the discovery rules first
     $delRules = API::DiscoveryRule()->get(array('hostids' => $templateids, 'nopermissions' => true, 'preservekeys' => true));
     if ($delRules) {
         API::DiscoveryRule()->delete(array_keys($delRules), true);
     }
     // delete the items
     $delItems = API::Item()->get(array('templateids' => $templateids, 'output' => API_OUTPUT_SHORTEN, 'nopermissions' => true, 'preservekeys' => true));
     if ($delItems) {
         API::Item()->delete(array_keys($delItems), true);
     }
     // delete screen items
     DBexecute('DELETE FROM screens_items WHERE ' . dbConditionInt('resourceid', $templateids) . ' AND resourcetype=' . SCREEN_RESOURCE_HOST_TRIGGERS);
     // delete host from maps
     if (!empty($templateids)) {
         DB::delete('sysmaps_elements', array('elementtype' => SYSMAP_ELEMENT_TYPE_HOST, 'elementid' => $templateids));
     }
     // disable actions
     // actions from conditions
     $actionids = array();
     $sql = 'SELECT DISTINCT actionid' . ' FROM conditions' . ' WHERE conditiontype=' . CONDITION_TYPE_HOST_TEMPLATE . ' AND ' . dbConditionString('value', $templateids);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     // actions from operations
     $sql = 'SELECT DISTINCT o.actionid' . ' FROM operations o,optemplate ot' . ' WHERE o.operationid=ot.operationid' . ' AND ' . dbConditionInt('ot.templateid', $templateids);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     if (!empty($actionids)) {
         DB::update('actions', array('values' => array('status' => ACTION_STATUS_DISABLED), 'where' => array('actionid' => $actionids)));
     }
     // delete action conditions
     DB::delete('conditions', array('conditiontype' => CONDITION_TYPE_HOST_TEMPLATE, 'value' => $templateids));
     // delete action operation commands
     $operationids = array();
     $sql = 'SELECT DISTINCT ot.operationid' . ' FROM optemplate ot' . ' WHERE ' . dbConditionInt('ot.templateid', $templateids);
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $operationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('optemplate', array('templateid' => $templateids));
     // delete empty operations
     $delOperationids = array();
     $sql = 'SELECT DISTINCT o.operationid' . ' FROM operations o' . ' WHERE ' . dbConditionInt('o.operationid', $operationids) . ' AND NOT EXISTS(SELECT NULL FROM optemplate ot WHERE ot.operationid=o.operationid)';
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $delOperationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('operations', array('operationid' => $delOperationids));
     // Applications
     $delApplications = API::Application()->get(array('templateids' => $templateids, 'output' => API_OUTPUT_SHORTEN, 'nopermissions' => 1, 'preservekeys' => 1));
     if (!empty($delApplications)) {
         API::Application()->delete(array_keys($delApplications), true);
     }
     DB::delete('hosts', array('hostid' => $templateids));
     // TODO: remove info from API
     foreach ($delTemplates as $template) {
         info(_s('Deleted: Template "%1$s".', $template['name']));
         add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_HOST, $template['hostid'], $template['host'], 'hosts', NULL, NULL);
     }
     return array('templateids' => $templateids);
 }
 public function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $itemids = array_keys($result);
     // adding applications
     if ($options['selectApplications'] !== null && $options['selectApplications'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'itemid', 'applicationid', 'items_applications');
         $applications = API::Application()->get(array('output' => $options['selectApplications'], 'applicationids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
         $result = $relationMap->mapMany($result, $applications, 'applications');
     }
     // adding triggers
     if (!is_null($options['selectTriggers'])) {
         if ($options['selectTriggers'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'itemid', 'triggerid', 'functions');
             $triggers = API::TriggerPrototype()->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::TriggerPrototype()->get(array('countOutput' => true, 'groupCount' => true, 'itemids' => $itemids));
             $triggers = zbx_toHash($triggers, 'itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($triggers[$itemid])) {
                     $result[$itemid]['triggers'] = $triggers[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['triggers'] = 0;
                 }
             }
         }
     }
     // adding graphs
     if (!is_null($options['selectGraphs'])) {
         if ($options['selectGraphs'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'itemid', 'graphid', 'graphs_items');
             $graphs = API::GraphPrototype()->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::GraphPrototype()->get(array('countOutput' => true, 'groupCount' => true, 'itemids' => $itemids));
             $graphs = zbx_toHash($graphs, 'itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($graphs[$itemid])) {
                     $result[$itemid]['graphs'] = $graphs[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['graphs'] = 0;
                 }
             }
         }
     }
     // adding discoveryrule
     if ($options['selectDiscoveryRule'] !== null && $options['selectDiscoveryRule'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'itemid', 'parent_itemid', 'item_discovery');
         $discoveryRules = API::DiscoveryRule()->get(array('output' => $options['selectDiscoveryRule'], 'itemids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true));
         $result = $relationMap->mapOne($result, $discoveryRules, 'discoveryRule');
     }
     return $result;
 }
}
// select allowed selected hosts
$params = array('output' => array('hostid', 'name', 'flags'), 'hostids' => $hosts_linked_to, 'templated_hosts' => 1, 'editable' => 1, 'output' => API_OUTPUT_EXTEND, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL), 'preservekeys' => true);
$hostsAllowed = API::Host()->get($params);
order_result($hostsAllowed, 'name');
// select selected hosts including read only and add them
$hostsAll = API::Host()->get(array('output' => array('hostid', 'name', 'flags'), 'hostids' => $hosts_linked_to, 'templated_hosts' => true));
order_result($hostsAll, 'name');
foreach ($hostsAll as $host) {
    $host_tb->addItem($host['hostid'], $host['name'], true, isset($hostsAllowed[$host['hostid']]));
}
$templateList->addRow(_('Hosts / templates'), $host_tb->Get(_('In'), array(_('Other | group') . SPACE, $cmbGroups)));
// FULL CLONE {
if ($_REQUEST['form'] == 'full_clone') {
    // template applications
    $templateApps = API::Application()->get(array('hostids' => $templateid, 'inherited' => false, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true));
    if (!empty($templateApps)) {
        $applicationsList = array();
        foreach ($templateApps as $tplAppId => $templateApp) {
            $applicationsList[$tplAppId] = $templateApp['name'];
        }
        order_result($applicationsList);
        $listBox = new CListBox('applications', null, 8);
        $listBox->setAttribute('disabled', 'disabled');
        $listBox->addItems($applicationsList);
        $templateList->addRow(_('Applications'), $listBox);
    }
    // items
    $hostItems = API::Item()->get(array('hostids' => $templateid, 'inherited' => false, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL), 'output' => array('itemid', 'key_', 'name', 'hostid')));
    if ($hostItems) {
        $hostItems = CMacrosResolverHelper::resolveItemNames($hostItems);