Esempio n. 1
0
function updateMessageSettings($messages)
{
    if (!isset($messages['enabled'])) {
        $messages['enabled'] = 0;
    }
    if (isset($messages['triggers.severities'])) {
        $messages['triggers.severities'] = serialize($messages['triggers.severities']);
    }
    $dbProfiles = DBselect('SELECT p.profileid,p.idx,p.source,p.value_str' . ' FROM profiles p' . ' WHERE p.userid=' . CWebUser::$data['userid'] . ' AND ' . dbConditionString('p.idx', array('web.messages')));
    while ($profile = DBfetch($dbProfiles)) {
        $profile['value'] = $profile['value_str'];
        $dbMessages[$profile['source']] = $profile;
    }
    $inserts = array();
    $updates = array();
    foreach ($messages as $key => $value) {
        $values = array('userid' => CWebUser::$data['userid'], 'idx' => 'web.messages', 'source' => $key, 'value_str' => $value, 'type' => PROFILE_TYPE_STR);
        if (!isset($dbMessages[$key])) {
            $inserts[] = $values;
        } elseif ($dbMessages[$key]['value'] != $value) {
            $updates[] = array('values' => $values, 'where' => array('profileid' => $dbMessages[$key]['profileid']));
        }
    }
    try {
        DB::insert('profiles', $inserts);
        DB::update('profiles', $updates);
    } catch (APIException $e) {
        error($e->getMessage());
    }
    return $messages;
}
 /**
  * Import template screens.
  *
  * @param array $allScreens
  *
  * @return void
  */
 public function import(array $allScreens)
 {
     $screensToCreate = array();
     $screensToUpdate = array();
     foreach ($allScreens as $template => $screens) {
         // TODO: select all at once out of loop
         $dbScreens = DBselect('SELECT s.screenid,s.name FROM screens s WHERE' . ' s.templateid=' . zbx_dbstr($this->referencer->resolveTemplate($template)) . ' AND ' . dbConditionString('s.name', array_keys($screens)));
         while ($dbScreen = DBfetch($dbScreens)) {
             $screens[$dbScreen['name']]['screenid'] = $dbScreen['screenid'];
         }
         foreach ($screens as $screen) {
             $screen = $this->resolveScreenReferences($screen);
             if (isset($screen['screenid'])) {
                 $screensToUpdate[] = $screen;
             } else {
                 $screen['templateid'] = $this->referencer->resolveTemplate($template);
                 $screensToCreate[] = $screen;
             }
         }
     }
     if ($this->options['templateScreens']['createMissing'] && $screensToCreate) {
         API::TemplateScreen()->create($screensToCreate);
     }
     if ($this->options['templateScreens']['updateExisting'] && $screensToUpdate) {
         API::TemplateScreen()->update($screensToUpdate);
     }
 }
 /**
  * Delete host groups.
  *
  * @param array $groupids
  *
  * @return boolean
  */
 public function delete($groupids)
 {
     if (empty($groupids)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.'));
     }
     $groupids = zbx_toArray($groupids);
     $options = array('groupids' => $groupids, 'editable' => true, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true);
     $delGroups = $this->get($options);
     foreach ($groupids as $groupid) {
         if (!isset($delGroups[$groupid])) {
             self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
         }
         if ($delGroups[$groupid]['internal'] == ZBX_INTERNAL_GROUP) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Group "%1$s" is internal and can not be deleted.', $delGroups[$groupid]['name']));
         }
     }
     $dltGroupids = getDeletableHostGroups($groupids);
     if (count($groupids) != count($dltGroupids)) {
         foreach ($groupids as $groupid) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Group "%s" cannot be deleted, because some hosts depend on it.', $delGroups[$groupid]['name']));
         }
     }
     $dbScripts = API::Script()->get(array('groupids' => $groupids, 'output' => array('scriptid', 'groupid'), 'nopermissions' => true));
     if (!empty($dbScripts)) {
         foreach ($dbScripts as $script) {
             if ($script['groupid'] == 0) {
                 continue;
             }
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Group "%s" cannot be deleted, because it is used in a global script.', $delGroups[$script['groupid']]['name']));
         }
     }
     // delete screens items
     $resources = array(SCREEN_RESOURCE_HOSTGROUP_TRIGGERS, SCREEN_RESOURCE_HOSTS_INFO, SCREEN_RESOURCE_TRIGGERS_INFO, SCREEN_RESOURCE_TRIGGERS_OVERVIEW, SCREEN_RESOURCE_DATA_OVERVIEW);
     DB::delete('screens_items', array('resourceid' => $groupids, 'resourcetype' => $resources));
     // delete sysmap element
     if (!empty($groupids)) {
         DB::delete('sysmaps_elements', array('elementtype' => SYSMAP_ELEMENT_TYPE_HOST_GROUP, 'elementid' => $groupids));
     }
     // disable actions
     // actions from conditions
     $actionids = array();
     $dbActions = DBselect('SELECT DISTINCT c.actionid' . ' FROM conditions c' . ' WHERE c.conditiontype=' . CONDITION_TYPE_HOST_GROUP . ' AND ' . dbConditionString('c.value', $groupids));
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     // actions from operations
     $dbActions = DBselect('SELECT DISTINCT o.actionid' . ' FROM operations o,opgroup og' . ' WHERE o.operationid=og.operationid' . ' AND ' . dbConditionInt('og.groupid', $groupids));
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     if (!empty($actionids)) {
         $update = array();
         $update[] = array('values' => array('status' => ACTION_STATUS_DISABLED), 'where' => array('actionid' => $actionids));
         DB::update('actions', $update);
     }
     // delete action conditions
     DB::delete('conditions', array('conditiontype' => CONDITION_TYPE_HOST_GROUP, 'value' => $groupids));
     // delete action operation commands
     $operationids = array();
     $dbOperations = DBselect('SELECT DISTINCT og.operationid' . ' FROM opgroup og' . ' WHERE ' . dbConditionInt('og.groupid', $groupids));
     while ($dbOperation = DBfetch($dbOperations)) {
         $operationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('opgroup', array('groupid' => $groupids));
     // delete empty operations
     $delOperationids = array();
     $dbOperations = DBselect('SELECT DISTINCT o.operationid' . ' FROM operations o' . ' WHERE ' . dbConditionInt('o.operationid', $operationids) . ' AND NOT EXISTS (SELECT NULL FROM opgroup og WHERE o.operationid=og.operationid)');
     while ($dbOperation = DBfetch($dbOperations)) {
         $delOperationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('operations', array('operationid' => $delOperationids));
     // host groups
     DB::delete('groups', array('groupid' => $groupids));
     // TODO: remove audit
     foreach ($groupids as $groupid) {
         add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_HOST_GROUP, $groupid, $delGroups[$groupid]['name'], 'groups', null, null);
     }
     return array('groupids' => $groupids);
 }
Esempio n. 4
0
 /**
  * Apply filter conditions to sql built query.
  *
  * @param string $table
  * @param array  $options
  * @param array  $sqlParts
  *
  * @return bool
  */
 protected function dbFilter($table, $options, &$sqlParts)
 {
     list($table, $tableShort) = explode(' ', $table);
     $tableSchema = DB::getSchema($table);
     $filter = [];
     foreach ($options['filter'] as $field => $value) {
         // skip missing fields and text fields (not supported by Oracle)
         // skip empty values
         if (!isset($tableSchema['fields'][$field]) || $tableSchema['fields'][$field]['type'] == DB::FIELD_TYPE_TEXT || zbx_empty($value)) {
             continue;
         }
         zbx_value2array($value);
         $fieldName = $this->fieldId($field, $tableShort);
         $filter[$field] = DB::isNumericFieldType($tableSchema['fields'][$field]['type']) ? dbConditionInt($fieldName, $value) : dbConditionString($fieldName, $value);
     }
     if ($filter) {
         if (isset($sqlParts['where']['filter'])) {
             $filter[] = $sqlParts['where']['filter'];
         }
         if (is_null($options['searchByAny']) || $options['searchByAny'] === false || count($filter) == 1) {
             $sqlParts['where']['filter'] = implode(' AND ', $filter);
         } else {
             $sqlParts['where']['filter'] = '(' . implode(' OR ', $filter) . ')';
         }
         return true;
     }
     return false;
 }
