update alert message ignored api

This commit is contained in:
liugq 2022-06-10 15:12:04 +08:00
parent e07e40774b
commit e6e5c9f502
4 changed files with 13 additions and 2 deletions

View File

@ -61,10 +61,13 @@ type AlertMessage struct {
Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated: { type: date }"` Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated: { type: date }"`
RuleID string `json:"rule_id" elastic_mapping:"rule_id: { type: keyword,copy_to:search_text }"` RuleID string `json:"rule_id" elastic_mapping:"rule_id: { type: keyword,copy_to:search_text }"`
ResourceID string `json:"resource_id" elastic_mapping:"resource_id: { type: keyword,copy_to:search_text }"` ResourceID string `json:"resource_id" elastic_mapping:"resource_id: { type: keyword,copy_to:search_text }"`
ResourceName string `json:"resource_name" elastic_mapping:"resource_name: { type: keyword,copy_to:search_text }"`
Title string `json:"title" elastic_mapping:"title: { type: keyword,copy_to:search_text }"` Title string `json:"title" elastic_mapping:"title: { type: keyword,copy_to:search_text }"`
Message string `json:"message" elastic_mapping:"content: { type: keyword,copy_to:search_text }"` Message string `json:"message" elastic_mapping:"content: { type: keyword,copy_to:search_text }"`
Status string `json:"status" elastic_mapping:"status: { type: keyword,copy_to:search_text }"` Status string `json:"status" elastic_mapping:"status: { type: keyword,copy_to:search_text }"`
IgnoredTime time.Time `json:"ignored_time,omitempty" elastic_mapping:"ignored_time: { type: date }"` IgnoredTime time.Time `json:"ignored_time,omitempty" elastic_mapping:"ignored_time: { type: date }"`
IgnoredReason string `json:"ignored_reason,omitempty" elastic_mapping:"ignored_reason: { type: keyword,copy_to:search_text }"`
IgnoredUser string `json:"ignored_user,omitempty" elastic_mapping:"ignored_user: { type: keyword,copy_to:search_text }"`
Severity string `json:"severity" elastic_mapping:"severity: { type: keyword }"` Severity string `json:"severity" elastic_mapping:"severity: { type: keyword }"`
SearchText string `json:"-" elastic_mapping:"search_text:{type:text,index_prefixes:{},index_phrases:true, analyzer:suggest_text_search }"` SearchText string `json:"-" elastic_mapping:"search_text:{type:text,index_prefixes:{},index_phrases:true, analyzer:suggest_text_search }"`
} }

View File

@ -23,6 +23,7 @@ import (
func (h *AlertAPI) ignoreAlertMessage(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { func (h *AlertAPI) ignoreAlertMessage(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
body := struct { body := struct {
Messages []alerting.AlertMessage `json:"messages"` Messages []alerting.AlertMessage `json:"messages"`
IgnoredReason string `json:"ignored_reason"`
}{} }{}
err := h.DecodeJSON(req, &body) err := h.DecodeJSON(req, &body)
if err != nil { if err != nil {
@ -38,6 +39,7 @@ func (h *AlertAPI) ignoreAlertMessage(w http.ResponseWriter, req *http.Request,
for _, msg := range body.Messages { for _, msg := range body.Messages {
messageIDs = append(messageIDs, msg.ID) messageIDs = append(messageIDs, msg.ID)
} }
currentUser := h.GetCurrentUser(req)
queryDsl := util.MapStr{ queryDsl := util.MapStr{
"query": util.MapStr{ "query": util.MapStr{
"bool": util.MapStr{ "bool": util.MapStr{
@ -58,7 +60,7 @@ func (h *AlertAPI) ignoreAlertMessage(w http.ResponseWriter, req *http.Request,
}, },
}, },
"script": util.MapStr{ "script": util.MapStr{
"source": fmt.Sprintf("ctx._source['status'] = '%s';ctx._source['ignored_time']='%s'", alerting.MessageStateIgnored, time.Now().Format(time.RFC3339Nano)), "source": fmt.Sprintf("ctx._source['status'] = '%s';ctx._source['ignored_time']='%s';ctx._source['ignored_reason']='%s';ctx._source['ignored_user']='%s'", alerting.MessageStateIgnored, time.Now().Format(time.RFC3339Nano), body.IgnoredReason, currentUser),
}, },
} }
err = orm.UpdateBy(alerting.AlertMessage{}, util.MustToJSONBytes(queryDsl)) err = orm.UpdateBy(alerting.AlertMessage{}, util.MustToJSONBytes(queryDsl))

View File

@ -304,6 +304,7 @@ func (alertAPI *AlertAPI) updateRule(w http.ResponseWriter, req *http.Request, p
} }
//update task //update task
task.StopTask(id) task.StopTask(id)
clearKV(rule.ID)
eng := alerting2.GetEngine(rule.Resource.Type) eng := alerting2.GetEngine(rule.Resource.Type)
ruleTask := task.ScheduleTask{ ruleTask := task.ScheduleTask{
ID: rule.ID, ID: rule.ID,
@ -316,7 +317,6 @@ func (alertAPI *AlertAPI) updateRule(w http.ResponseWriter, req *http.Request, p
}else{ }else{
task.DeleteTask(id) task.DeleteTask(id)
} }
clearKV(rule.ID)
alertAPI.WriteJSON(w, util.MapStr{ alertAPI.WriteJSON(w, util.MapStr{
"_id": rule.ID, "_id": rule.ID,
@ -327,6 +327,7 @@ func (alertAPI *AlertAPI) updateRule(w http.ResponseWriter, req *http.Request, p
func clearKV(ruleID string){ func clearKV(ruleID string){
_ = kv.DeleteKey(alerting2.KVLastNotificationTime, []byte(ruleID)) _ = kv.DeleteKey(alerting2.KVLastNotificationTime, []byte(ruleID))
_ = kv.DeleteKey(alerting2.KVLastEscalationTime, []byte(ruleID)) _ = kv.DeleteKey(alerting2.KVLastEscalationTime, []byte(ruleID))
_ = kv.DeleteKey(alerting2.KVLastMessageState,[]byte(ruleID))
} }
func (alertAPI *AlertAPI) deleteRule(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { func (alertAPI *AlertAPI) deleteRule(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
@ -586,10 +587,13 @@ func (alertAPI *AlertAPI) enableRule(w http.ResponseWriter, req *http.Request, p
Description: obj.Metrics.Expression, Description: obj.Metrics.Expression,
Task: eng.GenerateTask(obj), Task: eng.GenerateTask(obj),
} }
task.DeleteTask(ruleTask.ID)
clearKV(ruleTask.ID)
task.RegisterScheduleTask(ruleTask) task.RegisterScheduleTask(ruleTask)
task.StartTask(ruleTask.ID) task.StartTask(ruleTask.ID)
}else{ }else{
task.DeleteTask(id) task.DeleteTask(id)
clearKV(id)
} }
obj.Enabled = reqObj.Enabled obj.Enabled = reqObj.Enabled
err = orm.Save(obj) err = orm.Save(obj)

View File

@ -688,12 +688,14 @@ func (engine *Engine) Do(rule *alerting.Rule) error {
alertItem.Message = paramsCtx[alerting2.ParamMessage].(string) alertItem.Message = paramsCtx[alerting2.ParamMessage].(string)
alertItem.Title = paramsCtx[alerting2.ParamTitle].(string) alertItem.Title = paramsCtx[alerting2.ParamTitle].(string)
if alertMessage == nil || alertMessage.Status == alerting.MessageStateRecovered { if alertMessage == nil || alertMessage.Status == alerting.MessageStateRecovered {
fmt.Println(rule.ID, alertMessage)
msg := &alerting.AlertMessage{ msg := &alerting.AlertMessage{
RuleID: rule.ID, RuleID: rule.ID,
Created: time.Now(), Created: time.Now(),
Updated: time.Now(), Updated: time.Now(),
ID: util.GetUUID(), ID: util.GetUUID(),
ResourceID: rule.Resource.ID, ResourceID: rule.Resource.ID,
ResourceName: rule.Resource.Name,
Status: alerting.MessageStateAlerting, Status: alerting.MessageStateAlerting,
Severity: severity, Severity: severity,
Title: alertItem.Title, Title: alertItem.Title,