/** * Get interface data. * * @param array $options * @param array $options['nodeids'] Node IDs * @param array $options['hostids'] Interface IDs * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins * @param boolean $options['selectHosts'] select Interface hosts * @param boolean $options['selectItems'] select Items * @param int $options['count'] count Interfaces, returned column name is rowscount * @param string $options['pattern'] search hosts by pattern in Interface name * @param int $options['limit'] limit selection * @param string $options['sortfield'] field to sort by * @param string $options['sortorder'] sort order * * @return array|boolean Interface data as array or false if error */ public function get(array $options = array()) { $result = array(); $nodeCheck = false; $userType = self::$userData['type']; $userId = self::$userData['userid']; $sqlParts = array('select' => array('interface' => 'hi.interfaceid'), 'from' => array('interface' => 'interface hi'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'interfaceids' => null, 'itemids' => null, 'triggerids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectHosts' => null, 'selectItems' => 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 hi.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>' . PERM_DENY . ' AND MAX(r.permission)>=' . $permission . ')'; } // nodeids $nodeids = is_null($options['nodeids']) ? get_current_nodeid() : $options['nodeids']; // interfaceids if (!is_null($options['interfaceids'])) { zbx_value2array($options['interfaceids']); $sqlParts['where']['interfaceid'] = dbConditionInt('hi.interfaceid', $options['interfaceids']); if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'hi.interfaceid', $nodeids); } } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); $sqlParts['select']['hostid'] = 'hi.hostid'; $sqlParts['where']['hostid'] = dbConditionInt('hi.hostid', $options['hostids']); if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'hi.hostid', $nodeids); } } // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); $sqlParts['select']['itemid'] = 'i.itemid'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where'][] = dbConditionInt('i.itemid', $options['itemids']); $sqlParts['where']['hi'] = 'hi.interfaceid=i.interfaceid'; if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'i.itemid', $nodeids); } } // triggerids if (!is_null($options['triggerids'])) { zbx_value2array($options['triggerids']); $sqlParts['select']['triggerid'] = 'f.triggerid'; $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where'][] = dbConditionInt('f.triggerid', $options['triggerids']); $sqlParts['where']['hi'] = 'hi.hostid=i.hostid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'f.triggerid', $nodeids); } } // node check, should last, after all ****IDS checks if (!$nodeCheck) { $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'hi.interfaceid', $nodeids); } // search if (is_array($options['search'])) { zbx_db_search('interface hi', $options, $sqlParts); } // filter if (is_array($options['filter'])) { $this->dbFilter('interface hi', $options, $sqlParts); } // 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); $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts); $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']); while ($interface = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $interface; } else { $result = $interface['rowscount']; } } else { if (!isset($result[$interface['interfaceid']])) { $result[$interface['interfaceid']] = array(); } // itemids if (isset($interface['itemid']) && is_null($options['selectItems'])) { if (!isset($result[$interface['interfaceid']]['items'])) { $result[$interface['interfaceid']]['items'] = array(); } $result[$interface['interfaceid']]['items'][] = array('itemid' => $interface['itemid']); unset($interface['itemid']); } $result[$interface['interfaceid']] += $interface; } } 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; }
/** * Get Template data * * @param array $options * @return array|boolean Template data as array or false if error */ public function get($options = array()) { $result = array(); $nodeCheck = false; $userType = self::$userData['type']; $userid = self::$userData['userid']; $sqlParts = array('select' => array('templates' => 'h.hostid'), 'from' => array('hosts' => 'hosts h'), 'where' => array('h.status=' . HOST_STATUS_TEMPLATE), 'group' => array(), 'order' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'parentTemplateids' => null, 'hostids' => null, 'graphids' => null, 'itemids' => null, 'triggerids' => null, 'with_items' => null, 'with_triggers' => null, 'with_graphs' => null, 'with_httptests' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => '', 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectGroups' => null, 'selectHosts' => null, 'selectTemplates' => null, 'selectParentTemplates' => null, 'selectItems' => null, 'selectDiscoveries' => null, 'selectTriggers' => null, 'selectGraphs' => null, 'selectApplications' => null, 'selectMacros' => null, 'selectScreens' => null, 'selectHttpTests' => 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 h.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>' . PERM_DENY . ' AND MAX(r.permission)>=' . $permission . ')'; } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // groupids if (!is_null($options['groupids'])) { zbx_value2array($options['groupids']); $sqlParts['select']['groupid'] = 'hg.groupid'; $sqlParts['from']['hosts_groups'] = 'hosts_groups hg'; $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']); $sqlParts['where']['hgh'] = 'hg.hostid=h.hostid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['hg'] = 'hg.groupid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'hg.groupid', $nodeids); } } // templateids if (!is_null($options['templateids'])) { zbx_value2array($options['templateids']); $sqlParts['where']['templateid'] = dbConditionInt('h.hostid', $options['templateids']); if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'h.hostid', $nodeids); } } // parentTemplateids if (!is_null($options['parentTemplateids'])) { zbx_value2array($options['parentTemplateids']); $sqlParts['select']['parentTemplateid'] = 'ht.templateid as parentTemplateid'; $sqlParts['from']['hosts_templates'] = 'hosts_templates ht'; $sqlParts['where'][] = dbConditionInt('ht.templateid', $options['parentTemplateids']); $sqlParts['where']['hht'] = 'h.hostid=ht.hostid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['templateid'] = 'ht.templateid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'ht.templateid', $nodeids); } } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); $sqlParts['select']['linked_hostid'] = 'ht.hostid as linked_hostid'; $sqlParts['from']['hosts_templates'] = 'hosts_templates ht'; $sqlParts['where'][] = dbConditionInt('ht.hostid', $options['hostids']); $sqlParts['where']['hht'] = 'h.hostid=ht.templateid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['ht'] = 'ht.hostid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'ht.hostid', $nodeids); } } // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); $sqlParts['select']['itemid'] = 'i.itemid'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where'][] = dbConditionInt('i.itemid', $options['itemids']); $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'i.itemid', $nodeids); } } // triggerids if (!is_null($options['triggerids'])) { zbx_value2array($options['triggerids']); $sqlParts['select']['triggerid'] = 'f.triggerid'; $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where'][] = dbConditionInt('f.triggerid', $options['triggerids']); $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'f.triggerid', $nodeids); } } // graphids if (!is_null($options['graphids'])) { zbx_value2array($options['graphids']); $sqlParts['select']['graphid'] = 'gi.graphid'; $sqlParts['from']['graphs_items'] = 'graphs_items gi'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where'][] = dbConditionInt('gi.graphid', $options['graphids']); $sqlParts['where']['igi'] = 'i.itemid=gi.itemid'; $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'gi.graphid', $nodeids); } } // node check !!!! // should last, after all ****IDS checks if (!$nodeCheck) { $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'h.hostid', $nodeids); } // with_items if (!is_null($options['with_items'])) { $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM items i' . ' WHERE h.hostid=i.hostid' . ' AND i.flags IN (' . ZBX_FLAG_DISCOVERY_NORMAL . ',' . ZBX_FLAG_DISCOVERY_CREATED . ')' . ')'; } // with_triggers if (!is_null($options['with_triggers'])) { $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM items i,functions f,triggers t' . ' WHERE i.hostid=h.hostid' . ' AND i.itemid=f.itemid' . ' AND f.triggerid=t.triggerid' . ' AND t.flags IN (' . ZBX_FLAG_DISCOVERY_NORMAL . ',' . ZBX_FLAG_DISCOVERY_CREATED . ')' . ')'; } // with_graphs if (!is_null($options['with_graphs'])) { $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM items i,graphs_items gi,graphs g' . ' WHERE i.hostid=h.hostid' . ' AND i.itemid=gi.itemid' . ' AND gi.graphid=g.graphid' . ' AND g.flags IN (' . ZBX_FLAG_DISCOVERY_NORMAL . ',' . ZBX_FLAG_DISCOVERY_CREATED . ')' . ')'; } // with_httptests if (!empty($options['with_httptests'])) { $sqlParts['where'][] = 'EXISTS (SELECT ht.httptestid FROM httptest ht WHERE ht.hostid=h.hostid)'; } // filter if (is_array($options['filter'])) { $this->dbFilter('hosts h', $options, $sqlParts); } // search if (is_array($options['search'])) { zbx_db_search('hosts h', $options, $sqlParts); } // 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); $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts); $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']); while ($template = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $template; } else { $result = $template['rowscount']; } } else { $template['templateid'] = $template['hostid']; unset($template['hostid']); if (!isset($result[$template['templateid']])) { $result[$template['templateid']] = array(); } // groupids if (isset($template['groupid']) && is_null($options['selectGroups'])) { if (!isset($result[$template['templateid']]['groups'])) { $result[$template['templateid']]['groups'] = array(); } $result[$template['templateid']]['groups'][] = array('groupid' => $template['groupid']); unset($template['groupid']); } // hostids if (isset($template['linked_hostid']) && is_null($options['selectHosts'])) { if (!isset($result[$template['templateid']]['hosts'])) { $result[$template['templateid']]['hosts'] = array(); } $result[$template['templateid']]['hosts'][] = array('hostid' => $template['linked_hostid']); unset($template['linked_hostid']); } // parentTemplateids if (isset($template['parentTemplateid']) && is_null($options['selectParentTemplates'])) { if (!isset($result[$template['templateid']]['parentTemplates'])) { $result[$template['templateid']]['parentTemplates'] = array(); } $result[$template['templateid']]['parentTemplates'][] = array('templateid' => $template['parentTemplateid']); unset($template['parentTemplateid']); } // itemids if (isset($template['itemid']) && is_null($options['selectItems'])) { if (!isset($result[$template['templateid']]['items'])) { $result[$template['templateid']]['items'] = array(); } $result[$template['templateid']]['items'][] = array('itemid' => $template['itemid']); unset($template['itemid']); } // triggerids if (isset($template['triggerid']) && is_null($options['selectTriggers'])) { if (!isset($result[$template['templateid']]['triggers'])) { $result[$template['templateid']]['triggers'] = array(); } $result[$template['templateid']]['triggers'][] = array('triggerid' => $template['triggerid']); unset($template['triggerid']); } // graphids if (isset($template['graphid']) && is_null($options['selectGraphs'])) { if (!isset($result[$template['templateid']]['graphs'])) { $result[$template['templateid']]['graphs'] = array(); } $result[$template['templateid']]['graphs'][] = array('graphid' => $template['graphid']); unset($template['graphid']); } $result[$template['templateid']] += $template; } } 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 discovery service data. * * @param array $options * @param array $options['nodeids'] Node IDs * @param array $options['groupids'] ServiceGroup IDs * @param array $options['hostids'] Service IDs * @param bool $options['monitored_hosts'] only monitored Services * @param bool $options['templated_hosts'] include templates in result * @param bool $options['with_items'] only with items * @param bool $options['with_triggers'] only with triggers * @param bool $options['with_httptests'] only with http tests * @param bool $options['with_graphs'] only with graphs * @param bool $options['editable'] only with read-write permission. Ignored for SuperAdmins * @param bool $options['selectGroups'] select ServiceGroups * @param bool $options['selectTemplates'] select Templates * @param bool $options['selectItems'] select Items * @param bool $options['selectTriggers'] select Triggers * @param bool $options['selectGraphs'] select Graphs * @param int $options['count'] count Services, returned column name is rowscount * @param string $options['pattern'] search hosts by pattern in Service name * @param string $options['extendPattern'] search hosts by pattern in Service name, ip and DNS * @param int $options['limit'] limit selection * @param string $options['sortfield'] field to sort by * @param string $options['sortorder'] sort order * * @return array service data as array or false if error */ public function get($options = array()) { $result = array(); $nodeCheck = false; $userType = self::$userData['type']; $sqlParts = array('select' => array('dservices' => 'ds.dserviceid'), 'from' => array('dservices' => 'dservices ds'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'dserviceids' => null, 'dhostids' => null, 'dcheckids' => null, 'druleids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectDRules' => null, 'selectDHosts' => null, 'selectHosts' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null); $options = zbx_array_merge($defOptions, $options); // editable + PERMISSION CHECK if (USER_TYPE_SUPER_ADMIN == $userType) { } elseif (is_null($options['editable']) && self::$userData['type'] == USER_TYPE_ZABBIX_ADMIN) { } elseif (!is_null($options['editable']) && self::$userData['type'] != USER_TYPE_SUPER_ADMIN) { return array(); } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // dserviceids if (!is_null($options['dserviceids'])) { zbx_value2array($options['dserviceids']); $sqlParts['where']['dserviceid'] = dbConditionInt('ds.dserviceid', $options['dserviceids']); if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'ds.dserviceid', $nodeids); } } // dhostids if (!is_null($options['dhostids'])) { zbx_value2array($options['dhostids']); $sqlParts['select']['dhostid'] = 'ds.dhostid'; $sqlParts['where'][] = dbConditionInt('ds.dhostid', $options['dhostids']); if (!is_null($options['groupCount'])) { $sqlParts['group']['dhostid'] = 'ds.dhostid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'ds.dhostid', $nodeids); } } // dcheckids if (!is_null($options['dcheckids'])) { zbx_value2array($options['dcheckids']); $sqlParts['select']['dcheckid'] = 'dc.dcheckid'; $sqlParts['from']['dhosts'] = 'dhosts dh'; $sqlParts['from']['dchecks'] = 'dchecks dc'; $sqlParts['where'][] = dbConditionInt('dc.dcheckid', $options['dcheckids']); $sqlParts['where']['dhds'] = 'dh.dhostid=ds.dhostid'; $sqlParts['where']['dcdh'] = 'dc.druleid=dh.druleid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['dcheckid'] = 'dc.dcheckid'; } } // druleids if (!is_null($options['druleids'])) { zbx_value2array($options['druleids']); $sqlParts['select']['druleid'] = 'dh.druleid'; $sqlParts['from']['dhosts'] = 'dhosts dh'; $sqlParts['where']['druleid'] = dbConditionInt('dh.druleid', $options['druleids']); $sqlParts['where']['dhds'] = 'dh.dhostid=ds.dhostid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['druleid'] = 'dh.druleid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'dh.druleid', $nodeids); } } // node check !!!!! // should last, after all ****IDS checks if (!$nodeCheck) { $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'ds.dserviceid', $nodeids); } // filter if (is_array($options['filter'])) { $this->dbFilter('dservices ds', $options, $sqlParts); } // search if (is_array($options['search'])) { zbx_db_search('dservices ds', $options, $sqlParts); } // 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); $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts); $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']); while ($dservice = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $dservice; } else { $result = $dservice['rowscount']; } } else { if (!isset($result[$dservice['dserviceid']])) { $result[$dservice['dserviceid']] = array(); } // druleids if (isset($dservice['druleid']) && is_null($options['selectDRules'])) { if (!isset($result[$dservice['dserviceid']]['drules'])) { $result[$dservice['dserviceid']]['drules'] = array(); } $result[$dservice['dserviceid']]['drules'][] = array('druleid' => $dservice['druleid']); } // dhostids if (isset($dservice['dhostid']) && is_null($options['selectDHosts'])) { if (!isset($result[$dservice['dserviceid']]['dhosts'])) { $result[$dservice['dserviceid']]['dhosts'] = array(); } $result[$dservice['dserviceid']]['dhosts'][] = array('dhostid' => $dservice['dhostid']); } $result[$dservice['dserviceid']] += $dservice; } } if (!is_null($options['countOutput'])) { return $result; } if ($result) { $result = $this->addRelatedObjects($options, $result); $result = $this->unsetExtraFields($result, array('dhostid'), $options['output']); } // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
/** * Get Media types data * * @param array $options * @param array $options['nodeids'] filter by Node IDs * @param array $options['mediatypeids'] filter by Mediatype IDs * @param boolean $options['type'] filter by Mediatype type [ USER_TYPE_ZABBIX_USER: 1, USER_TYPE_ZABBIX_ADMIN: 2, USER_TYPE_SUPER_ADMIN: 3 ] * @param boolean $options['output'] output only Mediatype IDs if not set. * @param boolean $options['count'] output only count of objects in result. ( result returned in property 'rowscount' ) * @param string $options['pattern'] filter by Host name containing only give pattern * @param int $options['limit'] output will be limited to given number * @param string $options['sortfield'] output will be sorted by given property [ 'mediatypeid', 'alias' ] * @param string $options['sortorder'] output will be sorted in given order [ 'ASC', 'DESC' ] * @return array */ public function get($options = array()) { $result = array(); $nodeCheck = false; $userType = self::$userData['type']; $sqlParts = array('select' => array('media_type' => 'mt.mediatypeid'), 'from' => array('media_type' => 'media_type mt'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'mediatypeids' => null, 'mediaids' => null, 'userids' => null, 'editable' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectUsers' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null); $options = zbx_array_merge($defOptions, $options); // permission check if (USER_TYPE_SUPER_ADMIN == $userType) { } elseif (is_null($options['editable']) && self::$userData['type'] == USER_TYPE_ZABBIX_ADMIN) { } elseif (!is_null($options['editable']) || self::$userData['type'] != USER_TYPE_SUPER_ADMIN) { return array(); } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // mediatypeids if (!is_null($options['mediatypeids'])) { zbx_value2array($options['mediatypeids']); $sqlParts['where'][] = dbConditionInt('mt.mediatypeid', $options['mediatypeids']); if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'mt.mediatypeid', $nodeids); } } // mediaids if (!is_null($options['mediaids'])) { zbx_value2array($options['mediaids']); $sqlParts['select']['mediaid'] = 'm.mediaid'; $sqlParts['from']['media'] = 'media m'; $sqlParts['where'][] = dbConditionInt('m.mediaid', $options['mediaids']); $sqlParts['where']['mmt'] = 'm.mediatypeid=mt.mediatypeid'; if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'm.mediaid', $nodeids); } } // userids if (!is_null($options['userids'])) { zbx_value2array($options['userids']); $sqlParts['select']['userid'] = 'm.userid'; $sqlParts['from']['media'] = 'media m'; $sqlParts['where'][] = dbConditionInt('m.userid', $options['userids']); $sqlParts['where']['mmt'] = 'm.mediatypeid=mt.mediatypeid'; if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'm.userid', $nodeids); } } // should last, after all ****IDS checks if (!$nodeCheck) { $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'mt.mediatypeid', $nodeids); } // filter if (is_array($options['filter'])) { $this->dbFilter('media_type mt', $options, $sqlParts); } // search if (is_array($options['search'])) { zbx_db_search('media_type mt', $options, $sqlParts); } // 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); $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts); $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']); while ($mediatype = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $mediatype; } else { $result = $mediatype['rowscount']; } } else { if (!isset($result[$mediatype['mediatypeid']])) { $result[$mediatype['mediatypeid']] = array(); } // userids if (isset($mediatype['userid']) && is_null($options['selectUsers'])) { if (!isset($result[$mediatype['mediatypeid']]['users'])) { $result[$mediatype['mediatypeid']]['users'] = array(); } $result[$mediatype['mediatypeid']]['users'][] = array('userid' => $mediatype['userid']); unset($mediatype['userid']); } $result[$mediatype['mediatypeid']] += $mediatype; } } 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 UserMacros data. * * @param array $options * @param array $options['nodeids'] node ids * @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'] tempalate 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('nodeids' => null, '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_REFER, '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)>=' . $permission . ')'; } } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // global macro if (!is_null($options['globalmacro'])) { $sqlPartsGlobal['where'] = sqlPartDbNode($sqlPartsGlobal['where'], 'gm.globalmacroid', $nodeids); $options['groupids'] = null; $options['hostmacroids'] = null; $options['triggerids'] = null; $options['hostids'] = null; $options['itemids'] = null; $options['selectGroups'] = null; $options['selectTemplates'] = null; $options['selectHosts'] = null; } else { $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'hm.hostmacroid', $nodeids); } // 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['select']['groupid'] = 'hg.groupid'; $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['select']['hostid'] = 'hm.hostid'; $sqlParts['where'][] = dbConditionInt('hm.hostid', $options['hostids']); } // templateids if (!is_null($options['templateids'])) { zbx_value2array($options['templateids']); $sqlParts['select']['templateid'] = 'ht.templateid'; $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 { if (!isset($result[$macro['globalmacroid']])) { $result[$macro['globalmacroid']] = array(); } $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 { if (!isset($result[$macro['hostmacroid']])) { $result[$macro['hostmacroid']] = array(); } // groupids if (isset($macro['groupid'])) { if (!isset($result[$macro['hostmacroid']]['groups'])) { $result[$macro['hostmacroid']]['groups'] = array(); } $result[$macro['hostmacroid']]['groups'][] = array('groupid' => $macro['groupid']); unset($macro['groupid']); } // templateids if (isset($macro['templateid'])) { if (!isset($result[$macro['hostmacroid']]['templates'])) { $result[$macro['hostmacroid']]['templates'] = array(); } $result[$macro['hostmacroid']]['templates'][] = array('templateid' => $macro['templateid']); unset($macro['templateid']); } // hostids if (isset($macro['hostid'])) { if (!isset($result[$macro['hostmacroid']]['hosts'])) { $result[$macro['hostmacroid']]['hosts'] = array(); } $result[$macro['hostmacroid']]['hosts'][] = array('hostid' => $macro['hostid']); } $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; }
function get_accessible_groups_by_rights(&$rights, $user_type, $perm, $perm_res = null, $nodeid = null) { $result = array(); $where = array(); if (!is_null($nodeid)) { $where = sqlPartDbNode($where, 'g.groupid', $nodeid); } if (count($where)) { $where = ' WHERE ' . implode(' AND ', $where); } else { $where = ''; } $group_perm = array(); foreach ($rights as $right) { $group_perm[$right['id']] = $right['permission']; } $dbHostGroups = DBselect('SELECT n.nodeid AS nodeid,n.name AS node_name,g.*,' . PERM_DENY . ' AS permission' . ' FROM groups g' . ' LEFT JOIN nodes n ON ' . DBid2nodeid('g.groupid') . '=n.nodeid' . $where); while ($dbHostGroup = DBfetch($dbHostGroups)) { if (USER_TYPE_SUPER_ADMIN == $user_type) { $dbHostGroup['permission'] = PERM_READ_WRITE; } else { if (isset($group_perm[$dbHostGroup['groupid']])) { $dbHostGroup['permission'] = $group_perm[$dbHostGroup['groupid']]; } else { if (is_null($dbHostGroup['nodeid'])) { $dbHostGroup['nodeid'] = id2nodeid($dbHostGroup['groupid']); } $dbHostGroup['permission'] = PERM_DENY; } } if ($dbHostGroup['permission'] < $perm) { continue; } switch ($perm_res) { case PERM_RES_DATA_ARRAY: $result[$dbHostGroup['groupid']] = $dbHostGroup; break; default: $result[$dbHostGroup['groupid']] = $dbHostGroup['groupid']; } } CArrayHelper::sort($result, array(array('field' => 'node_name', 'order' => ZBX_SORT_UP), array('field' => 'name', 'order' => ZBX_SORT_UP))); return $result; }
/** * Get events data. * * @param _array $options * @param array $options['itemids'] * @param array $options['hostids'] * @param array $options['groupids'] * @param array $options['eventids'] * @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($options = array()) { $result = array(); $nodeCheck = false; $userType = self::$userData['type']; $userid = self::$userData['userid']; $sqlParts = array('select' => array($this->fieldId('eventid')), 'from' => array('events' => 'events e'), 'where' => array(), 'order' => array(), 'group' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'triggerids' => null, 'objectids' => null, 'eventids' => null, 'editable' => null, 'object' => EVENT_OBJECT_TRIGGER, 'source' => EVENT_SOURCE_TRIGGERS, 'acknowledged' => null, 'nopermissions' => null, 'value' => null, 'time_from' => null, 'time_till' => null, 'eventid_from' => null, 'eventid_till' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectHosts' => null, 'selectItems' => null, 'selectTriggers' => null, 'selectRelatedObject' => null, 'select_alerts' => null, 'select_acknowledges' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null); $options = zbx_array_merge($defOptions, $options); $this->checkDeprecatedParam($options, 'selectTriggers'); $this->checkDeprecatedParam($options, 'selectItems'); $this->checkDeprecatedParam($options, 'sortfield', 'object'); $options = $this->convertDeprecatedParam($options, 'triggerids', 'objectids'); $this->validateGet($options); // editable + PERMISSION CHECK if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) { // triggers if ($options['object'] == EVENT_OBJECT_TRIGGER) { // specific triggers if ($options['objectids'] !== null) { $triggers = API::Trigger()->get(array('triggerids' => $options['objectids'], 'editable' => $options['editable'])); $options['objectids'] = zbx_objectValues($triggers, 'triggerid'); } else { $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ; $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', getUserGroupsByUserId($userid)) . ' WHERE e.objectid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY f.triggerid' . ' HAVING MIN(r.permission)>' . PERM_DENY . ' AND MAX(r.permission)>=' . $permission . ')'; } } elseif ($options['object'] == EVENT_OBJECT_ITEM || $options['object'] == EVENT_OBJECT_LLDRULE) { // specific items or LLD rules if ($options['objectids'] !== null) { if ($options['object'] == EVENT_OBJECT_ITEM) { $items = API::Item()->get(array('output' => array('itemid'), 'itemids' => $options['objectids'], 'editable' => $options['editable'])); $options['objectids'] = zbx_objectValues($items, 'itemid'); } elseif ($options['object'] == EVENT_OBJECT_LLDRULE) { $items = API::DiscoveryRule()->get(array('itemids' => $options['objectids'], 'editable' => $options['editable'])); $options['objectids'] = zbx_objectValues($items, 'itemid'); } } else { $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ; $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM items i,hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', getUserGroupsByUserId($userid)) . ' WHERE e.objectid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>' . PERM_DENY . ' AND MAX(r.permission)>=' . $permission . ')'; } } } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // eventids if (!is_null($options['eventids'])) { zbx_value2array($options['eventids']); $sqlParts['where'][] = dbConditionInt('e.eventid', $options['eventids']); if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'e.objectid', $nodeids); } } // objectids if ($options['objectids'] !== null && in_array($options['object'], array(EVENT_OBJECT_TRIGGER, EVENT_OBJECT_ITEM, EVENT_OBJECT_LLDRULE))) { zbx_value2array($options['objectids']); $sqlParts['where'][] = dbConditionInt('e.objectid', $options['objectids']); if (!is_null($options['groupCount'])) { $sqlParts['group']['objectid'] = 'e.objectid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'e.objectid', $nodeids); } } // groupids if (!is_null($options['groupids'])) { zbx_value2array($options['groupids']); $sqlParts = $this->addQuerySelect('hg.groupid', $sqlParts); // triggers if ($options['object'] == EVENT_OBJECT_TRIGGER) { $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts_groups'] = 'hosts_groups hg'; $sqlParts['where']['hg'] = dbConditionInt('hg.groupid', $options['groupids']); $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid'; $sqlParts['where']['fe'] = 'f.triggerid=e.objectid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; } elseif ($options['object'] == EVENT_OBJECT_LLDRULE || $options['object'] == EVENT_OBJECT_ITEM) { $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts_groups'] = 'hosts_groups hg'; $sqlParts['where']['hg'] = dbConditionInt('hg.groupid', $options['groupids']); $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid'; $sqlParts['where']['fi'] = 'e.objectid=i.itemid'; } } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); $sqlParts = $this->addQuerySelect('i.hostid', $sqlParts); // triggers if ($options['object'] == EVENT_OBJECT_TRIGGER) { $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where']['i'] = dbConditionInt('i.hostid', $options['hostids']); $sqlParts['where']['ft'] = 'f.triggerid=e.objectid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; } elseif ($options['object'] == EVENT_OBJECT_LLDRULE || $options['object'] == EVENT_OBJECT_ITEM) { $sqlParts['from']['items'] = 'items i'; $sqlParts['where']['i'] = dbConditionInt('i.hostid', $options['hostids']); $sqlParts['where']['fi'] = 'e.objectid=i.itemid'; } } // should last, after all ****IDS checks if (!$nodeCheck) { $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'e.eventid', $nodeids); } // object if (!is_null($options['object'])) { $sqlParts['where']['o'] = 'e.object=' . zbx_dbstr($options['object']); } // source if (!is_null($options['source'])) { $sqlParts['where'][] = 'e.source=' . zbx_dbstr($options['source']); } // acknowledged if (!is_null($options['acknowledged'])) { $sqlParts['where'][] = 'e.acknowledged=' . ($options['acknowledged'] ? 1 : 0); } // time_from if (!is_null($options['time_from'])) { $sqlParts['where'][] = 'e.clock>=' . zbx_dbstr($options['time_from']); } // time_till if (!is_null($options['time_till'])) { $sqlParts['where'][] = 'e.clock<=' . zbx_dbstr($options['time_till']); } // eventid_from if (!is_null($options['eventid_from'])) { $sqlParts['where'][] = 'e.eventid>=' . zbx_dbstr($options['eventid_from']); } // eventid_till if (!is_null($options['eventid_till'])) { $sqlParts['where'][] = 'e.eventid<=' . zbx_dbstr($options['eventid_till']); } // value if (!is_null($options['value'])) { zbx_value2array($options['value']); $sqlParts['where'][] = dbConditionInt('e.value', $options['value']); } // search if (is_array($options['search'])) { zbx_db_search('events e', $options, $sqlParts); } // filter if (is_array($options['filter'])) { $this->dbFilter('events e', $options, $sqlParts); } // 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); $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts); $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']); while ($event = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $event; } else { $result = $event['rowscount']; } } else { if (!isset($result[$event['eventid']])) { $result[$event['eventid']] = array(); } // hostids if (isset($event['hostid']) && is_null($options['selectHosts'])) { if (!isset($result[$event['eventid']]['hosts'])) { $result[$event['eventid']]['hosts'] = array(); } $result[$event['eventid']]['hosts'][] = array('hostid' => $event['hostid']); unset($event['hostid']); } $result[$event['eventid']] += $event; } } if (!is_null($options['countOutput'])) { return $result; } if ($result) { $result = $this->addRelatedObjects($options, $result); $result = $this->unsetExtraFields($result, array('object', 'objectid'), $options['output']); } // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
/** * Modifies the SQL parts to implement all of the node related options. * * @param string $tableName * @param string $tableAlias * @param array $options * @param array $sqlParts * * @return array */ protected function applyQueryNodeOptions($tableName, $tableAlias, array $options, array $sqlParts) { $pkOption = $this->pkOption($tableName); $pkFieldId = $this->fieldId($this->pk($tableName), $tableAlias); // if no specific ids are given, apply the node filter if (!isset($options[$pkOption])) { $nodeIds = isset($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], $pkFieldId, $nodeIds); } return $sqlParts; }
/** * Get history data. * * @param array $options * @param array $options['itemids'] * @param boolean $options['editable'] * @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(); $nodeCheck = false; $sqlParts = array('select' => array('history' => 'h.itemid'), 'from' => array(), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $defOptions = array('history' => ITEM_VALUE_TYPE_UINT64, 'nodeids' => null, 'hostids' => null, 'itemids' => null, 'triggerids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'time_from' => null, 'time_till' => null, 'output' => API_OUTPUT_REFER, 'countOutput' => null, 'groupCount' => null, 'groupOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null); $options = zbx_array_merge($defOptions, $options); if (!($tableName = CHistoryManager::getTableName($options['history']))) { $tableName = 'history'; } $sqlParts['from']['history'] = $tableName . ' h'; // editable + PERMISSION CHECK if (USER_TYPE_SUPER_ADMIN == self::$userData['type'] || $options['nopermissions']) { } else { $items = API::Item()->get(array('itemids' => $options['itemids'] === null ? null : $options['itemids'], 'output' => array('itemid'), 'editable' => $options['editable'], 'preservekeys' => true, 'webitems' => true)); $options['itemids'] = array_keys($items); } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); $sqlParts['where']['itemid'] = dbConditionInt('h.itemid', $options['itemids']); if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'h.itemid', $nodeids); } } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); $sqlParts['select']['hostid'] = 'i.hostid'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where']['i'] = dbConditionInt('i.hostid', $options['hostids']); $sqlParts['where']['hi'] = 'h.itemid=i.itemid'; if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'i.hostid', $nodeids); } } // should be last, after all ****IDS checks if (!$nodeCheck) { $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'h.itemid', $nodeids); } // time_from if (!is_null($options['time_from'])) { $sqlParts['select']['clock'] = 'h.clock'; $sqlParts['where']['clock_from'] = 'h.clock>=' . zbx_dbstr($options['time_from']); } // time_till if (!is_null($options['time_till'])) { $sqlParts['select']['clock'] = 'h.clock'; $sqlParts['where']['clock_till'] = 'h.clock<=' . zbx_dbstr($options['time_till']); } // filter if (is_array($options['filter'])) { $this->dbFilter($sqlParts['from']['history'], $options, $sqlParts); } // search if (is_array($options['search'])) { zbx_db_search($sqlParts['from']['history'], $options, $sqlParts); } // output if ($options['output'] == API_OUTPUT_EXTEND) { unset($sqlParts['select']['clock']); $sqlParts['select']['history'] = 'h.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sqlParts['select'] = array('count(DISTINCT h.hostid) as rowscount'); // groupCount if (!is_null($options['groupCount'])) { foreach ($sqlParts['group'] as $key => $fields) { $sqlParts['select'][$key] = $fields; } } } // groupOutput $groupOutput = false; if (!is_null($options['groupOutput'])) { if (str_in_array('h.' . $options['groupOutput'], $sqlParts['select']) || str_in_array('h.*', $sqlParts['select'])) { $groupOutput = true; } } // sorting $sqlParts = $this->applyQuerySortOptions($tableName, $this->tableAlias(), $options, $sqlParts); // 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['order'] = array_unique($sqlParts['order']); $sqlSelect = ''; $sqlFrom = ''; $sqlOrder = ''; if (!empty($sqlParts['select'])) { $sqlSelect .= implode(',', $sqlParts['select']); } if (!empty($sqlParts['from'])) { $sqlFrom .= implode(',', $sqlParts['from']); } $sqlWhere = !empty($sqlParts['where']) ? ' WHERE ' . implode(' AND ', $sqlParts['where']) : ''; if (!empty($sqlParts['order'])) { $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']); } $sqlLimit = $sqlParts['limit']; $sql = 'SELECT ' . $sqlSelect . ' FROM ' . $sqlFrom . $sqlWhere . $sqlOrder; $dbRes = DBselect($sql, $sqlLimit); $count = 0; $group = array(); while ($data = DBfetch($dbRes)) { if ($options['countOutput']) { $result = $data; } else { $itemids[$data['itemid']] = $data['itemid']; $result[$count] = array(); // hostids if (isset($data['hostid'])) { if (!isset($result[$count]['hosts'])) { $result[$count]['hosts'] = array(); } $result[$count]['hosts'][] = array('hostid' => $data['hostid']); unset($data['hostid']); } // triggerids if (isset($data['triggerid'])) { if (!isset($result[$count]['triggers'])) { $result[$count]['triggers'] = array(); } $result[$count]['triggers'][] = array('triggerid' => $data['triggerid']); unset($data['triggerid']); } $result[$count] += $data; // grouping if ($groupOutput) { $dataid = $data[$options['groupOutput']]; if (!isset($group[$dataid])) { $group[$dataid] = array(); } $group[$dataid][] = $result[$count]; } $count++; } } if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
/** * Get Users data * * @param array $options * @param array $options['nodeids'] filter by Node IDs * @param array $options['usrgrpids'] filter by UserGroup IDs * @param array $options['userids'] filter by User IDs * @param boolean $options['type'] filter by User type [ USER_TYPE_ZABBIX_USER: 1, USER_TYPE_ZABBIX_ADMIN: 2, USER_TYPE_SUPER_ADMIN: 3 ] * @param boolean $options['getAccess'] extend with access data for each User * @param boolean $options['count'] output only count of objects in result. ( result returned in property 'rowscount' ) * @param string $options['pattern'] filter by Host name containing only give pattern * @param int $options['limit'] output will be limited to given number * @param string $options['sortfield'] output will be sorted by given property [ 'userid', 'alias' ] * @param string $options['sortorder'] output will be sorted in given order [ 'ASC', 'DESC' ] * @return array */ public function get($options = array()) { $result = array(); $nodeCheck = false; $userType = self::$userData['type']; $sqlParts = array('select' => array('media' => 'm.mediaid'), 'from' => array('media' => 'media m'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'usrgrpids' => null, 'userids' => null, 'mediaids' => null, 'mediatypeids' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'editable' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null); $options = zbx_array_merge($defOptions, $options); // permission check if (USER_TYPE_SUPER_ADMIN == $userType) { } elseif (is_null($options['editable']) && self::$userData['type'] == USER_TYPE_ZABBIX_ADMIN) { $sqlParts['from']['users_groups'] = 'users_groups ug'; $sqlParts['where']['mug'] = 'm.userid=ug.userid'; $sqlParts['where'][] = 'ug.usrgrpid IN (' . ' SELECT uug.usrgrpid' . ' FROM users_groups uug' . ' WHERE uug.userid=' . self::$userData['userid'] . ' )'; } elseif (!is_null($options['editable']) || self::$userData['type'] != USER_TYPE_SUPER_ADMIN) { $options['userids'] = self::$userData['userid']; } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // mediaids if (!is_null($options['mediaids'])) { zbx_value2array($options['mediaids']); $sqlParts['where'][] = dbConditionInt('m.mediaid', $options['mediaids']); if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'm.mediaid', $nodeids); } } // userids if (!is_null($options['userids'])) { zbx_value2array($options['userids']); $sqlParts['select']['userid'] = 'u.userid'; $sqlParts['from']['users'] = 'users u'; $sqlParts['where'][] = dbConditionInt('u.userid', $options['userids']); $sqlParts['where']['mu'] = 'm.userid=u.userid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['userid'] = 'm.userid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'u.userid', $nodeids); } } // usrgrpids if (!is_null($options['usrgrpids'])) { zbx_value2array($options['usrgrpids']); $sqlParts['select']['usrgrpid'] = 'ug.usrgrpid'; $sqlParts['from']['users_groups'] = 'users_groups ug'; $sqlParts['where'][] = dbConditionInt('ug.usrgrpid', $options['usrgrpids']); $sqlParts['where']['mug'] = 'm.userid=ug.userid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['usrgrpid'] = 'ug.usrgrpid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'ug.usrgrpid', $nodeids); } } // mediatypeids if (!is_null($options['mediatypeids'])) { zbx_value2array($options['mediatypeids']); $sqlParts['select']['mediatypeid'] = 'm.mediatypeid'; $sqlParts['where'][] = dbConditionInt('m.mediatypeid', $options['mediatypeids']); if (!is_null($options['groupCount'])) { $sqlParts['group']['mediatypeid'] = 'm.mediatypeid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'm.mediatypeid', $nodeids); } } // should last, after all ****IDS checks if (!$nodeCheck) { $sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'm.mediaid', $nodeids); } // filter if (is_array($options['filter'])) { $this->dbFilter('media m', $options, $sqlParts); } // search if (is_array($options['search'])) { if ($options['search']['passwd']) { self::exception(ZBX_API_ERROR_PARAMETERS, _('It is not possible to search by user password.')); } zbx_db_search('media m', $options, $sqlParts); } // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sqlParts['limit'] = $options['limit']; } $mediaids = 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); $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']); while ($media = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $media; } else { $result = $media['rowscount']; } } else { $mediaids[$media['mediaid']] = $media['mediaid']; if (!isset($result[$media['mediaid']])) { $result[$media['mediaid']] = array(); } $result[$media['mediaid']] += $media; } } if (!is_null($options['countOutput'])) { return $result; } // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }