|
@@ -0,0 +1,139 @@
|
|
|
+package handler
|
|
|
+
|
|
|
+import (
|
|
|
+ "crypto/rand"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "io"
|
|
|
+ "math/big"
|
|
|
+ "net/http"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/go-admin-team/go-admin-core/logger"
|
|
|
+)
|
|
|
+
|
|
|
+type SingleMsg struct {
|
|
|
+ ID string `json:"id"`
|
|
|
+ MsgContent string `json:"msgcontent"`
|
|
|
+ MsgType string `json:"msgtype"`
|
|
|
+ PhoneNum string `json:"phonenum"`
|
|
|
+}
|
|
|
+
|
|
|
+type SendMsg struct {
|
|
|
+ SubmitTime string `json:"submittime"`
|
|
|
+ MqReceipt bool `json:"mqreceipt"`
|
|
|
+ ExtCode string `json:"extcode"`
|
|
|
+ MsgList []SingleMsg `json:"msgList"`
|
|
|
+}
|
|
|
+
|
|
|
+func sm3(data string) string {
|
|
|
+ // 这里应该实现SM3算法,由于Go标准库中没有直接支持,这里只是返回一个模拟的字符串
|
|
|
+ return "fake-sm3-hash"
|
|
|
+}
|
|
|
+
|
|
|
+func (e *EventHandler) SendMsg() *EventHandler {
|
|
|
+ logger.Debugf("In sms Send func, %s", e.RP.AppAlias)
|
|
|
+ //判断该应用是否添加了短信通知
|
|
|
+ _, ok := e.SmsInfo.AppsGroup[e.RP.AppAlias]
|
|
|
+ if !ok {
|
|
|
+ return e
|
|
|
+ }
|
|
|
+ logger.Debug("has key......", e.JR.AlertStatus)
|
|
|
+ //判断是否需要告警
|
|
|
+ if !(e.JR.AlertStatus == FIRING) {
|
|
|
+ return e
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.Info("start to send sms...")
|
|
|
+
|
|
|
+ appkey := e.SmsInfo.Appkey
|
|
|
+ appsecret := e.SmsInfo.Appsecret
|
|
|
+
|
|
|
+ headers := make(map[string]string)
|
|
|
+ nonce, err := rand.Int(rand.Reader, big.NewInt(1<<62))
|
|
|
+ if err != nil {
|
|
|
+ e.Errs["rander error"] = err
|
|
|
+ return e
|
|
|
+ }
|
|
|
+ timestamp := time.Now().UnixNano() / 1e6
|
|
|
+
|
|
|
+ headers["x-system-key"] = appkey
|
|
|
+ headers["x-system-nonce"] = nonce.String()
|
|
|
+ headers["x-system-timestamp"] = fmt.Sprintf("%d", timestamp)
|
|
|
+ headers["x-system-token"] = sm3(appkey + fmt.Sprintf("%d", timestamp) + appsecret + nonce.String())
|
|
|
+
|
|
|
+ // set smsContent
|
|
|
+ // 所属系统:{{app_name}}
|
|
|
+ // 事件名称:{{events_name}}
|
|
|
+ // 事件创建时间:{{date_time}}
|
|
|
+ // 事件描述:{{events}}`
|
|
|
+ e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{app_name}}", e.RP.AppName, -1)
|
|
|
+ e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{events_name}}", e.RP.RuleName, -1)
|
|
|
+ e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{date_time}}", e.RP.CreateTime.Format("2006-01-02 15:04:05"), -1)
|
|
|
+ e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{events}}", fmt.Sprintf("%s, %d分钟内发生%s%v次, 当前值为: %v",
|
|
|
+ e.RP.RuleName, e.AC.Interval, e.AC.Condition, e.JR.CompareV, e.JR.CompareV), -1)
|
|
|
+ logger.Info("sms build: ", e.SmsInfo.SmsTpl)
|
|
|
+
|
|
|
+ MsgList := make([]SingleMsg, 0)
|
|
|
+ phoneList, ok := e.SmsInfo.AppsGroup[e.RP.AppAlias]
|
|
|
+ if ok {
|
|
|
+
|
|
|
+ for id, phone := range phoneList {
|
|
|
+ ssm := SingleMsg{
|
|
|
+ ID: fmt.Sprintf("%d", id),
|
|
|
+ MsgContent: e.SmsInfo.SmsTpl,
|
|
|
+ MsgType: "",
|
|
|
+ PhoneNum: phone,
|
|
|
+ }
|
|
|
+ MsgList = append(MsgList, ssm)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sendMsg := SendMsg{
|
|
|
+ SubmitTime: time.Now().Format("2006-01-02 15:04:05"),
|
|
|
+ MqReceipt: false,
|
|
|
+ ExtCode: "",
|
|
|
+ MsgList: MsgList,
|
|
|
+ }
|
|
|
+
|
|
|
+ jsonData, err := json.Marshal(sendMsg)
|
|
|
+ if err != nil {
|
|
|
+ e.Errs["sms json marshal error"] = err
|
|
|
+ return e
|
|
|
+ }
|
|
|
+
|
|
|
+ client := &http.Client{}
|
|
|
+ req, err := http.NewRequest("POST", e.SmsInfo.Url, strings.NewReader(string(jsonData)))
|
|
|
+ if err != nil {
|
|
|
+ e.Errs["sms post build error"] = err
|
|
|
+ return e
|
|
|
+ }
|
|
|
+
|
|
|
+ for key, value := range headers {
|
|
|
+ req.Header.Set(key, value)
|
|
|
+ }
|
|
|
+
|
|
|
+ resp, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ e.Errs["sms post do error"] = err
|
|
|
+ return e
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ body, err := io.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ e.Errs["sms body read error"] = err
|
|
|
+ return e
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.Infof("sms response body: %v", string(body))
|
|
|
+ e.JR.AlertStatus = SENDSUCCESS
|
|
|
+ return e
|
|
|
+}
|
|
|
+
|
|
|
+// smsTpl =`
|
|
|
+// 【江苏省电力公司】
|
|
|
+// 所属系统:{{app_alias}}
|
|
|
+// 事件名称:{{events_name}}
|
|
|
+// 事件创建时间:{{date_time}}
|
|
|
+// 事件描述:{{events}}`
|