From 4585588a44baaf663b7727aceef06ff60f997da7 Mon Sep 17 00:00:00 2001 From: 710leo <710leo@gmail.com> Date: Tue, 14 Apr 2020 23:44:21 +0800 Subject: [PATCH] refactor sender --- configs/alarm.yml | 8 ++++++-- modules/agent/utils/utils.go | 1 - modules/alarm/g/config.go | 29 ++++++++++++++++------------- modules/alarm/g/const.go | 14 ++++++++++++++ modules/alarm/sender/builder.go | 20 ++++++++++++++++---- modules/alarm/sender/sender.go | 20 +++++++------------- 6 files changed, 59 insertions(+), 33 deletions(-) diff --git a/configs/alarm.yml b/configs/alarm.yml index ada1cc4..dd843fa 100644 --- a/configs/alarm.yml +++ b/configs/alarm.yml @@ -1,3 +1,5 @@ +debug: true +smsEnabled: true rpc: listen: "0.0.0.0:1986" web: @@ -6,15 +8,17 @@ web: timeout: 300 interval: 60 #get strategy interval smtp: + enabled: true addr: "mail.addr:25" username: "mail@mail.com" password: "" from: "mail@mail.com" wechat: + enabled: false toparty: "@all" #需要发送指定人请使用"@all" agentid: 1000008 - corpid: "wx4323ae792e1SWEW" - corpsecret: "kq01-IEYUAsqyB2afrSpZDSWWED_u8kJcaS-1231231" + corpid: "" + corpsecret: "" worker: sms: 10 #send sms concurrency mail: 50 #send mail concurrency diff --git a/modules/agent/utils/utils.go b/modules/agent/utils/utils.go index f56b133..276aca3 100644 --- a/modules/agent/utils/utils.go +++ b/modules/agent/utils/utils.go @@ -20,7 +20,6 @@ const ( REQ_TIMEOUT = 1 INVALID_RESP_CODE = 2 KEYWORD_UNMATCH = 3 - DNS_ERROR = 4 ) func CheckTargetStatus(item *dataobj.DetectedItem) { diff --git a/modules/alarm/g/config.go b/modules/alarm/g/config.go index 0d9da2c..da26338 100644 --- a/modules/alarm/g/config.go +++ b/modules/alarm/g/config.go @@ -10,13 +10,14 @@ import ( ) type GlobalConfig struct { - Debug bool `yaml:"debug"` - Remain int `yaml:"remain"` - Rpc *RpcConfig `yaml:"rpc"` - Web *WebConfig `yaml:"web"` - Worker *WorkerConfig `yaml:"worker"` - Smtp *SmtpConfig `yaml:"smtp"` - WeChat *WeChatConfig `yaml:"wechat"` + Debug bool `yaml:"debug"` + SmsEnabled bool `yaml:"smsEnabled"` + Remain int `yaml:"remain"` + Rpc *RpcConfig `yaml:"rpc"` + Web *WebConfig `yaml:"web"` + Worker *WorkerConfig `yaml:"worker"` + Smtp *SmtpConfig `yaml:"smtp"` + WeChat *WeChatConfig `yaml:"wechat"` } type MysqlConfig struct { @@ -36,12 +37,13 @@ type WebConfig struct { } type WorkerConfig struct { - Sms int `yaml:"sms"` - Mail int `yaml:"mail"` + Sms int `yaml:"sms"` + Mail int `yaml:"mail"` WeChat int `yaml:"wechat"` } type SmtpConfig struct { + Enabled bool `yaml:"enabled"` Addr string `yaml:"addr"` Username string `yaml:"username"` Password string `yaml:"password"` @@ -50,10 +52,11 @@ type SmtpConfig struct { } type WeChatConfig struct { - ToParty string `json:"toparty"` - AgentId int `json:agentid` - CorpId string `json:corpid` - CorpSecret string `json:corpsecret` + Enabled bool `yaml:"enabled"` + ToParty string `yaml:"toparty"` + AgentId int `yaml:agentid` + CorpId string `yaml:corpid` + CorpSecret string `yaml:corpsecret` } var ( diff --git a/modules/alarm/g/const.go b/modules/alarm/g/const.go index 493c5ad..ff4ee54 100644 --- a/modules/alarm/g/const.go +++ b/modules/alarm/g/const.go @@ -4,6 +4,20 @@ const ( VERSION = "0.1.1" ) +const ( + NO_ERROR = 0 + REQ_TIMEOUT = 1 + INVALID_RESP_CODE = 2 + KEYWORD_UNMATCH = 3 +) + +var EventStatus = map[int64]string{ + NO_ERROR: "ok", + REQ_TIMEOUT: "timeout", + INVALID_RESP_CODE: "bad resp code", + KEYWORD_UNMATCH: "keyword unmatch", +} + //0.0.2 fix event_id error //0.1.0 support send sms shell //0.1.1 use mysql store event diff --git a/modules/alarm/sender/builder.go b/modules/alarm/sender/builder.go index 5a9ad43..f6c746d 100644 --- a/modules/alarm/sender/builder.go +++ b/modules/alarm/sender/builder.go @@ -14,15 +14,16 @@ func BuildMail(event *dataobj.Event) string { strategy, _ := cache.StrategyMap.Get(event.StrategyId) respTime := fmt.Sprintf("%dms", event.RespTime) return fmt.Sprintf( - "状态:%s\nUrl:%s\n备注:%s\nIP:%s\n返回状态码:%s\n响应时间:%s\n时间:%s\n报警次数:%d\n", + "状态:%s\n结果:%s\nUrl:%s\nIP:%s\n返回状态码:%s\n响应时间:%s\n时间:%s\n报警次数:%d\n备注:%s\n", event.Status, + g.EventStatus[event.Result], event.Url, - strategy.Note, event.Ip, event.RespCode, respTime, humanTime(event.EventTime), event.CurrentStep, + strategy.Note, ) } @@ -31,7 +32,7 @@ func BuildSms(event *dataobj.Event) string { return fmt.Sprintf( "[%s][%s %s][%s][%s][%s][O%d]", event.Status, - showSubString(event.Url, 50), + showSubString(event.Url, 100), event.Ip, event.RespCode, respTime, @@ -74,6 +75,10 @@ func showSubString(str string, length int) string { } func WriteSms(tos []string, content string) { + if !g.Config.SmsEnabled { + return + } + if len(tos) == 0 { return } @@ -84,6 +89,10 @@ func WriteSms(tos []string, content string) { } func WriteMail(tos []string, subject, content string) { + if !g.Config.Smtp.Enabled { + return + } + if len(tos) == 0 { return } @@ -94,10 +103,13 @@ func WriteMail(tos []string, subject, content string) { } func WriteWeChat(tos []string, content string) { + if !g.Config.WeChat.Enabled { + return + } if len(tos) == 0 { return } weChat := &g.WeChat{Tos: strings.Join(tos, "|"), Content: content} WeChatWorkerChan <- 1 go sendWeChat(weChat) -} \ No newline at end of file +} diff --git a/modules/alarm/sender/sender.go b/modules/alarm/sender/sender.go index 2734f45..8dfbcce 100644 --- a/modules/alarm/sender/sender.go +++ b/modules/alarm/sender/sender.go @@ -28,7 +28,7 @@ var ( SmsWorkerChan chan int MailWorkerChan chan int WeChatWorkerChan chan int - requestError = errors.New("request error,check url or network") + requestError = errors.New("request error,check url or network") ) const ( @@ -100,32 +100,26 @@ func sendMail(mail *g.Mail) { s := smtp.NewSMTP(g.Config.Smtp.Addr, g.Config.Smtp.Username, g.Config.Smtp.Password, g.Config.Smtp.Tls, false, false) err := s.SendMail(g.Config.Smtp.From, strings.Replace(mail.Tos, ",", ";", -1), mail.Subject, mail.Content, "text") if err != nil { - log.Println(err, "tos:", mail.Tos) - //SendSmsToMaintainer("sender:" + err.Error()) + log.Printf("send mail err:%v tos:%v\n", err, mail.Tos) + return } if g.Config.Debug { - resp := "ok" - if err != nil { - resp = err.Error() - } log.Println("==mail==>>>>", mail) - log.Println("<<<<==mail==", resp) } } - func sendWeChat(weChat *g.WeChat) { defer func() { <-WeChatWorkerChan }() - var msg = weChatMsg { - ToUser: weChat.Tos, + var msg = weChatMsg{ + ToUser: weChat.Tos, ToParty: g.Config.WeChat.ToParty, MsgType: "text", AgentId: g.Config.WeChat.AgentId, - Text: map[string]string{"content": weChat.Content}, + Text: map[string]string{"content": weChat.Content}, } token, err := GetToken(g.Config.WeChat.CorpId, g.Config.WeChat.CorpSecret) buf, err := json.Marshal(msg) @@ -224,4 +218,4 @@ func SendMsg(AccessToken string, msgBody []byte) error { return errors.New(string(buf)) } return nil -} \ No newline at end of file +}