コード例 #1
0
ファイル: triggers.inc.php プロジェクト: rennhak/zabbix
function update_trigger($triggerid, $expression = NULL, $description = NULL, $type = NULL, $priority = NULL, $status = NULL, $comments = NULL, $url = NULL, $deps = array(), $templateid = 0)
{
    $trigger = get_trigger_by_triggerid($triggerid);
    $trig_hosts = get_hosts_by_triggerid($triggerid);
    $trig_host = DBfetch($trig_hosts);
    $event_to_unknown = false;
    if (is_null($expression)) {
        /* Restore expression */
        $expression = explode_exp($trigger["expression"], 0);
    } else {
        if ($expression != explode_exp($trigger["expression"], 0)) {
            $event_to_unknown = true;
        }
    }
    if (!validate_expression($expression)) {
        return false;
    }
    $exp_hosts = get_hosts_by_expression($expression);
    if ($exp_hosts) {
        $chd_hosts = get_hosts_by_templateid($trig_host["hostid"]);
        if (DBfetch($chd_hosts)) {
            $exp_host = DBfetch($exp_hosts);
            $db_chd_triggers = get_triggers_by_templateid($triggerid);
            while ($db_chd_trigger = DBfetch($db_chd_triggers)) {
                $chd_trig_hosts = get_hosts_by_triggerid($db_chd_trigger["triggerid"]);
                $chd_trig_host = DBfetch($chd_trig_hosts);
                $newexpression = str_replace("{" . $exp_host["host"] . ":", "{" . $chd_trig_host["host"] . ":", $expression);
                // recursion
                update_trigger($db_chd_trigger["triggerid"], $newexpression, $description, $type, $priority, NULL, $comments, $url, replace_template_dependencies($deps, $chd_trig_host['hostid']), $triggerid);
            }
        }
    }
    $result = delete_function_by_triggerid($triggerid);
    if (!$result) {
        return $result;
    }
    $expression = implode_exp($expression, $triggerid);
    /* errors can be ignored cose function must return NULL */
    if ($event_to_unknown) {
        add_event($triggerid, TRIGGER_VALUE_UNKNOWN);
    }
    reset_items_nextcheck($triggerid);
    $sql = "UPDATE triggers SET";
    if (!is_null($expression)) {
        $sql .= ' expression=' . zbx_dbstr($expression) . ',';
    }
    if (!is_null($description)) {
        $sql .= ' description=' . zbx_dbstr($description) . ',';
    }
    if (!is_null($type)) {
        $sql .= ' type=' . $type . ',';
    }
    if (!is_null($priority)) {
        $sql .= ' priority=' . $priority . ',';
    }
    if (!is_null($status)) {
        $sql .= ' status=' . $status . ',';
    }
    if (!is_null($comments)) {
        $sql .= ' comments=' . zbx_dbstr($comments) . ',';
    }
    if (!is_null($url)) {
        $sql .= ' url=' . zbx_dbstr($url) . ',';
    }
    if (!is_null($templateid)) {
        $sql .= ' templateid=' . $templateid . ',';
    }
    $sql .= ' value=2 WHERE triggerid=' . $triggerid;
    $result = DBexecute($sql);
    delete_dependencies_by_triggerid($triggerid);
    foreach ($deps as $id => $triggerid_up) {
        if (!($result2 = add_trigger_dependency($triggerid, $triggerid_up))) {
            error(S_INCORRECT_DEPENDENCY . ' [' . expand_trigger_description($triggerid_up) . ']');
        }
        $result &= $result2;
    }
    if ($result) {
        $trig_hosts = get_hosts_by_triggerid($triggerid);
        $msg = "Trigger '" . $trigger["description"] . "' updated";
        $trig_host = DBfetch($trig_hosts);
        if ($trig_host) {
            $msg .= " for host '" . $trig_host["host"] . "'";
        }
        info($msg);
    }
    if ($result) {
        $trigger_new = get_trigger_by_triggerid($triggerid);
        add_audit_ext(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_TRIGGER, $triggerid, $trigger["description"], 'triggers', $trigger, $trigger_new);
    }
    return $result;
}
コード例 #2
0
function update_trigger($triggerid, $expression = NULL, $description = NULL, $type = NULL, $priority = NULL, $status = NULL, $comments = NULL, $url = NULL, $deps = array(), $templateid = 0)
{
    $trigger = get_trigger_by_triggerid($triggerid);
    $trig_hosts = get_hosts_by_triggerid($triggerid);
    $trig_host = DBfetch($trig_hosts);
    $event_to_unknown = false;
    // Restore expression
    if (is_null($expression)) {
        $expression = explode_exp($trigger['expression'], 0);
        $expr = new CTriggerExpression(array('expression' => $expression));
    } else {
        $expr = new CTriggerExpression(array('expression' => $expression));
        $event_to_unknown = empty($expr->errors) && $expression != explode_exp($trigger['expression'], 0);
    }
    if (!empty($expr->errors)) {
        foreach ($expr->errors as $error) {
            error($error);
        }
        return false;
    }
    if (!is_null($deps) && !validate_trigger_dependency($expression, $deps)) {
        error(S_WRONG_DEPENDENCY_ERROR);
        return false;
    }
    if (is_null($description)) {
        $description = $trigger['description'];
    }
    if (CTrigger::exists(array('description' => $description, 'expression' => $expression))) {
        $host = reset($expr->data['hosts']);
        $options = array('filter' => array('description' => $description, 'host' => $host), 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
        $triggers_exist = CTrigger::get($options);
        $trigger_exist = false;
        foreach ($triggers_exist as $tnum => $tr) {
            $tmp_exp = explode_exp($tr['expression'], false);
            if (strcmp($tmp_exp, $expression) == 0) {
                $trigger_exist = $tr;
                break;
            }
        }
        if ($trigger_exist && $trigger_exist['triggerid'] != $trigger['triggerid']) {
            error('Trigger with name "' . $trigger['description'] . '" and expression "' . $expression . '" already exists.');
            return false;
        } else {
            if (!$trigger_exist) {
                error('No Permissions');
                return false;
            }
        }
    }
    $exp_hosts = $expr->data['hosts'];
    if (!empty($exp_hosts)) {
        $chd_hosts = get_hosts_by_templateid($trig_host['hostid']);
        if (DBfetch($chd_hosts)) {
            $expHostName = reset($exp_hosts);
            $db_chd_triggers = get_triggers_by_templateid($triggerid);
            while ($db_chd_trigger = DBfetch($db_chd_triggers)) {
                $chd_trig_hosts = get_hosts_by_triggerid($db_chd_trigger['triggerid']);
                $chd_trig_host = DBfetch($chd_trig_hosts);
                $newexpression = str_replace('{' . $expHostName . ':', '{' . $chd_trig_host['host'] . ':', $expression);
                // recursion
                update_trigger($db_chd_trigger['triggerid'], $newexpression, $description, $type, $priority, $status, $comments, $url, is_null($deps) ? null : replace_template_dependencies($deps, $chd_trig_host['hostid']), $triggerid);
            }
        }
    }
    $result = delete_function_by_triggerid($triggerid);
    if (!$result) {
        return $result;
    }
    $expression = implode_exp($expression, $triggerid);
    if (is_null($expression)) {
        return false;
    }
    $update_values = array();
    if (!is_null($expression)) {
        $update_values['expression'] = $expression;
    }
    if (!is_null($description)) {
        $update_values['description'] = $description;
    }
    if (!is_null($type)) {
        $update_values['type'] = $type;
    }
    if (!is_null($priority)) {
        $update_values['priority'] = $priority;
    }
    if (!is_null($status)) {
        $update_values['status'] = $status;
    }
    if (!is_null($comments)) {
        $update_values['comments'] = $comments;
    }
    if (!is_null($url)) {
        $update_values['url'] = $url;
    }
    if (!is_null($templateid)) {
        $update_values['templateid'] = $templateid;
    }
    if ($event_to_unknown || !is_null($status) && $status != TRIGGER_STATUS_ENABLED) {
        if ($trigger['value'] != TRIGGER_VALUE_UNKNOWN) {
            addEvent($triggerid, TRIGGER_VALUE_UNKNOWN);
            $update_values['value'] = TRIGGER_VALUE_UNKNOWN;
            $update_values['lastchange'] = time();
        }
    }
    DB::update('triggers', array('values' => $update_values, 'where' => array('triggerid=' . $triggerid)));
    if (!is_null($deps)) {
        delete_dependencies_by_triggerid($triggerid);
        foreach ($deps as $id => $triggerid_up) {
            if (!($result2 = add_trigger_dependency($triggerid, $triggerid_up))) {
                error(S_INCORRECT_DEPENDENCY . ' [' . expand_trigger_description($triggerid_up) . ']');
            }
            $result &= $result2;
        }
    }
    if ($result) {
        $trig_hosts = get_hosts_by_triggerid($triggerid);
        $msg = S_TRIGGER . SPACE . '"' . $trigger['description'] . '"' . SPACE . S_UPDATED_SMALL;
        $trig_host = DBfetch($trig_hosts);
        if ($trig_host) {
            $msg .= SPACE . S_FOR_HOST_SMALL . SPACE . '"' . $trig_host['host'] . '"';
        }
        info($msg);
    }
    if ($result) {
        $trigger_new = get_trigger_by_triggerid($triggerid);
        add_audit_ext(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_TRIGGER, $triggerid, $trig_host['host'] . ':' . $trigger['description'], 'triggers', $trigger, $trigger_new);
    }
    $result = $result ? $triggerid : $result;
    return $result;
}