apm_white_list_v2.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package containers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "github.com/coroot/coroot-node-agent/common"
  7. "github.com/coroot/coroot-node-agent/config"
  8. "github.com/coroot/coroot-node-agent/utils"
  9. . "github.com/coroot/coroot-node-agent/utils/modelse"
  10. log "github.com/sirupsen/logrus"
  11. "os"
  12. "path"
  13. )
  14. func (r *Registry) getWhiteListAll() []WhiteSettingInfo {
  15. return r.whiteListRules["all"]
  16. }
  17. func (r *Registry) setWhiteListV2(whiteData WhiteDataV2) {
  18. whiteListMap := make(WhiteListMap)
  19. code := "all"
  20. for _, setting := range whiteData.SettingList {
  21. setting.Filters = setting.ProcessKey
  22. whiteListMap[code] = append(whiteListMap[code], setting)
  23. }
  24. r.whiteListRules = whiteListMap
  25. }
  26. func (r *Registry) pullWhiteListV2() (bool, error) {
  27. if r.isFusing {
  28. return false, nil
  29. }
  30. if common.IsOpenFilter() {
  31. return false, nil
  32. }
  33. // 若配置文件中存在 agent.rules,则直接使用,不走接口
  34. if agentRules := config.Get().AgentRules(); len(agentRules) > 0 {
  35. whiteData := buildWhiteDataFromAgentRules(agentRules)
  36. r.setWhiteListV2(whiteData)
  37. saveRule(whiteData)
  38. return true, nil
  39. }
  40. nodeInfo := r.nodeInfo.GetNodeInfo()
  41. if nodeInfo == nil {
  42. return false, fmt.Errorf("could not find node info")
  43. }
  44. whiteListReq := WhiteListReq{
  45. HostId: nodeInfo.HostID,
  46. AccountId: nodeInfo.AccountID,
  47. WhiteType: 2,
  48. }
  49. whiteData, err := r.connServer.WhiteListV2(whiteListReq)
  50. if err != nil {
  51. log.Errorf("report WhiteList info error is %v.", err)
  52. return false, err
  53. }
  54. //fmt.Println(r.whiteLastUpdatedTime)
  55. //fmt.Println(whiteData.LastUpdatedTime)
  56. // 后端切换主机与全局规则时,时间不会变化
  57. // 不用更新
  58. //if r.whiteLastUpdatedTime == whiteData.LastUpdatedTime {
  59. // return false, nil
  60. //}
  61. //
  62. //// 更新时间
  63. r.whiteLastUpdatedTime = whiteData.LastUpdatedTime
  64. r.setWhiteListV2(whiteData)
  65. saveRule(whiteData)
  66. return true, nil
  67. }
  68. func buildWhiteDataFromAgentRules(rules []config.AgentRule) WhiteDataV2 {
  69. var settings []WhiteSettingInfo
  70. for _, rule := range rules {
  71. settings = append(settings, WhiteSettingInfo{
  72. AppName: rule.AppName,
  73. ProcessKey: rule.Filters,
  74. })
  75. }
  76. return WhiteDataV2{SettingList: settings, LastUpdatedTime: int(time.Now().Unix())}
  77. }
  78. func saveRule(runtimeApps WhiteDataV2) {
  79. appStr, _ := json.Marshal(runtimeApps)
  80. dumpPath := path.Join(utils.GetDefaultRuntimePath(), "memdump")
  81. err := os.MkdirAll(dumpPath, 0755)
  82. if err != nil {
  83. log.Error(err)
  84. }
  85. fileName := path.Join(dumpPath, "rule.snap")
  86. err = os.WriteFile(fileName, appStr, 0644)
  87. if err != nil {
  88. log.Error(err)
  89. }
  90. }