Explorar el Código

Fixed #TASK_QT-9810 主机注册

Carl hace 1 año
padre
commit
305b7d7216

+ 2 - 2
containers/apm_register_app.go

@@ -17,7 +17,7 @@ func (c *Container) RegisterAppInfo(r *Registry, pid uint32) error {
 	whiteAppName := c.WhiteSettingInfo.AppName
 	if originAppName != whiteAppName {
 		if c.AppInfo.CodeType.IsUnknownCode() || c.AppInfo.CodeType.IsWaitCheck() {
-			return fmt.Errorf("[Register] Unknown app code,Wait buildIDs.")
+			return fmt.Errorf("[register app] Unknown app code,Wait buildIDs.")
 		}
 		//c.AppInfo.AppId, err = utils.BuildInt64ID(whiteAppName).ToInt64()
 		c.AppInfo.AppIdHash.IntVal, err = utils.BuildInt64ID(whiteAppName).ToInt64()
@@ -41,7 +41,7 @@ func (c *Container) RegisterAppInfo(r *Registry, pid uint32) error {
 			App_type:    1,
 			HostId:      hostId,
 		}
-		klog.Infof("[register] Register App: %s.", registerAppReq.String())
+		klog.Infof("[register app] Register App req: %s.", registerAppReq.String())
 		err = r.connServer.RegisterApp(registerAppReq)
 		if err != nil {
 			klog.WithError(err).Errorf("[register] Failed Register App %s.", registerAppReq.String())

+ 32 - 0
containers/apm_register_host.go

@@ -0,0 +1,32 @@
+package containers
+
+import (
+	"fmt"
+	. "github.com/coroot/coroot-node-agent/utils/modelse"
+	"runtime"
+)
+
+func (r *Registry) RegisterHost() error {
+	// 注册
+	nodeInfo := r.nodeInfo.GetNodeInfo()
+	if nodeInfo == nil {
+		return fmt.Errorf("could not find node info")
+	}
+	req := RegisterHostReq{
+		LicenseKey:    nodeInfo.LicenseKey,
+		HostId:        nodeInfo.HostID,
+		HostIp:        nodeInfo.HostIp,
+		HostName:      "smartagent",
+		HostNameAlias: nodeInfo.Hostname,
+		HostType:      1,
+		Version:       "0.0.1",
+		SysVersion:    runtime.GOOS + " " + runtime.GOARCH,
+		SystemUuid:    nodeInfo.SystemUUID,
+		Ebpf:          true,
+	}
+	err := r.connServer.RegisterHost(req)
+	if err != nil {
+		return err
+	}
+	return nil
+}

+ 6 - 1
containers/apm_white_list.go

@@ -1,6 +1,7 @@
 package containers
 
 import (
+	"fmt"
 	"github.com/coroot/coroot-node-agent/common"
 	"github.com/coroot/coroot-node-agent/utils"
 	. "github.com/coroot/coroot-node-agent/utils/modelse"
@@ -30,8 +31,12 @@ func (r *Registry) getWhiteList() (bool, error) {
 	}
 	//hostId, _ := utils.GetHostID()
 	//fmt.Println(hostId)
+	nodeInfo := r.nodeInfo.GetNodeInfo()
+	if nodeInfo == nil {
+		return false, fmt.Errorf("could not find node info")
+	}
 	whiteListReq := WhiteListReq{
-		HostId:    10154813500555812,
+		HostId:    nodeInfo.HostID,
 		AccountId: utils.GetAccountID(),
 	}
 	whiteData, err := r.connServer.WhiteList(whiteListReq)

+ 5 - 3
containers/container_apm.go

@@ -499,7 +499,7 @@ func (c *Container) verifyAttachConditions(r *Registry, pid uint32) bool {
 	if p != nil && c.checkEventReady() {
 		codeType := c.GetCodeTypeFromCache(pid)
 		if codeType.IsUnknownCode() {
-			klog.WithField("pid", pid).Infof("[verify] unknown language.")
+			klog.WithField("pid", pid).Debug("[verify] unknown language.")
 			return false
 		}
 		cmdline := p.GetCmdline()
@@ -507,8 +507,8 @@ func (c *Container) verifyAttachConditions(r *Registry, pid uint32) bool {
 			return false
 		}
 		whiteListByCode := r.getWhiteListByCodeType(codeType)
-		klog.WithField("pid", pid).WithField("codeType", codeType.String()).
-			Infof("[verify] white list %v", whiteListByCode)
+		//klog.WithField("pid", pid).WithField("codeType", codeType.String()).
+		//	Infof("[verify] white list %v", utils.ToString(whiteListByCode))
 		// 当前语言的白名单规则
 		for _, setting := range whiteListByCode {
 			ruleVal := setting.Filters
@@ -519,8 +519,10 @@ func (c *Container) verifyAttachConditions(r *Registry, pid uint32) bool {
 			if strings.Contains(cmdline, ruleVal) {
 				c.WhiteSettingInfo = setting
 				klog.WithField("pid", pid).
+					WithField("codeType", codeType.String()).
 					WithField("ruleVal", ruleVal).
 					WithField("cmdline", cmdline).
+					WithField("white list", utils.ToString(whiteListByCode)).
 					Infoln("[verify] check successful.")
 				return true
 			}

+ 11 - 3
containers/registry.go

@@ -59,6 +59,7 @@ type Registry struct {
 	whiteListRules       WhiteListMap
 	whiteLastUpdatedTime int
 	connServer           ServerWorker
+	nodeInfo             *NodeInfoT
 }
 
 var (
@@ -66,7 +67,7 @@ var (
 	uprobesMap map[string]tracer.Uprobe
 )
 
-func NewRegistry(reg prometheus.Registerer, kernelVersion string, processInfoCh chan<- ProcessInfo) (*Registry, error) {
+func NewRegistry(reg prometheus.Registerer, kernelVersion string, nodeInfo *NodeInfoT, processInfoCh chan<- ProcessInfo) (*Registry, error) {
 	ns, err := proc.GetSelfNetNs()
 	if err != nil {
 		return nil, err
@@ -123,13 +124,14 @@ func NewRegistry(reg prometheus.Registerer, kernelVersion string, processInfoCh
 
 		tracer:         ebpftracer.NewTracer(kernelVersion, *flags.DisableL7Tracing, *flags.DisableE2ETracing, *flags.DisableStackTracing),
 		whiteListRules: make(WhiteListMap),
+		nodeInfo:       nodeInfo,
 	}
 	// 初始化软负载集群节点
 	proxyClient, clientErr := NewProxyClient(*flags.ConfigEndpoint, false)
 	if clientErr == nil {
 		// 负载健康检测
 		try.Go(proxyClient.CheckEndpoints, CatchFn)
-		log.Infof("New Proxy Client success.config_server is [%s]", "")
+		log.Infof("New Proxy Client success.config_server is [%s]", *flags.ConfigEndpoint)
 	} else {
 		log.WithError(clientErr).Errorf("NewProxyClient error, Please check [export CONFIG_ENDPOINT=ip:port]")
 		return nil, clientErr
@@ -140,6 +142,12 @@ func NewRegistry(reg prometheus.Registerer, kernelVersion string, processInfoCh
 		log.Errorf("init connServer error:%s.", err)
 		return nil, err
 	}
+	// Register Host
+	err = r.RegisterHost()
+
+	if err != nil {
+		return nil, err
+	}
 
 	if err = reg.Register(r); err != nil {
 		return nil, err
@@ -215,7 +223,7 @@ func (r *Registry) handleEvents(ch <-chan ebpftracer.Event) {
 						c.detachUprobes(pid)
 					}
 				}
-				if c.AppInfo.AppName != "" {
+				if c != nil && c.AppInfo.AppName != "" {
 					detail := AppStatusInfo{
 						Pid:        pid,
 						ProcName:   c.containerName,

+ 6 - 3
main.go

@@ -149,7 +149,7 @@ func main() {
 
 	whitelistNodeExternalNetworks()
 
-	machineId := machineID()
+	machineId := utils.MachineID()
 	tracing.Init(machineId, hostname, version)
 	logs.Init(machineId, hostname, version)
 
@@ -163,8 +163,11 @@ func main() {
 	}
 
 	//processInfoCh := profiling.Init(machineId, hostname)
-
-	cr, err := containers.NewRegistry(registerer, kv, nil)
+	nodeInfo, err := node.NewNodeInfo(hostname, kv, machineId)
+	if err != nil || nodeInfo == nil {
+		log.Fatalln(err)
+	}
+	cr, err := containers.NewRegistry(registerer, kv, nodeInfo, nil)
 
 	if err != nil {
 		log.Fatalln(err)

+ 23 - 0
node/apm_host_info.go

@@ -0,0 +1,23 @@
+package node
+
+import (
+	"github.com/coroot/coroot-node-agent/utils"
+	. "github.com/coroot/coroot-node-agent/utils/modelse"
+)
+
+func NewNodeInfo(name, kv, machineId string) (*NodeInfoT, error) {
+	ip, err := utils.GetRealIp()
+	if err != nil {
+		return nil, err
+	}
+	n := &NodeInfoT{
+		Hostname:      name,
+		HostIp:        ip,
+		KernelVersion: kv,
+		SystemUUID:    machineId,
+		HostID:        utils.GetIntHostID(),
+		AccountID:     utils.GetAccountID(),
+		LicenseKey:    "J45Engw88NeHUZ4Q7qNsK8L47FTH**QvgW113IEnsNaBNMR5zZ**oj/g!!!!",
+	}
+	return n, nil
+}

+ 1 - 1
utils/id.go

@@ -92,7 +92,7 @@ func MachineID() string {
 		if err != nil {
 			continue
 		}
-		id := strings.TrimSpace(strings.Replace(string(payload), "-", "", -1))
+		id := strings.TrimSpace(strings.Replace(string(payload), "\n", "", -1))
 		return id
 	}
 	return ""

+ 18 - 0
utils/ip.go

@@ -0,0 +1,18 @@
+package utils
+
+import "net"
+
+func GetRealIp() (string, error) {
+	conn, err := net.Dial("udp", "8.8.8.8:8")
+	if err != nil {
+		panic(err)
+	}
+	defer conn.Close()
+	localAddr := conn.LocalAddr().(*net.UDPAddr)
+	address := localAddr.String()
+	ipAddress, _, err := net.SplitHostPort(address)
+	if err != nil {
+		return "", err
+	}
+	return ipAddress, nil
+}

+ 13 - 1
utils/modelse/models.go

@@ -3,7 +3,6 @@ package modelse
 import (
 	"encoding/json"
 	"fmt"
-
 	log "github.com/sirupsen/logrus"
 )
 
@@ -99,6 +98,19 @@ var (
 	InvaildTask = 500002
 )
 
+type RegisterHostReq struct {
+	LicenseKey    string `json:"license_key"`
+	HostId        int64  `json:"host_id"`
+	HostIp        string `json:"host_ip"`
+	HostName      string `json:"host_name"`
+	HostNameAlias string `json:"host_name_alias"`
+	HostType      int    `json:"host_type"`
+	Version       string `json:"version"`
+	SysVersion    string `json:"sys_version"`
+	SystemUuid    string `json:"system_uuid"`
+	Ebpf          bool   `json:"ebpf"`
+}
+
 type RegisterAppReq struct {
 	AppId       int64  `json:"appId"`
 	AppName     string `json:"appName"`

+ 26 - 0
utils/modelse/node.go

@@ -0,0 +1,26 @@
+package modelse
+
+import (
+	"sync"
+)
+
+type NodeInfoT struct {
+	Hostname      string
+	HostIp        string
+	KernelVersion string
+	SystemUUID    string
+	HostID        int64
+	AccountID     int
+	LicenseKey    string
+	Lock          sync.RWMutex
+}
+
+type NodeInfoInterface interface {
+	GetNodeInfo() *NodeInfoT
+}
+
+func (n *NodeInfoT) GetNodeInfo() *NodeInfoT {
+	n.Lock.Lock()
+	defer n.Lock.Unlock()
+	return n
+}

+ 8 - 0
utils/util.go

@@ -906,3 +906,11 @@ func getNSpidValue(filePath string) (int, error) {
 
 	return 0, errors.New("NSpid value not found!")
 }
+
+func ToString(v interface{}) string {
+	b, err := json.Marshal(v)
+	if err != nil {
+		return err.Error()
+	}
+	return string(b)
+}

+ 24 - 7
utils/worker/serverWorker.go

@@ -3,6 +3,7 @@ package worker
 import (
 	"encoding/json"
 	"fmt"
+	"github.com/coroot/coroot-node-agent/utils"
 	. "github.com/coroot/coroot-node-agent/utils/modelse"
 	log "github.com/sirupsen/logrus"
 	"net/http"
@@ -27,6 +28,8 @@ const (
 type ServerWorker interface {
 	//InstallReport(r ReportRequest) error
 
+	RegisterHost(RegisterHostReq) error
+
 	RegisterApp(RegisterAppReq) error
 
 	WhiteList(WhiteListReq) (WhiteData, error)
@@ -44,27 +47,39 @@ type ServerWorker interface {
 	//GetInfo() (string, string)
 }
 
+func (w *ServerHTTPWorker) RegisterHost(request RegisterHostReq) error {
+	log.Infof("[server register host] request:%v.", utils.ToString(request))
+	result, err := w.requestServer("/v2/app/registerHost", request)
+	log.Infof("[server register host] resp:%v.", string(result))
+	if err != nil {
+		log.WithError(err).Errorf("[server register] Failed RegisterApp request:%v.", utils.ToString(request))
+		return err
+	}
+	return nil
+}
+
 func (w *ServerHTTPWorker) RegisterApp(request RegisterAppReq) error {
-	log.Infof("[register] Register App request:%v.", request.String())
-	_, err := w.requestServer("/v2/app/create", request)
+	log.Infof("[server register app] request:%v.", utils.ToString(request))
+	result, err := w.requestServer("/v2/app/create", request)
+	log.Infof("[server register app] resp data:%v.", string(result))
 	if err != nil {
-		log.WithError(err).Errorf("[register] Failed RegisterApp request:%v.", request.String())
+		log.WithError(err).Errorf("[server register] Failed RegisterApp request:%v.", utils.ToString(request))
 		return err
 	}
 	return nil
 }
 
 func (w *ServerHTTPWorker) WhiteList(request WhiteListReq) (WhiteData, error) {
-	log.Infof("WhiteList request:%v.", request.String())
+	log.Infof("[server whitelist] request:%v.", request.String())
 	response := WhiteData{}
 	result, err := w.requestServer("/api/v1/agent/whitelist", request)
+	log.Infof("[server whitelist] resp data:%v.", string(result))
 	if err != nil {
 		return response, err
 	}
-
 	err = json.Unmarshal(result, &response)
 	if err != nil {
-		log.WithError(err).Errorf("Failed RegisterApp request:%v.", request.String())
+		log.WithError(err).Errorf("[server whitelist] Failed RegisterApp request:%v.", request.String())
 		return response, err
 	}
 	return response, nil
@@ -88,7 +103,8 @@ type ServerHTTPWorker struct {
 	daemonId        string
 	connectList     []string
 	tokenList       []string
-	proxyClient     *Client
+	//nodeInfo 		*NodeInfo
+	proxyClient *Client
 }
 
 func NewServerHTTPWorker() (*ServerHTTPWorker, error) {
@@ -101,6 +117,7 @@ func NewServerHTTPWorker() (*ServerHTTPWorker, error) {
 		mux: new(sync.Mutex),
 	}
 	s.proxyClient, _ = GetProxyClient()
+	//s.nodeInfo,_ = node.GetNodeInfo()
 	return s, nil
 }