Exemplo n.º 1
0
 /**
  * Process screen.
  *
  * @return CDiv (screen inside container)
  */
 public function get()
 {
     $this->dataId = 'discovery';
     $sort_field = $this->data['sort'];
     $sort_order = $this->data['sortorder'];
     $druleid = $this->data['druleid'];
     // discovery rules
     $options = ['output' => ['druleid', 'name'], 'selectDHosts' => ['dhostid', 'status', 'lastup', 'lastdown'], 'filter' => ['status' => DRULE_STATUS_ACTIVE]];
     if ($druleid > 0) {
         $options['druleids'] = $druleid;
         // set selected discovery rule id
     }
     $drules = API::DRule()->get($options);
     if ($drules) {
         order_result($drules, 'name');
     }
     // discovery services
     $options = ['selectHosts' => ['hostid', 'name', 'status'], 'output' => ['dserviceid', 'type', 'key_', 'port', 'status', 'lastup', 'lastdown', 'ip', 'dns'], 'sortfield' => $sort_field, 'sortorder' => $sort_order, 'limitSelects' => 1];
     if ($druleid > 0) {
         $options['druleids'] = $druleid;
     } else {
         $options['druleids'] = zbx_objectValues($drules, 'druleid');
     }
     $dservices = API::DService()->get($options);
     // user macros
     $macros = API::UserMacro()->get(['output' => ['macro', 'value'], 'globalmacro' => true]);
     $macros = zbx_toHash($macros, 'macro');
     // services
     $services = [];
     foreach ($dservices as $dservice) {
         $key_ = $dservice['key_'];
         if ($key_ !== '') {
             if (array_key_exists($key_, $macros)) {
                 $key_ = $macros[$key_]['value'];
             }
             $key_ = ': ' . $key_;
         }
         $service_name = discovery_check_type2str($dservice['type']) . discovery_port2str($dservice['type'], $dservice['port']) . $key_;
         $services[$service_name] = 1;
     }
     ksort($services);
     // discovery services to hash
     $dservices = zbx_toHash($dservices, 'dserviceid');
     // discovery hosts
     $dhosts = API::DHost()->get(['druleids' => zbx_objectValues($drules, 'druleid'), 'selectDServices' => ['dserviceid', 'ip', 'dns', 'type', 'status', 'key_'], 'output' => ['dhostid', 'lastdown', 'lastup', 'druleid']]);
     $dhosts = zbx_toHash($dhosts, 'dhostid');
     $header = [make_sorting_header(_('Discovered device'), 'ip', $sort_field, $sort_order, 'zabbix.php?action=discovery.view'), _('Monitored host'), _('Uptime') . '/' . _('Downtime')];
     foreach ($services as $name => $foo) {
         $header[] = (new CColHeader($name))->addClass('vertical_rotation');
     }
     // create table
     $table = (new CTableInfo())->makeVerticalRotation()->setHeader($header);
     foreach ($drules as $drule) {
         $discovery_info = [];
         foreach ($drule['dhosts'] as $dhost) {
             if ($dhost['status'] == DHOST_STATUS_DISABLED) {
                 $hclass = 'disabled';
                 $htime = $dhost['lastdown'];
             } else {
                 $hclass = 'enabled';
                 $htime = $dhost['lastup'];
             }
             // $primary_ip stores the primary host ip of the dhost
             $primary_ip = '';
             foreach ($dhosts[$dhost['dhostid']]['dservices'] as $dservice) {
                 $dservice = $dservices[$dservice['dserviceid']];
                 $hostName = '';
                 $host = reset($dservices[$dservice['dserviceid']]['hosts']);
                 if (!is_null($host)) {
                     $hostName = $host['name'];
                 }
                 if ($primary_ip !== '') {
                     if ($primary_ip === $dservice['ip']) {
                         $htype = 'primary';
                     } else {
                         $htype = 'slave';
                     }
                 } else {
                     $primary_ip = $dservice['ip'];
                     $htype = 'primary';
                 }
                 if (!array_key_exists($dservice['ip'], $discovery_info)) {
                     $discovery_info[$dservice['ip']] = ['ip' => $dservice['ip'], 'dns' => $dservice['dns'], 'type' => $htype, 'class' => $hclass, 'host' => $hostName, 'time' => $htime];
                 }
                 if ($dservice['status'] == DSVC_STATUS_DISABLED) {
                     $class = ZBX_STYLE_INACTIVE_BG;
                     $time = 'lastdown';
                 } else {
                     $class = ZBX_STYLE_ACTIVE_BG;
                     $time = 'lastup';
                 }
                 $key_ = $dservice['key_'];
                 if ($key_ !== '') {
                     if (array_key_exists($key_, $macros)) {
                         $key_ = $macros[$key_]['value'];
                     }
                     $key_ = NAME_DELIMITER . $key_;
                 }
                 $service_name = discovery_check_type2str($dservice['type']) . discovery_port2str($dservice['type'], $dservice['port']) . $key_;
                 $discovery_info[$dservice['ip']]['services'][$service_name] = ['class' => $class, 'time' => $dservice[$time]];
             }
         }
         if ($druleid == 0 && $discovery_info) {
             $col = new CCol([bold($drule['name']), SPACE . '(' . _n('%d device', '%d devices', count($discovery_info)) . ')']);
             $col->setColSpan(count($services) + 3);
             $table->addRow($col);
         }
         order_result($discovery_info, $sort_field, $sort_order);
         foreach ($discovery_info as $ip => $h_data) {
             $dns = $h_data['dns'] == '' ? '' : ' (' . $h_data['dns'] . ')';
             $row = [$h_data['type'] == 'primary' ? (new CSpan($ip . $dns))->addClass($h_data['class']) : new CSpan(SPACE . SPACE . $ip . $dns), new CSpan(array_key_exists('host', $h_data) ? $h_data['host'] : ''), (new CSpan($h_data['time'] == 0 || $h_data['type'] === 'slave' ? '' : convert_units(['value' => time() - $h_data['time'], 'units' => 'uptime'])))->addClass($h_data['class'])];
             foreach ($services as $name => $foo) {
                 $class = null;
                 $time = SPACE;
                 $hint = (new CDiv(SPACE))->addClass($class);
                 $hint_table = null;
                 if (array_key_exists($name, $h_data['services'])) {
                     $class = $h_data['services'][$name]['class'];
                     $time = $h_data['services'][$name]['time'];
                     $hint_table = (new CTableInfo())->setAttribute('style', 'width: auto;');
                     if ($class == ZBX_STYLE_ACTIVE_BG) {
                         $hint_table->setHeader(_('Uptime'));
                     } else {
                         $hint_table->setHeader(_('Downtime'));
                     }
                     $hint_table->addRow((new CCol(zbx_date2age($h_data['services'][$name]['time'])))->addClass($class));
                 }
                 $column = (new CCol($hint))->addClass($class);
                 if (!is_null($hint_table)) {
                     $column->setHint($hint_table);
                 }
                 $row[] = $column;
             }
             $table->addRow($row);
         }
     }
     return $this->getOutput($table, true, $this->data);
 }
