alarm_msg.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package alarm_msg
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "eta/eta_forum_task/utils"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. )
  12. // SendAlarmMsg
  13. // projectName-项目名称
  14. // runMode-运行模式
  15. // msgBody-消息内容
  16. // level:消息基本,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息
  17. func SendAlarmMsg(msgBody string, level int) {
  18. utils.FileLog.Info(msgBody)
  19. if utils.AlarmMsgUrl == `` {
  20. return
  21. }
  22. params := make(map[string]interface{})
  23. params["ProjectName"] = utils.APP_NAME_CN
  24. params["RunMode"] = utils.RunMode
  25. params["MsgBody"] = msgBody
  26. params["Level"] = level
  27. param, err := json.Marshal(params)
  28. if err != nil {
  29. utils.FileLog.Info("SendAlarmMsg json.Marshal Err:" + err.Error())
  30. return
  31. }
  32. utils.FileLog.Info(string(param))
  33. _, _ = Post(utils.AlarmMsgUrl, string(param), "application/json")
  34. }
  35. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  36. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  37. if len(params) > 0 && params[0] != "" {
  38. contentType = params[0]
  39. }
  40. sTime := time.Now()
  41. resp, err := http.Post(url,
  42. contentType,
  43. strings.NewReader(postData))
  44. eTime := time.Now()
  45. t := eTime.Sub(sTime).Nanoseconds() / 1000000
  46. if err != nil {
  47. return nil, err
  48. }
  49. defer resp.Body.Close()
  50. b, err := ioutil.ReadAll(resp.Body)
  51. fmt.Println(t, "ms", "POST", "URL", url, "DATA", postData, "RESPONSE", string(b))
  52. return b, err
  53. }
  54. func Post(url, postData string, params ...string) ([]byte, error) {
  55. if strings.HasPrefix(url, "https://") {
  56. return HttpsPost(url, postData, params...)
  57. } else {
  58. return HttpPost(url, postData, params...)
  59. }
  60. }
  61. func HttpsPost(url, postData string, params ...string) ([]byte, error) {
  62. body := ioutil.NopCloser(strings.NewReader(postData))
  63. tr := &http.Transport{
  64. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  65. DisableCompression: true,
  66. }
  67. client := &http.Client{Transport: tr}
  68. req, err := http.NewRequest("POST", url, body)
  69. if err != nil {
  70. return nil, err
  71. }
  72. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  73. if len(params) > 0 && params[0] != "" {
  74. contentType = params[0]
  75. }
  76. req.Header.Set("Content-Type", contentType)
  77. sTime := time.Now()
  78. resp, err := client.Do(req)
  79. eTime := time.Now()
  80. t := eTime.Sub(sTime).Nanoseconds() / 1000000
  81. if err != nil {
  82. return nil, err
  83. }
  84. defer resp.Body.Close()
  85. b, err := ioutil.ReadAll(resp.Body)
  86. fmt.Println(t, "ms", "POST", "URL", url, "DATA", postData, "RESPONSE", string(b))
  87. return b, err
  88. }