Skip to content

Commit

Permalink
refactor sender
Browse files Browse the repository at this point in the history
  • Loading branch information
710leo committed Apr 14, 2020
1 parent ec69dbf commit 4585588
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 33 deletions.
8 changes: 6 additions & 2 deletions configs/alarm.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
debug: true
smsEnabled: true
rpc:
listen: "0.0.0.0:1986"
web:
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion modules/agent/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (
REQ_TIMEOUT = 1
INVALID_RESP_CODE = 2
KEYWORD_UNMATCH = 3
DNS_ERROR = 4
)

func CheckTargetStatus(item *dataobj.DetectedItem) {
Expand Down
29 changes: 16 additions & 13 deletions modules/alarm/g/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"`
Expand All @@ -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 (
Expand Down
14 changes: 14 additions & 0 deletions modules/alarm/g/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 16 additions & 4 deletions modules/alarm/sender/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)
}
}
20 changes: 7 additions & 13 deletions modules/alarm/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -224,4 +218,4 @@ func SendMsg(AccessToken string, msgBody []byte) error {
return errors.New(string(buf))
}
return nil
}
}

0 comments on commit 4585588

Please sign in to comment.