sms.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package handler
  2. import (
  3. "crypto/rand"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "math/big"
  8. "net/http"
  9. "strings"
  10. "time"
  11. "github.com/go-admin-team/go-admin-core/logger"
  12. "github.com/tjfoc/gmsm/sm3"
  13. )
  14. type SingleMsg struct {
  15. ID string `json:"id"`
  16. MsgContent string `json:"msgcontent"`
  17. MsgType string `json:"msgtype"`
  18. PhoneNum string `json:"phonenum"`
  19. }
  20. type SendMsg struct {
  21. SubmitTime string `json:"submittime"`
  22. MqReceipt bool `json:"mqreceipt"`
  23. ExtCode string `json:"extcode"`
  24. MsgList []SingleMsg `json:"msgList"`
  25. }
  26. func SM3(data string) string {
  27. // 这里应该实现SM3算法,由于Go标准库中没有直接支持,这里只是返回一个模拟的字符串
  28. h := sm3.New()
  29. h.Write([]byte(data))
  30. sum := h.Sum(nil)
  31. logger.Debugf("digest value is: %x\n", sum)
  32. return string(sum)
  33. }
  34. func (e *EventHandler) SendMsg() *EventHandler {
  35. logger.Debugf("In sms Send func, %s", e.RP.AppAlias)
  36. //判断该应用是否添加了短信通知
  37. _, ok := e.SmsInfo.AppsGroup[e.RP.AppAlias]
  38. if !ok {
  39. return e
  40. }
  41. logger.Debug("has key......", e.JR.AlertStatus)
  42. //判断是否需要告警
  43. if !(e.JR.AlertStatus == FIRING) {
  44. return e
  45. }
  46. logger.Info("start to send sms...")
  47. appkey := e.SmsInfo.Appkey
  48. appsecret := e.SmsInfo.Appsecret
  49. headers := make(map[string]string)
  50. nonce, err := rand.Int(rand.Reader, big.NewInt(1<<62))
  51. if err != nil {
  52. e.Errs["rander error"] = err
  53. return e
  54. }
  55. timestamp := time.Now().UnixNano() / 1e6
  56. headers["x-system-key"] = appkey
  57. headers["x-system-nonce"] = nonce.String()
  58. headers["x-system-timestamp"] = fmt.Sprintf("%d", timestamp)
  59. headers["x-system-token"] = SM3(appkey + fmt.Sprintf("%d", timestamp) + appsecret + nonce.String())
  60. // set smsContent
  61. // 所属系统:{{app_name}}
  62. // 事件名称:{{events_name}}
  63. // 事件创建时间:{{date_time}}
  64. // 事件描述:{{events}}`
  65. e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{app_name}}", e.RP.AppName, -1)
  66. e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{events_name}}", e.RP.RuleName, -1)
  67. e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{date_time}}", e.RP.CreateTime.Format("2006-01-02 15:04:05"), -1)
  68. e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{events}}", fmt.Sprintf("%s, %d分钟内发生%s%v次, 当前值为: %v",
  69. e.RP.RuleName, e.AC.Interval, e.AC.Condition, e.JR.CompareV, e.JR.CompareV), -1)
  70. logger.Info("sms build: ", e.SmsInfo.SmsTpl)
  71. MsgList := make([]SingleMsg, 0)
  72. phoneList, ok := e.SmsInfo.AppsGroup[e.RP.AppAlias]
  73. if ok {
  74. for id, phone := range phoneList {
  75. ssm := SingleMsg{
  76. ID: fmt.Sprintf("%d", id),
  77. MsgContent: e.SmsInfo.SmsTpl,
  78. MsgType: "",
  79. PhoneNum: phone,
  80. }
  81. MsgList = append(MsgList, ssm)
  82. }
  83. }
  84. sendMsg := SendMsg{
  85. SubmitTime: time.Now().Format("2006-01-02 15:04:05"),
  86. MqReceipt: false,
  87. ExtCode: "",
  88. MsgList: MsgList,
  89. }
  90. jsonData, err := json.Marshal(sendMsg)
  91. if err != nil {
  92. e.Errs["sms json marshal error"] = err
  93. return e
  94. }
  95. client := &http.Client{}
  96. req, err := http.NewRequest("POST", e.SmsInfo.Url, strings.NewReader(string(jsonData)))
  97. if err != nil {
  98. e.Errs["sms post build error"] = err
  99. return e
  100. }
  101. for key, value := range headers {
  102. req.Header.Set(key, value)
  103. }
  104. resp, err := client.Do(req)
  105. if err != nil {
  106. e.Errs["sms post do error"] = err
  107. return e
  108. }
  109. defer resp.Body.Close()
  110. body, err := io.ReadAll(resp.Body)
  111. if err != nil {
  112. e.Errs["sms body read error"] = err
  113. return e
  114. }
  115. logger.Infof("sms response body: %v", string(body))
  116. e.JR.AlertStatus = SENDSUCCESS
  117. return e
  118. }
  119. // smsTpl =`
  120. // 【江苏省电力公司】
  121. // 所属系统:{{app_alias}}
  122. // 事件名称:{{events_name}}
  123. // 事件创建时间:{{date_time}}
  124. // 事件描述:{{events}}`