alarm.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "go-common/app/admin/main/apm/conf"
  8. "go-common/app/admin/main/apm/model/databus"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "net/http"
  12. )
  13. //Alarm ...
  14. // func (s *Service) Alarm(c context.Context, group, action string) (result *databus.AlarmOpen, err error) {
  15. // url := conf.Conf.Alarm.DatabusURL
  16. // var jsonBytes []byte
  17. // body := &struct {
  18. // Action string `json:"Action"`
  19. // PublicKey string `json:"PublicKey"`
  20. // Signature int8 `json:"Signature"`
  21. // Group string `json:"Group"`
  22. // }{
  23. // Action: action,
  24. // PublicKey: conf.Conf.Alarm.DatabusKey,
  25. // Signature: 1,
  26. // Group: group,
  27. // }
  28. // if jsonBytes, err = json.Marshal(body); err != nil {
  29. // log.Error("json.Marshal(body) error(%v)", err)
  30. // return
  31. // }
  32. // req, err := http.NewRequest("POST", url, strings.NewReader(string(jsonBytes)))
  33. // if err != nil {
  34. // err = ecode.RequestErr
  35. // return
  36. // }
  37. // req.Header.Set("Content-Type", "application/json")
  38. // // req.Header.Set("Cookie", cookie)
  39. // result = &databus.AlarmOpen{}
  40. // if err = s.client.Do(c, req, result); err != nil {
  41. // fmt.Printf("result=(%v) error=(%v)", result, err)
  42. // log.Error("Alarm() error(%v)", err)
  43. // err = ecode.RequestErr
  44. // return
  45. // }
  46. // return
  47. // }
  48. //Opsmind ...
  49. func (s *Service) Opsmind(c context.Context, project, group, action, Owners string, percentage, fortime int64, silence bool) (result *databus.Res, err error) {
  50. var scopes []databus.Scope
  51. scopes = append(scopes, databus.Scope{Type: 0, Key: "group", Val: []string{group}})
  52. var owner databus.Owner
  53. owner.Owner = Owners
  54. owner.App = project
  55. url := conf.Conf.Alarm.DatabusURL
  56. body := &struct {
  57. Action string `json:"Action"`
  58. PublicKey string `json:"PublicKey"`
  59. Signature int8 `json:"Signature"`
  60. PolicyID string `json:"PolicyId"`
  61. CateGory string `json:"CateGory"`
  62. Silence bool `json:"Silence"`
  63. Scope []databus.Scope `json:"Scope"`
  64. TriggerName string `json:"TriggerName"`
  65. TriggerOperator string `json:"TriggerOperator"`
  66. TriggerLevel string `json:"TriggerLevel"`
  67. TriggerThreshold int64 `json:"TriggerThreshold"`
  68. TriggerFor int64 `json:"TriggerFor"`
  69. TriggerNoDataFor int64 `json:"TriggerNoDataFor"`
  70. TriggerNotes databus.Owner `json:"TriggerNotes"`
  71. }{
  72. Action: action,
  73. PublicKey: conf.Conf.Alarm.DatabusKey,
  74. Signature: 1,
  75. PolicyID: "3mwipx2caggxc",
  76. CateGory: "Databus",
  77. Silence: silence,
  78. Scope: scopes,
  79. TriggerName: fmt.Sprintf("%s消费落后告警", group),
  80. TriggerOperator: "<",
  81. TriggerLevel: "P3",
  82. TriggerThreshold: percentage,
  83. TriggerFor: fortime,
  84. TriggerNoDataFor: 300,
  85. TriggerNotes: owner,
  86. }
  87. // json.NewEncoder(os.Stdout).Encode(body)
  88. buf := &bytes.Buffer{}
  89. err = json.NewEncoder(buf).Encode(body)
  90. if err != nil {
  91. log.Error("json.Marshal(body) error(%v)", err)
  92. return
  93. }
  94. req, err := http.NewRequest("POST", url, buf)
  95. if err != nil {
  96. err = ecode.RequestErr
  97. return
  98. }
  99. req.Header.Set("Content-Type", "application/json")
  100. // req.Header.Set("Cookie", cookie)
  101. result = &databus.Res{}
  102. if err = s.client.Do(c, req, result); err != nil {
  103. log.Error("Alarm() error(%v)", err)
  104. err = ecode.RequestErr
  105. return
  106. }
  107. return
  108. }
  109. // OpsmindRemove ...
  110. func (s *Service) OpsmindRemove(c context.Context, adjustid, action string) (result *databus.Res, err error) {
  111. url := conf.Conf.Alarm.DatabusURL
  112. body := &struct {
  113. Action string `json:"Action"`
  114. PublicKey string `json:"PublicKey"`
  115. Signature int8 `json:"Signature"`
  116. PolicyID string `json:"PolicyId"`
  117. AdjustID string `json:"AdjustId"`
  118. }{
  119. Action: action,
  120. PublicKey: conf.Conf.Alarm.DatabusKey,
  121. Signature: 1,
  122. PolicyID: "3mwipx2caggxc",
  123. AdjustID: adjustid,
  124. }
  125. // json.NewEncoder(os.Stdout).Encode(body)
  126. buf := &bytes.Buffer{}
  127. err = json.NewEncoder(buf).Encode(body)
  128. if err != nil {
  129. log.Error("json.Marshal(body) error(%v)", err)
  130. return
  131. }
  132. req, err := http.NewRequest("POST", url, buf)
  133. if err != nil {
  134. err = ecode.RequestErr
  135. return
  136. }
  137. req.Header.Set("Content-Type", "application/json")
  138. // req.Header.Set("Cookie", cookie)
  139. result = &databus.Res{}
  140. if err = s.client.Do(c, req, result); err != nil {
  141. log.Error("Alarm() error(%v)", err)
  142. err = ecode.RequestErr
  143. return
  144. }
  145. return
  146. }
  147. //OpsmindQuery ...
  148. func (s *Service) OpsmindQuery(c context.Context, group, action string) (result *databus.ResQuery, err error) {
  149. var query []databus.Query
  150. query = append(query, databus.Query{Key: "group", Val: []string{group}})
  151. url := conf.Conf.Alarm.DatabusURL
  152. body := &struct {
  153. Action string `json:"Action"`
  154. PublicKey string `json:"PublicKey"`
  155. Signature int8 `json:"Signature"`
  156. Query []databus.Query `json:"Query"`
  157. }{
  158. Action: action,
  159. PublicKey: conf.Conf.Alarm.DatabusKey,
  160. Signature: 1,
  161. Query: query,
  162. }
  163. // json.NewEncoder(os.Stdout).Encode(body)
  164. buf := &bytes.Buffer{}
  165. err = json.NewEncoder(buf).Encode(body)
  166. if err != nil {
  167. log.Error("json.Marshal(body) error(%v)", err)
  168. return
  169. }
  170. req, err := http.NewRequest("POST", url, buf)
  171. if err != nil {
  172. err = ecode.RequestErr
  173. return
  174. }
  175. req.Header.Set("Content-Type", "application/json")
  176. // req.Header.Set("Cookie", cookie)
  177. result = &databus.ResQuery{}
  178. if err = s.client.Do(c, req, result); err != nil {
  179. log.Error("Alarm() error(%v)", err)
  180. err = ecode.RequestErr
  181. }
  182. return
  183. }