Esempio n. 5
0
 /**
  * Get GraphPrototype data
  *
  * @param array $options
  * @return array
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     $sqlParts = array('select' => array('graphs' => 'g.graphid'), 'from' => array('graphs' => 'graphs g'), 'where' => array('g.flags=' . ZBX_FLAG_DISCOVERY_PROTOTYPE), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('groupids' => null, 'templateids' => null, 'hostids' => null, 'graphids' => null, 'itemids' => null, 'discoveryids' => null, 'templated' => null, 'inherited' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_EXTEND, 'selectGroups' => null, 'selectTemplates' => null, 'selectHosts' => null, 'selectItems' => null, 'selectGraphItems' => null, 'selectDiscoveryRule' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($defOptions, $options);
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
         $userGroups = getUserGroupsByUserId($userid);
         // check permissions by graph items
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM graphs_items gi,items i,hosts_groups hgg' . ' LEFT JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE g.graphid=gi.graphid' . ' AND gi.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY i.hostid' . ' HAVING MAX(permission)<' . zbx_dbstr($permission) . ' OR MIN(permission) IS NULL' . ' OR MIN(permission)=' . PERM_DENY . ')';
         // check permissions by Y min item
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM items i,hosts_groups hgg' . ' LEFT JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE g.ymin_type=' . GRAPH_YAXIS_TYPE_ITEM_VALUE . ' AND g.ymin_itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY i.hostid' . ' HAVING MAX(permission)<' . $permission . ' OR MIN(permission) IS NULL' . ' OR MIN(permission)=' . PERM_DENY . ')';
         // check permissions by Y max item
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM items i,hosts_groups hgg' . ' LEFT JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE g.ymax_type=' . GRAPH_YAXIS_TYPE_ITEM_VALUE . ' AND g.ymax_itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY i.hostid' . ' HAVING MAX(permission)<' . $permission . ' OR MIN(permission) IS NULL' . ' OR MIN(permission)=' . PERM_DENY . ')';
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']);
         $sqlParts['where'][] = 'hg.hostid=i.hostid';
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
         $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['hg'] = 'hg.groupid';
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['where'][] = dbConditionInt('i.hostid', $options['hostids']);
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.hostid';
         }
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         $sqlParts['where'][] = dbConditionInt('g.graphid', $options['graphids']);
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where'][] = dbConditionInt('gi.itemid', $options['itemids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['gi'] = 'gi.itemid';
         }
     }
     // discoveryids
     if (!is_null($options['discoveryids'])) {
         zbx_value2array($options['discoveryids']);
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['item_discovery'] = 'item_discovery id';
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where']['giid'] = 'gi.itemid=id.itemid';
         $sqlParts['where'][] = dbConditionInt('id.parent_itemid', $options['discoveryids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['id'] = 'id.parent_itemid';
         }
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
         $sqlParts['where']['ggi'] = 'g.graphid=gi.graphid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 'g.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 'g.templateid IS NULL';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('graphs g', $options, $sqlParts);
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('graphs g', $options, $sqlParts);
         if (isset($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sqlParts['from']['graphs_items'] = 'graphs_items gi';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['from']['hosts'] = 'hosts h';
             $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
             $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
             $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
             $sqlParts['where']['host'] = dbConditionString('h.host', $options['filter']['host']);
         }
         if (isset($options['filter']['hostid'])) {
             zbx_value2array($options['filter']['hostid']);
             $sqlParts['from']['graphs_items'] = 'graphs_items gi';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
             $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
             $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['filter']['hostid']);
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $dbRes = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
     while ($graph = DBfetch($dbRes)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $graph;
             } else {
                 $result = $graph['rowscount'];
             }
         } else {
             $result[$graph['graphid']] = $graph;
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     if ($result) {
         $result = $this->addRelatedObjects($options, $result);
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
 /**
  * Get GraphPrototype data
  *
  * @param array $options
  * @return array
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     // allowed columns for sorting
     $sortColumns = array('graphid', 'name', 'graphtype');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     $sqlParts = array('select' => array('graphs' => 'g.graphid'), 'from' => array('graphs' => 'graphs g'), 'where' => array('g.flags=' . ZBX_FLAG_DISCOVERY_CHILD), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'graphids' => null, 'itemids' => null, 'discoveryids' => null, 'type' => null, 'templated' => null, 'inherited' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectGroups' => null, 'selectTemplates' => null, 'selectHosts' => null, 'selectItems' => null, 'selectGraphItems' => null, 'selectDiscoveryRule' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($defOptions, $options);
     if (is_array($options['output'])) {
         unset($sqlParts['select']['graphs']);
         $dbTable = DB::getSchema('graphs');
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 'g.' . $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 graphs_items gi,items i,hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE g.graphid=gi.graphid' . ' AND gi.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY gi.graphid' . ' HAVING MIN(r.permission)>=' . $permission . ')';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['groupid'] = 'hg.groupid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']);
         $sqlParts['where'][] = 'hg.hostid=i.hostid';
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
         $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['hg'] = 'hg.groupid';
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['hostid'] = 'i.hostid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['where'][] = dbConditionInt('i.hostid', $options['hostids']);
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.hostid';
         }
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         $sqlParts['where'][] = dbConditionInt('g.graphid', $options['graphids']);
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['itemid'] = 'gi.itemid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where'][] = dbConditionInt('gi.itemid', $options['itemids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['gi'] = 'gi.itemid';
         }
     }
     // discoveryids
     if (!is_null($options['discoveryids'])) {
         zbx_value2array($options['discoveryids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['itemid'] = 'id.parent_itemid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['item_discovery'] = 'item_discovery id';
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where']['giid'] = 'gi.itemid=id.itemid';
         $sqlParts['where'][] = dbConditionInt('id.parent_itemid', $options['discoveryids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['id'] = 'id.parent_itemid';
         }
     }
     // type
     if (!is_null($options['type'])) {
         $sqlParts['where'][] = 'g.type=' . zbx_dbstr($options['type']);
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
         $sqlParts['where']['ggi'] = 'g.graphid=gi.graphid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 'g.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 'g.templateid IS NULL';
         }
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['graphs'] = 'g.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('count(DISTINCT g.graphid) as rowscount');
         // groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sqlParts['group'] as $key => $fields) {
                 $sqlParts['select'][$key] = $fields;
             }
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('graphs g', $options, $sqlParts);
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('graphs g', $options, $sqlParts);
         if (isset($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sqlParts['from']['graphs_items'] = 'graphs_items gi';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['from']['hosts'] = 'hosts h';
             $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
             $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
             $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
             $sqlParts['where']['host'] = dbConditionString('h.host', $options['filter']['host']);
         }
         if (isset($options['filter']['hostid'])) {
             zbx_value2array($options['filter']['hostid']);
             $sqlParts['from']['graphs_items'] = 'graphs_items gi';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
             $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
             $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['filter']['hostid']);
         }
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 'g');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $graphids = array();
     $sqlParts['select'] = array_unique($sqlParts['select']);
     $sqlParts['from'] = array_unique($sqlParts['from']);
     $sqlParts['where'] = array_unique($sqlParts['where']);
     $sqlParts['group'] = array_unique($sqlParts['group']);
     $sqlParts['order'] = array_unique($sqlParts['order']);
     $sqlSelect = '';
     $sqlFrom = '';
     $sqlWhere = '';
     $sqlGroup = '';
     $sqlOrder = '';
     if (!empty($sqlParts['select'])) {
         $sqlSelect .= implode(',', $sqlParts['select']);
     }
     if (!empty($sqlParts['from'])) {
         $sqlFrom .= implode(',', $sqlParts['from']);
     }
     if (!empty($sqlParts['where'])) {
         $sqlWhere .= ' AND ' . implode(' AND ', $sqlParts['where']);
     }
     if (!empty($sqlParts['group'])) {
         $sqlWhere .= ' GROUP BY ' . implode(',', $sqlParts['group']);
     }
     if (!empty($sqlParts['order'])) {
         $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']);
     }
     $sqlLimit = $sqlParts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . DBin_node('g.graphid', $nodeids) . $sqlWhere . $sqlGroup . $sqlOrder;
     $dbRes = DBselect($sql, $sqlLimit);
     while ($graph = DBfetch($dbRes)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $graph;
             } else {
                 $result = $graph['rowscount'];
             }
         } else {
             $graphids[$graph['graphid']] = $graph['graphid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$graph['graphid']] = array('graphid' => $graph['graphid']);
             } else {
                 if (!isset($result[$graph['graphid']])) {
                     $result[$graph['graphid']] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$graph['graphid']]['hosts'])) {
                     $result[$graph['graphid']]['hosts'] = array();
                 }
                 if (!is_null($options['selectGraphItems']) && !isset($result[$graph['graphid']]['gitems'])) {
                     $result[$graph['graphid']]['gitems'] = array();
                 }
                 if (!is_null($options['selectTemplates']) && !isset($result[$graph['graphid']]['templates'])) {
                     $result[$graph['graphid']]['templates'] = array();
                 }
                 if (!is_null($options['selectItems']) && !isset($result[$graph['graphid']]['items'])) {
                     $result[$graph['graphid']]['items'] = array();
                 }
                 if (!is_null($options['selectDiscoveryRule']) && !isset($result[$graph['graphid']]['discoveryRule'])) {
                     $result[$graph['graphid']]['discoveryRule'] = array();
                 }
                 // hostids
                 if (isset($graph['hostid']) && is_null($options['selectHosts'])) {
                     if (!isset($result[$graph['graphid']]['hosts'])) {
                         $result[$graph['graphid']]['hosts'] = array();
                     }
                     $result[$graph['graphid']]['hosts'][] = array('hostid' => $graph['hostid']);
                     unset($graph['hostid']);
                 }
                 // itemids
                 if (isset($graph['itemid']) && is_null($options['selectItems'])) {
                     if (!isset($result[$graph['graphid']]['items'])) {
                         $result[$graph['graphid']]['items'] = array();
                     }
                     $result[$graph['graphid']]['items'][] = array('itemid' => $graph['itemid']);
                     unset($graph['itemid']);
                 }
                 $result[$graph['graphid']] += $graph;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     // adding GraphItems
     if (!is_null($options['selectGraphItems']) && str_in_array($options['selectGraphItems'], $subselectsAllowedOutputs)) {
         $gitems = API::GraphItem()->get(array('nodeids' => $nodeids, 'output' => $options['selectGraphItems'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true));
         foreach ($gitems as $gitem) {
             $ggraphs = $gitem['graphs'];
             unset($gitem['graphs']);
             foreach ($ggraphs as $graph) {
                 $result[$graph['graphid']]['gitems'][$gitem['gitemid']] = $gitem;
             }
         }
     }
     // adding Hostgroups
     if (!is_null($options['selectGroups'])) {
         if (is_array($options['selectGroups']) || str_in_array($options['selectGroups'], $subselectsAllowedOutputs)) {
             $groups = API::HostGroup()->get(array('nodeids' => $nodeids, 'output' => $options['selectGroups'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true));
             foreach ($groups as $group) {
                 $ggraphs = $group['graphs'];
                 unset($group['graphs']);
                 foreach ($ggraphs as $graph) {
                     $result[$graph['graphid']]['groups'][] = $group;
                 }
             }
         }
     }
     // adding Hosts
     if (!is_null($options['selectHosts'])) {
         if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) {
             $hosts = API::Host()->get(array('nodeids' => $nodeids, 'output' => $options['selectHosts'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true));
             foreach ($hosts as $host) {
                 $hgraphs = $host['graphs'];
                 unset($host['graphs']);
                 foreach ($hgraphs as $graph) {
                     $result[$graph['graphid']]['hosts'][] = $host;
                 }
             }
         }
     }
     // adding Templates
     if (!is_null($options['selectTemplates']) && str_in_array($options['selectTemplates'], $subselectsAllowedOutputs)) {
         $templates = API::Template()->get(array('nodeids' => $nodeids, 'output' => $options['selectTemplates'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true));
         foreach ($templates as $template) {
             $tgraphs = $template['graphs'];
             unset($template['graphs']);
             foreach ($tgraphs as $graph) {
                 $result[$graph['graphid']]['templates'][] = $template;
             }
         }
     }
     // adding Items
     if (!is_null($options['selectItems']) && str_in_array($options['selectItems'], $subselectsAllowedOutputs)) {
         $items = API::Item()->get(array('nodeids' => $nodeids, 'output' => $options['selectItems'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true, 'filter' => array('flags' => null)));
         foreach ($items as $item) {
             $igraphs = $item['graphs'];
             unset($item['graphs']);
             foreach ($igraphs as $graph) {
                 $result[$graph['graphid']]['items'][] = $item;
             }
         }
     }
     // adding discoveryRule
     if (!is_null($options['selectDiscoveryRule'])) {
         $ruleids = $ruleMap = array();
         $dbRules = DBselect('SELECT id.parent_itemid,gi.graphid' . ' FROM item_discovery id,graphs_items gi' . ' WHERE ' . dbConditionInt('gi.graphid', $graphids) . ' AND gi.itemid=id.itemid');
         while ($rule = DBfetch($dbRules)) {
             $ruleids[$rule['parent_itemid']] = $rule['parent_itemid'];
             $ruleMap[$rule['graphid']] = $rule['parent_itemid'];
         }
         $objParams = array('nodeids' => $nodeids, 'itemids' => $ruleids, 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectDiscoveryRule']) || str_in_array($options['selectDiscoveryRule'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectDiscoveryRule'];
             $discoveryRules = API::DiscoveryRule()->get($objParams);
             foreach ($result as $graphid => $graph) {
                 if (isset($ruleMap[$graphid]) && isset($discoveryRules[$ruleMap[$graphid]])) {
                     $result[$graphid]['discoveryRule'] = $discoveryRules[$ruleMap[$graphid]];
                 }
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Esempio n. 7
0
 /**
  * Delete Host.
  *
  * @param array	$hostIds
  * @param bool	$nopermissions
  *
  * @return array
  */
 public function delete(array $hostIds, $nopermissions = false)
 {
     $this->validateDelete($hostIds, $nopermissions);
     // delete the discovery rules first
     $delRules = API::DiscoveryRule()->get(['output' => ['itemid'], 'hostids' => $hostIds, 'nopermissions' => true, 'preservekeys' => true]);
     if ($delRules) {
         API::DiscoveryRule()->delete(array_keys($delRules), true);
     }
     // delete the items
     $delItems = API::Item()->get(['templateids' => $hostIds, 'output' => ['itemid'], 'nopermissions' => true, 'preservekeys' => true]);
     if ($delItems) {
         API::Item()->delete(array_keys($delItems), true);
     }
     // delete web tests
     $delHttptests = [];
     $dbHttptests = get_httptests_by_hostid($hostIds);
     while ($dbHttptest = DBfetch($dbHttptests)) {
         $delHttptests[$dbHttptest['httptestid']] = $dbHttptest['httptestid'];
     }
     if (!empty($delHttptests)) {
         API::HttpTest()->delete($delHttptests, true);
     }
     // delete screen items
     DB::delete('screens_items', ['resourceid' => $hostIds, 'resourcetype' => SCREEN_RESOURCE_HOST_TRIGGERS]);
     // delete host from maps
     if (!empty($hostIds)) {
         DB::delete('sysmaps_elements', ['elementtype' => SYSMAP_ELEMENT_TYPE_HOST, 'elementid' => $hostIds]);
     }
     // disable actions
     // actions from conditions
     $actionids = [];
     $sql = 'SELECT DISTINCT actionid' . ' FROM conditions' . ' WHERE conditiontype=' . CONDITION_TYPE_HOST . ' AND ' . dbConditionString('value', $hostIds);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     // actions from operations
     $sql = 'SELECT DISTINCT o.actionid' . ' FROM operations o, opcommand_hst oh' . ' WHERE o.operationid=oh.operationid' . ' AND ' . dbConditionInt('oh.hostid', $hostIds);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     if (!empty($actionids)) {
         $update = [];
         $update[] = ['values' => ['status' => ACTION_STATUS_DISABLED], 'where' => ['actionid' => $actionids]];
         DB::update('actions', $update);
     }
     // delete action conditions
     DB::delete('conditions', ['conditiontype' => CONDITION_TYPE_HOST, 'value' => $hostIds]);
     // delete action operation commands
     $operationids = [];
     $sql = 'SELECT DISTINCT oh.operationid' . ' FROM opcommand_hst oh' . ' WHERE ' . dbConditionInt('oh.hostid', $hostIds);
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $operationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('opcommand_hst', ['hostid' => $hostIds]);
     // delete empty operations
     $delOperationids = [];
     $sql = 'SELECT DISTINCT o.operationid' . ' FROM operations o' . ' WHERE ' . dbConditionInt('o.operationid', $operationids) . ' AND NOT EXISTS(SELECT oh.opcommand_hstid FROM opcommand_hst oh WHERE oh.operationid=o.operationid)';
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $delOperationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('operations', ['operationid' => $delOperationids]);
     $hosts = API::Host()->get(['output' => ['hostid', 'name'], 'hostids' => $hostIds, 'nopermissions' => true]);
     // delete host inventory
     DB::delete('host_inventory', ['hostid' => $hostIds]);
     // delete host applications
     DB::delete('applications', ['hostid' => $hostIds]);
     // delete host
     DB::delete('hosts', ['hostid' => $hostIds]);
     // TODO: remove info from API
     foreach ($hosts as $host) {
         info(_s('Deleted: Host "%1$s".', $host['name']));
         add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_HOST, $host['hostid'], $host['name'], 'hosts', NULL, NULL);
     }
     // remove Monitoring > Latest data toggle profile values related to given hosts
     DB::delete('profiles', ['idx' => 'web.latest.toggle_other', 'idx2' => $hostIds]);
     return ['hostids' => $hostIds];
 }
 /**
  * Get Itemprototype data
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     // allowed columns for sorting
     $sortColumns = array('itemid', 'name', 'key_', 'delay', 'history', 'trends', 'type', 'status');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     $sqlParts = array('select' => array('items' => 'i.itemid'), 'from' => array('items' => 'items i'), 'where' => array('i.flags=' . ZBX_FLAG_DISCOVERY_CHILD), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'itemids' => null, 'discoveryids' => null, 'graphids' => null, 'triggerids' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectHosts' => null, 'selectApplications' => null, 'selectTriggers' => null, 'selectGraphs' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($defOptions, $options);
     if (is_array($options['output'])) {
         unset($sqlParts['select']['items']);
         $dbTable = DB::getSchema('items');
         $sqlParts['select']['itemid'] = 'i.itemid';
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 'i.' . $field;
             }
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $userGroups = getUserGroupsByUserId($userid);
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE i.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>=' . $permission . ')';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sqlParts['select']['hostid'] = 'i.hostid';
         }
         $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['hostids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.hostid';
         }
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         $sqlParts['where']['itemid'] = dbConditionInt('i.itemid', $options['itemids']);
     }
     // discoveryids
     if (!is_null($options['discoveryids'])) {
         zbx_value2array($options['discoveryids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['discoveryid'] = 'id.parent_itemid';
         }
         $sqlParts['from']['item_discovery'] = 'item_discovery id';
         $sqlParts['where'][] = dbConditionInt('id.parent_itemid', $options['discoveryids']);
         $sqlParts['where']['idi'] = 'i.itemid=id.itemid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['id'] = 'id.parent_itemid';
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['triggerid'] = 'f.triggerid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['where'][] = dbConditionInt('f.triggerid', $options['triggerids']);
         $sqlParts['where']['if'] = 'i.itemid=f.itemid';
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['graphid'] = 'gi.graphid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['where'][] = dbConditionInt('gi.graphid', $options['graphids']);
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 'i.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 'i.templateid IS NULL';
         }
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // monitored
     if (!is_null($options['monitored'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['monitored']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_MONITORED;
             $sqlParts['where'][] = 'i.status=' . ITEM_STATUS_ACTIVE;
         } else {
             $sqlParts['where'][] = '(h.status<>' . HOST_STATUS_MONITORED . ' OR i.status<>' . ITEM_STATUS_ACTIVE . ')';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('items i', $options, $sqlParts);
     }
     // --- FILTER ---
     if (is_array($options['filter'])) {
         $this->dbFilter('items i', $options, $sqlParts);
         if (isset($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sqlParts['from']['hosts'] = 'hosts h';
             $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
             $sqlParts['where']['h'] = dbConditionString('h.host', $options['filter']['host']);
         }
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['items'] = 'i.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('count(DISTINCT i.itemid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sqlParts['group'] as $key => $fields) {
                 $sqlParts['select'][$key] = $fields;
             }
         }
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 'i');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     //----------
     $itemids = array();
     $sqlParts['select'] = array_unique($sqlParts['select']);
     $sqlParts['from'] = array_unique($sqlParts['from']);
     $sqlParts['where'] = array_unique($sqlParts['where']);
     $sqlParts['group'] = array_unique($sqlParts['group']);
     $sqlParts['order'] = array_unique($sqlParts['order']);
     $sqlSelect = '';
     $sqlFrom = '';
     $sqlWhere = '';
     $sqlGroup = '';
     $sqlOrder = '';
     if (!empty($sqlParts['select'])) {
         $sqlSelect .= implode(',', $sqlParts['select']);
     }
     if (!empty($sqlParts['from'])) {
         $sqlFrom .= implode(',', $sqlParts['from']);
     }
     if (!empty($sqlParts['where'])) {
         $sqlWhere .= ' AND ' . implode(' AND ', $sqlParts['where']);
     }
     if (!empty($sqlParts['group'])) {
         $sqlWhere .= ' GROUP BY ' . implode(',', $sqlParts['group']);
     }
     if (!empty($sqlParts['order'])) {
         $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']);
     }
     $sqlLimit = $sqlParts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . DBin_node('i.itemid', $nodeids) . $sqlWhere . $sqlGroup . $sqlOrder;
     //SDI($sql);
     $res = DBselect($sql, $sqlLimit);
     while ($item = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $item;
             } else {
                 $result = $item['rowscount'];
             }
         } else {
             $itemids[$item['itemid']] = $item['itemid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$item['itemid']] = array('itemid' => $item['itemid']);
             } else {
                 if (!isset($result[$item['itemid']])) {
                     $result[$item['itemid']] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$item['itemid']]['hosts'])) {
                     $result[$item['itemid']]['hosts'] = array();
                 }
                 if (!is_null($options['selectApplications']) && !isset($result[$item['itemid']]['applications'])) {
                     $result[$item['itemid']]['applications'] = array();
                 }
                 if (!is_null($options['selectTriggers']) && !isset($result[$item['itemid']]['triggers'])) {
                     $result[$item['itemid']]['triggers'] = array();
                 }
                 if (!is_null($options['selectGraphs']) && !isset($result[$item['itemid']]['graphs'])) {
                     $result[$item['itemid']]['graphs'] = array();
                 }
                 // hostids
                 if (isset($item['hostid']) && is_null($options['selectHosts'])) {
                     if (!isset($result[$item['itemid']]['hosts'])) {
                         $result[$item['itemid']]['hosts'] = array();
                     }
                     $result[$item['itemid']]['hosts'][] = array('hostid' => $item['hostid']);
                 }
                 // triggerids
                 if (isset($item['triggerid']) && is_null($options['selectTriggers'])) {
                     if (!isset($result[$item['itemid']]['triggers'])) {
                         $result[$item['itemid']]['triggers'] = array();
                     }
                     $result[$item['itemid']]['triggers'][] = array('triggerid' => $item['triggerid']);
                     unset($item['triggerid']);
                 }
                 // graphids
                 if (isset($item['graphid']) && is_null($options['selectGraphs'])) {
                     if (!isset($result[$item['itemid']]['graphs'])) {
                         $result[$item['itemid']]['graphs'] = array();
                     }
                     $result[$item['itemid']]['graphs'][] = array('graphid' => $item['graphid']);
                     unset($item['graphid']);
                 }
                 // discoveryids
                 if (isset($item['discoveryids'])) {
                     if (!isset($result[$item['itemid']]['discovery'])) {
                         $result[$item['itemid']]['discovery'] = array();
                     }
                     $result[$item['itemid']]['discovery'][] = array('ruleid' => $item['item_parentid']);
                     unset($item['item_parentid']);
                 }
                 $result[$item['itemid']] += $item;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     // Adding Objects
     // Adding hosts
     if (!is_null($options['selectHosts'])) {
         if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) {
             $objParams = array('nodeids' => $nodeids, 'itemids' => $itemids, 'templated_hosts' => 1, 'output' => $options['selectHosts'], 'nopermissions' => 1, 'preservekeys' => 1);
             $hosts = API::Host()->get($objParams);
             foreach ($hosts as $host) {
                 $hitems = $host['items'];
                 unset($host['items']);
                 foreach ($hitems as $inum => $item) {
                     $result[$item['itemid']]['hosts'][] = $host;
                 }
             }
             $templates = API::Template()->get($objParams);
             foreach ($templates as $template) {
                 $titems = $template['items'];
                 unset($template['items']);
                 foreach ($titems as $inum => $item) {
                     $result[$item['itemid']]['hosts'][] = $template;
                 }
             }
         }
     }
     // Adding triggers
     if (!is_null($options['selectTriggers'])) {
         $objParams = array('nodeids' => $nodeids, 'itemids' => $itemids, 'preservekeys' => true);
         if (in_array($options['selectTriggers'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectTriggers'];
             $triggers = API::TriggerPrototype()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($triggers, 'description');
             }
             $count = array();
             foreach ($triggers as $triggerid => $trigger) {
                 unset($triggers[$triggerid]['items']);
                 foreach ($trigger['items'] as $item) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$item['itemid']])) {
                             $count[$item['itemid']] = 0;
                         }
                         $count[$item['itemid']]++;
                         if ($count[$item['itemid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$item['itemid']]['triggers'][] =& $triggers[$triggerid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectTriggers']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $triggers = API::TriggerPrototype()->get($objParams);
             $triggers = zbx_toHash($triggers, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($triggers[$itemid])) {
                     $result[$itemid]['triggers'] = $triggers[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['triggers'] = 0;
                 }
             }
         }
     }
     // Adding applications
     if (!is_null($options['selectApplications']) && str_in_array($options['selectApplications'], $subselectsAllowedOutputs)) {
         $objParams = array('nodeids' => $nodeids, 'output' => $options['selectApplications'], 'itemids' => $itemids, 'preservekeys' => 1);
         $applications = API::Application()->get($objParams);
         foreach ($applications as $applicationid => $application) {
             $aitems = $application['items'];
             unset($application['items']);
             foreach ($aitems as $inum => $item) {
                 $result[$item['itemid']]['applications'][] = $application;
             }
         }
     }
     // Adding graphs
     if (!is_null($options['selectGraphs'])) {
         $objParams = array('nodeids' => $nodeids, 'itemids' => $itemids, 'preservekeys' => true);
         if (in_array($options['selectGraphs'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectGraphs'];
             $graphs = API::GraphPrototype()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($graphs, 'name');
             }
             $count = array();
             foreach ($graphs as $graphid => $graph) {
                 unset($graphs[$graphid]['items']);
                 foreach ($graph['items'] as $item) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$item['itemid']])) {
                             $count[$item['itemid']] = 0;
                         }
                         $count[$item['itemid']]++;
                         if ($count[$item['itemid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$item['itemid']]['graphs'][] =& $graphs[$graphid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectGraphs']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $graphs = API::GraphPrototype()->get($objParams);
             $graphs = zbx_toHash($graphs, 'parent_itemid');
             foreach ($result as $itemid => $item) {
                 if (isset($graphs[$itemid])) {
                     $result[$itemid]['graphs'] = $graphs[$itemid]['rowscount'];
                 } else {
                     $result[$itemid]['graphs'] = 0;
                 }
             }
         }
     }
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Esempio n. 9
0
 /**
  * Check if any item from list already exists.
  * If items have item ids it will check for existing item with different itemid.
  *
  * @throw APIException
  *
  * @param array $items
  */
 protected function checkExistingItems(array $items)
 {
     $itemKeysByHostId = array();
     $itemIds = array();
     foreach ($items as $item) {
         if (!isset($itemKeysByHostId[$item['hostid']])) {
             $itemKeysByHostId[$item['hostid']] = array();
         }
         $itemKeysByHostId[$item['hostid']][] = $item['key_'];
         if (isset($item['itemid'])) {
             $itemIds[] = $item['itemid'];
         }
     }
     $sqlWhere = array();
     foreach ($itemKeysByHostId as $hostId => $keys) {
         $sqlWhere[] = '(i.hostid=' . $hostId . ' AND ' . dbConditionString('i.key_', $keys) . ')';
     }
     if ($sqlWhere) {
         $sql = 'SELECT i.key_,h.host' . ' FROM items i,hosts h' . ' WHERE i.hostid=h.hostid AND (' . implode(' OR ', $sqlWhere) . ')';
         // if we update existing items we need to exclude them from result.
         if ($itemIds) {
             $sql .= ' AND ' . dbConditionInt('i.itemid', $itemIds, true);
         }
         $dbItems = DBselect($sql, 1);
         while ($dbItem = DBfetch($dbItems)) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Item with key "%1$s" already exists on "%2$s".', $dbItem['key_'], $dbItem['host']));
         }
     }
 }