Exemplo n.º 2
0
 protected function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $eventIds = array_keys($result);
     // adding hosts
     if ($options['selectHosts'] !== null && $options['selectHosts'] != API_OUTPUT_COUNT) {
         // trigger events
         if ($options['object'] == EVENT_OBJECT_TRIGGER) {
             $query = DBselect('SELECT e.eventid,i.hostid' . ' FROM events e,functions f,items i' . ' WHERE ' . dbConditionInt('e.eventid', $eventIds) . ' AND e.objectid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND e.object=' . zbx_dbstr($options['object']) . ' AND e.source=' . zbx_dbstr($options['source']));
         } elseif ($options['object'] == EVENT_OBJECT_ITEM || $options['object'] == EVENT_OBJECT_LLDRULE) {
             $query = DBselect('SELECT e.eventid,i.hostid' . ' FROM events e,items i' . ' WHERE ' . dbConditionInt('e.eventid', $eventIds) . ' AND e.objectid=i.itemid' . ' AND e.object=' . zbx_dbstr($options['object']) . ' AND e.source=' . zbx_dbstr($options['source']));
         }
         $relationMap = new CRelationMap();
         while ($relation = DBfetch($query)) {
             $relationMap->addRelation($relation['eventid'], $relation['hostid']);
         }
         $hosts = API::Host()->get(array('output' => $options['selectHosts'], 'hostids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true));
         $result = $relationMap->mapMany($result, $hosts, 'hosts');
     }
     // adding the related object
     if ($options['selectRelatedObject'] !== null && $options['selectRelatedObject'] != API_OUTPUT_COUNT && $options['object'] != EVENT_OBJECT_AUTOREGHOST) {
         $relationMap = new CRelationMap();
         foreach ($result as $event) {
             $relationMap->addRelation($event['eventid'], $event['objectid']);
         }
         switch ($options['object']) {
             case EVENT_OBJECT_TRIGGER:
                 $api = API::Trigger();
                 break;
             case EVENT_OBJECT_DHOST:
                 $api = API::DHost();
                 break;
             case EVENT_OBJECT_DSERVICE:
                 $api = API::DService();
                 break;
             case EVENT_OBJECT_ITEM:
                 $api = API::Item();
                 break;
             case EVENT_OBJECT_LLDRULE:
                 $api = API::DiscoveryRule();
                 break;
         }
         $objects = $api->get(array('output' => $options['selectRelatedObject'], $api->pkOption() => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true));
         $result = $relationMap->mapOne($result, $objects, 'relatedObject');
     }
     // adding alerts
     if ($options['select_alerts'] !== null && $options['select_alerts'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'eventid', 'alertid', 'alerts');
         $alerts = API::Alert()->get(array('output' => $options['select_alerts'], 'selectMediatypes' => API_OUTPUT_EXTEND, 'alertids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true, 'sortfield' => 'clock', 'sortorder' => ZBX_SORT_DOWN));
         $result = $relationMap->mapMany($result, $alerts, 'alerts');
     }
     // adding acknowledges
     if ($options['select_acknowledges'] !== null) {
         if ($options['select_acknowledges'] != API_OUTPUT_COUNT) {
             // create the base query
             $sqlParts = API::getApiService()->createSelectQueryParts('acknowledges', 'a', array('output' => $this->outputExtend($options['select_acknowledges'], array('acknowledgeid', 'eventid', 'clock')), 'filter' => array('eventid' => $eventIds)));
             $sqlParts['order'][] = 'a.clock DESC';
             // if the user data is requested via extended output or specified fields, join the users table
             $userFields = array('alias', 'name', 'surname');
             $requestUserData = array();
             foreach ($userFields as $userField) {
                 if ($this->outputIsRequested($userField, $options['select_acknowledges'])) {
                     $requestUserData[] = $userField;
                 }
             }
             if ($requestUserData) {
                 foreach ($requestUserData as $userField) {
                     $sqlParts = $this->addQuerySelect('u.' . $userField, $sqlParts);
                 }
                 $sqlParts['from'][] = 'users u';
                 $sqlParts['where'][] = 'a.userid=u.userid';
             }
             $acknowledges = DBFetchArrayAssoc(DBselect($this->createSelectQueryFromParts($sqlParts)), 'acknowledgeid');
             $relationMap = $this->createRelationMap($acknowledges, 'eventid', 'acknowledgeid');
             $acknowledges = $this->unsetExtraFields($acknowledges, array('eventid', 'acknowledgeid', 'clock'), $options['select_acknowledges']);
             $result = $relationMap->mapMany($result, $acknowledges, 'acknowledges');
         } else {
             $acknowledges = DBFetchArrayAssoc(DBselect('SELECT COUNT(a.acknowledgeid) AS rowscount,a.eventid' . ' FROM acknowledges a' . ' WHERE ' . dbConditionInt('a.eventid', $eventIds) . ' GROUP BY a.eventid'), 'eventid');
             foreach ($result as &$event) {
                 if (isset($acknowledges[$event['eventid']])) {
                     $event['acknowledges'] = $acknowledges[$event['eventid']]['rowscount'];
                 } else {
                     $event['acknowledges'] = 0;
                 }
             }
             unset($event);
         }
     }
     return $result;
 }
Exemplo n.º 3
0
 protected function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $dhostIds = array_keys($result);
     // select_drules
     if ($options['selectDRules'] !== null && $options['selectDRules'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'dhostid', 'druleid');
         $drules = API::DRule()->get(array('output' => $options['selectDRules'], 'druleids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
         if (!is_null($options['limitSelects'])) {
             order_result($drules, 'name');
         }
         $result = $relationMap->mapMany($result, $drules, 'drules', $options['limitSelects']);
     }
     // selectDServices
     if (!is_null($options['selectDServices'])) {
         if ($options['selectDServices'] != API_OUTPUT_COUNT) {
             $dservices = API::DService()->get(array('output' => $this->outputExtend($options['selectDServices'], array('dserviceid', 'dhostid')), 'dhostids' => $dhostIds, 'preservekeys' => true));
             $relationMap = $this->createRelationMap($dservices, 'dhostid', 'dserviceid');
             $dservices = $this->unsetExtraFields($dservices, array('dserviceid', 'dhostid'), $options['selectDServices']);
             if (!is_null($options['limitSelects'])) {
                 order_result($dservices, 'name');
             }
             $result = $relationMap->mapMany($result, $dservices, 'dservices', $options['limitSelects']);
         } else {
             $dservices = API::DService()->get(array('output' => $options['selectDServices'], 'dhostids' => $dhostIds, 'countOutput' => true, 'groupCount' => true));
             $dservices = zbx_toHash($dservices, 'dhostid');
             foreach ($result as $dhostid => $dhost) {
                 if (isset($dservices[$dhostid])) {
                     $result[$dhostid]['dservices'] = $dservices[$dhostid]['rowscount'];
                 } else {
                     $result[$dhostid]['dservices'] = 0;
                 }
             }
         }
     }
     return $result;
 }
Exemplo n.º 4
0
 if ($data['pageFilter']->druleid > 0) {
     $options['druleids'] = $data['pageFilter']->druleid;
     // set selected discovery rule id
 }
 $data['drules'] = API::DRule()->get($options);
 if (!empty($data['drules'])) {
     order_result($data['drules'], 'name');
 }
 // discovery services
 $options = array('selectHosts' => array('hostid', 'name', 'status'), 'output' => API_OUTPUT_EXTEND, 'sortfield' => $sortField, 'sortorder' => $sortOrder, 'limitSelects' => 1);
 if (!empty($data['druleid'])) {
     $options['druleids'] = $data['druleid'];
 } else {
     $options['druleids'] = zbx_objectValues($data['drules'], 'druleid');
 }
 $dservices = API::DService()->get($options);
 // user macros
 $data['macros'] = API::UserMacro()->get(array('output' => API_OUTPUT_EXTEND, 'globalmacro' => true));
 $data['macros'] = zbx_toHash($data['macros'], 'macro');
 // services
 $data['services'] = array();
 foreach ($dservices as $dservice) {
     $key_ = $dservice['key_'];
     if (!zbx_empty($key_)) {
         if (isset($data['macros'][$key_])) {
             $key_ = $data['macros'][$key_]['value'];
         }
         $key_ = ': ' . $key_;
     }
     $serviceName = discovery_check_type2str($dservice['type']) . discovery_port2str($dservice['type'], $dservice['port']) . $key_;
     $data['services'][$serviceName] = 1;
Exemplo n.º 5
0
 /**
  * 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_historical_items'] only with historical items
  * @param boolean $options['with_triggers'] only with triggers
  * @param boolean $options['with_httptests'] only with 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['selectTemplates'] select Templates
  * @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 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();
     $nodeCheck = false;
     $userType = self::$userData['type'];
     // allowed columns for sorting
     $sortColumns = array('dhostid', 'druleid');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     $sqlParts = array('select' => array('dhosts' => 'dh.dhostid'), 'from' => array('dhosts' => 'dhosts dh'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'druleids' => null, 'dhostids' => null, 'dserviceids' => null, 'groupids' => null, 'hostids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectDRules' => null, 'selectDServices' => null, 'selectGroups' => null, 'selectHosts' => 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']['dhosts']);
         $dbTable = DB::getSchema('dhosts');
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 'dh.' . $field;
             }
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $userType) {
     } elseif (is_null($options['editable']) && self::$userData['type'] == USER_TYPE_ZABBIX_ADMIN) {
     } elseif (!is_null($options['editable']) && self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
         return array();
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // dhostids
     if (!is_null($options['dhostids'])) {
         zbx_value2array($options['dhostids']);
         $sqlParts['where']['dhostid'] = dbConditionInt('dh.dhostid', $options['dhostids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sqlParts['where'][] = DBin_node('dh.dhostid', $nodeids);
         }
     }
     // druleids
     if (!is_null($options['druleids'])) {
         zbx_value2array($options['druleids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['druleid'] = 'dh.druleid';
         }
         $sqlParts['where']['druleid'] = dbConditionInt('dh.druleid', $options['druleids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['druleid'] = 'dh.druleid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sqlParts['where'][] = DBin_node('dh.druleid', $nodeids);
         }
     }
     // dserviceids
     if (!is_null($options['dserviceids'])) {
         zbx_value2array($options['dserviceids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['dserviceids'] = 'ds.dserviceids';
         }
         $sqlParts['from']['dservices'] = 'dservices ds';
         $sqlParts['where'][] = dbConditionInt('ds.dserviceid', $options['dserviceids']);
         $sqlParts['where']['dhds'] = 'dh.dhostid=ds.dhostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['dserviceids'] = 'ds.dserviceid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sqlParts['where'][] = DBin_node('ds.dserviceid', $nodeids);
         }
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['groupid'] = 'hg.groupid';
         }
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']);
         $sqlParts['where']['dhh'] = 'h.ip=dh.ip';
         $sqlParts['where']['hgh'] = 'hg.hostid=h.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['groupid'] = 'hg.groupid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sqlParts['where'][] = DBin_node('hg.groupid', $nodeids);
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['hostid'] = 'h.hostid';
         }
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where'][] = dbConditionInt('h.hostid', $options['hostids']);
         $sqlParts['where']['dhh'] = 'h.ip=dh.ip';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['hostid'] = 'h.hostid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sqlParts['where'][] = DBin_node('h.hostid', $nodeids);
         }
     }
     // node check !!!!!
     // should be last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sqlParts['where'][] = DBin_node('dh.dhostid', $nodeids);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['dhosts'] = 'dh.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('count(DISTINCT dh.dhostid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sqlParts['group'] as $key => $fields) {
                 $sqlParts['select'][$key] = $fields;
             }
         }
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('dhosts dh', $options, $sqlParts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('dhosts dh', $options, $sqlParts);
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 'dh');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     //-------
     $dhostids = 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 .= 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 ' . $sqlWhere . $sqlGroup . $sqlOrder;
     //SDI($sql);
     $res = DBselect($sql, $sqlLimit);
     while ($dhost = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $dhost;
             } else {
                 $result = $dhost['rowscount'];
             }
         } else {
             $dhostids[$dhost['dhostid']] = $dhost['dhostid'];
             //				$dips[$dhost['ip']] = $dhost['ip'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$dhost['dhostid']] = array('dhostid' => $dhost['dhostid']);
             } else {
                 if (!isset($result[$dhost['dhostid']])) {
                     $result[$dhost['dhostid']] = array();
                 }
                 if (!is_null($options['selectDRules']) && !isset($result[$dhost['dhostid']]['drules'])) {
                     $result[$dhost['dhostid']]['drules'] = array();
                 }
                 if (!is_null($options['selectDServices']) && !isset($result[$dhost['dhostid']]['dservices'])) {
                     $result[$dhost['dhostid']]['dservices'] = array();
                 }
                 if (!is_null($options['selectGroups']) && !isset($result[$dhost['dhostid']]['groups'])) {
                     $result[$dhost['dhostid']]['groups'] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$dhost['dhostid']]['hosts'])) {
                     $result[$dhost['dhostid']]['hosts'] = array();
                 }
                 // druleids
                 if (isset($dhost['druleid']) && is_null($options['selectDRules'])) {
                     if (!isset($result[$dhost['dhostid']]['drules'])) {
                         $result[$dhost['dhostid']]['drules'] = array();
                     }
                     $result[$dhost['dhostid']]['drules'][] = array('druleid' => $dhost['druleid']);
                 }
                 // dserviceids
                 if (isset($dhost['dserviceid']) && is_null($options['selectDServices'])) {
                     if (!isset($result[$dhost['dhostid']]['dservices'])) {
                         $result[$dhost['dhostid']]['dservices'] = array();
                     }
                     $result[$dhost['dhostid']]['dservices'][] = array('dserviceid' => $dhost['dserviceid']);
                     unset($dhost['dserviceid']);
                 }
                 // groupids
                 if (isset($dhost['groupid']) && is_null($options['selectGroups'])) {
                     if (!isset($result[$dhost['dhostid']]['groups'])) {
                         $result[$dhost['dhostid']]['groups'] = array();
                     }
                     $result[$dhost['dhostid']]['groups'][] = array('groupid' => $dhost['groupid']);
                     unset($dhost['groupid']);
                 }
                 // hostids
                 if (isset($dhost['hostid']) && is_null($options['selectHosts'])) {
                     if (!isset($result[$dhost['hostid']]['hosts'])) {
                         $result[$dhost['dhostid']]['hosts'] = array();
                     }
                     $result[$dhost['dhostid']]['hosts'][] = array('hostid' => $dhost['hostid']);
                     unset($dhost['hostid']);
                 }
                 $result[$dhost['dhostid']] += $dhost;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     // Adding Objects
     // select_drules
     if (!is_null($options['selectDRules'])) {
         $objParams = array('nodeids' => $nodeids, 'dhostids' => $dhostids, 'preservekeys' => 1);
         if (is_array($options['selectDRules']) || str_in_array($options['selectDRules'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectDRules'];
             $drules = API::DRule()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($drules, 'name');
             }
             foreach ($drules as $druleid => $drule) {
                 unset($drules[$druleid]['dhosts']);
                 $count = array();
                 foreach ($drule['dhosts'] as $dnum => $dhost) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dhost['dhostid']])) {
                             $count[$dhost['dhostid']] = 0;
                         }
                         $count[$dhost['dhostid']]++;
                         if ($count[$dhost['dhostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dhost['dhostid']]['drules'][] =& $drules[$druleid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectDRules']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $drules = API::DRule()->get($objParams);
             $drules = zbx_toHash($drules, 'dhostid');
             foreach ($result as $dhostid => $dhost) {
                 if (isset($drules[$dhostid])) {
                     $result[$dhostid]['drules'] = $drules[$dhostid]['rowscount'];
                 } else {
                     $result[$dhostid]['drules'] = 0;
                 }
             }
         }
     }
     // selectDServices
     if (!is_null($options['selectDServices'])) {
         $objParams = array('nodeids' => $nodeids, 'dhostids' => $dhostids, 'preservekeys' => 1);
         if (is_array($options['selectDServices']) || str_in_array($options['selectDServices'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectDServices'];
             $dservices = API::DService()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($dservices, 'name');
             }
             foreach ($dservices as $dserviceid => $dservice) {
                 unset($dservices[$dserviceid]['dhosts']);
                 foreach ($dservice['dhosts'] as $dnum => $dhost) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dhost['dhostid']])) {
                             $count[$dhost['dhostid']] = 0;
                         }
                         $count[$dhost['dhostid']]++;
                         if ($count[$dhost['dhostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dhost['dhostid']]['dservices'][] =& $dservices[$dserviceid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectDServices']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $dservices = API::DService()->get($objParams);
             $dservices = zbx_toHash($dservices, 'dhostid');
             foreach ($result as $dhostid => $dhost) {
                 if (isset($dservices[$dhostid])) {
                     $result[$dhostid]['dservices'] = $dservices[$dhostid]['rowscount'];
                 } else {
                     $result[$dhostid]['dservices'] = 0;
                 }
             }
         }
     }
     // TODO :selectGroups
     if (!is_null($options['selectGroups']) && str_in_array($options['selectGroups'], $subselectsAllowedOutputs)) {
         $objParams = array('nodeids' => $nodeids, 'output' => $options['selectGroups'], 'hostids' => $dhostids, 'preservekeys' => 1);
         $groups = API::HostGroup()->get($objParams);
         foreach ($groups as $groupid => $group) {
             $ghosts = $group['hosts'];
             unset($group['hosts']);
             foreach ($ghosts as $num => $dhost) {
                 $result[$dhost['hostid']]['groups'][] = $group;
             }
         }
     }
     // selectHosts
     if (!is_null($options['selectHosts'])) {
         $objParams = array('nodeids' => $nodeids, 'dhostids' => $dhostids, 'preservekeys' => 1);
         if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectHosts'];
             $hosts = API::Host()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($hosts, 'host');
             }
             foreach ($hosts as $hostid => $host) {
                 unset($hosts[$hostid]['dhosts']);
                 foreach ($host['dhosts'] as $dnum => $dhost) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dhost['dhostid']])) {
                             $count[$dhost['dhostid']] = 0;
                         }
                         $count[$dhost['dhostid']]++;
                         if ($count[$dhost['dhostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dhost['dhostid']]['hosts'][] =& $hosts[$hostid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectHosts']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $hosts = API::Host()->get($objParams);
             $hosts = zbx_toHash($hosts, 'hostid');
             foreach ($result as $dhostid => $dhost) {
                 if (isset($hosts[$dhostid])) {
                     $result[$dhostid]['hosts'] = $hosts[$dhostid]['rowscount'];
                 } else {
                     $result[$dhostid]['hosts'] = 0;
                 }
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }