/**
  * Get Service data
  *
  * @param _array $options
  * @param array $options['nodeids'] Node IDs
  * @param array $options['groupids'] ServiceGroup IDs
  * @param array $options['hostids'] Service IDs
  * @param boolean $options['monitored_hosts'] only monitored Services
  * @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 ServiceGroups
  * @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 Services, returned column name is rowscount
  * @param string $options['pattern'] search hosts by pattern in Service name
  * @param string $options['extendPattern'] search hosts by pattern in Service 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 Service 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('dserviceid', 'dhostid', 'ip');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     $sqlParts = array('select' => array('dservices' => 'ds.dserviceid'), 'from' => array('dservices' => 'dservices ds'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'dserviceids' => null, 'dhostids' => null, 'dcheckids' => null, 'druleids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectDRules' => null, 'selectDHosts' => null, 'selectDChecks' => 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']['dservices']);
         $dbTable = DB::getSchema('dservices');
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 's.' . $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();
     // dserviceids
     if (!is_null($options['dserviceids'])) {
         zbx_value2array($options['dserviceids']);
         $sqlParts['where']['dserviceid'] = dbConditionInt('ds.dserviceid', $options['dserviceids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sqlParts['where'][] = DBin_node('ds.dserviceid', $nodeids);
         }
     }
     // dhostids
     if (!is_null($options['dhostids'])) {
         zbx_value2array($options['dhostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['dhostid'] = 'ds.dhostid';
         }
         $sqlParts['where'][] = dbConditionInt('ds.dhostid', $options['dhostids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['dhostid'] = 'ds.dhostid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sqlParts['where'][] = DBin_node('ds.dhostid', $nodeids);
         }
     }
     // dcheckids
     if (!is_null($options['dcheckids'])) {
         zbx_value2array($options['dcheckids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['dcheckid'] = 'dc.dcheckid';
         }
         $sqlParts['from']['dhosts'] = 'dhosts dh';
         $sqlParts['from']['dchecks'] = 'dchecks dc';
         $sqlParts['where'][] = dbConditionInt('dc.dcheckid', $options['dcheckids']);
         $sqlParts['where']['dhds'] = 'dh.hostid=ds.hostid';
         $sqlParts['where']['dcdh'] = 'dc.druleid=dh.druleid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['dcheckid'] = 'dc.dcheckid';
         }
     }
     // druleids
     if (!is_null($options['druleids'])) {
         zbx_value2array($options['druleids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['druleid'] = 'dh.druleid';
         }
         $sqlParts['from']['dhosts'] = 'dhosts dh';
         $sqlParts['where']['druleid'] = dbConditionInt('dh.druleid', $options['druleids']);
         $sqlParts['where']['dhds'] = 'dh.dhostid=ds.dhostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['druleid'] = 'dh.druleid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sqlParts['where'][] = DBin_node('dh.druleid', $nodeids);
         }
     }
     // node check !!!!!
     // should last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sqlParts['where'][] = DBin_node('ds.dserviceid', $nodeids);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['dservices'] = 'ds.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('count(DISTINCT ds.dserviceid) 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('dservices ds', $options, $sqlParts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('dservices ds', $options, $sqlParts);
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 'ds');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     //-------
     $dserviceids = 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 ($dservice = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $dservice;
             } else {
                 $result = $dservice['rowscount'];
             }
         } else {
             $dserviceids[$dservice['dserviceid']] = $dservice['dserviceid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$dservice['dserviceid']] = array('dserviceid' => $dservice['dserviceid']);
             } else {
                 if (!isset($result[$dservice['dserviceid']])) {
                     $result[$dservice['dserviceid']] = array();
                 }
                 if (!is_null($options['selectDRules']) && !isset($result[$dservice['dserviceid']]['drules'])) {
                     $result[$dservice['dserviceid']]['drules'] = array();
                 }
                 if (!is_null($options['selectDHosts']) && !isset($result[$dservice['dserviceid']]['dhosts'])) {
                     $result[$dservice['dserviceid']]['dhosts'] = array();
                 }
                 if (!is_null($options['selectDChecks']) && !isset($result[$dservice['dserviceid']]['dchecks'])) {
                     $result[$dservice['dserviceid']]['dchecks'] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$dservice['dserviceid']]['hosts'])) {
                     $result[$dservice['dserviceid']]['hosts'] = array();
                 }
                 // druleids
                 if (isset($dservice['druleid']) && is_null($options['selectDRules'])) {
                     if (!isset($result[$dservice['dserviceid']]['drules'])) {
                         $result[$dservice['dserviceid']]['drules'] = array();
                     }
                     $result[$dservice['dserviceid']]['drules'][] = array('druleid' => $dservice['druleid']);
                 }
                 // dhostids
                 if (isset($dservice['dhostid']) && is_null($options['selectDHosts'])) {
                     if (!isset($result[$dservice['dserviceid']]['dhosts'])) {
                         $result[$dservice['dserviceid']]['dhosts'] = array();
                     }
                     $result[$dservice['dserviceid']]['dhosts'][] = array('dhostid' => $dservice['dhostid']);
                 }
                 // dcheckids
                 if (isset($dservice['dcheckid']) && is_null($options['selectDChecks'])) {
                     if (!isset($result[$dservice['dserviceid']]['dchecks'])) {
                         $result[$dservice['dserviceid']]['dchecks'] = array();
                     }
                     $result[$dservice['dserviceid']]['dchecks'][] = array('dcheckid' => $dservice['dcheckid']);
                 }
                 $result[$dservice['dserviceid']] += $dservice;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     // Adding Objects
     // select_drules
     if (!is_null($options['selectDRules'])) {
         $objParams = array('nodeids' => $nodeids, 'dserviceids' => $dserviceids, '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]['dservices']);
                 $count = array();
                 foreach ($drule['dservices'] as $dnum => $dservice) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dservice['dserviceid']])) {
                             $count[$dservice['dserviceid']] = 0;
                         }
                         $count[$dservice['dserviceid']]++;
                         if ($count[$dservice['dserviceid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dservice['dserviceid']]['drules'][] =& $drules[$druleid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectDRules']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $drules = API::DRule()->get($objParams);
             $drules = zbx_toHash($drules, 'dserviceid');
             foreach ($result as $dserviceid => $dservice) {
                 if (isset($drules[$dserviceid])) {
                     $result[$dserviceid]['drules'] = $drules[$dserviceid]['rowscount'];
                 } else {
                     $result[$dserviceid]['drules'] = 0;
                 }
             }
         }
     }
     // selectDHosts
     if (!is_null($options['selectDHosts'])) {
         $objParams = array('nodeids' => $nodeids, 'dserviceids' => $dserviceids, 'preservekeys' => 1);
         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');
             }
             foreach ($dhosts as $dhostid => $dhost) {
                 unset($dhosts[$dhostid]['dservices']);
                 foreach ($dhost['dservices'] as $snum => $dservice) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dservice['dserviceid']])) {
                             $count[$dservice['dserviceid']] = 0;
                         }
                         $count[$dservice['dserviceid']]++;
                         if ($count[$dservice['dserviceid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dservice['dserviceid']]['dhosts'][] =& $dhosts[$dhostid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectDHosts']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $dhosts = API::DHost()->get($objParams);
             $dhosts = zbx_toHash($dhosts, 'dhostid');
             foreach ($result as $dserviceid => $dservice) {
                 if (isset($dhosts[$dserviceid])) {
                     $result[$dserviceid]['dhosts'] = $dhosts[$dserviceid]['rowscount'];
                 } else {
                     $result[$dserviceid]['dhosts'] = 0;
                 }
             }
         }
     }
     // selectHosts
     if (!is_null($options['selectHosts'])) {
         $objParams = array('nodeids' => $nodeids, 'dserviceids' => $dserviceids, 'preservekeys' => 1, 'sortfield' => 'status');
         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, 'hostid');
             }
             foreach ($hosts as $hostid => $host) {
                 unset($hosts[$hostid]['dservices']);
                 foreach ($host['dservices'] as $dnum => $dservice) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dservice['dserviceid']])) {
                             $count[$dservice['dserviceid']] = 0;
                         }
                         $count[$dservice['dserviceid']]++;
                         if ($count[$dservice['dserviceid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dservice['dserviceid']]['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 $dserviceid => $dservice) {
                 if (isset($hosts[$dserviceid])) {
                     $result[$dserviceid]['hosts'] = $hosts[$dserviceid]['rowscount'];
                 } else {
                     $result[$dserviceid]['hosts'] = 0;
                 }
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Beispiel #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;
 }
 /**
  * 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);
 }
Beispiel #4
0
        $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;
    }
    ksort($data['services']);
    // discovery services to hash
    $data['dservices'] = zbx_toHash($dservices, 'dserviceid');
    // discovery hosts
    $data['dhosts'] = API::DHost()->get(array('druleids' => zbx_objectValues($data['drules'], 'druleid'), 'selectDServices' => array('dserviceid', 'ip', 'dns', 'type', 'status', 'key_'), 'output' => array('dhostid', 'lastdown', 'lastup', 'druleid')));
    $data['dhosts'] = zbx_toHash($data['dhosts'], 'dhostid');
}
// render view
$discoveryView = new CView('monitoring.discovery', $data);
$discoveryView->render();
$discoveryView->show();
require_once dirname(__FILE__) . '/include/page_footer.php';
 /**
  * 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 #6
0
 protected function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $druleids = array_keys($result);
     // Adding Discovery Checks
     if (!is_null($options['selectDChecks'])) {
         if ($options['selectDChecks'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'druleid', 'dcheckid', 'dchecks');
             $dchecks = API::DCheck()->get(array('output' => $options['selectDChecks'], 'dcheckids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true));
             if (!is_null($options['limitSelects'])) {
                 order_result($dchecks, 'dcheckid');
             }
             $result = $relationMap->mapMany($result, $dchecks, 'dchecks', $options['limitSelects']);
         } else {
             $dchecks = API::DCheck()->get(array('druleids' => $druleids, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
             $dchecks = zbx_toHash($dchecks, 'druleid');
             foreach ($result as $druleid => $drule) {
                 if (isset($dchecks[$druleid])) {
                     $result[$druleid]['dchecks'] = $dchecks[$druleid]['rowscount'];
                 } else {
                     $result[$druleid]['dchecks'] = 0;
                 }
             }
         }
     }
     // Adding Discovery Hosts
     if (!is_null($options['selectDHosts'])) {
         if ($options['selectDHosts'] != API_OUTPUT_COUNT) {
             $relationMap = $this->createRelationMap($result, 'druleid', 'dhostid', 'dhosts');
             $dhosts = API::DHost()->get(array('output' => $options['selectDHosts'], 'dhostids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
             if (!is_null($options['limitSelects'])) {
                 order_result($dhosts, 'dhostid');
             }
             $result = $relationMap->mapMany($result, $dhosts, 'dhosts', $options['limitSelects']);
         } else {
             $dhosts = API::DHost()->get(array('druleids' => $druleids, 'countOutput' => true, 'groupCount' => true));
             $dhosts = zbx_toHash($dhosts, 'druleid');
             foreach ($result as $druleid => $drule) {
                 if (isset($dhosts[$druleid])) {
                     $result[$druleid]['dhosts'] = $dhosts[$druleid]['rowscount'];
                 } else {
                     $result[$druleid]['dhosts'] = 0;
                 }
             }
         }
     }
     return $result;
 }
Beispiel #7
0
 protected function addRelatedObjects(array $options, array $result)
 {
     $result = parent::addRelatedObjects($options, $result);
     $dserviceIds = array_keys($result);
     // select_drules
     if ($options['selectDRules'] !== null && $options['selectDRules'] != API_OUTPUT_COUNT) {
         $relationMap = new CRelationMap();
         // discovered items
         $dbRules = DBselect('SELECT ds.dserviceid,dh.druleid' . ' FROM dservices ds,dhosts dh' . ' WHERE ' . dbConditionInt('ds.dserviceid', $dserviceIds) . ' AND ds.dhostid=dh.dhostid');
         while ($rule = DBfetch($dbRules)) {
             $relationMap->addRelation($rule['dserviceid'], $rule['druleid']);
         }
         $drules = API::DRule()->get(['output' => $options['selectDRules'], 'druleids' => $relationMap->getRelatedIds(), 'preservekeys' => true]);
         if (!is_null($options['limitSelects'])) {
             order_result($drules, 'name');
         }
         $result = $relationMap->mapMany($result, $drules, 'drules');
     }
     // selectDHosts
     if ($options['selectDHosts'] !== null && $options['selectDHosts'] != API_OUTPUT_COUNT) {
         $relationMap = $this->createRelationMap($result, 'dserviceid', 'dhostid');
         $dhosts = API::DHost()->get(['output' => $options['selectDHosts'], 'dhosts' => $relationMap->getRelatedIds(), 'preservekeys' => true]);
         if (!is_null($options['limitSelects'])) {
             order_result($dhosts, 'dhostid');
         }
         $result = $relationMap->mapMany($result, $dhosts, 'dhosts', $options['limitSelects']);
     }
     // selectHosts
     if (!is_null($options['selectHosts'])) {
         if ($options['selectHosts'] != API_OUTPUT_COUNT) {
             $relationMap = new CRelationMap();
             // discovered items
             $dbRules = DBselect('SELECT ds.dserviceid,i.hostid' . ' FROM dservices ds,interface i' . ' WHERE ' . dbConditionInt('ds.dserviceid', $dserviceIds) . ' AND ds.ip=i.ip');
             while ($rule = DBfetch($dbRules)) {
                 $relationMap->addRelation($rule['dserviceid'], $rule['hostid']);
             }
             $hosts = API::Host()->get(['output' => $options['selectHosts'], 'hostids' => $relationMap->getRelatedIds(), 'preservekeys' => true, 'sortfield' => 'status']);
             if (!is_null($options['limitSelects'])) {
                 order_result($hosts, 'hostid');
             }
             $result = $relationMap->mapMany($result, $hosts, 'hosts', $options['limitSelects']);
         } else {
             $hosts = API::Host()->get(['dserviceids' => $dserviceIds, 'countOutput' => true, 'groupCount' => true]);
             $hosts = zbx_toHash($hosts, 'hostid');
             foreach ($result as $dserviceid => $dservice) {
                 if (isset($hosts[$dserviceid])) {
                     $result[$dserviceid]['hosts'] = $hosts[$dserviceid]['rowscount'];
                 } else {
                     $result[$dserviceid]['hosts'] = 0;
                 }
             }
         }
     }
     return $result;
 }
Beispiel #8
0
        $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;
    }
    ksort($data['services']);
    // discovery services to hash
    $data['dservices'] = zbx_toHash($dservices, 'dserviceid');
    // discovery hosts
    $data['dhosts'] = API::DHost()->get(array('druleids' => zbx_objectValues($data['drules'], 'druleid'), 'selectDServices' => API_OUTPUT_REFER, 'output' => API_OUTPUT_REFER));
    $data['dhosts'] = zbx_toHash($data['dhosts'], 'dhostid');
}
// render view
$discoveryView = new CView('monitoring.discovery', $data);
$discoveryView->render();
$discoveryView->show();
require_once dirname(__FILE__) . '/include/page_footer.php';