Esempio n. 10
0
 /**
  * Delete proxy.
  *
  * @param array	$proxyIds
  *
  * @return array
  */
 public function delete(array $proxyIds)
 {
     $this->validateDelete($proxyIds);
     $dbProxies = DBselect('SELECT h.hostid,h.host' . ' FROM hosts h' . ' WHERE ' . dbConditionInt('h.hostid', $proxyIds));
     $dbProxies = DBfetchArrayAssoc($dbProxies, 'hostid');
     $actionIds = array();
     // get conditions
     $dbActions = DBselect('SELECT DISTINCT c.actionid' . ' FROM conditions c' . ' WHERE c.conditiontype=' . CONDITION_TYPE_PROXY . ' AND ' . dbConditionString('c.value', $proxyIds));
     while ($dbAction = DBfetch($dbActions)) {
         $actionIds[$dbAction['actionid']] = $dbAction['actionid'];
     }
     if ($actionIds) {
         DB::update('actions', array('values' => array('status' => ACTION_STATUS_DISABLED), 'where' => array('actionid' => $actionIds)));
     }
     // delete action conditions
     DB::delete('conditions', array('conditiontype' => CONDITION_TYPE_PROXY, 'value' => $proxyIds));
     // delete interface
     DB::delete('interface', array('hostid' => $proxyIds));
     // delete host
     DB::delete('hosts', array('hostid' => $proxyIds));
     // TODO: remove info from API
     foreach ($dbProxies as $proxy) {
         info(_s('Deleted: Proxy "%1$s".', $proxy['host']));
         add_audit(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_PROXY, '[' . $proxy['host'] . '] [' . $proxy['hostid'] . ']');
     }
     return array('proxyids' => $proxyIds);
 }
Esempio n. 11
0
 /**
  * Resolve functional macros, like {hostname:key.function(param)}.
  * If macro can not be resolved it is replaced with UNRESOLVED_MACRO_STRING string i.e. "*UNKNOWN*".
  *
  * Supports function "last", "min", "max" and "avg".
  * Supports seconds as parameters, except "last" function.
  * Second parameter like {hostname:key.last(0,86400) and offsets like {hostname:key.last(#1)} are not supported.
  * Supports postfixes s,m,h,d and w for parameter.
  *
  * @param array  $sourceStringList			list of strings from graphs in which macros should be resolved
  * @param array  $itemsList					list of lists of graph items used in graphs
  * @param int    $itemsList[n][m]['hostid']	n-th graph m-th item corresponding host ID
  * @param string $itemsList[n][m]['host']	n-th graph m-th item corresponding host name
  *
  * @return array	list of strings, possibly with macros in them replaced with resolved values
  */
 private function resolveGraphsFunctionalItemMacros(array $sourceStringList, array $itemsList)
 {
     $hostKeyPairs = [];
     $matchesList = [];
     $items = reset($itemsList);
     foreach ($sourceStringList as $sourceString) {
         /*
          * Extract all macros into $matches - keys: macros, hosts, keys, functions and parameters are used
          * searches for macros, for example, "{somehost:somekey["param[123]"].min(10m)}"
          */
         preg_match_all('/(?P<macros>{' . '(?P<hosts>(' . ZBX_PREG_HOST_FORMAT . '|({(' . self::PATTERN_HOST_INTERNAL . ')' . self::PATTERN_MACRO_PARAM . '}))):' . '(?P<keys>' . ZBX_PREG_ITEM_KEY_FORMAT . ')\\.' . '(?P<functions>(last|max|min|avg))\\(' . '(?P<parameters>([0-9]+[' . ZBX_TIME_SUFFIXES . ']?)?)' . '\\)}{1})/Uux', $sourceString, $matches, PREG_OFFSET_CAPTURE);
         foreach ($matches['hosts'] as $i => &$host) {
             $host[0] = $this->resolveGraphPositionalMacros($host[0], $items);
             if ($host[0] !== UNRESOLVED_MACRO_STRING) {
                 // Take note that resolved host has a such key (and it is used in a macro).
                 if (!isset($hostKeyPairs[$host[0]])) {
                     $hostKeyPairs[$host[0]] = [];
                 }
                 $hostKeyPairs[$host[0]][$matches['keys'][$i][0]] = true;
             }
         }
         unset($host);
         // Remember match for later use.
         $matchesList[] = $matches;
         $items = next($itemsList);
     }
     /*
      * If no host/key pairs found in macro-like parts of source string then there is nothing to do but return
      * source strings as they are.
      */
     if (!$hostKeyPairs) {
         return $sourceStringList;
     }
     // Build item retrieval query from host-key pairs and get all necessary items for all source strings.
     $queryParts = [];
     foreach ($hostKeyPairs as $host => $keys) {
         $queryParts[] = '(h.host=' . zbx_dbstr($host) . ' AND ' . dbConditionString('i.key_', array_keys($keys)) . ')';
     }
     $items = DBfetchArrayAssoc(DBselect('SELECT h.host,i.key_,i.itemid,i.value_type,i.units,i.valuemapid' . ' FROM items i,hosts h' . ' WHERE i.hostid=h.hostid' . ' AND (' . join(' OR ', $queryParts) . ')'), 'itemid');
     // Get items for which user has permission.
     $allowedItems = API::Item()->get(['itemids' => array_keys($items), 'webitems' => true, 'output' => ['itemid', 'value_type', 'lastvalue', 'lastclock'], 'preservekeys' => true]);
     // Get map item data only for those allowed items and set "value_type" for allowed items.
     foreach ($items as $item) {
         if (isset($allowedItems[$item['itemid']])) {
             $item['lastvalue'] = $allowedItems[$item['itemid']]['lastvalue'];
             $item['lastclock'] = $allowedItems[$item['itemid']]['lastclock'];
             $hostKeyPairs[$item['host']][$item['key_']] = $item;
         }
     }
     /*
      * Replace macros with their corresponding values in graph strings and replace macros with their resolved
      * values in source strings.
      */
     $matches = reset($matchesList);
     foreach ($sourceStringList as &$sourceString) {
         /*
          * We iterate array backwards so that replacing unresolved macro string (see lower) with actual value
          * does not mess up originally captured offsets.
          */
         $i = count($matches['macros']);
         while ($i--) {
             $host = $matches['hosts'][$i][0];
             $key = $matches['keys'][$i][0];
             $function = $matches['functions'][$i][0];
             $parameter = $matches['parameters'][$i][0];
             // If host is real and item exists and has permissions.
             if ($host !== UNRESOLVED_MACRO_STRING && is_array($hostKeyPairs[$host][$key])) {
                 $item = $hostKeyPairs[$host][$key];
                 // Macro function is "last".
                 if ($function == 'last') {
                     $value = $item['lastclock'] > 0 ? formatHistoryValue($item['lastvalue'], $item) : UNRESOLVED_MACRO_STRING;
                 } else {
                     $value = getItemFunctionalValue($item, $function, $parameter);
                 }
             } else {
                 $value = UNRESOLVED_MACRO_STRING;
             }
             /*
              * Replace macro string with actual, resolved string value. This is safe because we start from far
              * end of $sourceString.
              */
             $sourceString = substr_replace($sourceString, $value, $matches['macros'][$i][1], strlen($matches['macros'][$i][0]));
         }
         // Advance to next matches for next $sourceString.
         $matches = next($matchesList);
     }
     unset($sourceString);
     return $sourceStringList;
 }
