alert_cur_event.go 11.1 KB
Newer Older
U
UlricQin 已提交
1 2 3
package models

import (
4
	"bytes"
U
Ulric Qin 已提交
5
	"fmt"
6
	"html/template"
U
UlricQin 已提交
7 8
	"strconv"
	"strings"
9 10

	"github.com/didi/nightingale/v5/src/pkg/tplx"
U
UlricQin 已提交
11 12 13 14 15
)

type AlertCurEvent struct {
	Id                 int64             `json:"id" gorm:"primaryKey"`
	Cluster            string            `json:"cluster"`
U
Ulric Qin 已提交
16 17 18
	GroupId            int64             `json:"group_id"`   // busi group id
	GroupName          string            `json:"group_name"` // busi group name
	Hash               string            `json:"hash"`       // rule_id + vector_key
U
UlricQin 已提交
19 20 21
	RuleId             int64             `json:"rule_id"`
	RuleName           string            `json:"rule_name"`
	RuleNote           string            `json:"rule_note"`
22 23
	RuleProd           string            `json:"rule_prod"`
	RuleAlgo           string            `json:"rule_algo"`
U
UlricQin 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	Severity           int               `json:"severity"`
	PromForDuration    int               `json:"prom_for_duration"`
	PromQl             string            `json:"prom_ql"`
	PromEvalInterval   int               `json:"prom_eval_interval"`
	Callbacks          string            `json:"-"`                  // for db
	CallbacksJSON      []string          `json:"callbacks" gorm:"-"` // for fe
	RunbookUrl         string            `json:"runbook_url"`
	NotifyRecovered    int               `json:"notify_recovered"`
	NotifyChannels     string            `json:"-"`                          // for db
	NotifyChannelsJSON []string          `json:"notify_channels" gorm:"-"`   // for fe
	NotifyGroups       string            `json:"-"`                          // for db
	NotifyGroupsJSON   []string          `json:"notify_groups" gorm:"-"`     // for fe
	NotifyGroupsObj    []*UserGroup      `json:"notify_groups_obj" gorm:"-"` // for fe
	TargetIdent        string            `json:"target_ident"`
	TargetNote         string            `json:"target_note"`
	TriggerTime        int64             `json:"trigger_time"`
	TriggerValue       string            `json:"trigger_value"`
X
xtan 已提交
41 42 43 44 45 46 47 48
	Tags               string            `json:"-"`                         // for db
	TagsJSON           []string          `json:"tags" gorm:"-"`             // for fe
	TagsMap            map[string]string `json:"-" gorm:"-"`                // for internal usage
	IsRecovered        bool              `json:"is_recovered" gorm:"-"`     // for notify.py
	NotifyUsersObj     []*User           `json:"notify_users_obj" gorm:"-"` // for notify.py
	LastEvalTime       int64             `json:"last_eval_time" gorm:"-"`   // for notify.py 上次计算的时间
	LastSentTime       int64             `json:"last_sent_time" gorm:"-"`   // 上次发送时间
	NotifyCurNumber    int               `json:"notify_cur_number"`         // notify: current number
U
UlricQin 已提交
49 50 51 52 53 54 55 56 57 58
}

func (e *AlertCurEvent) TableName() string {
	return "alert_cur_event"
}

func (e *AlertCurEvent) Add() error {
	return Insert(e)
}

