/** * Update triggers. * * @param array $triggers * * @return array */ public function update(array $triggers) { $triggers = zbx_toArray($triggers); $triggerIds = zbx_objectValues($triggers, 'triggerid'); $dbTriggers = $this->get(array('triggerids' => $triggerIds, 'editable' => true, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true)); $triggers = $this->extendObjects($this->tableName(), $triggers, array('description')); foreach ($triggers as $key => $trigger) { if (!isset($dbTriggers[$trigger['triggerid']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!')); } if (!isset($trigger['triggerid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Wrong fields for trigger.')); } // check for "templateid", because it is not allowed if (array_key_exists('templateid', $trigger)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot update "templateid" for trigger prototype "%1$s".', $trigger['description'])); } $dbTrigger = $dbTriggers[$trigger['triggerid']]; if (isset($trigger['expression'])) { $expressionFull = explode_exp($dbTrigger['expression']); if (strcmp($trigger['expression'], $expressionFull) == 0) { unset($triggers[$key]['expression']); } // check item prototypes $triggerExpression = new CTriggerExpression(); if (!$triggerExpression->parse($trigger['expression'])) { self::exception(ZBX_API_ERROR_PARAMETERS, $triggerExpression->error); } $items = getExpressionItems($triggerExpression); $hasPrototype = false; foreach ($items as $item) { if ($item['flags'] == ZBX_FLAG_DISCOVERY_PROTOTYPE) { $hasPrototype = true; break; } } if (!$hasPrototype) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Trigger prototype expression "%1$s" must contain at least one item prototype.', $trigger['expression'])); } } if (isset($trigger['description']) && strcmp($trigger['description'], $dbTrigger['comments']) == 0) { unset($triggers[$key]['description']); } if (isset($trigger['priority']) && $trigger['priority'] == $dbTrigger['priority']) { unset($triggers[$key]['priority']); } if (isset($trigger['type']) && $trigger['type'] == $dbTrigger['type']) { unset($triggers[$key]['type']); } if (isset($trigger['comments']) && strcmp($trigger['comments'], $dbTrigger['comments']) == 0) { unset($triggers[$key]['comments']); } if (isset($trigger['url']) && strcmp($trigger['url'], $dbTrigger['url']) == 0) { unset($triggers[$key]['url']); } if (isset($trigger['status']) && $trigger['status'] == $dbTrigger['status']) { unset($triggers[$key]['status']); } $this->checkIfExistsOnHost($trigger); } $this->updateReal($triggers); foreach ($triggers as $trigger) { $trigger['flags'] = ZBX_FLAG_DISCOVERY_PROTOTYPE; $this->inherit($trigger); } return array('triggerids' => $triggerIds); }
/** * 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); }