Esempio n. 12
0
 /**
  * Delete related action conditions.
  *
  * @param array $dCheckIds
  */
 protected function deleteActionConditions(array $dCheckIds)
 {
     $actionIds = array();
     // conditions
     $dbActions = DBselect('SELECT DISTINCT c.actionid' . ' FROM conditions c' . ' WHERE c.conditiontype=' . CONDITION_TYPE_DCHECK . ' AND ' . dbConditionString('c.value', $dCheckIds) . ' ORDER BY c.actionid');
     while ($dbAction = DBfetch($dbActions)) {
         $actionIds[] = $dbAction['actionid'];
     }
     // disabling actions with deleted conditions
     if ($actionIds) {
         DB::update('actions', array('values' => array('status' => ACTION_STATUS_DISABLED), 'where' => array('actionid' => $actionIds)));
         DB::delete('conditions', array('conditiontype' => CONDITION_TYPE_DCHECK, 'value' => $dCheckIds));
     }
 }
Esempio n. 13
0
 /**
  * Removes profile values from DB and profiles cache
  *
  * @param string $idx	first identifier
  * @param mixed  $idx2	second identifier, which can be list of identifiers as well
  */
 public static function delete($idx, $idx2)
 {
     if (!is_array($idx2)) {
         $idx2 = array($idx2);
     }
     // remove from DB
     DBexecute('DELETE FROM profiles WHERE idx=' . zbx_dbstr($idx) . ' AND ' . dbConditionString('idx2', $idx2));
     // remove from cache
     if (!is_null(self::$profiles)) {
         foreach ($idx2 as $v) {
             unset(self::$profiles[$idx][$v]);
         }
     }
 }
Esempio n. 14
0
 /**
  * Check if a host with the same value in $field already exists on an LLD rule.
  * If host prototypes have host IDs it will check for existing prototypes with different host IDs.
  *
  * @throw APIException
  *
  * @param array $hostPrototypes
  * @param string $field				name of the field to check uniqueness by
  * @param string $error				error message in case duplicates are found
  */
 protected function checkExistingHostPrototypes(array $hostPrototypes, $field, $error)
 {
     $valuesByDiscoveryRuleId = [];
     $hostIds = [];
     foreach ($hostPrototypes as $hostPrototype) {
         $valuesByDiscoveryRuleId[$hostPrototype['ruleid']][] = $hostPrototype[$field];
         if (isset($hostPrototype['hostid'])) {
             $hostIds[] = $hostPrototype['hostid'];
         }
     }
     $sqlWhere = [];
     foreach ($valuesByDiscoveryRuleId as $discoveryRuleId => $values) {
         $sqlWhere[] = '(hd.parent_itemid=' . zbx_dbstr($discoveryRuleId) . ' AND ' . dbConditionString('h.' . $field, $values) . ')';
     }
     if ($sqlWhere) {
         $sql = 'SELECT i.name as discovery_name,h.' . $field . ' FROM hosts h,host_discovery hd,items i' . ' WHERE h.hostid=hd.hostid AND hd.parent_itemid=i.itemid AND (' . implode(' OR ', $sqlWhere) . ')';
         // if we update existing items we need to exclude them from result.
         if ($hostIds) {
             $sql .= ' AND ' . dbConditionInt('h.hostid', $hostIds, true);
         }
         $query = DBselect($sql, 1);
         while ($row = DBfetch($query)) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s($error, $row[$field], $row['discovery_name']));
         }
     }
 }
Esempio n. 15
0
 /**
  * Get actions data
  *
  * @param array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['actionids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['extendoutput']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  *
  * @return array|int item data as array or false if error
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     $sqlParts = array('select' => array('actions' => 'a.actionid'), 'from' => array('actions' => 'actions a'), 'where' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'actionids' => null, 'triggerids' => null, 'mediatypeids' => null, 'usrgrpids' => null, 'userids' => null, 'scriptids' => null, 'nopermissions' => null, 'editable' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectConditions' => null, 'selectOperations' => null, 'countOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($defOptions, $options);
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         // conditions are checked here by sql, operations after, by api queries
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
         $userGroups = getUserGroupsByUserId($userid);
         // condition hostgroup
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM conditions cc' . ' LEFT JOIN rights r' . ' ON r.id=' . zbx_dbcast_2bigint('cc.value') . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE a.actionid=cc.actionid' . ' AND cc.conditiontype=' . CONDITION_TYPE_HOST_GROUP . ' GROUP BY cc.value' . ' HAVING MIN(r.permission) IS NULL' . ' OR MIN(r.permission)=' . PERM_DENY . ' OR MAX(r.permission)<' . $permission . ')';
         // condition host or template
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM conditions cc,hosts_groups hgg' . ' LEFT JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE a.actionid=cc.actionid' . ' AND ' . zbx_dbcast_2bigint('cc.value') . '=hgg.hostid' . ' AND cc.conditiontype IN (' . CONDITION_TYPE_HOST . ',' . CONDITION_TYPE_TEMPLATE . ')' . ' GROUP BY cc.value' . ' HAVING MIN(r.permission) IS NULL' . ' OR MIN(r.permission)=' . PERM_DENY . ' OR MAX(r.permission)<' . $permission . ')';
         // condition trigger
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM conditions cc,functions f,items i,hosts_groups hgg' . ' LEFT JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE a.actionid=cc.actionid' . ' AND ' . zbx_dbcast_2bigint('cc.value') . '=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' AND cc.conditiontype=' . CONDITION_TYPE_TRIGGER . ' GROUP BY cc.value' . ' HAVING MIN(r.permission) IS NULL' . ' OR MIN(r.permission)=' . PERM_DENY . ' OR MAX(r.permission)<' . $permission . ')';
     }
     // actionids
     if (!is_null($options['actionids'])) {
         zbx_value2array($options['actionids']);
         $sqlParts['select']['actionid'] = 'a.actionid';
         $sqlParts['where'][] = dbConditionInt('a.actionid', $options['actionids']);
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         $sqlParts['select']['groupids'] = 'cg.value';
         $sqlParts['from']['conditions_groups'] = 'conditions cg';
         $sqlParts['where'][] = dbConditionString('cg.value', $options['groupids']);
         $sqlParts['where']['ctg'] = 'cg.conditiontype=' . CONDITION_TYPE_HOST_GROUP;
         $sqlParts['where']['acg'] = 'a.actionid=cg.actionid';
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         $sqlParts['select']['hostids'] = 'ch.value';
         $sqlParts['from']['conditions_hosts'] = 'conditions ch';
         $sqlParts['where'][] = dbConditionString('ch.value', $options['hostids']);
         $sqlParts['where']['cth'] = 'ch.conditiontype=' . CONDITION_TYPE_HOST;
         $sqlParts['where']['ach'] = 'a.actionid=ch.actionid';
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         $sqlParts['select']['triggerids'] = 'ct.value';
         $sqlParts['from']['conditions_triggers'] = 'conditions ct';
         $sqlParts['where'][] = dbConditionString('ct.value', $options['triggerids']);
         $sqlParts['where']['ctt'] = 'ct.conditiontype=' . CONDITION_TYPE_TRIGGER;
         $sqlParts['where']['act'] = 'a.actionid=ct.actionid';
     }
     // mediatypeids
     if (!is_null($options['mediatypeids'])) {
         zbx_value2array($options['mediatypeids']);
         $sqlParts['select']['mediatypeid'] = 'om.mediatypeid';
         $sqlParts['from']['opmessage'] = 'opmessage om';
         $sqlParts['from']['operations_media'] = 'operations omed';
         $sqlParts['where'][] = dbConditionInt('om.mediatypeid', $options['mediatypeids']);
         $sqlParts['where']['aomed'] = 'a.actionid=omed.actionid';
         $sqlParts['where']['oom'] = 'omed.operationid=om.operationid';
     }
     // operation messages
     // usrgrpids
     if (!is_null($options['usrgrpids'])) {
         zbx_value2array($options['usrgrpids']);
         $sqlParts['select']['usrgrpid'] = 'omg.usrgrpid';
         $sqlParts['from']['opmessage_grp'] = 'opmessage_grp omg';
         $sqlParts['from']['operations_usergroups'] = 'operations oug';
         $sqlParts['where'][] = dbConditionInt('omg.usrgrpid', $options['usrgrpids']);
         $sqlParts['where']['aoug'] = 'a.actionid=oug.actionid';
         $sqlParts['where']['oomg'] = 'oug.operationid=omg.operationid';
     }
     // userids
     if (!is_null($options['userids'])) {
         zbx_value2array($options['userids']);
         $sqlParts['select']['userid'] = 'omu.userid';
         $sqlParts['from']['opmessage_usr'] = '******';
         $sqlParts['from']['operations_users'] = 'operations ou';
         $sqlParts['where'][] = dbConditionInt('omu.userid', $options['userids']);
         $sqlParts['where']['aou'] = 'a.actionid=ou.actionid';
         $sqlParts['where']['oomu'] = 'ou.operationid=omu.operationid';
     }
     // operation commands
     // scriptids
     if (!is_null($options['scriptids'])) {
         zbx_value2array($options['scriptids']);
         $sqlParts['select']['scriptid'] = 'oc.scriptid';
         $sqlParts['from']['opcommand'] = 'opcommand oc';
         $sqlParts['from']['operations_scripts'] = 'operations os';
         $sqlParts['where'][] = '(' . dbConditionInt('oc.scriptid', $options['scriptids']) . ' AND oc.type=' . ZBX_SCRIPT_TYPE_GLOBAL_SCRIPT . ')';
         $sqlParts['where']['aos'] = 'a.actionid=os.actionid';
         $sqlParts['where']['ooc'] = 'os.operationid=oc.operationid';
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('actions a', $options, $sqlParts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('actions a', $options, $sqlParts);
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $actionids = array();
     $sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $dbRes = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
     while ($action = DBfetch($dbRes)) {
         if ($options['countOutput']) {
             $result = $action['rowscount'];
         } else {
             $actionids[$action['actionid']] = $action['actionid'];
             if (!isset($result[$action['actionid']])) {
                 $result[$action['actionid']] = array();
             }
             $result[$action['actionid']] += $action;
             // return mediatype as array
             if (!empty($action['mediatypeid'])) {
                 $result[$action['actionid']]['mediatypeids'][] = $action['mediatypeid'];
             }
             unset($result[$action['actionid']]['mediatypeid']);
         }
     }
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         // check hosts, templates
         $hosts = $hostids = array();
         $sql = 'SELECT o.actionid,och.hostid' . ' FROM operations o,opcommand_hst och' . ' WHERE o.operationid=och.operationid' . ' AND och.hostid<>0' . ' AND ' . dbConditionInt('o.actionid', $actionids);
         $dbHosts = DBselect($sql);
         while ($host = DBfetch($dbHosts)) {
             if (!isset($hosts[$host['hostid']])) {
                 $hosts[$host['hostid']] = array();
             }
             $hosts[$host['hostid']][$host['actionid']] = $host['actionid'];
             $hostids[$host['hostid']] = $host['hostid'];
         }
         $dbTemplates = DBselect('SELECT o.actionid,ot.templateid' . ' FROM operations o,optemplate ot' . ' WHERE o.operationid=ot.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($template = DBfetch($dbTemplates)) {
             if (!isset($hosts[$template['templateid']])) {
                 $hosts[$template['templateid']] = array();
             }
             $hosts[$template['templateid']][$template['actionid']] = $template['actionid'];
             $hostids[$template['templateid']] = $template['templateid'];
         }
         $allowedHosts = API::Host()->get(array('hostids' => $hostids, 'output' => array('hostid'), 'editable' => $options['editable'], 'templated_hosts' => true, 'preservekeys' => true));
         foreach ($hostids as $hostid) {
             if (isset($allowedHosts[$hostid])) {
                 continue;
             }
             foreach ($hosts[$hostid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         unset($allowedHosts);
         // check hostgroups
         $groups = $groupids = array();
         $dbGroups = DBselect('SELECT o.actionid,ocg.groupid' . ' FROM operations o,opcommand_grp ocg' . ' WHERE o.operationid=ocg.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($group = DBfetch($dbGroups)) {
             if (!isset($groups[$group['groupid']])) {
                 $groups[$group['groupid']] = array();
             }
             $groups[$group['groupid']][$group['actionid']] = $group['actionid'];
             $groupids[$group['groupid']] = $group['groupid'];
         }
         $dbGroups = DBselect('SELECT o.actionid,og.groupid' . ' FROM operations o,opgroup og' . ' WHERE o.operationid=og.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($group = DBfetch($dbGroups)) {
             if (!isset($groups[$group['groupid']])) {
                 $groups[$group['groupid']] = array();
             }
             $groups[$group['groupid']][$group['actionid']] = $group['actionid'];
             $groupids[$group['groupid']] = $group['groupid'];
         }
         $allowedGroups = API::HostGroup()->get(array('groupids' => $groupids, 'output' => array('groupid'), 'editable' => $options['editable'], 'preservekeys' => true));
         foreach ($groupids as $groupid) {
             if (isset($allowedGroups[$groupid])) {
                 continue;
             }
             foreach ($groups[$groupid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         unset($allowedGroups);
         // check scripts
         $scripts = $scriptids = array();
         $dbScripts = DBselect('SELECT o.actionid,oc.scriptid' . ' FROM operations o,opcommand oc' . ' WHERE o.operationid=oc.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids) . ' AND oc.type=' . ZBX_SCRIPT_TYPE_GLOBAL_SCRIPT);
         while ($script = DBfetch($dbScripts)) {
             if (!isset($scripts[$script['scriptid']])) {
                 $scripts[$script['scriptid']] = array();
             }
             $scripts[$script['scriptid']][$script['actionid']] = $script['actionid'];
             $scriptids[$script['scriptid']] = $script['scriptid'];
         }
         $allowedScripts = API::Script()->get(array('scriptids' => $scriptids, 'output' => array('scriptid'), 'preservekeys' => true));
         foreach ($scriptids as $scriptid) {
             if (isset($allowedScripts[$scriptid])) {
                 continue;
             }
             foreach ($scripts[$scriptid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         unset($allowedScripts);
         // check users
         $users = $userids = array();
         $dbUsers = DBselect('SELECT o.actionid,omu.userid' . ' FROM operations o,opmessage_usr omu' . ' WHERE o.operationid=omu.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($user = DBfetch($dbUsers)) {
             if (!isset($users[$user['userid']])) {
                 $users[$user['userid']] = array();
             }
             $users[$user['userid']][$user['actionid']] = $user['actionid'];
             $userids[$user['userid']] = $user['userid'];
         }
         $allowedUsers = API::User()->get(array('userids' => $userids, 'output' => array('userid'), 'preservekeys' => true));
         foreach ($userids as $userid) {
             if (isset($allowedUsers[$userid])) {
                 continue;
             }
             foreach ($users[$userid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
         // check usergroups
         $usrgrps = $usrgrpids = array();
         $dbUsergroups = DBselect('SELECT o.actionid,omg.usrgrpid' . ' FROM operations o,opmessage_grp omg' . ' WHERE o.operationid=omg.operationid' . ' AND ' . dbConditionInt('o.actionid', $actionids));
         while ($usrgrp = DBfetch($dbUsergroups)) {
             if (!isset($usrgrps[$usrgrp['usrgrpid']])) {
                 $usrgrps[$usrgrp['usrgrpid']] = array();
             }
             $usrgrps[$usrgrp['usrgrpid']][$usrgrp['actionid']] = $usrgrp['actionid'];
             $usrgrpids[$usrgrp['usrgrpid']] = $usrgrp['usrgrpid'];
         }
         $allowedUsergrps = API::UserGroup()->get(array('usrgrpids' => $usrgrpids, 'output' => array('usrgrpid'), 'preservekeys' => true));
         foreach ($usrgrpids as $usrgrpid) {
             if (isset($allowedUsergrps[$usrgrpid])) {
                 continue;
             }
             foreach ($usrgrps[$usrgrpid] as $actionid) {
                 unset($result[$actionid], $actionids[$actionid]);
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     if ($result) {
         $result = $this->addRelatedObjects($options, $result);
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Esempio n. 16
0
 /**
  * @param $triggers
  */
 protected function createReal(array &$triggers)
 {
     $triggers = zbx_toArray($triggers);
     // insert triggers without expression
     $triggersCopy = $triggers;
     for ($i = 0, $size = count($triggersCopy); $i < $size; $i++) {
         unset($triggersCopy[$i]['expression']);
     }
     $triggerIds = DB::insert('triggers', $triggersCopy);
     unset($triggersCopy);
     $allHosts = [];
     $allowedHosts = [];
     $triggersAndHosts = [];
     $triggerExpression = [];
     foreach ($triggers as $tnum => $trigger) {
         $triggerId = $triggers[$tnum]['triggerid'] = $triggerIds[$tnum];
         $hosts = [];
         try {
             $expression = implode_exp($trigger['expression'], $triggerId, $hosts);
         } catch (Exception $e) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot implode expression "%s".', $trigger['expression']) . ' ' . $e->getMessage());
         }
         $this->validateItems($trigger);
         foreach ($hosts as $host) {
             $allHosts[] = $host;
         }
         $triggersAndHosts[$triggerId] = $hosts;
         $triggerExpression[$triggerId] = $expression;
     }
     $allHosts = array_unique($allHosts);
     $dbHostsStatuses = DBselect('SELECT h.host,h.status' . ' FROM hosts h' . ' WHERE ' . dbConditionString('h.host', $allHosts));
     while ($allowedHostsData = DBfetch($dbHostsStatuses)) {
         if ($allowedHostsData['status'] != HOST_STATUS_TEMPLATE) {
             $allowedHosts[] = $allowedHostsData['host'];
         }
     }
     // update triggers expression
     foreach ($triggers as $tnum => $trigger) {
         $triggerId = $triggers[$tnum]['triggerid'] = $triggerIds[$tnum];
         DB::update('triggers', ['values' => ['expression' => $triggerExpression[$triggerId]], 'where' => ['triggerid' => $triggerId]]);
         info(_s('Created: Trigger "%1$s" on "%2$s".', $trigger['description'], implode(', ', $triggersAndHosts[$triggerId])));
         add_audit_ext(AUDIT_ACTION_ADD, AUDIT_RESOURCE_TRIGGER, $triggerId, $trigger['description'], null, null, null);
     }
 }
Esempio n. 17
0
 /**
  * Delete data from DB.
  *
  * Example:
  * DB::delete('applications', array('applicationid'=>array(1, 8, 6)));
  * DELETE FROM applications WHERE applicationid IN (1, 8, 6)
  *
  * DB::delete('applications', array('applicationid'=>array(1), 'templateid'=array(10)));
  * DELETE FROM applications WHERE applicationid IN (1) AND templateid IN (10)
  *
  * @param string $table
  * @param array  $wheres pair of fieldname => fieldvalues
  * @param bool   $use_or
  *
  * @return bool
  */
 public static function delete($table, $wheres, $use_or = false)
 {
     if (empty($wheres) || !is_array($wheres)) {
         self::exception(self::DBEXECUTE_ERROR, _s('Cannot perform delete statement on table "%1$s" without where condition.', $table));
     }
     $table_schema = self::getSchema($table);
     $sqlWhere = array();
     foreach ($wheres as $field => $values) {
         if (!isset($table_schema['fields'][$field]) || is_null($values)) {
             self::exception(self::DBEXECUTE_ERROR, _s('Incorrect field "%1$s" name or value in where statement for table "%2$s".', $field, $table));
         }
         $values = zbx_toArray($values);
         sort($values);
         // sorting ids to prevent deadlocks when two transactions depends from each other
         $sqlWhere[] = dbConditionString($field, $values);
     }
     $sql = 'DELETE FROM ' . $table . ' WHERE ' . implode($use_or ? ' OR ' : ' AND ', $sqlWhere);
     if (!DBexecute($sql)) {
         self::exception(self::DBEXECUTE_ERROR, _s('SQL statement execution has failed "%1$s"', $sql));
     }
     return true;
 }
 /**
  * Select host prototype ids for previously added host prototypes names.
  */
 protected function selectHostPrototypes()
 {
     if (!empty($this->hostPrototypes)) {
         $this->hostPrototypesRefs = array();
         $sqlWhere = array();
         foreach ($this->hostPrototypes as $host => $discoveryRule) {
             $hostId = $this->resolveHostOrTemplate($host);
             foreach ($discoveryRule as $discoveryRuleKey => $hostPrototypes) {
                 $discoveryRuleId = $this->resolveItem($hostId, $discoveryRuleKey);
                 if ($hostId) {
                     $sqlWhere[] = '(hd.parent_itemid=' . zbx_dbstr($discoveryRuleId) . ' AND ' . dbConditionString('h.host', $hostPrototypes) . ')';
                 }
             }
         }
         if ($sqlWhere) {
             $query = DBselect('SELECT h.host,h.hostid,hd.parent_itemid,i.hostid AS parent_hostid ' . ' FROM hosts h,host_discovery hd,items i' . ' WHERE h.hostid=hd.hostid' . ' AND hd.parent_itemid=i.itemid' . ' AND ' . implode(' OR ', $sqlWhere));
             while ($data = DBfetch($query)) {
                 $this->hostPrototypesRefs[$data['parent_hostid']][$data['parent_itemid']][$data['host']] = $data['hostid'];
             }
         }
     }
 }
Esempio n. 19
0
 /**
  * Import images.
  *
  * @throws Exception
  */
 protected function processImages()
 {
     $allImages = $this->getFormattedImages();
     if (empty($allImages)) {
         return;
     }
     $imagesToUpdate = array();
     $allImages = zbx_toHash($allImages, 'name');
     $dbImages = DBselect('SELECT i.imageid,i.name FROM images i WHERE ' . dbConditionString('i.name', array_keys($allImages)));
     while ($dbImage = DBfetch($dbImages)) {
         $dbImage['image'] = $allImages[$dbImage['name']]['image'];
         $imagesToUpdate[] = $dbImage;
         unset($allImages[$dbImage['name']]);
     }
     if ($this->options['images']['createMissing']) {
         API::Image()->create(array_values($allImages));
     }
     if ($this->options['images']['updateExisting']) {
         API::Image()->update($imagesToUpdate);
     }
 }
 /**
  * Delete Template
  *
  * @param array $templateids
  * @param array $templateids['templateids']
  * @return boolean
  */
 public function delete($templateids)
 {
     if (empty($templateids)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.'));
     }
     $templateids = zbx_toArray($templateids);
     $options = array('templateids' => $templateids, 'editable' => true, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true);
     $delTemplates = $this->get($options);
     foreach ($templateids as $templateid) {
         if (!isset($delTemplates[$templateid])) {
             self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
         }
     }
     API::Template()->unlink($templateids, null, true);
     // delete the discovery rules first
     $delRules = API::DiscoveryRule()->get(array('hostids' => $templateids, 'nopermissions' => true, 'preservekeys' => true));
     if ($delRules) {
         API::DiscoveryRule()->delete(array_keys($delRules), true);
     }
     // delete the items
     $delItems = API::Item()->get(array('templateids' => $templateids, 'output' => API_OUTPUT_SHORTEN, 'nopermissions' => true, 'preservekeys' => true));
     if ($delItems) {
         API::Item()->delete(array_keys($delItems), true);
     }
     // delete screen items
     DBexecute('DELETE FROM screens_items WHERE ' . dbConditionInt('resourceid', $templateids) . ' AND resourcetype=' . SCREEN_RESOURCE_HOST_TRIGGERS);
     // delete host from maps
     if (!empty($templateids)) {
         DB::delete('sysmaps_elements', array('elementtype' => SYSMAP_ELEMENT_TYPE_HOST, 'elementid' => $templateids));
     }
     // disable actions
     // actions from conditions
     $actionids = array();
     $sql = 'SELECT DISTINCT actionid' . ' FROM conditions' . ' WHERE conditiontype=' . CONDITION_TYPE_HOST_TEMPLATE . ' AND ' . dbConditionString('value', $templateids);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     // actions from operations
     $sql = 'SELECT DISTINCT o.actionid' . ' FROM operations o,optemplate ot' . ' WHERE o.operationid=ot.operationid' . ' AND ' . dbConditionInt('ot.templateid', $templateids);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     if (!empty($actionids)) {
         DB::update('actions', array('values' => array('status' => ACTION_STATUS_DISABLED), 'where' => array('actionid' => $actionids)));
     }
     // delete action conditions
     DB::delete('conditions', array('conditiontype' => CONDITION_TYPE_HOST_TEMPLATE, 'value' => $templateids));
     // delete action operation commands
     $operationids = array();
     $sql = 'SELECT DISTINCT ot.operationid' . ' FROM optemplate ot' . ' WHERE ' . dbConditionInt('ot.templateid', $templateids);
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $operationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('optemplate', array('templateid' => $templateids));
     // delete empty operations
     $delOperationids = array();
     $sql = 'SELECT DISTINCT o.operationid' . ' FROM operations o' . ' WHERE ' . dbConditionInt('o.operationid', $operationids) . ' AND NOT EXISTS(SELECT NULL FROM optemplate ot WHERE ot.operationid=o.operationid)';
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $delOperationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('operations', array('operationid' => $delOperationids));
     // Applications
     $delApplications = API::Application()->get(array('templateids' => $templateids, 'output' => API_OUTPUT_SHORTEN, 'nopermissions' => 1, 'preservekeys' => 1));
     if (!empty($delApplications)) {
         API::Application()->delete(array_keys($delApplications), true);
     }
     DB::delete('hosts', array('hostid' => $templateids));
     // TODO: remove info from API
     foreach ($delTemplates as $template) {
         info(_s('Deleted: Template "%1$s".', $template['name']));
         add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_HOST, $template['hostid'], $template['host'], 'hosts', NULL, NULL);
     }
     return array('templateids' => $templateids);
 }