U
Ulric Qin 已提交
59 60 61 62 63
type AggrRule struct {
	Type  string
	Value string
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
func (e *AlertCurEvent) ParseRuleNote() error {
	e.RuleNote = strings.TrimSpace(e.RuleNote)

	if e.RuleNote == "" {
		return nil
	}

	var defs = []string{
		"{{$labels := .TagsMap}}",
		"{{$value := .TriggerValue}}",
	}

	text := strings.Join(append(defs, e.RuleNote), "")
	t, err := template.New(fmt.Sprint(e.RuleId)).Funcs(tplx.TemplateFuncMap).Parse(text)
	if err != nil {
		return err
	}

	var body bytes.Buffer
	err = t.Execute(&body, e)
	if err != nil {
		return err
	}

	e.RuleNote = body.String()
	return nil
}

U
Ulric Qin 已提交
92 93 94 95 96 97 98 99 100 101 102 103
func (e *AlertCurEvent) GenCardTitle(rules []*AggrRule) string {
	arr := make([]string, len(rules))
	for i := 0; i < len(rules); i++ {
		rule := rules[i]

		if rule.Type == "field" {
			arr[i] = e.GetField(rule.Value)
		}

		if rule.Type == "tagkey" {
			arr[i] = e.GetTagValue(rule.Value)
		}
U
Ulric Qin 已提交
104 105 106 107

		if len(arr[i]) == 0 {
			arr[i] = "Null"
		}
U
Ulric Qin 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	}
	return strings.Join(arr, "::")
}

func (e *AlertCurEvent) GetTagValue(tagkey string) string {
	for _, tag := range e.TagsJSON {
		i := strings.Index(tag, tagkey+"=")
		if i >= 0 {
			return tag[len(tagkey+"="):]
		}
	}
	return ""
}

func (e *AlertCurEvent) GetField(field string) string {
	switch field {
	case "cluster":
		return e.Cluster
	case "group_id":
		return fmt.Sprint(e.GroupId)
	case "group_name":
		return e.GroupName
	case "rule_id":
		return fmt.Sprint(e.RuleId)
	case "rule_name":
		return e.RuleName
	case "severity":
U
Ulric Qin 已提交
135
		return fmt.Sprint(e.Severity)
U
Ulric Qin 已提交
136 137 138 139 140 141 142 143 144 145 146
	case "runbook_url":
		return e.RunbookUrl
	case "target_ident":
		return e.TargetIdent
	case "target_note":
		return e.TargetNote
	default:
		return ""
	}
}

U
UlricQin 已提交
147 148
func (e *AlertCurEvent) ToHis() *AlertHisEvent {
	isRecovered := 0
U
Ulric Qin 已提交
149
	var recoverTime int64 = 0
U
UlricQin 已提交
150 151
	if e.IsRecovered {
		isRecovered = 1
U
Ulric Qin 已提交
152
		recoverTime = e.LastEvalTime
U
UlricQin 已提交
153 154 155 156 157 158
	}

	return &AlertHisEvent{
		IsRecovered:      isRecovered,
		Cluster:          e.Cluster,
		GroupId:          e.GroupId,
U
Ulric Qin 已提交
159
		GroupName:        e.GroupName,
U
UlricQin 已提交
160 161 162
		Hash:             e.Hash,
		RuleId:           e.RuleId,
		RuleName:         e.RuleName,
163 164
		RuleProd:         e.RuleProd,
		RuleAlgo:         e.RuleAlgo,
U
UlricQin 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
		RuleNote:         e.RuleNote,
		Severity:         e.Severity,
		PromForDuration:  e.PromForDuration,
		PromQl:           e.PromQl,
		PromEvalInterval: e.PromEvalInterval,
		Callbacks:        e.Callbacks,
		RunbookUrl:       e.RunbookUrl,
		NotifyRecovered:  e.NotifyRecovered,
		NotifyChannels:   e.NotifyChannels,
		NotifyGroups:     e.NotifyGroups,
		TargetIdent:      e.TargetIdent,
		TargetNote:       e.TargetNote,
		TriggerTime:      e.TriggerTime,
		TriggerValue:     e.TriggerValue,
		Tags:             e.Tags,
U
Ulric Qin 已提交
180
		RecoverTime:      recoverTime,
181
		LastEvalTime:     e.LastEvalTime,
X
xtan 已提交
182
		NotifyCurNumber:  e.NotifyCurNumber,
U
UlricQin 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	}
}

func (e *AlertCurEvent) DB2FE() {
	e.NotifyChannelsJSON = strings.Fields(e.NotifyChannels)
	e.NotifyGroupsJSON = strings.Fields(e.NotifyGroups)
	e.CallbacksJSON = strings.Fields(e.Callbacks)
	e.TagsJSON = strings.Split(e.Tags, ",,")
}

func (e *AlertCurEvent) DB2Mem() {
	e.IsRecovered = false
	e.NotifyGroupsJSON = strings.Fields(e.NotifyGroups)
	e.CallbacksJSON = strings.Fields(e.Callbacks)
	e.NotifyChannelsJSON = strings.Fields(e.NotifyChannels)
	e.TagsJSON = strings.Split(e.Tags, ",,")
	e.TagsMap = make(map[string]string)
	for i := 0; i < len(e.TagsJSON); i++ {
		pair := strings.TrimSpace(e.TagsJSON[i])
		if pair == "" {
			continue
		}

		arr := strings.Split(pair, "=")
		if len(arr) != 2 {
			continue
		}

		e.TagsMap[arr[0]] = arr[1]
	}
}

// for webui
func (e *AlertCurEvent) FillNotifyGroups(cache map[int64]*UserGroup) error {
	// some user-group already deleted ?
	count := len(e.NotifyGroupsJSON)
	if count == 0 {
		e.NotifyGroupsObj = []*UserGroup{}
		return nil
	}

	for i := range e.NotifyGroupsJSON {
		id, err := strconv.ParseInt(e.NotifyGroupsJSON[i], 10, 64)
		if err != nil {
			continue
		}

		ug, has := cache[id]
		if has {
			e.NotifyGroupsObj = append(e.NotifyGroupsObj, ug)
			continue
		}

		ug, err = UserGroupGetById(id)
		if err != nil {
			return err
		}

		if ug != nil {
			e.NotifyGroupsObj = append(e.NotifyGroupsObj, ug)
			cache[id] = ug
		}
	}

	return nil
}

Y
Yening Qin 已提交
250 251
func AlertCurEventTotal(prod string, bgid, stime, etime int64, severity int, clusters []string, query string) (int64, error) {
	session := DB().Model(&AlertCurEvent{}).Where("trigger_time between ? and ? and rule_prod = ?", stime, etime, prod)
U
Ulric Qin 已提交
252 253 254 255

	if bgid > 0 {
		session = session.Where("group_id = ?", bgid)
	}
U
UlricQin 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268

	if severity >= 0 {
		session = session.Where("severity = ?", severity)
	}

	if len(clusters) > 0 {
		session = session.Where("cluster in ?", clusters)
	}

	if query != "" {
		arr := strings.Fields(query)
		for i := 0; i < len(arr); i++ {
			qarg := "%" + arr[i] + "%"
U
bugfix  
UlricQin 已提交
269
			session = session.Where("rule_name like ? or tags like ?", qarg, qarg)
U
UlricQin 已提交
270 271 272 273 274 275
		}
	}

	return Count(session)
}

Y
Yening Qin 已提交
276 277
func AlertCurEventGets(prod string, bgid, stime, etime int64, severity int, clusters []string, query string, limit, offset int) ([]AlertCurEvent, error) {
	session := DB().Where("trigger_time between ? and ? and rule_prod = ?", stime, etime, prod)
U
Ulric Qin 已提交
278 279 280 281

	if bgid > 0 {
		session = session.Where("group_id = ?", bgid)
	}
U
UlricQin 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294

	if severity >= 0 {
		session = session.Where("severity = ?", severity)
	}

	if len(clusters) > 0 {
		session = session.Where("cluster in ?", clusters)
	}

	if query != "" {
		arr := strings.Fields(query)
		for i := 0; i < len(arr); i++ {
			qarg := "%" + arr[i] + "%"
U
bugfix  
UlricQin 已提交
295
			session = session.Where("rule_name like ? or tags like ?", qarg, qarg)
U
UlricQin 已提交
296 297 298 299
		}
	}

	var lst []AlertCurEvent
300
	err := session.Order("id desc").Limit(limit).Offset(offset).Find(&lst).Error
U
UlricQin 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

	if err == nil {
		for i := 0; i < len(lst); i++ {
			lst[i].DB2FE()
		}
	}

	return lst, err
}

func AlertCurEventDel(ids []int64) error {
	if len(ids) == 0 {
		return nil
	}

	return DB().Where("id in ?", ids).Delete(&AlertCurEvent{}).Error
}

func AlertCurEventDelByHash(hash string) error {
	return DB().Where("hash = ?", hash).Delete(&AlertCurEvent{}).Error
}

func AlertCurEventExists(where string, args ...interface{}) (bool, error) {
	return Exists(DB().Model(&AlertCurEvent{}).Where(where, args...))
}

func AlertCurEventGet(where string, args ...interface{}) (*AlertCurEvent, error) {
	var lst []*AlertCurEvent
	err := DB().Where(where, args...).Find(&lst).Error
	if err != nil {
		return nil, err
	}

	if len(lst) == 0 {
		return nil, nil
	}

	lst[0].DB2FE()
	lst[0].FillNotifyGroups(make(map[int64]*UserGroup))

	return lst[0], nil
}

func AlertCurEventGetById(id int64) (*AlertCurEvent, error) {
	return AlertCurEventGet("id=?", id)
}

type AlertNumber struct {
	GroupId    int64
	GroupCount int64
}

// for busi_group list page
func AlertNumbers(bgids []int64) (map[int64]int64, error) {
	ret := make(map[int64]int64)
	if len(bgids) == 0 {
		return ret, nil
	}

	var arr []AlertNumber
	err := DB().Model(&AlertCurEvent{}).Select("group_id", "count(*) as group_count").Where("group_id in ?", bgids).Group("group_id").Find(&arr).Error
	if err != nil {
		return nil, err
	}

	for i := 0; i < len(arr); i++ {
		ret[arr[i].GroupId] = arr[i].GroupCount
	}

	return ret, nil
}

func AlertCurEventGetAll(cluster string) ([]*AlertCurEvent, error) {
	session := DB().Model(&AlertCurEvent{})

	if cluster != "" {
		session = session.Where("cluster = ?", cluster)
	}

	var lst []*AlertCurEvent
	err := session.Find(&lst).Error
	return lst, err
}

U
Ulric Qin 已提交
385 386 387 388 389 390 391 392
func AlertCurEventGetByIds(ids []int64) ([]*AlertCurEvent, error) {
	var lst []*AlertCurEvent

	if len(ids) == 0 {
		return lst, nil
	}

	err := DB().Where("id in ?", ids).Order("id desc").Find(&lst).Error
U
Ulric Qin 已提交
393 394 395 396 397 398
	if err == nil {
		for i := 0; i < len(lst); i++ {
			lst[i].DB2FE()
		}
	}

U
Ulric Qin 已提交
399 400 401
	return lst, err
}

U
UlricQin 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
func AlertCurEventGetByRule(ruleId int64) ([]*AlertCurEvent, error) {
	var lst []*AlertCurEvent
	err := DB().Where("rule_id=?", ruleId).Find(&lst).Error
	return lst, err
}

func AlertCurEventGetMap(cluster string) (map[int64]map[string]struct{}, error) {
	session := DB().Model(&AlertCurEvent{})
	if cluster != "" {
		session = session.Where("cluster = ?", cluster)
	}

	var lst []*AlertCurEvent
	err := session.Select("rule_id", "hash").Find(&lst).Error
	if err != nil {
		return nil, err
	}

	ret := make(map[int64]map[string]struct{})
	for i := 0; i < len(lst); i++ {
		rid := lst[i].RuleId
		hash := lst[i].Hash
		if _, has := ret[rid]; has {
			ret[rid][hash] = struct{}{}
		} else {
			ret[rid] = make(map[string]struct{})
			ret[rid][hash] = struct{}{}
		}
	}

	return ret, nil
}