monitor.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "go-common/app/admin/main/aegis/model/monitor"
  6. "go-common/library/log"
  7. "time"
  8. )
  9. // MonitorBuzResult 获取业务的监控结果
  10. func (s *Service) MonitorBuzResult(c context.Context, bid int64) (res []*monitor.RuleResultData, err error) {
  11. var (
  12. rules []*monitor.Rule
  13. uids []int64
  14. uNames map[int64]string
  15. statsMap map[int64]*monitor.Stats
  16. min, max int64
  17. )
  18. statsMap = make(map[int64]*monitor.Stats)
  19. if rules, err = s.mysql.MoniBizRules(c, bid); err != nil {
  20. log.Error("s.MonitorResult(%d) error:%v", bid, err)
  21. return
  22. }
  23. for _, rule := range rules {
  24. uids = append(uids, rule.UID)
  25. if min, max, err = s.monitorNotifyTime(rule.RuleConf); err != nil {
  26. log.Error("s.MonitorBuzResult(%d) s.monitorNotifyTime(%+v) error:%v", bid, rule.RuleConf, err)
  27. continue
  28. }
  29. if statsMap[rule.ID], err = s.redis.MoniRuleStats(c, rule.ID, min, max); err != nil {
  30. log.Error("s.redis.MoniRuleStats(%d,%+v) error:%v", rule.ID, rule.RuleConf, err)
  31. err = nil
  32. statsMap[rule.ID] = &monitor.Stats{}
  33. }
  34. }
  35. if uNames, err = s.http.GetUnames(c, uids); err != nil {
  36. log.Error("s.MonitorResult(%d) s.http.ManagerUNames(%v) error:%v", bid, uids, err)
  37. err = nil
  38. }
  39. for _, rule := range rules {
  40. var (
  41. uName string
  42. stats *monitor.Stats
  43. )
  44. if _, ok := uNames[rule.UID]; ok {
  45. uName = uNames[rule.UID]
  46. }
  47. if _, ok := statsMap[rule.ID]; ok {
  48. stats = statsMap[rule.ID]
  49. }
  50. data := &monitor.RuleResultData{
  51. Rule: rule,
  52. User: &monitor.User{
  53. ID: rule.UID,
  54. UserName: uName,
  55. NickName: uName,
  56. },
  57. Stats: stats,
  58. }
  59. res = append(res, data)
  60. }
  61. return
  62. }
  63. // MonitorResultOids 获取
  64. func (s *Service) MonitorResultOids(c context.Context, rid int64) (res map[int64]int, err error) {
  65. var (
  66. min, max int64
  67. rule *monitor.Rule
  68. )
  69. if rule, err = s.mysql.MoniRule(c, rid); err != nil {
  70. log.Error("s.MonitorResultOids(%d) error:%v", rid, err)
  71. return
  72. }
  73. if min, max, err = s.monitorNotifyTime(rule.RuleConf); err != nil {
  74. log.Error("s.MonitorResultOids(%d) s.monitorNotifyTime() error:%v", rid, err)
  75. return
  76. }
  77. return s.redis.MoniRuleOids(c, rid, min, max)
  78. }
  79. // monitorNotifyTime 计算监控报警的score区间
  80. func (s *Service) monitorNotifyTime(conf *monitor.RuleConf) (tFrom, tTo int64, err error) {
  81. now := time.Now().Unix()
  82. if _, ok := conf.NotifyCdt["time"]; !ok {
  83. err = errors.New("配置的 NotifyCdt 中不存在 time")
  84. return
  85. }
  86. timeCdt := conf.NotifyCdt["time"].Value
  87. compCdt := conf.NotifyCdt["time"].Comp
  88. switch compCdt {
  89. case monitor.CompGT:
  90. tFrom = 0
  91. tTo = now - timeCdt
  92. case monitor.CompLT:
  93. tFrom = now - timeCdt
  94. tTo = now
  95. default:
  96. err = errors.New("配置的 NotifyCdt 中 comparison 不合法: " + compCdt)
  97. return
  98. }
  99. return
  100. }