$triggerIdsToUpdate[] = $triggerid; } } $childTriggerIds = array_keys($db_triggers); } while (!empty($childTriggerIds)); DB::update('triggers', array('values' => array('status' => $status), 'where' => array('triggerid' => $triggerIdsToUpdate))); // if disable trigger, unknown event must be created if ($status == TRIGGER_STATUS_DISABLED) { $valueTriggerIds = array(); $db_triggers = DBselect('SELECT t.triggerid' . ' FROM triggers t,functions f,items i,hosts h' . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND ' . dbConditionInt('t.triggerid', $triggerIdsToUpdate) . ' AND t.value_flags=' . TRIGGER_VALUE_FLAG_NORMAL . ' AND h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')'); while ($row = DBfetch($db_triggers)) { $valueTriggerIds[] = $row['triggerid']; } if (!empty($valueTriggerIds)) { DB::update('triggers', array('values' => array('value_flags' => TRIGGER_VALUE_FLAG_UNKNOWN, 'error' => _('Trigger status became "Disabled".')), 'where' => array('triggerid' => $valueTriggerIds))); addUnknownEvent($valueTriggerIds); } } // get updated triggers with additional data $db_triggers = API::Trigger()->get(array('triggerids' => $triggerIdsToUpdate, 'output' => array('triggerid', 'description'), 'preservekeys' => true, 'selectHosts' => API_OUTPUT_EXTEND, 'nopermissions' => true)); foreach ($db_triggers as $triggerid => $trigger) { $host = reset($trigger['hosts']); add_audit_ext(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_TRIGGER, $triggerid, $host['host'] . ': ' . $trigger['description'], 'triggers', $statusOld, $statusNew); } } DBend(true); } catch (Exception $e) { DBend(false); $go_result = false; } show_messages($go_result, _('Status updated'), _('Cannot update status'));
function updateTriggerValueToUnknownByHostId($hostids) { zbx_value2array($hostids); $triggerids = array(); $result = DBselect('SELECT DISTINCT t.triggerid' . ' FROM hosts h,items i,functions f,triggers t' . ' WHERE h.hostid=i.hostid' . ' AND i.itemid=f.itemid' . ' AND f.triggerid=t.triggerid' . ' AND ' . dbConditionInt('h.hostid', $hostids) . ' AND h.status=' . HOST_STATUS_MONITORED . ' AND t.value_flags=' . TRIGGER_VALUE_FLAG_NORMAL); while ($row = DBfetch($result)) { $triggerids[] = $row['triggerid']; } if (!empty($triggerids)) { DB::update('triggers', array('values' => array('value_flags' => TRIGGER_VALUE_FLAG_UNKNOWN, 'error' => _s('Host status became "%s"', _('Not monitored'))), 'where' => array('triggerid' => $triggerids))); addUnknownEvent($triggerids); } return true; }
/** * @param $triggers */ protected function updateReal(array $triggers) { $triggers = zbx_toArray($triggers); $infos = array(); $dbTriggers = $this->get(array('triggerids' => zbx_objectValues($triggers, 'triggerid'), 'output' => API_OUTPUT_EXTEND, 'selectHosts' => array('name'), 'selectDependencies' => API_OUTPUT_REFER, 'preservekeys' => true, 'nopermissions' => true)); $descriptionChanged = $expressionChanged = false; foreach ($triggers as &$trigger) { $dbTrigger = $dbTriggers[$trigger['triggerid']]; $hosts = zbx_objectValues($dbTrigger['hosts'], 'name'); if (isset($trigger['description']) && strcmp($dbTrigger['description'], $trigger['description']) != 0) { $descriptionChanged = true; } else { $trigger['description'] = $dbTrigger['description']; } $expressionFull = explode_exp($dbTrigger['expression']); if (isset($trigger['expression']) && strcmp($expressionFull, $trigger['expression']) != 0) { $this->validateItems($trigger); $expressionChanged = true; $expressionFull = $trigger['expression']; $trigger['error'] = 'Trigger expression updated. No status update so far.'; } if ($expressionChanged) { // check the expression $expressionData = new CTriggerExpression(); if (!$expressionData->parse($expressionFull)) { self::exception(ZBX_API_ERROR_PARAMETERS, $expressionData->error); } // if the trigger contains templates, delete any events that may exist if ($this->expressionHasTemplates($expressionData)) { DB::delete('events', array('object' => EVENT_OBJECT_TRIGGER, 'objectid' => $trigger['triggerid'])); } DB::delete('functions', array('triggerid' => $trigger['triggerid'])); try { $trigger['expression'] = implode_exp($expressionFull, $trigger['triggerid'], $hosts); } catch (Exception $e) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot implode expression "%s".', $expressionFull) . ' ' . $e->getMessage()); } if (isset($trigger['status']) && $trigger['status'] != TRIGGER_STATUS_ENABLED) { if ($trigger['value_flags'] == TRIGGER_VALUE_FLAG_NORMAL) { addUnknownEvent($trigger['triggerid']); $trigger['value_flags'] = TRIGGER_VALUE_FLAG_UNKNOWN; } } // if the expression has changed, we must revalidate the existing dependencies if (!isset($trigger['dependencies'])) { $trigger['dependencies'] = zbx_objectValues($dbTrigger['dependencies'], 'triggerid'); } } $triggerUpdate = $trigger; if (!$descriptionChanged) { unset($triggerUpdate['description']); } if (!$expressionChanged) { unset($triggerUpdate['expression']); } DB::update('triggers', array('values' => $triggerUpdate, 'where' => array('triggerid' => $trigger['triggerid']))); // update service status if (isset($trigger['priority']) && $trigger['priority'] != $dbTrigger['priority']) { $serviceStatus = $dbTrigger['value'] == TRIGGER_VALUE_TRUE ? $trigger['priority'] : 0; updateServices($trigger['triggerid'], $serviceStatus); } // restore the full expression to properly validate dependencies $trigger['expression'] = $expressionChanged ? explode_exp($trigger['expression']) : $expressionFull; $infos[] = _s('Updated: Trigger "%1$s" on "%2$s".', $trigger['description'], implode(', ', $hosts)); add_audit_ext(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_TRIGGER, $dbTrigger['triggerid'], $dbTrigger['description'], null, $dbTrigger, $triggerUpdate); } unset($trigger); foreach ($infos as $info) { info($info); } }