function check_right_on_trigger_by_expression($permission, $expression) { $expressionData = new CTriggerExpression(); if (!$expressionData->parse($expression)) { error($expressionData->error); return false; } $expressionHosts = $expressionData->getHosts(); $hosts = API::Host()->get(array('filter' => array('host' => $expressionHosts), 'editable' => $permission == PERM_READ_WRITE ? 1 : null, 'output' => array('hostid', 'host'), 'templated_hosts' => true, 'preservekeys' => true)); $hosts = zbx_toHash($hosts, 'host'); foreach ($expressionHosts as $host) { if (!isset($hosts[$host])) { error(_s('Incorrect trigger expression. Host "%1$s" does not exist or you have no access to this host.', $host)); return false; } } return true; }
/** * Returns true if the given expression contains templates. * * @param CTriggerExpression $exp * * @return bool */ protected function expressionHasTemplates(CTriggerExpression $expressionData) { $hosts = API::Host()->get(['output' => ['status'], 'filter' => ['name' => $expressionData->getHosts()], 'templated_hosts' => true]); foreach ($hosts as $host) { if ($host['status'] == HOST_STATUS_TEMPLATE) { return true; } } return false; }
/** * Checks that no trigger with the same description and expression as $trigger exist on the given host. * Assumes the given trigger is valid. * * @throws APIException if at least one trigger exists * * @param array $trigger a trigger with an exploded expression * @param null $hostid * * @return void */ protected function checkIfExistsOnHost(array $trigger, $hostId = null) { // skip the check if the description and expression haven't been changed if (!isset($trigger['description']) && !isset($trigger['expression'])) { return; } // make sure we have all the required data if (!isset($trigger['description']) || !isset($trigger['expression'])) { $explodeExpression = !isset($trigger['expression']); $trigger = $this->extendObject($this->tableName(), $trigger, array('description', 'expression')); if ($explodeExpression) { $trigger['expression'] = explode_exp($trigger['expression']); } } $filter = array('description' => $trigger['description']); if ($hostId) { $filter['hostid'] = $hostId; } else { $expressionData = new CTriggerExpression($trigger['expression']); $expressionData->parse($trigger['expression']); $expressionHosts = $expressionData->getHosts(); $filter['host'] = reset($expressionHosts); } $triggers = $this->get(array('filter' => $filter, 'output' => array('expression', 'triggerid'), 'nopermissions' => true)); foreach ($triggers as $dbTrigger) { $tmpExp = explode_exp($dbTrigger['expression']); // check if the expressions are also equal and that this is a different trigger $differentTrigger = !isset($trigger['triggerid']) || !idcmp($trigger['triggerid'], $dbTrigger['triggerid']); if (strcmp($tmpExp, $trigger['expression']) == 0 && $differentTrigger) { $options = array('output' => array('name'), 'templated_hosts' => true, 'nopermissions' => true, 'limit' => 1); if (isset($filter['host'])) { $options['filter'] = array('host' => $filter['host']); } else { $options['hostids'] = $hostId; } $host = API::Host()->get($options); $host = reset($host); self::exception(ZBX_API_ERROR_PARAMETERS, _s('Trigger "%1$s" already exists on "%2$s".', $trigger['description'], $host['name'])); } } }
/** * Checks if trigger prototype is in valid state. Checks trigger expression. * * @param array $triggerPrototype * * @return void */ protected function validateTriggerPrototypeExpression(array $triggerPrototype) { $triggerExpression = new CTriggerExpression(); if (!$triggerExpression->parse($triggerPrototype['expression'])) { self::exception(ZBX_API_ERROR_PARAMETERS, $triggerExpression->error); } $expressionHostnames = $triggerExpression->getHosts(); $this->checkTemplatesAndHostsTogether($expressionHostnames); $triggerExpressionItems = getExpressionItems($triggerExpression); $this->checkDiscoveryRuleCount($triggerPrototype, $triggerExpressionItems); }