Esempio n. 21
0
 /**
  * Get UserMacros data.
  *
  * @param array $options
  * @param array $options['groupids'] usermacrosgroup ids
  * @param array $options['hostids'] host ids
  * @param array $options['hostmacroids'] host macros ids
  * @param array $options['globalmacroids'] global macros ids
  * @param array $options['templateids'] template ids
  * @param boolean $options['globalmacro'] only global macros
  * @param boolean $options['selectGroups'] select groups
  * @param boolean $options['selectHosts'] select hosts
  * @param boolean $options['selectTemplates'] select templates
  *
  * @return array|boolean UserMacros data as array or false if error
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     $sqlParts = array('select' => array('macros' => 'hm.hostmacroid'), 'from' => array('hostmacro hm'), 'where' => array(), 'order' => array(), 'limit' => null);
     $sqlPartsGlobal = array('select' => array('macros' => 'gm.globalmacroid'), 'from' => array('globalmacro gm'), 'where' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('groupids' => null, 'hostids' => null, 'hostmacroids' => null, 'globalmacroids' => null, 'templateids' => null, 'globalmacro' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_EXTEND, 'selectGroups' => null, 'selectHosts' => null, 'selectTemplates' => null, 'countOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($defOptions, $options);
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         if (!is_null($options['editable']) && !is_null($options['globalmacro'])) {
             return array();
         } else {
             $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
             $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 hm.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>' . PERM_DENY . ' AND MAX(r.permission)>=' . zbx_dbstr($permission) . ')';
         }
     }
     // global macro
     if (!is_null($options['globalmacro'])) {
         $options['groupids'] = null;
         $options['hostmacroids'] = null;
         $options['triggerids'] = null;
         $options['hostids'] = null;
         $options['itemids'] = null;
         $options['selectGroups'] = null;
         $options['selectTemplates'] = null;
         $options['selectHosts'] = null;
     }
     // globalmacroids
     if (!is_null($options['globalmacroids'])) {
         zbx_value2array($options['globalmacroids']);
         $sqlPartsGlobal['where'][] = dbConditionInt('gm.globalmacroid', $options['globalmacroids']);
     }
     // hostmacroids
     if (!is_null($options['hostmacroids'])) {
         zbx_value2array($options['hostmacroids']);
         $sqlParts['where'][] = dbConditionInt('hm.hostmacroid', $options['hostmacroids']);
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']);
         $sqlParts['where']['hgh'] = 'hg.hostid=hm.hostid';
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         $sqlParts['where'][] = dbConditionInt('hm.hostid', $options['hostids']);
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         $sqlParts['from']['macros_templates'] = 'hosts_templates ht';
         $sqlParts['where'][] = dbConditionInt('ht.templateid', $options['templateids']);
         $sqlParts['where']['hht'] = 'hm.hostid=ht.hostid';
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('hostmacro hm', $options, $sqlParts);
         zbx_db_search('globalmacro gm', $options, $sqlPartsGlobal);
     }
     // filter
     if (is_array($options['filter'])) {
         if (isset($options['filter']['macro'])) {
             zbx_value2array($options['filter']['macro']);
             $sqlParts['where'][] = dbConditionString('hm.macro', $options['filter']['macro']);
             $sqlPartsGlobal['where'][] = dbConditionString('gm.macro', $options['filter']['macro']);
         }
     }
     // sorting
     $sqlParts = $this->applyQuerySortOptions('hostmacro', 'hm', $options, $sqlParts);
     $sqlPartsGlobal = $this->applyQuerySortOptions('globalmacro', 'gm', $options, $sqlPartsGlobal);
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
         $sqlPartsGlobal['limit'] = $options['limit'];
     }
     // init GLOBALS
     if (!is_null($options['globalmacro'])) {
         $sqlPartsGlobal = $this->applyQueryOutputOptions('globalmacro', 'gm', $options, $sqlPartsGlobal);
         $res = DBselect($this->createSelectQueryFromParts($sqlPartsGlobal), $sqlPartsGlobal['limit']);
         while ($macro = DBfetch($res)) {
             if ($options['countOutput']) {
                 $result = $macro['rowscount'];
             } else {
                 $result[$macro['globalmacroid']] = $macro;
             }
         }
     } else {
         $sqlParts = $this->applyQueryOutputOptions('hostmacro', 'hm', $options, $sqlParts);
         $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
         while ($macro = DBfetch($res)) {
             if ($options['countOutput']) {
                 $result = $macro['rowscount'];
             } else {
                 $result[$macro['hostmacroid']] = $macro;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     if ($result) {
         $result = $this->addRelatedObjects($options, $result);
         $result = $this->unsetExtraFields($result, array('hostid'), $options['output']);
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Esempio n. 22
0
 /**
  * Delete Host
  *
  * @param array $hosts
  * @param array $hosts[0, ...]['hostid'] Host ID to delete
  *
  * @return array|boolean
  */
 public function delete($hosts)
 {
     if (empty($hosts)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.'));
     }
     $hosts = zbx_toArray($hosts);
     $hostids = zbx_objectValues($hosts, 'hostid');
     $this->checkInput($hosts, __FUNCTION__);
     // delete the discovery rules first
     $delRules = API::DiscoveryRule()->get(array('hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true));
     if ($delRules) {
         API::DiscoveryRule()->delete(array_keys($delRules), true);
     }
     // delete the items
     $delItems = API::Item()->get(array('templateids' => $hostids, 'output' => API_OUTPUT_SHORTEN, 'nopermissions' => true, 'preservekeys' => true));
     if ($delItems) {
         API::Item()->delete(array_keys($delItems), true);
     }
     // delete web tests
     $delHttptests = array();
     $dbHttptests = get_httptests_by_hostid($hostids);
     while ($dbHttptest = DBfetch($dbHttptests)) {
         $delHttptests[$dbHttptest['httptestid']] = $dbHttptest['httptestid'];
     }
     if (!empty($delHttptests)) {
         API::WebCheck()->delete($delHttptests);
     }
     // delete screen items
     DB::delete('screens_items', array('resourceid' => $hostids, 'resourcetype' => SCREEN_RESOURCE_HOST_TRIGGERS));
     // delete host from maps
     if (!empty($hostids)) {
         DB::delete('sysmaps_elements', array('elementtype' => SYSMAP_ELEMENT_TYPE_HOST, 'elementid' => $hostids));
     }
     // disable actions
     // actions from conditions
     $actionids = array();
     $sql = 'SELECT DISTINCT actionid' . ' FROM conditions' . ' WHERE conditiontype=' . CONDITION_TYPE_HOST . ' AND ' . dbConditionString('value', $hostids);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     // actions from operations
     $sql = 'SELECT DISTINCT o.actionid' . ' FROM operations o, opcommand_hst oh' . ' WHERE o.operationid=oh.operationid' . ' AND ' . dbConditionInt('oh.hostid', $hostids);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     if (!empty($actionids)) {
         $update = array();
         $update[] = array('values' => array('status' => ACTION_STATUS_DISABLED), 'where' => array('actionid' => $actionids));
         DB::update('actions', $update);
     }
     // delete action conditions
     DB::delete('conditions', array('conditiontype' => CONDITION_TYPE_HOST, 'value' => $hostids));
     // delete action operation commands
     $operationids = array();
     $sql = 'SELECT DISTINCT oh.operationid' . ' FROM opcommand_hst oh' . ' WHERE ' . dbConditionInt('oh.hostid', $hostids);
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $operationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('opcommand_hst', array('hostid' => $hostids));
     // delete empty operations
     $delOperationids = array();
     $sql = 'SELECT DISTINCT o.operationid' . ' FROM operations o' . ' WHERE ' . dbConditionInt('o.operationid', $operationids) . ' AND NOT EXISTS(SELECT oh.opcommand_hstid FROM opcommand_hst oh WHERE oh.operationid=o.operationid)';
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $delOperationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('operations', array('operationid' => $delOperationids));
     $hosts = API::Host()->get(array('output' => array('hostid', 'name'), 'hostids' => $hostids, 'nopermissions' => true));
     // delete host inventory
     DB::delete('host_inventory', array('hostid' => $hostids));
     // delete host applications
     DB::delete('applications', array('hostid' => $hostids));
     // delete host
     DB::delete('hosts', array('hostid' => $hostids));
     // TODO: remove info from API
     foreach ($hosts as $host) {
         info(_s('Deleted: Host "%1$s".', $host['name']));
         add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_HOST, $host['hostid'], $host['name'], 'hosts', NULL, NULL);
     }
     return array('hostids' => $hostids);
 }
Esempio n. 23
0
 /**
  * Delete proxy.
  *
  * @param string|array $proxyIds
  *
  * @return array
  */
 public function delete($proxyIds)
 {
     $proxyIds = zbx_toArray($proxyIds);
     // deprecated input support
     if ($proxyIds && is_array($proxyIds[0])) {
         $this->deprecated('Passing objects is deprecated, use an array of IDs instead.');
         foreach ($proxyIds as $proxyId) {
             if (!check_db_fields(array('proxyid' => null), $proxyId)) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('No proxy ID given.'));
             }
         }
         $proxyIds = zbx_objectValues($proxyIds, 'proxyid');
     }
     $this->validateDelete($proxyIds);
     $dbProxies = DBselect('SELECT h.hostid,h.host' . ' FROM hosts h' . ' WHERE ' . dbConditionInt('h.hostid', $proxyIds));
     $dbProxies = DBfetchArrayAssoc($dbProxies, 'hostid');
     $actionIds = array();
     // get conditions
     $dbActions = DBselect('SELECT DISTINCT c.actionid' . ' FROM conditions c' . ' WHERE c.conditiontype=' . CONDITION_TYPE_PROXY . ' AND ' . dbConditionString('c.value', $proxyIds));
     while ($dbAction = DBfetch($dbActions)) {
         $actionIds[$dbAction['actionid']] = $dbAction['actionid'];
     }
     if ($actionIds) {
         DB::update('actions', array('values' => array('status' => ACTION_STATUS_DISABLED), 'where' => array('actionid' => $actionIds)));
     }
     // delete action conditions
     DB::delete('conditions', array('conditiontype' => CONDITION_TYPE_PROXY, 'value' => $proxyIds));
     // delete interface
     DB::delete('interface', array('hostid' => $proxyIds));
     // delete host
     DB::delete('hosts', array('hostid' => $proxyIds));
     // TODO: remove info from API
     foreach ($dbProxies as $proxy) {
         info(_s('Deleted: Proxy "%1$s".', $proxy['host']));
         add_audit(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_PROXY, '[' . $proxy['host'] . '] [' . $proxy['hostid'] . ']');
     }
     return array('proxyids' => $proxyIds);
 }
Esempio n. 24
0
 /**
  * Delete Host
  *
  * @param string|array 	$hostIds
  * @param bool			$nopermissions
  *
  * @return array|boolean
  */
 public function delete($hostIds, $nopermissions = false)
 {
     $hostIds = zbx_toArray($hostIds);
     // deprecated input support
     if ($hostIds && is_array($hostIds[0])) {
         $this->deprecated('Passing objects is deprecated, use an array of IDs instead.');
         foreach ($hostIds as $host) {
             if (!check_db_fields(array('hostid' => null), $host)) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('No host ID given.'));
             }
         }
         $hostIds = zbx_objectValues($hostIds, 'hostid');
     }
     $this->validateDelete($hostIds, $nopermissions);
     // delete the discovery rules first
     $delRules = API::DiscoveryRule()->get(array('hostids' => $hostIds, 'nopermissions' => true, 'preservekeys' => true));
     if ($delRules) {
         API::DiscoveryRule()->delete(array_keys($delRules), true);
     }
     // delete the items
     $delItems = API::Item()->get(array('templateids' => $hostIds, 'output' => array('itemid'), 'nopermissions' => true, 'preservekeys' => true));
     if ($delItems) {
         API::Item()->delete(array_keys($delItems), true);
     }
     // delete web tests
     $delHttptests = array();
     $dbHttptests = get_httptests_by_hostid($hostIds);
     while ($dbHttptest = DBfetch($dbHttptests)) {
         $delHttptests[$dbHttptest['httptestid']] = $dbHttptest['httptestid'];
     }
     if (!empty($delHttptests)) {
         API::HttpTest()->delete($delHttptests, true);
     }
     // delete screen items
     DB::delete('screens_items', array('resourceid' => $hostIds, 'resourcetype' => SCREEN_RESOURCE_HOST_TRIGGERS));
     // delete host from maps
     if (!empty($hostIds)) {
         DB::delete('sysmaps_elements', array('elementtype' => SYSMAP_ELEMENT_TYPE_HOST, 'elementid' => $hostIds));
     }
     // disable actions
     // actions from conditions
     $actionids = array();
     $sql = 'SELECT DISTINCT actionid' . ' FROM conditions' . ' WHERE conditiontype=' . CONDITION_TYPE_HOST . ' AND ' . dbConditionString('value', $hostIds);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     // actions from operations
     $sql = 'SELECT DISTINCT o.actionid' . ' FROM operations o, opcommand_hst oh' . ' WHERE o.operationid=oh.operationid' . ' AND ' . dbConditionInt('oh.hostid', $hostIds);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     if (!empty($actionids)) {
         $update = array();
         $update[] = array('values' => array('status' => ACTION_STATUS_DISABLED), 'where' => array('actionid' => $actionids));
         DB::update('actions', $update);
     }
     // delete action conditions
     DB::delete('conditions', array('conditiontype' => CONDITION_TYPE_HOST, 'value' => $hostIds));
     // delete action operation commands
     $operationids = array();
     $sql = 'SELECT DISTINCT oh.operationid' . ' FROM opcommand_hst oh' . ' WHERE ' . dbConditionInt('oh.hostid', $hostIds);
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $operationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('opcommand_hst', array('hostid' => $hostIds));
     // delete empty operations
     $delOperationids = array();
     $sql = 'SELECT DISTINCT o.operationid' . ' FROM operations o' . ' WHERE ' . dbConditionInt('o.operationid', $operationids) . ' AND NOT EXISTS(SELECT oh.opcommand_hstid FROM opcommand_hst oh WHERE oh.operationid=o.operationid)';
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $delOperationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('operations', array('operationid' => $delOperationids));
     $hosts = API::Host()->get(array('output' => array('hostid', 'name'), 'hostids' => $hostIds, 'nopermissions' => true));
     // delete host inventory
     DB::delete('host_inventory', array('hostid' => $hostIds));
     // delete host applications
     DB::delete('applications', array('hostid' => $hostIds));
     // delete host
     DB::delete('hosts', array('hostid' => $hostIds));
     // TODO: remove info from API
     foreach ($hosts as $host) {
         info(_s('Deleted: Host "%1$s".', $host['name']));
         add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_HOST, $host['hostid'], $host['name'], 'hosts', NULL, NULL);
     }
     // remove Monitoring > Latest data toggle profile values related to given hosts
     CProfile::delete('web.latest.toggle_other', $hostIds);
     return array('hostids' => $hostIds);
 }
