Example #1
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;
    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;
}
function copy_triggers($srcid, $destid)
{
    try {
        $options = array('hostids' => $srcid, 'output' => API_OUTPUT_EXTEND, 'templated_hosts' => 1);
        $src = CHost::get($options);
        if (empty($src)) {
            throw new Exception();
        }
        $src = reset($src);
        $options = array('hostids' => $destid, 'output' => API_OUTPUT_EXTEND, 'templated_hosts' => 1);
        $dest = CHost::get($options);
        if (empty($dest)) {
            throw new Exception();
        }
        $dest = reset($dest);
        $options = array('hostids' => $srcid, 'output' => API_OUTPUT_EXTEND, 'inherited' => 0, 'select_dependencies' => API_OUTPUT_EXTEND);
        $triggers = CTrigger::get($options);
        $hash = array();
        foreach ($triggers as $trigger) {
            $expr = explode_exp($trigger['expression'], 0);
            $expr = str_replace($src['host'] . ':', $dest['host'] . ':', $expr);
            $trigger['expression'] = $expr;
            $result = CTrigger::create($trigger);
            if (!$result) {
                throw new Exception();
            }
            $hash[$trigger['triggerid']] = reset($result['triggerids']);
        }
        foreach ($triggers as $trigger) {
            foreach ($trigger['dependencies'] as $dep) {
                if (isset($hash[$dep['triggerid']])) {
                    $dep = $hash[$dep['triggerid']];
                } else {
                    $dep = $dep['triggerid'];
                }
                $res = add_trigger_dependency($hash[$trigger['triggerid']], $dep);
                if (!$res) {
                    throw new Exception();
                }
            }
        }
        return true;
    } catch (Exception $e) {
        return false;
    }
}