本文整理汇总了PHP中dbConditionString函数的典型用法代码示例。如果您正苦于以下问题:PHP dbConditionString函数的具体用法?PHP dbConditionString怎么用?PHP dbConditionString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dbConditionString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: updateMessageSettings
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;
}
示例2: import
/**
* 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);
}
}
示例3: delete
/**
* 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);
}
示例4: dbFilter
/**
* 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;
}
示例5: get
//.........这里部分代码省略.........
// 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;
}
示例6: get
//.........这里部分代码省略.........
$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']);
示例7: delete
/**
* 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];
}
示例8: get
//.........这里部分代码省略.........
}
// 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 = '';
示例9: checkExistingItems
/**
* 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']));
}
}
}
示例10: delete
/**
* 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);
}
示例11: resolveGraphsFunctionalItemMacros
/**
* 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.
//.........这里部分代码省略.........
示例12: deleteActionConditions
/**
* 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));
}
}
示例13: delete
/**
* 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]);
}
}
}
示例14: checkExistingHostPrototypes
/**
* 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']));
}
}
}
示例15: get
/**
* 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'] = 'opmessage_usr omu';
$sqlParts['from']['operations_users'] = 'operations ou';
$sqlParts['where'][] = dbConditionInt('omu.userid', $options['userids']);
//.........这里部分代码省略.........