Esempio n. 25
0
 /**
  * Get items data.
  *
  * @param array  $options
  * @param array  $options['itemids']
  * @param array  $options['hostids']
  * @param array  $options['groupids']
  * @param array  $options['triggerids']
  * @param array  $options['applicationids']
  * @param bool   $options['status']
  * @param bool   $options['templated_items']
  * @param bool   $options['editable']
  * @param bool   $options['count']
  * @param string $options['pattern']
  * @param int    $options['limit']
  * @param string $options['order']
  *
  * @return array|int item data as array or false if error
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     $sqlParts = array('select' => array('items' => 'i.itemid'), 'from' => array('items' => 'items i'), 'where' => array('webtype' => 'i.type<>' . ITEM_TYPE_HTTPTEST, 'flags' => 'i.flags IN (' . ZBX_FLAG_DISCOVERY_NORMAL . ',' . ZBX_FLAG_DISCOVERY_CREATED . ')'), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('groupids' => null, 'templateids' => null, 'hostids' => null, 'proxyids' => null, 'itemids' => null, 'interfaceids' => null, 'graphids' => null, 'triggerids' => null, 'applicationids' => null, 'webitems' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'editable' => null, 'nopermissions' => null, 'group' => null, 'host' => null, 'application' => null, 'with_triggers' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_EXTEND, 'selectHosts' => null, 'selectInterfaces' => null, 'selectTriggers' => null, 'selectGraphs' => null, 'selectApplications' => null, 'selectDiscoveryRule' => null, 'selectItemDiscovery' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($defOptions, $options);
     // editable + permission check
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
         $userGroups = getUserGroupsByUserId($userid);
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE i.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>' . PERM_DENY . ' AND MAX(r.permission)>=' . zbx_dbstr($permission) . ')';
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         $sqlParts['where']['itemid'] = dbConditionInt('i.itemid', $options['itemids']);
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['hostids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.hostid';
         }
     }
     // interfaceids
     if (!is_null($options['interfaceids'])) {
         zbx_value2array($options['interfaceids']);
         $sqlParts['where']['interfaceid'] = dbConditionInt('i.interfaceid', $options['interfaceids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.interfaceid';
         }
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']);
         $sqlParts['where'][] = 'hg.hostid=i.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['hg'] = 'hg.groupid';
         }
     }
     // proxyids
     if (!is_null($options['proxyids'])) {
         zbx_value2array($options['proxyids']);
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where'][] = dbConditionInt('h.proxy_hostid', $options['proxyids']);
         $sqlParts['where'][] = 'h.hostid=i.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['h'] = 'h.proxy_hostid';
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['where'][] = dbConditionInt('f.triggerid', $options['triggerids']);
         $sqlParts['where']['if'] = 'i.itemid=f.itemid';
     }
     // applicationids
     if (!is_null($options['applicationids'])) {
         zbx_value2array($options['applicationids']);
         $sqlParts['from']['items_applications'] = 'items_applications ia';
         $sqlParts['where'][] = dbConditionInt('ia.applicationid', $options['applicationids']);
         $sqlParts['where']['ia'] = 'ia.itemid=i.itemid';
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['where'][] = dbConditionInt('gi.graphid', $options['graphids']);
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
     }
     // webitems
     if (!is_null($options['webitems'])) {
         unset($sqlParts['where']['webtype']);
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 'i.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 'i.templateid IS NULL';
         }
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // monitored
     if (!is_null($options['monitored'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['monitored']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_MONITORED;
             $sqlParts['where'][] = 'i.status=' . ITEM_STATUS_ACTIVE;
         } else {
             $sqlParts['where'][] = '(h.status<>' . HOST_STATUS_MONITORED . ' OR i.status<>' . ITEM_STATUS_ACTIVE . ')';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('items i', $options, $sqlParts);
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('items i', $options, $sqlParts);
         if (isset($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sqlParts['from']['hosts'] = 'hosts h';
             $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
             $sqlParts['where']['h'] = dbConditionString('h.host', $options['filter']['host'], false, true);
         }
         if (array_key_exists('flags', $options['filter']) && (is_null($options['filter']['flags']) || !zbx_empty($options['filter']['flags']))) {
             unset($sqlParts['where']['flags']);
         }
     }
     // group
     if (!is_null($options['group'])) {
         $sqlParts['from']['groups'] = 'groups g';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where']['ghg'] = 'g.groupid=hg.groupid';
         $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sqlParts['where'][] = ' g.name=' . zbx_dbstr($options['group']);
     }
     // host
     if (!is_null($options['host'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         $sqlParts['where'][] = ' h.host=' . zbx_dbstr($options['host']);
     }
     // application
     if (!is_null($options['application'])) {
         $sqlParts['from']['applications'] = 'applications a';
         $sqlParts['from']['items_applications'] = 'items_applications ia';
         $sqlParts['where']['aia'] = 'a.applicationid = ia.applicationid';
         $sqlParts['where']['iai'] = 'ia.itemid=i.itemid';
         $sqlParts['where'][] = ' a.name=' . zbx_dbstr($options['application']);
     }
     // with_triggers
     if (!is_null($options['with_triggers'])) {
         if ($options['with_triggers'] == 1) {
             $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM functions ff,triggers t' . ' WHERE i.itemid=ff.itemid' . ' AND ff.triggerid=t.triggerid' . ' AND t.flags IN (' . ZBX_FLAG_DISCOVERY_NORMAL . ',' . ZBX_FLAG_DISCOVERY_CREATED . ')' . ')';
         } else {
             $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM functions ff,triggers t' . ' WHERE i.itemid=ff.itemid' . ' AND ff.triggerid=t.triggerid' . ' AND t.flags IN (' . ZBX_FLAG_DISCOVERY_NORMAL . ',' . ZBX_FLAG_DISCOVERY_CREATED . ')' . ')';
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
     while ($item = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $item;
             } else {
                 $result = $item['rowscount'];
             }
         } else {
             $result[$item['itemid']] = $item;
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     // add other related objects
     if ($result) {
         $result = $this->addRelatedObjects($options, $result);
         $result = $this->unsetExtraFields($result, array('hostid', 'interfaceid', 'value_type'), $options['output']);
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
 /**
  * Create web scenario steps with items.
  *
  * @param $httpTest
  * @param $websteps
  */
 protected function createStepsReal($httpTest, $websteps)
 {
     $webstepsNames = zbx_objectValues($websteps, 'name');
     if (!preg_grep('/' . ZBX_PREG_PARAMS . '/i', $webstepsNames)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Scenario step name should contain only printable characters.'));
     }
     $sql = 'SELECT h.httpstepid,h.name' . ' FROM httpstep h' . ' WHERE h.httptestid=' . zbx_dbstr($httpTest['httptestid']) . ' AND ' . dbConditionString('h.name', $webstepsNames);
     if ($httpstepData = DBfetch(DBselect($sql))) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Step "%s" already exists.', $httpstepData['name']));
     }
     foreach ($websteps as $snum => $webstep) {
         $websteps[$snum]['httptestid'] = $httpTest['httptestid'];
         if ($webstep['no'] <= 0) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _('Scenario step number cannot be less than 1.'));
         }
     }
     $webstepids = DB::insert('httpstep', $websteps);
     foreach ($websteps as $snum => $webstep) {
         $webstepid = $webstepids[$snum];
         $stepitems = array(array('name' => _s('Download speed for step "%1$s" of scenario "%2$s".', '$2', '$1'), 'key_' => 'web.test.in[' . $httpTest['name'] . ',' . $webstep['name'] . ',bps]', 'value_type' => ITEM_VALUE_TYPE_FLOAT, 'units' => 'Bps', 'httpstepitemtype' => HTTPSTEP_ITEM_TYPE_IN), array('name' => _s('Response time for step "%1$s" of scenario "%2$s".', '$2', '$1'), 'key_' => 'web.test.time[' . $httpTest['name'] . ',' . $webstep['name'] . ',resp]', 'value_type' => ITEM_VALUE_TYPE_FLOAT, 'units' => 's', 'httpstepitemtype' => HTTPSTEP_ITEM_TYPE_TIME), array('name' => _s('Response code for step "%1$s" of scenario "%2$s".', '$2', '$1'), 'key_' => 'web.test.rspcode[' . $httpTest['name'] . ',' . $webstep['name'] . ']', 'value_type' => ITEM_VALUE_TYPE_UINT64, 'units' => '', 'httpstepitemtype' => HTTPSTEP_ITEM_TYPE_RSPCODE));
         foreach ($stepitems as &$item) {
             $itemsExist = API::Item()->exists(array('key_' => $item['key_'], 'hostid' => $httpTest['hostid']));
             if ($itemsExist) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Web item with key "%s" already exists.', $item['key_']));
             }
             $item['hostid'] = $httpTest['hostid'];
             $item['delay'] = $httpTest['delay'];
             $item['type'] = ITEM_TYPE_HTTPTEST;
             $item['data_type'] = ITEM_DATA_TYPE_DECIMAL;
             $item['history'] = $this->history;
             $item['trends'] = $this->trends;
             $item['status'] = HTTPTEST_STATUS_ACTIVE == $httpTest['status'] ? ITEM_STATUS_ACTIVE : ITEM_STATUS_DISABLED;
         }
         unset($item);
         $stepItemids = DB::insert('items', $stepitems);
         $itemApplications = array();
         foreach ($stepItemids as $itemid) {
             $itemApplications[] = array('applicationid' => $httpTest['applicationid'], 'itemid' => $itemid);
         }
         DB::insert('items_applications', $itemApplications);
         $webstepitems = array();
         foreach ($stepitems as $inum => $item) {
             $webstepitems[] = array('httpstepid' => $webstepid, 'itemid' => $stepItemids[$inum], 'type' => $item['httpstepitemtype']);
         }
         DB::insert('httpstepitem', $webstepitems);
         foreach ($stepitems as $stepitem) {
             info(_s('Web item "%s" created.', $stepitem['key_']));
         }
     }
 }
Esempio n. 27
0
 protected function deleteChecks(array $checkids)
 {
     $actionids = array();
     // conditions
     $sql = 'SELECT DISTINCT actionid ' . ' FROM conditions ' . ' WHERE conditiontype=' . CONDITION_TYPE_DCHECK . ' AND ' . dbConditionString('value', $checkids);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[] = $dbAction['actionid'];
     }
     // disabling actions with deleted conditions
     if (!empty($actionids)) {
         DBexecute('UPDATE actions ' . ' SET status=' . ACTION_STATUS_DISABLED . ' WHERE ' . dbConditionInt('actionid', $actionids));
         // delete action conditions
         DBexecute('DELETE FROM conditions ' . ' WHERE conditiontype=' . CONDITION_TYPE_DCHECK . ' AND ' . dbConditionString('value', $checkids));
     }
     DB::delete('dchecks', array('dcheckid' => $checkids));
 }
Esempio n. 28
0
 /**
  * Get TriggerPrototypes data.
  *
  * @param array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['triggerids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  *
  * @return array|int item data as array or false if error
  */
 public function get(array $options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     $sqlParts = array('select' => array('triggers' => 't.triggerid'), 'from' => array('t' => 'triggers t'), 'where' => array('t.flags=' . ZBX_FLAG_DISCOVERY_PROTOTYPE), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'triggerids' => null, 'itemids' => null, 'applicationids' => null, 'discoveryids' => null, 'functions' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'active' => null, 'maintenance' => null, 'nopermissions' => null, 'editable' => null, 'group' => null, 'host' => null, 'min_severity' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'expandExpression' => null, 'expandData' => null, 'output' => API_OUTPUT_REFER, 'selectGroups' => null, 'selectHosts' => null, 'selectItems' => null, 'selectFunctions' => null, 'selectDiscoveryRule' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($defOptions, $options);
     // editable + permission check
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
         $userGroups = getUserGroupsByUserId($userid);
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM functions f,items i,hosts_groups hgg' . ' LEFT JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY i.hostid' . ' HAVING MAX(permission)<' . $permission . ' OR MIN(permission) IS NULL' . ' OR MIN(permission)=' . PERM_DENY . ')';
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         $sqlParts['select']['groupid'] = 'hg.groupid';
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['groupid'] = dbConditionInt('hg.groupid', $options['groupids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['hg'] = 'hg.groupid';
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         $sqlParts['select']['hostid'] = 'i.hostid';
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['hostids']);
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.hostid';
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         $sqlParts['where']['triggerid'] = dbConditionInt('t.triggerid', $options['triggerids']);
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         $sqlParts['select']['itemid'] = 'f.itemid';
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['where']['itemid'] = dbConditionInt('f.itemid', $options['itemids']);
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['f'] = 'f.itemid';
         }
     }
     // applicationids
     if (!is_null($options['applicationids'])) {
         zbx_value2array($options['applicationids']);
         $sqlParts['select']['applicationid'] = 'a.applicationid';
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['applications'] = 'applications a';
         $sqlParts['where']['a'] = dbConditionInt('a.applicationid', $options['applicationids']);
         $sqlParts['where']['ia'] = 'i.hostid=a.hostid';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
     }
     // discoveryids
     if (!is_null($options['discoveryids'])) {
         zbx_value2array($options['discoveryids']);
         $sqlParts['select']['itemid'] = 'id.parent_itemid';
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['item_discovery'] = 'item_discovery id';
         $sqlParts['where']['fid'] = 'f.itemid=id.itemid';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where'][] = dbConditionInt('id.parent_itemid', $options['discoveryids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['id'] = 'id.parent_itemid';
         }
     }
     // functions
     if (!is_null($options['functions'])) {
         zbx_value2array($options['functions']);
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where'][] = dbConditionString('f.function', $options['functions']);
     }
     // monitored
     if (!is_null($options['monitored'])) {
         $sqlParts['where']['monitored'] = ' NOT EXISTS (' . ' SELECT NULL' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT NULL' . ' FROM items ii,hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND (' . ' ii.status<>' . ITEM_STATUS_ACTIVE . ' OR hh.status<>' . HOST_STATUS_MONITORED . ' )' . ' )' . ' )';
         $sqlParts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // active
     if (!is_null($options['active'])) {
         $sqlParts['where']['active'] = ' NOT EXISTS (' . ' SELECT NULL' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT NULL' . ' FROM items ii,hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND  hh.status<>' . HOST_STATUS_MONITORED . ' )' . ' )';
         $sqlParts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // maintenance
     if (!is_null($options['maintenance'])) {
         $sqlParts['where'][] = ($options['maintenance'] == 0 ? ' NOT ' : '') . ' EXISTS (' . ' SELECT NULL' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT NULL' . ' FROM items ii,hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND hh.maintenance_status=1' . ' )' . ' )';
         $sqlParts['where'][] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 't.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 't.templateid IS NULL';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('triggers t', $options, $sqlParts);
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('triggers t', $options, $sqlParts);
         if (isset($options['filter']['host']) && !is_null($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sqlParts['from']['functions'] = 'functions f';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
             $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
             $sqlParts['from']['hosts'] = 'hosts h';
             $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
             $sqlParts['where']['host'] = dbConditionString('h.host', $options['filter']['host']);
         }
         if (isset($options['filter']['hostid']) && !is_null($options['filter']['hostid'])) {
             zbx_value2array($options['filter']['hostid']);
             $sqlParts['from']['functions'] = 'functions f';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
             $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
             $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['filter']['hostid']);
         }
     }
     // group
     if (!is_null($options['group'])) {
         $sqlParts['select']['name'] = 'g.name';
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['from']['groups'] = 'groups g';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sqlParts['where']['ghg'] = 'g.groupid=hg.groupid';
         $sqlParts['where']['group'] = ' g.name=' . zbx_dbstr($options['group']);
     }
     // host
     if (!is_null($options['host'])) {
         $sqlParts['select']['host'] = 'h.host';
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['i'] = dbConditionInt('i.hostid', $options['hostids']);
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         $sqlParts['where']['host'] = ' h.host=' . zbx_dbstr($options['host']);
     }
     // min_severity
     if (!is_null($options['min_severity'])) {
         $sqlParts['where'][] = 't.priority>=' . zbx_dbstr($options['min_severity']);
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $triggerids = array();
     $sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $dbRes = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
     while ($trigger = DBfetch($dbRes)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $trigger;
             } else {
                 $result = $trigger['rowscount'];
             }
         } else {
             $triggerids[$trigger['triggerid']] = $trigger['triggerid'];
             if (!isset($result[$trigger['triggerid']])) {
                 $result[$trigger['triggerid']] = array();
             }
             // groups
             if (isset($trigger['groupid']) && is_null($options['selectGroups'])) {
                 if (!isset($result[$trigger['triggerid']]['groups'])) {
                     $result[$trigger['triggerid']]['groups'] = array();
                 }
                 $result[$trigger['triggerid']]['groups'][] = array('groupid' => $trigger['groupid']);
                 unset($trigger['groupid']);
             }
             // hostids
             if (isset($trigger['hostid']) && is_null($options['selectHosts'])) {
                 if (!isset($result[$trigger['triggerid']]['hosts'])) {
                     $result[$trigger['triggerid']]['hosts'] = array();
                 }
                 $result[$trigger['triggerid']]['hosts'][] = array('hostid' => $trigger['hostid']);
                 if (is_null($options['expandData'])) {
                     unset($trigger['hostid']);
                 }
             }
             // itemids
             if (isset($trigger['itemid']) && is_null($options['selectItems'])) {
                 if (!isset($result[$trigger['triggerid']]['items'])) {
                     $result[$trigger['triggerid']]['items'] = array();
                 }
                 $result[$trigger['triggerid']]['items'][] = array('itemid' => $trigger['itemid']);
                 unset($trigger['itemid']);
             }
             // expand expression
             if ($options['expandExpression'] !== null && isset($trigger['expression'])) {
                 $trigger['expression'] = explode_exp($trigger['expression'], false, true);
             }
             $result[$trigger['triggerid']] += $trigger;
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     if ($result) {
         $result = $this->addRelatedObjects($options, $result);
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Esempio n. 29
0
 /**
  * Get Itemprototype data.
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     $sqlParts = array('select' => array('items' => 'i.itemid'), 'from' => array('items' => 'items i'), 'where' => array('i.flags=' . ZBX_FLAG_DISCOVERY_PROTOTYPE), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('groupids' => null, 'templateids' => null, 'hostids' => null, 'itemids' => null, 'discoveryids' => null, 'graphids' => null, 'triggerids' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_EXTEND, 'selectHosts' => null, 'selectApplications' => null, 'selectTriggers' => null, 'selectGraphs' => null, 'selectDiscoveryRule' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($defOptions, $options);
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
         $userGroups = getUserGroupsByUserId($userid);
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE i.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>' . PERM_DENY . ' AND MAX(r.permission)>=' . zbx_dbstr($permission) . ')';
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['hostids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.hostid';
         }
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         $sqlParts['where']['itemid'] = dbConditionInt('i.itemid', $options['itemids']);
     }
     // discoveryids
     if (!is_null($options['discoveryids'])) {
         zbx_value2array($options['discoveryids']);
         $sqlParts['from']['item_discovery'] = 'item_discovery id';
         $sqlParts['where'][] = dbConditionInt('id.parent_itemid', $options['discoveryids']);
         $sqlParts['where']['idi'] = 'i.itemid=id.itemid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['id'] = 'id.parent_itemid';
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['where'][] = dbConditionInt('f.triggerid', $options['triggerids']);
         $sqlParts['where']['if'] = 'i.itemid=f.itemid';
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['where'][] = dbConditionInt('gi.graphid', $options['graphids']);
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 'i.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 'i.templateid IS NULL';
         }
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // monitored
     if (!is_null($options['monitored'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['monitored']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_MONITORED;
             $sqlParts['where'][] = 'i.status=' . ITEM_STATUS_ACTIVE;
         } else {
             $sqlParts['where'][] = '(h.status<>' . HOST_STATUS_MONITORED . ' OR i.status<>' . ITEM_STATUS_ACTIVE . ')';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('items i', $options, $sqlParts);
     }
     // --- FILTER ---
     if (is_array($options['filter'])) {
         $this->dbFilter('items i', $options, $sqlParts);
         if (isset($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sqlParts['from']['hosts'] = 'hosts h';
             $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
             $sqlParts['where']['h'] = dbConditionString('h.host', $options['filter']['host']);
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     //----------
     $sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
     while ($item = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $item;
             } else {
                 $result = $item['rowscount'];
             }
         } else {
             $result[$item['itemid']] = $item;
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     // add other related objects
     if ($result) {
         $result = $this->addRelatedObjects($options, $result);
         $result = $this->unsetExtraFields($result, array('hostid'), $options['output']);
     }
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
 /**
  * Get TriggerPrototypes data
  *
  * @param array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['triggerids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  * @return array|int item data as array or false if error
  */
 public function get(array $options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     // allowed columns for sorting
     $sortColumns = array('triggerid', 'description', 'status', 'priority');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     $sqlParts = array('select' => array('triggers' => 't.triggerid'), 'from' => array('t' => 'triggers t'), 'where' => array('t.flags=' . ZBX_FLAG_DISCOVERY_CHILD), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'triggerids' => null, 'itemids' => null, 'applicationids' => null, 'discoveryids' => null, 'functions' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'active' => null, 'maintenance' => null, 'nopermissions' => null, 'editable' => null, 'group' => null, 'host' => null, 'min_severity' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'expandExpression' => null, 'expandData' => null, 'output' => API_OUTPUT_REFER, 'selectGroups' => null, 'selectHosts' => null, 'selectItems' => null, 'selectFunctions' => null, 'selectDiscoveryRule' => 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']['triggers']);
         $dbTable = DB::getSchema('triggers');
         $sqlParts['select']['triggerid'] = 't.triggerid';
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 't.' . $field;
             }
         }
         // ignore the "expandExpression" parameter if the expression is not requested
         if ($options['expandExpression'] !== null && !str_in_array('expression', $options['output'])) {
             $options['expandExpression'] = null;
         }
         $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 functions f,items i,hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY f.triggerid' . ' HAVING MIN(r.permission)>=' . $permission . ')';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['groupid'] = 'hg.groupid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['groupid'] = dbConditionInt('hg.groupid', $options['groupids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['hg'] = 'hg.groupid';
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['hostid'] = 'i.hostid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['hostids']);
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.hostid';
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         $sqlParts['where']['triggerid'] = dbConditionInt('t.triggerid', $options['triggerids']);
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['itemid'] = 'f.itemid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['where']['itemid'] = dbConditionInt('f.itemid', $options['itemids']);
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['f'] = 'f.itemid';
         }
     }
     // applicationids
     if (!is_null($options['applicationids'])) {
         zbx_value2array($options['applicationids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['applicationid'] = 'a.applicationid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['applications'] = 'applications a';
         $sqlParts['where']['a'] = dbConditionInt('a.applicationid', $options['applicationids']);
         $sqlParts['where']['ia'] = 'i.hostid=a.hostid';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
     }
     // discoveryids
     if (!is_null($options['discoveryids'])) {
         zbx_value2array($options['discoveryids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['itemid'] = 'id.parent_itemid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['item_discovery'] = 'item_discovery id';
         $sqlParts['where']['fid'] = 'f.itemid=id.itemid';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where'][] = dbConditionInt('id.parent_itemid', $options['discoveryids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['id'] = 'id.parent_itemid';
         }
     }
     // functions
     if (!is_null($options['functions'])) {
         zbx_value2array($options['functions']);
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where'][] = dbConditionString('f.function', $options['functions']);
     }
     // monitored
     if (!is_null($options['monitored'])) {
         $sqlParts['where']['monitored'] = '' . ' NOT EXISTS (' . ' SELECT NULL' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT NULL' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND (' . ' ii.status<>' . ITEM_STATUS_ACTIVE . ' OR hh.status<>' . HOST_STATUS_MONITORED . ' )' . ' )' . ' )';
         $sqlParts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // active
     if (!is_null($options['active'])) {
         $sqlParts['where']['active'] = '' . ' NOT EXISTS (' . ' SELECT NULL' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT NULL' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND  hh.status<>' . HOST_STATUS_MONITORED . ' )' . ' )';
         $sqlParts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // maintenance
     if (!is_null($options['maintenance'])) {
         $sqlParts['where'][] = ($options['maintenance'] == 0 ? ' NOT ' : '') . ' EXISTS (' . ' SELECT NULL' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT NULL' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND hh.maintenance_status=1' . ' )' . ' )';
         $sqlParts['where'][] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 't.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 't.templateid IS NULL';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('triggers t', $options, $sqlParts);
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('triggers t', $options, $sqlParts);
         if (isset($options['filter']['host']) && !is_null($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sqlParts['from']['functions'] = 'functions f';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
             $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
             $sqlParts['from']['hosts'] = 'hosts h';
             $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
             $sqlParts['where']['host'] = dbConditionString('h.host', $options['filter']['host']);
         }
         if (isset($options['filter']['hostid']) && !is_null($options['filter']['hostid'])) {
             zbx_value2array($options['filter']['hostid']);
             $sqlParts['from']['functions'] = 'functions f';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
             $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
             $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['filter']['hostid']);
         }
     }
     // group
     if (!is_null($options['group'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['name'] = 'g.name';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['from']['groups'] = 'groups g';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sqlParts['where']['ghg'] = 'g.groupid = hg.groupid';
         $sqlParts['where']['group'] = ' g.name=' . zbx_dbstr($options['group']);
     }
     // host
     if (!is_null($options['host'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['host'] = 'h.host';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['i'] = dbConditionInt('i.hostid', $options['hostids']);
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         $sqlParts['where']['host'] = ' h.host=' . zbx_dbstr($options['host']);
     }
     // min_severity
     if (!is_null($options['min_severity'])) {
         $sqlParts['where'][] = 't.priority>=' . zbx_dbstr($options['min_severity']);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['triggers'] = 't.*';
     }
     // expandData
     if (!is_null($options['expandData'])) {
         $sqlParts['select']['host'] = 'h.host';
         $sqlParts['select']['hostid'] = 'h.hostid';
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('COUNT(DISTINCT t.triggerid) 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, 't');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $triggerids = array();
     $sqlParts['select'] = array_unique($sqlParts['select']);
     $sqlParts['from'] = array_unique($sqlParts['from']);
     $sqlParts['where'] = array_unique($sqlParts['where']);
     $sqlParts['group'] = array_unique($sqlParts['group']);
     $sqlParts['order'] = array_unique($sqlParts['order']);
     $sqlSelect = '';
     $sqlFrom = '';
     $sqlWhere = '';
     $sqlGroup = '';
     $sqlOrder = '';
     if (!empty($sqlParts['select'])) {
         $sqlSelect .= implode(',', $sqlParts['select']);
     }
     if (!empty($sqlParts['from'])) {
         $sqlFrom .= implode(',', $sqlParts['from']);
     }
     if (!empty($sqlParts['where'])) {
         $sqlWhere .= ' AND ' . implode(' AND ', $sqlParts['where']);
     }
     if (!empty($sqlParts['group'])) {
         $sqlWhere .= ' GROUP BY ' . implode(',', $sqlParts['group']);
     }
     if (!empty($sqlParts['order'])) {
         $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']);
     }
     $sqlLimit = $sqlParts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . DBin_node('t.triggerid', $nodeids) . $sqlWhere . $sqlGroup . $sqlOrder;
     $dbRes = DBselect($sql, $sqlLimit);
     while ($trigger = DBfetch($dbRes)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $trigger;
             } else {
                 $result = $trigger['rowscount'];
             }
         } else {
             $triggerids[$trigger['triggerid']] = $trigger['triggerid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$trigger['triggerid']] = array('triggerid' => $trigger['triggerid']);
             } else {
                 if (!isset($result[$trigger['triggerid']])) {
                     $result[$trigger['triggerid']] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$trigger['triggerid']]['hosts'])) {
                     $result[$trigger['triggerid']]['hosts'] = array();
                 }
                 if (!is_null($options['selectItems']) && !isset($result[$trigger['triggerid']]['items'])) {
                     $result[$trigger['triggerid']]['items'] = array();
                 }
                 if (!is_null($options['selectFunctions']) && !isset($result[$trigger['triggerid']]['functions'])) {
                     $result[$trigger['triggerid']]['functions'] = array();
                 }
                 if (!is_null($options['selectDiscoveryRule']) && !isset($result[$trigger['triggerid']]['discoveryRule'])) {
                     $result[$trigger['triggerid']]['discoveryRule'] = array();
                 }
                 // groups
                 if (isset($trigger['groupid']) && is_null($options['selectGroups'])) {
                     if (!isset($result[$trigger['triggerid']]['groups'])) {
                         $result[$trigger['triggerid']]['groups'] = array();
                     }
                     $result[$trigger['triggerid']]['groups'][] = array('groupid' => $trigger['groupid']);
                     unset($trigger['groupid']);
                 }
                 // hostids
                 if (isset($trigger['hostid']) && is_null($options['selectHosts'])) {
                     if (!isset($result[$trigger['triggerid']]['hosts'])) {
                         $result[$trigger['triggerid']]['hosts'] = array();
                     }
                     $result[$trigger['triggerid']]['hosts'][] = array('hostid' => $trigger['hostid']);
                     if (is_null($options['expandData'])) {
                         unset($trigger['hostid']);
                     }
                 }
                 // itemids
                 if (isset($trigger['itemid']) && is_null($options['selectItems'])) {
                     if (!isset($result[$trigger['triggerid']]['items'])) {
                         $result[$trigger['triggerid']]['items'] = array();
                     }
                     $result[$trigger['triggerid']]['items'][] = array('itemid' => $trigger['itemid']);
                     unset($trigger['itemid']);
                 }
                 // expand expression
                 if ($options['expandExpression'] !== null && $trigger['expression']) {
                     $trigger['expression'] = explode_exp($trigger['expression'], false, true);
                 }
                 $result[$trigger['triggerid']] += $trigger;
             }
         }
     }
     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' => $nodeids, 'output' => $options['selectGroups'], 'triggerids' => $triggerids, 'preservekeys' => true));
         foreach ($groups as $group) {
             $gtriggers = $group['triggers'];
             unset($group['triggers']);
             foreach ($gtriggers as $trigger) {
                 $result[$trigger['triggerid']]['groups'][] = $group;
             }
         }
     }
     // adding hosts
     if (!is_null($options['selectHosts'])) {
         $objParams = array('nodeids' => $nodeids, 'triggerids' => $triggerids, 'templated_hosts' => 1, 'nopermissions' => true, 'preservekeys' => true);
         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]['triggers']);
                 $count = array();
                 foreach ($host['triggers'] as $trigger) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$trigger['triggerid']])) {
                             $count[$trigger['triggerid']] = 0;
                         }
                         $count[$trigger['triggerid']]++;
                         if ($count[$trigger['triggerid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$trigger['triggerid']]['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 $triggerid => $trigger) {
                 $result[$triggerid]['hosts'] = isset($hosts[$triggerid]) ? $hosts[$triggerid]['rowscount'] : 0;
             }
         }
     }
     // adding functions
     if (!is_null($options['selectFunctions']) && str_in_array($options['selectFunctions'], $subselectsAllowedOutputs)) {
         $sqlSelect = $options['selectFunctions'] == API_OUTPUT_EXTEND ? 'f.*' : 'f.functionid, f.triggerid';
         $res = DBselect('SELECT ' . $sqlSelect . ' FROM functions f' . ' WHERE ' . dbConditionInt('f.triggerid', $triggerids));
         while ($function = DBfetch($res)) {
             $triggerid = $function['triggerid'];
             unset($function['triggerid']);
             $result[$triggerid]['functions'][] = $function;
         }
     }
     // adding items
     if (!is_null($options['selectItems']) && (is_array($options['selectItems']) || str_in_array($options['selectItems'], $subselectsAllowedOutputs))) {
         $items = API::Item()->get(array('nodeids' => $nodeids, 'output' => $options['selectItems'], 'triggerids' => $triggerids, 'webitems' => true, 'nopermissions' => true, 'preservekeys' => true, 'filter' => array('flags' => null)));
         foreach ($items as $item) {
             $itriggers = $item['triggers'];
             unset($item['triggers']);
             foreach ($itriggers as $trigger) {
                 $result[$trigger['triggerid']]['items'][] = $item;
             }
         }
     }
     // adding discoveryrule
     if (!is_null($options['selectDiscoveryRule'])) {
         $ruleids = $ruleMap = array();
         $dbRules = DBselect('SELECT id.parent_itemid,f.triggerid' . ' FROM item_discovery id,functions f' . ' WHERE ' . dbConditionInt('f.triggerid', $triggerids) . ' AND f.itemid=id.itemid');
         while ($rule = DBfetch($dbRules)) {
             $ruleids[$rule['parent_itemid']] = $rule['parent_itemid'];
             $ruleMap[$rule['triggerid']] = $rule['parent_itemid'];
         }
         $objParams = array('nodeids' => $nodeids, 'itemids' => $ruleids, 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectDiscoveryRule']) || str_in_array($options['selectDiscoveryRule'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectDiscoveryRule'];
             $discoveryRules = API::DiscoveryRule()->get($objParams);
             foreach ($result as $triggerid => $trigger) {
                 if (isset($ruleMap[$triggerid]) && isset($discoveryRules[$ruleMap[$triggerid]])) {
                     $result[$triggerid]['discoveryRule'] = $discoveryRules[$ruleMap[$triggerid]];
                 }
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }