package notice import ( "agent/common/config" "agent/common/log" "fmt" "framework/klib/khttp" "io/ioutil" "reflect" "strings" ) var emailTemplate = `

{{.MTitle}}

状态:{{.Status}}

报警策略:{{.Policy}}

监控项:{{.Metric}}

目标:{{.Target}}

告警内容: {{.Content}}

触发值:{{.Value}}

所属团队:{{.Group}}

告警发生时间:{{.StartTime}}

(本邮件由系统自动发出,请勿回复)

` var NoticeMode []Notice func init() { NoticeMode = append(NoticeMode, NewEmailNotice()) NoticeMode = append(NoticeMode, NewSmsNotice()) } type NoticeMessage struct { MTitle string Status string Policy string Metric string Target string Content string Value string Group string StartTime string State string Color string emails []string phones []string } func (n *NoticeMessage) SetEmails(emails []string) *NoticeMessage { n.emails = emails return n } func (n *NoticeMessage) SetPhones(phones []string) *NoticeMessage { n.phones = phones return n } func (n *NoticeMessage) SetPolicy(policy string) *NoticeMessage { n.Policy = policy return n } func (n *NoticeMessage) SetMetric(metric string) *NoticeMessage { n.Metric = metric return n } func (n *NoticeMessage) SetStatus(status int) *NoticeMessage { switch status { case 1: n.Status = "报警" n.MTitle = "firing" n.State = "1" n.Color = "#E6522C" case 2: n.Status = "恢复" n.MTitle = "resolved" n.State = "2" n.Color = "#00BFFF" } return n } func (n *NoticeMessage) SetTarget(target string) *NoticeMessage { n.Target = target return n } func (n *NoticeMessage) SetContent(content string) *NoticeMessage { n.Content = content return n } func (n *NoticeMessage) SetValue(value string) *NoticeMessage { n.Value = value return n } func (n *NoticeMessage) SetGroup(group string) *NoticeMessage { n.Group = group return n } func (n *NoticeMessage) SetStartTime(startTime string) *NoticeMessage { n.StartTime = startTime return n } func (n *NoticeMessage) buildEmail() string { defer func() { if x := recover(); x != nil { log.Error("[Exception]", x) log.TraceAll() } }() tpl := emailTemplate v := reflect.ValueOf(n) ele := v.Elem() t := ele.Type() for i := 0; i < t.NumField(); i++ { fv := ele.Field(i) if !fv.CanSet() { continue } tpl = strings.Replace(tpl, "{{."+t.Field(i).Name+"}}", fmt.Sprint(fv.Interface()), 1) } return tpl } func isEmptyValue(v reflect.Value) bool { switch v.Kind() { case reflect.Array, reflect.Map, reflect.Slice, reflect.String: return v.Len() == 0 case reflect.Bool: return !v.Bool() case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return v.Int() == 0 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return v.Uint() == 0 case reflect.Float32, reflect.Float64: return v.Float() == 0 case reflect.Interface, reflect.Ptr: return v.IsNil() } return false } func (n *NoticeMessage) buildSms() string { return "" } type Notice interface { Notice(*NoticeMessage) } //邮箱通知 type EmailNotice struct { ContentTemplet string Subject string } func NewEmailNotice() *EmailNotice { emailNotice := &EmailNotice{} emailNotice.ContentTemplet = "" emailNotice.Subject = "云平台监控报警通知" return emailNotice } //通知 func (e *EmailNotice) Notice(m *NoticeMessage) { e.notice(m.emails, m.buildEmail()) } //通知 func (e *EmailNotice) notice(receiver []string, content string) { if len(receiver) == 0 { return } url := config.GetEmailUrl() if url == "" { log.NoticeLog.Info("email url is empty") return } heads := make(map[string]string) heads["X-Api-ClientID"] = fmt.Sprint(config.GetClientID()) heads["X-Api-ClientSecret"] = config.GetClientSerect() data := make(map[string]interface{}) data["reciver_email"] = strings.Join(receiver, ";") data["subject"] = "云监控通知" data["email_content"] = content res, err := khttp.Post(url, data, heads) if err != nil { log.NoticeLog.Error(url, data, err) return } b, _ := ioutil.ReadAll(res.Body) log.NoticeLog.Info(url, data, string(b)) } //短信通知 type SmsNotice struct { smsCode int //短信模板id } func NewSmsNotice() *SmsNotice { SmsNotice := &SmsNotice{} SmsNotice.smsCode = 0 return SmsNotice } //通知 func (e *SmsNotice) Notice(m *NoticeMessage) { e.notice(m.phones, m.buildSms()) } //通知 func (e *SmsNotice) notice(receiver []string, content string) { if len(receiver) == 0 { return } if config.GetSmsUrl() == "" { return } khttp.Post(config.GetSmsUrl(), nil) }