item.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package api
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. "github.com/710leo/urlooker/dataobj"
  7. "github.com/710leo/urlooker/modules/web/g"
  8. "github.com/710leo/urlooker/modules/web/model"
  9. "github.com/710leo/urlooker/modules/web/sender"
  10. "github.com/710leo/urlooker/modules/web/utils"
  11. )
  12. func (this *Web) SendResult(req dataobj.SendResultReq, reply *string) error {
  13. for _, arg := range req.CheckResults {
  14. itemStatus := model.ItemStatus{
  15. Ip: req.Ip,
  16. Sid: arg.Sid,
  17. RespTime: arg.RespTime,
  18. RespCode: arg.RespCode,
  19. PushTime: arg.PushTime,
  20. Result: arg.Status,
  21. }
  22. relSidIp := model.RelSidIp{
  23. Sid: arg.Sid,
  24. Ip: req.Ip,
  25. Ts: time.Now().Unix(),
  26. }
  27. err := relSidIp.Save()
  28. if err != nil {
  29. log.Println("save sid_ip error:", err)
  30. *reply = "save sid_ip error:" + err.Error()
  31. return nil
  32. }
  33. err = itemStatus.Save()
  34. if err != nil {
  35. log.Println("save item error:", err)
  36. *reply = "save item error:" + err.Error()
  37. return nil
  38. }
  39. if g.Config.Alarm.Enable {
  40. node, err := sender.NodeRing.GetNode(itemStatus.PK())
  41. if err != nil {
  42. log.Println("error:", err)
  43. *reply = "get node error:" + err.Error()
  44. return nil
  45. }
  46. Q := sender.SendQueues[node]
  47. isSuccess := Q.PushFront(itemStatus)
  48. if !isSuccess {
  49. log.Println("push itemStatus error:", itemStatus)
  50. *reply = "push itemStatus error"
  51. return nil
  52. }
  53. }
  54. }
  55. if g.Config.Falcon.Enable {
  56. if len(req.CheckResults) > 0 {
  57. utils.PushFalcon(g.Config.Falcon.Addr, req.CheckResults, req.Ip)
  58. }
  59. }
  60. if g.Config.Prom.Enable {
  61. if len(req.CheckResults) > 0 {
  62. utils.PushPromethues(g.Config.Prom.Addr, req.CheckResults, req.Ip)
  63. }
  64. }
  65. if g.Config.Statsd.Enable {
  66. for _, detectRes := range req.CheckResults {
  67. metric := fmt.Sprintf("api_status_%d_%s_%s_", detectRes.Sid, detectRes.Target, req.Ip)
  68. err := utils.PushStatsd(metric, detectRes.Status)
  69. if err != nil {
  70. log.Println("push Statsd err:", err, detectRes)
  71. }
  72. metric = fmt.Sprintf("api_resptime_%d_%s_%s_", detectRes.Sid, detectRes.Target, req.Ip)
  73. err = utils.PushStatsd(metric, int64(detectRes.RespTime))
  74. if err != nil {
  75. log.Println("push Statsd err:", err, detectRes)
  76. }
  77. }
  78. }
  79. *reply = ""
  80. return nil
  81. }
  82. func (this *Web) GetItem(idc string, resp *dataobj.GetItemResponse) error {
  83. items, exists := g.DetectedItemMap.Get(idc)
  84. if !exists {
  85. resp.Message = "no found item assigned to " + idc
  86. }
  87. resp.Data = items
  88. return nil
  89. }