diff --git a/controllers/api/alert_noticeObjcet.go b/controllers/api/alert_noticeObjcet.go index 651a0fc..962fd2d 100644 --- a/controllers/api/alert_noticeObjcet.go +++ b/controllers/api/alert_noticeObjcet.go @@ -2,8 +2,8 @@ package api import ( "github.com/gin-gonic/gin" - "prometheus-manager/controllers/dao" - "prometheus-manager/utils/feishu" + "watchAlert/controllers/dao" + "watchAlert/utils/feishu" ) type AlertNoticeObjectController struct{} diff --git a/controllers/api/alert_silence.go b/controllers/api/alert_silence.go index 2f6481d..51c09b2 100644 --- a/controllers/api/alert_silence.go +++ b/controllers/api/alert_silence.go @@ -6,21 +6,19 @@ import ( "io/ioutil" "log" "net/http" - "prometheus-manager/controllers/dto" + "watchAlert/controllers/dto" ) type AlertSilenceController struct{} func (asc *AlertSilenceController) CreateSilence(ctx *gin.Context) { - var challengeInfo map[string]interface{} + var cardInfo dto.CardInfo uuid := ctx.Query("uuid") - body := ctx.Request.Body - bodyByte, _ := ioutil.ReadAll(body) - _ = json.Unmarshal(bodyByte, &challengeInfo) + _ = ctx.ShouldBindJSON(&cardInfo) - err := alertSilenceService.CreateAlertSilence(uuid, challengeInfo) + err := alertSilenceService.CreateAlertSilence(uuid, cardInfo) if err != nil { ctx.JSON(200, gin.H{ "code": 2001, diff --git a/controllers/api/duty_manage.go b/controllers/api/duty_manage.go index 51490d5..43f67b3 100644 --- a/controllers/api/duty_manage.go +++ b/controllers/api/duty_manage.go @@ -2,7 +2,7 @@ package api import ( "github.com/gin-gonic/gin" - "prometheus-manager/controllers/dao" + "watchAlert/controllers/dao" ) type DutyManageController struct{} diff --git a/controllers/api/duty_people.go b/controllers/api/duty_people.go index 8115809..6dbf43d 100644 --- a/controllers/api/duty_people.go +++ b/controllers/api/duty_people.go @@ -2,7 +2,7 @@ package api import ( "github.com/gin-gonic/gin" - "prometheus-manager/controllers/dao" + "watchAlert/controllers/dao" ) type DutyPeopleController struct{} diff --git a/controllers/api/duty_schedule.go b/controllers/api/duty_schedule.go index 578f63d..6bedd2d 100644 --- a/controllers/api/duty_schedule.go +++ b/controllers/api/duty_schedule.go @@ -2,8 +2,8 @@ package api import ( "github.com/gin-gonic/gin" - "prometheus-manager/controllers/dao" "strconv" + "watchAlert/controllers/dao" ) type DutyScheduleController struct{} diff --git a/controllers/api/entry.go b/controllers/api/entry.go index f515548..e33788e 100644 --- a/controllers/api/entry.go +++ b/controllers/api/entry.go @@ -1,6 +1,6 @@ package api -import "prometheus-manager/controllers/services" +import "watchAlert/controllers/services" type ApiGroup struct { AlertNoticeObjectController diff --git a/controllers/api/events.go b/controllers/api/events.go index f11d147..4c479d9 100644 --- a/controllers/api/events.go +++ b/controllers/api/events.go @@ -6,8 +6,8 @@ import ( "github.com/gin-gonic/gin" "io/ioutil" "log" - "prometheus-manager/globals" - "prometheus-manager/utils/http" + "watchAlert/globals" + "watchAlert/utils/http" ) type EventController struct{} diff --git a/controllers/api/rule.go b/controllers/api/rule.go index cabc8a1..51ecedc 100644 --- a/controllers/api/rule.go +++ b/controllers/api/rule.go @@ -2,7 +2,7 @@ package api import ( "github.com/gin-gonic/gin" - "prometheus-manager/utils/http" + "watchAlert/utils/http" ) type RuleController struct{} diff --git a/controllers/dto/feishu.go b/controllers/dto/feishu.go index f282065..7a83649 100644 --- a/controllers/dto/feishu.go +++ b/controllers/dto/feishu.go @@ -100,8 +100,24 @@ type CardInfo struct { } type CardInfoAction struct { - Value interface{} `json:"value"` - Tag string `json:"tag"` + Value SilenceValue `json:"value"` + Tag string `json:"tag"` +} + +type SilenceValue struct { + Comment string `json:"comment"` + CreatedBy string `json:"createdBy"` + EndsAt string `json:"endsAt"` + Id string `json:"id"` + Matchers []MatchersLabels `json:"matchers"` + StartsAt string `json:"startsAt"` +} + +type MatchersLabels struct { + IsEqual bool `json:"isEqual"` + IsRegex bool `json:"isRegex"` + Name string `json:"name"` + Value string `json:"value"` } // FeiShuUserInfo 飞书用户信息 diff --git a/controllers/dto/prometheus.go b/controllers/dto/prometheus.go index c30a31c..01f6673 100644 --- a/controllers/dto/prometheus.go +++ b/controllers/dto/prometheus.go @@ -4,8 +4,9 @@ import "time" // Alerts 接收Prometheus推送的告警 type Alerts struct { - AlertList []AlertInfo `json:"alerts"` - Receiver string `json:"receiver"` + AlertList []AlertInfo `json:"alerts"` + Receiver string `json:"receiver"` + Aggregated string `json:"aggregated"` } type AlertInfo struct { diff --git a/controllers/repo/alert_notice.go b/controllers/repo/alert_notice.go index 8c6193a..b52ef23 100644 --- a/controllers/repo/alert_notice.go +++ b/controllers/repo/alert_notice.go @@ -1,8 +1,8 @@ package repo import ( - "prometheus-manager/controllers/dao" - "prometheus-manager/globals" + "watchAlert/controllers/dao" + "watchAlert/globals" ) type AlertNoticeRepo struct{} diff --git a/controllers/repo/duty_schedule.go b/controllers/repo/duty_schedule.go index 55a5f2e..ee0b3ac 100644 --- a/controllers/repo/duty_schedule.go +++ b/controllers/repo/duty_schedule.go @@ -2,8 +2,8 @@ package repo import ( "fmt" - "prometheus-manager/controllers/dao" - "prometheus-manager/globals" + "watchAlert/controllers/dao" + "watchAlert/globals" ) type DutyScheduleRepo struct{} diff --git a/controllers/services/alert_noticeObject.go b/controllers/services/alert_noticeObject.go index 56cd8a1..19d90c5 100644 --- a/controllers/services/alert_noticeObject.go +++ b/controllers/services/alert_noticeObject.go @@ -2,11 +2,11 @@ package services import ( "bytes" - "prometheus-manager/controllers/dao" - "prometheus-manager/globals" - "prometheus-manager/utils/cmd" - "prometheus-manager/utils/feishu" - "prometheus-manager/utils/http" + "watchAlert/controllers/dao" + "watchAlert/globals" + "watchAlert/utils/cmd" + "watchAlert/utils/feishu" + "watchAlert/utils/http" ) type AlertNoticeService struct{} diff --git a/controllers/services/alert_silence.go b/controllers/services/alert_silence.go index aeed42d..10ffadc 100644 --- a/controllers/services/alert_silence.go +++ b/controllers/services/alert_silence.go @@ -6,44 +6,39 @@ import ( "fmt" "io/ioutil" "log" - dto2 "prometheus-manager/controllers/dto" - "prometheus-manager/globals" - "prometheus-manager/utils/cache" - "prometheus-manager/utils/http" "strings" + "watchAlert/controllers/dto" + "watchAlert/globals" + "watchAlert/utils/cache" + "watchAlert/utils/http" ) type AlertSilenceService struct{} type InterAlertSilenceService interface { - CreateAlertSilence(uuid string, challengeInfo map[string]interface{}) error + CreateAlertSilence(uuid string, cardInfo dto.CardInfo) error } func NewInterAlertSilenceService() InterAlertSilenceService { return &AlertSilenceService{} } -func (ass *AlertSilenceService) CreateAlertSilence(uuid string, challengeInfo map[string]interface{}) error { +func (ass *AlertSilenceService) CreateAlertSilence(uuid string, cardInfo dto.CardInfo) error { var ( - cardInfo dto2.CardInfo + cacheAlertInfo dto.AlertInfo + AlertInfoList []dto.AlertInfo ) - rawDataJson, _ := json.Marshal(challengeInfo) - _ = json.Unmarshal(rawDataJson, &cardInfo) - - // To Json - kLabel := cardInfo.Action.Value.(map[string]interface{}) + // 获取静默请求参数 + kLabel := cardInfo.Action.Value silencesValueJson, _ := json.Marshal(kLabel) bodyReader := bytes.NewReader(silencesValueJson) - fingerprintID := kLabel["comment"] - labelData := getAlertSilencesFingerprintID(fingerprintID.(string)) - if labelData == fingerprintID { - globals.Logger.Sugar().Info("报警静默已存在, 无需重新创建, 报警ID ->", labelData) - return fmt.Errorf("报警静默已存在, 无需重新创建") - } + // 检查是否已创建 + getAlertSilencesFingerprintID(kLabel.Comment) + // 发起静默请求 _, err := http.Post(globals.Config.AlertManager.URL+"/api/v2/silences", bodyReader) if err != nil { globals.Logger.Sugar().Error("创建报警静默失败 ->", string(silencesValueJson)) @@ -51,25 +46,19 @@ func (ass *AlertSilenceService) CreateAlertSilence(uuid string, challengeInfo ma } globals.Logger.Sugar().Info("创建报警静默成功 ->", string(silencesValueJson)) - var ( - promAlertManager = make(map[string]interface{}) - cacheAlertInfo interface{} - AlertInfo []interface{} - ) - - cacheValue := globals.CacheCli.Get(fingerprintID.(string)) + // 将告警状态转换为静默状态 + cacheValue := globals.CacheCli.Get(kLabel.Comment) cacheAlertJson, _ := json.Marshal(cacheValue.(cache.CacheItem).Values) _ = json.Unmarshal(cacheAlertJson, &cacheAlertInfo) - AlertInfo = append(AlertInfo, cacheAlertInfo) - - // 将告警状态转换为静默状态 - promAlertManager["alert"] = AlertInfo - promAlertManager["alert"].([]interface{})[0].(map[string]interface{})["status"] = "silence" - prometheusAlertBody, _ := json.Marshal(promAlertManager) + cacheAlertInfo.Status = "silence" + AlertInfoList = append(AlertInfoList, cacheAlertInfo) + alerts := dto.Alerts{ + AlertList: AlertInfoList, + } + body, _ := json.Marshal(alerts) // 发送消息卡片 - actionUserID := challengeInfo["user_id"].(string) - err = NewInterEventService().PushAlertToWebhook(actionUserID, prometheusAlertBody, uuid) + err = NewInterEventService().PushAlertToWebhook(cardInfo.UserID, body, uuid) if err != nil { log.Println("消息卡片发送失败", err) return err @@ -79,7 +68,7 @@ func (ass *AlertSilenceService) CreateAlertSilence(uuid string, challengeInfo ma } -func getAlertSilencesFingerprintID(fingerprintID string) (labelData interface{}) { +func getAlertSilencesFingerprintID(fingerprintID string) { resp, err := http.Get(globals.Config.AlertManager.URL + "/api/v2/silences") if err != nil { @@ -90,7 +79,7 @@ func getAlertSilencesFingerprintID(fingerprintID string) (labelData interface{}) content, err := ioutil.ReadAll(resp.Body) var ( - res []dto2.SearchAlertManager + res []dto.SearchAlertManager activeLabelList []interface{} ) err = json.Unmarshal(content, &res) @@ -105,9 +94,9 @@ func getAlertSilencesFingerprintID(fingerprintID string) (labelData interface{}) for _, v := range activeLabelList { activeV := strings.Replace(v.(string), "\"", "", -1) if activeV == fingerprintID { - return fingerprintID + globals.Logger.Sugar().Info("报警静默已存在, 无需重新创建, 报警ID ->", fingerprintID) + return } } - return nil } diff --git a/controllers/services/duty_manage.go b/controllers/services/duty_manage.go index 5b00442..45c0ae2 100644 --- a/controllers/services/duty_manage.go +++ b/controllers/services/duty_manage.go @@ -1,9 +1,9 @@ package services import ( - "prometheus-manager/controllers/dao" - "prometheus-manager/globals" - "prometheus-manager/utils/cmd" + "watchAlert/controllers/dao" + "watchAlert/globals" + "watchAlert/utils/cmd" ) type DutyManageService struct{} diff --git a/controllers/services/duty_people.go b/controllers/services/duty_people.go index 140a743..741656c 100644 --- a/controllers/services/duty_people.go +++ b/controllers/services/duty_people.go @@ -1,9 +1,9 @@ package services import ( - "prometheus-manager/controllers/dao" - "prometheus-manager/globals" - "prometheus-manager/utils/cmd" + "watchAlert/controllers/dao" + "watchAlert/globals" + "watchAlert/utils/cmd" ) type DutyPeopleService struct{} diff --git a/controllers/services/duty_peopleGroup.go b/controllers/services/duty_peopleGroup.go index 33f610e..e798d5d 100644 --- a/controllers/services/duty_peopleGroup.go +++ b/controllers/services/duty_peopleGroup.go @@ -2,8 +2,8 @@ package services import ( "log" - "prometheus-manager/controllers/dao" - "prometheus-manager/globals" + "watchAlert/controllers/dao" + "watchAlert/globals" ) type DutyPeopleGroupService struct{} diff --git a/controllers/services/duty_schedule.go b/controllers/services/duty_schedule.go index 2718f66..3357361 100644 --- a/controllers/services/duty_schedule.go +++ b/controllers/services/duty_schedule.go @@ -3,8 +3,8 @@ package services import ( "fmt" "log" - "prometheus-manager/controllers/dao" - "prometheus-manager/globals" + "watchAlert/controllers/dao" + "watchAlert/globals" "strconv" "time" ) diff --git a/controllers/services/events.go b/controllers/services/events.go index 88b5855..ac37923 100644 --- a/controllers/services/events.go +++ b/controllers/services/events.go @@ -2,12 +2,14 @@ package services import ( "encoding/json" - "prometheus-manager/controllers/dao" - "prometheus-manager/globals" - "prometheus-manager/utils/renderTemplates/aliyun" - prom "prometheus-manager/utils/renderTemplates/prometheus" + "fmt" "strconv" "time" + "watchAlert/controllers/dao" + "watchAlert/controllers/dto" + "watchAlert/globals" + "watchAlert/utils/renderTemplates/aliyun" + prom "watchAlert/utils/renderTemplates/prometheus" ) type EventService struct{} @@ -30,6 +32,7 @@ func (es *EventService) PushAlertToWebhook(actionUserID string, body []byte, uui switch alertNoticeObject.DataSource { case "Prometheus": + body = alertAggregation(body) err := prometheus(alertNoticeObject, actionUserID, body, dutyUser) if err != nil { return err @@ -86,3 +89,27 @@ func aLiYun(alertNotice dao.AlertNotice, body []byte, dutyUser string) error { return nil } + +func alertAggregation(body []byte) []byte { + + var ( + alerts dto.Alerts + alertName string + filteredAlertInfo []dto.AlertInfo + countIdenticalAlert = make(map[string]int) + ) + _ = json.Unmarshal(body, &alerts) + for k, v := range alerts.AlertList { + alertName = v.Labels["alertname"] + countIdenticalAlert[alertName] = k + 1 + } + if countIdenticalAlert[alertName] > 1 { + filteredAlertInfo = append(filteredAlertInfo, alerts.AlertList[0]) + alerts.AlertList = filteredAlertInfo + alerts.Aggregated = fmt.Sprintf("\n聚合 %d 条告警, 详情查看告警链接 ⬇️", countIdenticalAlert[alertName]) + } + + jsonData, _ := json.Marshal(alerts) + return jsonData + +} diff --git a/controllers/services/repo_entry.go b/controllers/services/repo_entry.go index 0fb4ea6..5ed0d01 100644 --- a/controllers/services/repo_entry.go +++ b/controllers/services/repo_entry.go @@ -1,6 +1,6 @@ package services -import "prometheus-manager/controllers/repo" +import "watchAlert/controllers/repo" var ( alertNotice = repo.RepoGroupEntry.AlertNoticeRepo diff --git a/controllers/services/rule_ruleGroups.go b/controllers/services/rule_ruleGroups.go index 472ceb9..0038401 100644 --- a/controllers/services/rule_ruleGroups.go +++ b/controllers/services/rule_ruleGroups.go @@ -6,9 +6,9 @@ import ( "gorm.io/gorm" "io" "io/ioutil" - "prometheus-manager/controllers/dao" - "prometheus-manager/controllers/dto" - "prometheus-manager/globals" + "watchAlert/controllers/dao" + "watchAlert/controllers/dto" + "watchAlert/globals" ) type RuleGroupService struct{} diff --git a/controllers/services/rules.go b/controllers/services/rules.go index 33bd76e..1258018 100644 --- a/controllers/services/rules.go +++ b/controllers/services/rules.go @@ -8,9 +8,9 @@ import ( "io/ioutil" "log" "os" - "prometheus-manager/controllers/dto" - "prometheus-manager/globals" "strings" + "watchAlert/controllers/dto" + "watchAlert/globals" ) type RuleService struct{} @@ -56,7 +56,7 @@ func (rs *RuleService) SelectPromRules(ruleGroup string) SearchRules { resRule SearchRules ) - ruleConfigFile := globals.Config.Prometheus.RulePath + "/" + ruleGroup + ".yaml" + ruleConfigFile, _ := getRuleConfigInfo(ruleGroup) file, err := ioutil.ReadFile(ruleConfigFile) if err != nil { log.Println("文件读取失败 ->", err) @@ -85,17 +85,17 @@ func (rs *RuleService) CreatePromRule(ruleGroup string, ruleBody io.ReadCloser) if len(ruleGroup) == 0 { return fmt.Errorf("RuleGroup 不能为空") } - ruleFilePath := globals.Config.Prometheus.RulePath + "/" + ruleGroup + ".yaml" + ruleByte, _ := ioutil.ReadAll(ruleBody) _ = json.Unmarshal(ruleByte, &rules) - _, err := os.Stat(ruleFilePath) - if err != nil { - _ = json.Unmarshal(ruleByte, &rules) + ruleFilePath, ruleConfig := getRuleConfigInfo(ruleGroup) + + if len(ruleConfig.Groups) == 0 { ruleHeader := RULEHEADER ruleHeader = strings.ReplaceAll(ruleHeader, "{{ name }}", rules.Groups[0].Name) - err = os.WriteFile(ruleFilePath, []byte(ruleHeader), 0644) + err := os.WriteFile(ruleFilePath, []byte(ruleHeader), 0644) if err != nil { log.Println("内容写入文件失败 ->", err) return err @@ -116,12 +116,14 @@ func (rs *RuleService) CreatePromRule(ruleGroup string, ruleBody io.ReadCloser) ruleContent = strings.ReplaceAll(ruleContent, "{{ description }}", rules.Groups[0].Rules[0].Annotations.Description) f, _ := os.OpenFile(ruleFilePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) - _, err = f.WriteString(ruleContent) + _, err := f.WriteString(ruleContent) if err != nil { log.Println("内容写入文件失败 ->", err) return err } + err = formatRuleConfig(ruleGroup) + err = globals.DBCli.Exec("UPDATE rule_group_data SET rule_number = rule_number + 1 WHERE name = ?", ruleGroup).Error if err != nil { return fmt.Errorf("数据库更新失败 -> %s", err) @@ -134,16 +136,11 @@ func (rs *RuleService) CreatePromRule(ruleGroup string, ruleBody io.ReadCloser) // DeletePromRule 删除告警规则 func (rs *RuleService) DeletePromRule(ruleGroup, ruleName string) error { - ruleFilePath := globals.Config.Prometheus.RulePath + "/" + ruleGroup + ".yaml" - var ( - rules dto.AlertRules - newAfter []dto.Rules - ) + var newAfter []dto.Rules - file, _ := os.ReadFile(ruleFilePath) - _ = yaml.Unmarshal(file, &rules) + ruleFilePath, ruleConfig := getRuleConfigInfo(ruleGroup) - after := rules + after := ruleConfig // 获取将被删除元素的下标 for i, v := range after.Groups { for _, vv := range v.Rules { @@ -181,25 +178,23 @@ func (rs *RuleService) DeletePromRule(ruleGroup, ruleName string) error { func (rs *RuleService) UpdatePromRule(ruleGroup, ruleName string, ruleBody io.ReadCloser) (dto.AlertRules, error) { var ( - ruleConfigFile, newRuleConfig dto.AlertRules + newRuleConfig dto.AlertRules ) - ruleFilePath := globals.Config.Prometheus.RulePath + "/" + ruleGroup + ".yaml" bodyIO, _ := ioutil.ReadAll(ruleBody) err := json.Unmarshal(bodyIO, &newRuleConfig) - fConfig, _ := ioutil.ReadFile(ruleFilePath) - err = yaml.Unmarshal(fConfig, &ruleConfigFile) + ruleFilePath, ruleConfig := getRuleConfigInfo(ruleGroup) - for k, v := range ruleConfigFile.Groups { + for k, v := range ruleConfig.Groups { for kk, vv := range v.Rules { if vv.Alert == ruleName { - ruleConfigFile.Groups[k].Rules[kk] = newRuleConfig.Groups[0].Rules[0] + ruleConfig.Groups[k].Rules[kk] = newRuleConfig.Groups[0].Rules[0] } } } - f, _ := yaml.Marshal(ruleConfigFile) + f, _ := yaml.Marshal(ruleConfig) err = os.WriteFile(ruleFilePath, f, 0644) if err != nil { log.Println("内容写入文件失败 ->", err) @@ -212,18 +207,9 @@ func (rs *RuleService) UpdatePromRule(ruleGroup, ruleName string, ruleBody io.Re func (rs *RuleService) GetPromRuleData(ruleGroup, ruleName string) (dto.Rules, error) { - var ( - allRules dto.AlertRules - getRule dto.Rules - ) + var getRule dto.Rules - ruleFilePath := globals.Config.Prometheus.RulePath + "/" + ruleGroup + ".yaml" - - file, err := ioutil.ReadFile(ruleFilePath) - if err != nil { - return dto.Rules{}, err - } - err = yaml.Unmarshal(file, &allRules) + _, allRules := getRuleConfigInfo(ruleGroup) for k, v := range allRules.Groups { for kk, vv := range v.Rules { @@ -237,3 +223,32 @@ func (rs *RuleService) GetPromRuleData(ruleGroup, ruleName string) (dto.Rules, e return getRule, nil } + +func getRuleConfigInfo(ruleGroup string) (string, dto.AlertRules) { + + var ruleConfig dto.AlertRules + + ruleFilePath := globals.Config.Prometheus.RulePath + "/" + ruleGroup + ".yaml" + + _, err := os.Stat(ruleFilePath) + if err != nil { + return ruleFilePath, dto.AlertRules{} + } + fConfig, _ := ioutil.ReadFile(ruleFilePath) + _ = yaml.Unmarshal(fConfig, &ruleConfig) + + return ruleFilePath, ruleConfig + +} + +func formatRuleConfig(ruleGroup string) error { + + ruleConfigPath, ruleConfig := getRuleConfigInfo(ruleGroup) + ruleConfigYaml, _ := yaml.Marshal(ruleConfig) + err := os.WriteFile(ruleConfigPath, ruleConfigYaml, 0644) + if err != nil { + return fmt.Errorf("内容写入文件失败 -> %s", err) + } + return nil + +} diff --git a/globals/global.go b/globals/global.go index 55117f1..0fab62a 100644 --- a/globals/global.go +++ b/globals/global.go @@ -4,8 +4,8 @@ import ( lark "github.com/larksuite/oapi-sdk-go/v3" "go.uber.org/zap" "gorm.io/gorm" - "prometheus-manager/controllers/dto" - "prometheus-manager/utils/cache" + "watchAlert/controllers/dto" + "watchAlert/utils/cache" ) var ( diff --git a/go.mod b/go.mod index f3e85a6..53ba501 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,9 @@ -module prometheus-manager +module watchAlert go 1.18 require ( + github.com/fatih/color v1.13.0 github.com/gin-gonic/gin v1.9.1 github.com/go-openapi/strfmt v0.21.7 github.com/google/uuid v1.3.0 @@ -35,6 +36,7 @@ require ( github.com/klauspost/cpuid/v2 v2.2.5 // indirect github.com/leodido/go-urn v1.2.4 // indirect github.com/magiconair/properties v1.8.7 // indirect + github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect diff --git a/go.sum b/go.sum index 3a4da66..f2135a2 100644 --- a/go.sum +++ b/go.sum @@ -70,6 +70,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po= github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= @@ -193,6 +195,11 @@ github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= @@ -395,6 +402,7 @@ golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -419,6 +427,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/initialize/client.go b/initialize/client.go index eb3b048..40f952e 100644 --- a/initialize/client.go +++ b/initialize/client.go @@ -5,9 +5,9 @@ import ( lark "github.com/larksuite/oapi-sdk-go/v3" "gorm.io/driver/mysql" "gorm.io/gorm" - dao2 "prometheus-manager/controllers/dao" - "prometheus-manager/globals" - "prometheus-manager/utils/cache" + dao2 "watchAlert/controllers/dao" + "watchAlert/globals" + "watchAlert/utils/cache" ) func InitClient() { diff --git a/initialize/config.go b/initialize/config.go index 3938489..d4d873b 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -3,7 +3,7 @@ package initialize import ( "github.com/spf13/viper" "log" - "prometheus-manager/globals" + "watchAlert/globals" ) var ( diff --git a/initialize/log.go b/initialize/log.go index 07b3005..33f370e 100644 --- a/initialize/log.go +++ b/initialize/log.go @@ -4,7 +4,7 @@ import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" "os" - "prometheus-manager/globals" + "watchAlert/globals" "time" ) diff --git a/initialize/routes.go b/initialize/routes.go index e2f6ba7..2bcd1c5 100644 --- a/initialize/routes.go +++ b/initialize/routes.go @@ -2,9 +2,9 @@ package initialize import ( "github.com/gin-gonic/gin" - "prometheus-manager/globals" - "prometheus-manager/routers" - "prometheus-manager/routers/v1" + "watchAlert/globals" + "watchAlert/routers" + "watchAlert/routers/v1" ) func InitRoute() { diff --git a/main.go b/main.go index d7748ca..2dad1db 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - "prometheus-manager/initialize" + "watchAlert/initialize" ) func main() { diff --git a/routers/v1/api_entry.go b/routers/v1/api_entry.go index 45c504e..2053f88 100644 --- a/routers/v1/api_entry.go +++ b/routers/v1/api_entry.go @@ -1,6 +1,6 @@ package v1 -import "prometheus-manager/controllers/api" +import "watchAlert/controllers/api" var ( AlertNoticeObject = api.ApiGroupApp.AlertNoticeObjectController diff --git a/utils/feishu/feishu.go b/utils/feishu/feishu.go index af70fcb..31acd4c 100644 --- a/utils/feishu/feishu.go +++ b/utils/feishu/feishu.go @@ -6,8 +6,8 @@ import ( "fmt" larkcore "github.com/larksuite/oapi-sdk-go/v3/core" larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" - "prometheus-manager/controllers/dto" - "prometheus-manager/globals" + "watchAlert/controllers/dto" + "watchAlert/globals" ) // GetFeiShuChatsID 获取机器人所在的群列表 diff --git a/utils/http/http.go b/utils/http/http.go index 50e303d..3f78c1b 100644 --- a/utils/http/http.go +++ b/utils/http/http.go @@ -4,7 +4,7 @@ import ( "bytes" "log" "net/http" - "prometheus-manager/globals" + "watchAlert/globals" ) func Get(url string) (*http.Response, error) { diff --git a/utils/renderTemplates/aliyun/aliyunSLS.go b/utils/renderTemplates/aliyun/aliyunSLS.go index 9434771..168c03a 100644 --- a/utils/renderTemplates/aliyun/aliyunSLS.go +++ b/utils/renderTemplates/aliyun/aliyunSLS.go @@ -3,9 +3,9 @@ package aliyun import ( "encoding/json" "fmt" - "prometheus-manager/controllers/dto" - "prometheus-manager/globals" - "prometheus-manager/utils/renderTemplates" + "watchAlert/controllers/dto" + "watchAlert/globals" + "watchAlert/utils/renderTemplates" "strings" ) diff --git a/utils/renderTemplates/aliyun/feishuTemplate.go b/utils/renderTemplates/aliyun/feishuTemplate.go index 48e4fa6..6f5cdd7 100644 --- a/utils/renderTemplates/aliyun/feishuTemplate.go +++ b/utils/renderTemplates/aliyun/feishuTemplate.go @@ -2,8 +2,8 @@ package aliyun import ( "fmt" - dto2 "prometheus-manager/controllers/dto" - "prometheus-manager/globals" + dto2 "watchAlert/controllers/dto" + "watchAlert/globals" "strconv" "time" ) diff --git a/utils/renderTemplates/feishu.go b/utils/renderTemplates/feishu.go index 7436f6b..16889de 100644 --- a/utils/renderTemplates/feishu.go +++ b/utils/renderTemplates/feishu.go @@ -7,8 +7,8 @@ import ( larkcore "github.com/larksuite/oapi-sdk-go/v3/core" larkcontact "github.com/larksuite/oapi-sdk-go/v3/service/contact/v3" larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" - "prometheus-manager/controllers/dto" - "prometheus-manager/globals" + "watchAlert/controllers/dto" + "watchAlert/globals" ) func PushFeiShu(chatId string, cardContentJson []string) error { diff --git a/utils/renderTemplates/prometheus/feishuTemplate.go b/utils/renderTemplates/prometheus/feishuTemplate.go index 04aedfa..8139ac1 100644 --- a/utils/renderTemplates/prometheus/feishuTemplate.go +++ b/utils/renderTemplates/prometheus/feishuTemplate.go @@ -2,12 +2,12 @@ package prometheus import ( "fmt" - dto2 "prometheus-manager/controllers/dto" - "prometheus-manager/globals" - "prometheus-manager/utils/renderTemplates" "strconv" "strings" "time" + dto2 "watchAlert/controllers/dto" + "watchAlert/globals" + "watchAlert/utils/renderTemplates" ) type FeiShu struct{} @@ -34,7 +34,7 @@ func (f *FeiShu) FeiShuMsgTemplate(prometheusAlert PrometheusAlert) (msg dto2.Fe switch prometheusAlert.alerts.Status { case "firing": - return firingMsgTemplate(defaultTemplate, prometheusAlert.alerts, prometheusAlert.actionValues, prometheusAlert.currentDutyUser) + return firingMsgTemplate(defaultTemplate, prometheusAlert.alerts, prometheusAlert.actionValues, prometheusAlert.currentDutyUser, prometheusAlert.aggregated) case "resolved": return resolvedMsgTemplate(defaultTemplate, prometheusAlert.alerts) case "silence": @@ -45,7 +45,7 @@ func (f *FeiShu) FeiShuMsgTemplate(prometheusAlert PrometheusAlert) (msg dto2.Fe } // firingMsgTemplate 告警模版 -func firingMsgTemplate(template dto2.FeiShuMsg, v dto2.AlertInfo, ActionsValueStr dto2.CreateAlertSilence, dutyUser string) dto2.FeiShuMsg { +func firingMsgTemplate(template dto2.FeiShuMsg, v dto2.AlertInfo, ActionsValueStr dto2.CreateAlertSilence, dutyUser string, aggregation string) dto2.FeiShuMsg { var ( confirmPrompt = "静默 " + strconv.Itoa(int(globals.Config.AlertManager.SilenceTime)) + " 分钟" @@ -213,7 +213,7 @@ func firingMsgTemplate(template dto2.FeiShuMsg, v dto2.AlertInfo, ActionsValueSt { Tag: "div", Text: dto2.Texts{ - Content: " ", + Content: aggregation, Tag: "plain_text", }, }, diff --git a/utils/renderTemplates/prometheus/prometheus.go b/utils/renderTemplates/prometheus/prometheus.go index a9824d6..370a1d9 100644 --- a/utils/renderTemplates/prometheus/prometheus.go +++ b/utils/renderTemplates/prometheus/prometheus.go @@ -3,11 +3,11 @@ package prometheus import ( "encoding/json" "fmt" - dto2 "prometheus-manager/controllers/dto" - "prometheus-manager/globals" - "prometheus-manager/utils/renderTemplates" "strings" "time" + dto2 "watchAlert/controllers/dto" + "watchAlert/globals" + "watchAlert/utils/renderTemplates" ) type renderPrometheus struct { @@ -15,10 +15,9 @@ type renderPrometheus struct { } type PrometheusAlert struct { - currentDutyUser string - actionUser string - alerts dto2.AlertInfo - actionValues dto2.CreateAlertSilence + currentDutyUser, actionUser, aggregated string + alerts dto2.AlertInfo + actionValues dto2.CreateAlertSilence } func PrometheusTemplate(actionUser string, alertMsg map[string]interface{}, dutyUser string) *renderPrometheus { @@ -70,6 +69,7 @@ func PrometheusTemplate(actionUser string, alertMsg map[string]interface{}, duty actionUser: actionUser, alerts: v, actionValues: actionValues, + aggregated: alerts.Aggregated, } // 数据返回给结构体