//数据包逻辑处理 func (this *handle) Handle(sess *tcp.Session, data []byte) { defer func() { if r := recover(); r != nil { lg.Error("Recovered in Handle", r) } }() var err error mt := types.MessageType(data[0]) lg.Info("receive %v %v", types.MessageTypeText[mt], string(data[1:])) if proxy.cfc.IsClosed() { return } switch mt { case types.MESS_POST_METRIC, types.MESS_POST_HOST_CONFIG, types.MESS_POST_HOST_ALIVE: proxy.cfc.Send(data) case types.MESS_GET_HOST_PLUGIN_LIST: proxy.cfc.Send(data) host := types.Host{} if err = host.Decode(data[1:]); err != nil { lg.Error("decode host error ", err.Error()) return } if resp := proxy.PluginList.Get(host.ID); len(resp) > 0 { sess.Send( types.Pack( types.MESS_GET_HOST_PLUGIN_LIST_RESP, &types.GetPluginResp{host.ID, resp}, )) } case types.MESS_GET_HOST_PLUGIN_LIST_RESP: resp := types.GetPluginResp{} if err = resp.Decode(data[1:]); err != nil { lg.Error("decode plugin response error %s", err) return } proxy.PluginList.Set(resp.HostID, resp.Plugins) default: lg.Error("Unknown Option: %v", mt) } }
func (this *ControllerHandle) Handle(sess *tcp.Session, data []byte) { defer func() { if err := recover(); err != nil { lg.Error("Recovered in HandleMessage", err) } }() mt := types.AlarmMessageType(data[0]) switch mt { case types.ALAR_MESS_INSPECTOR_HEARTBEAT: lg.Info("Receive %v %v", types.AlarmMessageTypeText[mt], string(data[1:])) heartbeat := &types.HeartBeat{} if err := heartbeat.Decode(data[1:]); err != nil { lg.Error(err.Error()) return } controller.refreshNode(heartbeat) case types.ALAR_MESS_INSPECTOR_TASK_REQUEST: tasks_resp := getInspectorTask() sess.Send(types.AlarmPack(types.ALAR_MESS_INSPECTOR_TASKS, tasks_resp)) case types.ALAR_MESS_INSPECTOR_RESULT: lg.Info("Receive %v %v", types.AlarmMessageTypeText[mt], string(data[1:])) result := &types.StrategyResult{} if err := result.Decode(data[1:]); err != nil { lg.Error(err.Error()) return } switch result.Priority { case types.PRIORITY_HIGH_LEVEL: controller.highResultPool.PutResult(result) case types.PRIORITY_MIDDLE_LEVEL: controller.lowResultPool.PutResult(result) case types.PRIORITY_LOW_LEVEL: controller.lowResultPool.PutResult(result) default: lg.Error("Unknown priority: %v", result.Priority) } default: lg.Error("Unknown option: %v", mt) } }
//数据包逻辑处理 //TODO: 获取所有插件md5、更新插件 func (this *handle) Handle(sess *tcp.Session, data []byte) { defer func() { if r := recover(); r != nil { lg.Error("Recovered in HandlerMessage", r) } }() var err error mt := types.MessageType(data[0]) lg.Info("receive %v %v", types.MessageTypeText[mt], string(data[1:])) switch mt { //agent上传基础信息, ip hostname agentVersion case types.MESS_POST_HOST_CONFIG: host := &types.Host{} err = host.Decode(data[1:]) if err != nil { lg.Error(err.Error()) sess.Close() return } if h := mydb.GetHost(host.ID); h == nil { //主机不存在 err := mydb.CreateHost(host.ID, host.SN, host.IP, host.Hostname, host.AgentVersion) if err != nil { lg.Error("create host error: %s", err.Error()) } else { lg.Info("create host:%v", host) } } else if h.IP != host.IP || h.Hostname != host.Hostname || h.AgentVersion != host.AgentVersion || h.SN != host.SN { lg.Info("update host: %v->%v", h, host) mydb.UpdateHost(host) } //同步metric case types.MESS_POST_METRIC: cfg := types.MetricConfig{} err = cfg.Decode(data[1:]) if err != nil { lg.Error(err.Error()) sess.Close() } host := mydb.GetHost(cfg.HostID) if host == nil { return } if mydb.MetricIsExists(cfg.HostID, cfg.SeriesData.GetMetric()) { return } if err = mydb.CreateMetric(cfg.HostID, cfg.SeriesData); err != nil { lg.Error("create metric error %v", err.Error()) } //获取需要执行的插件列表 case types.MESS_GET_HOST_PLUGIN_LIST: host := &types.Host{} err = host.Decode(data[1:]) if err != nil { return } sess.Send(types.Pack(types.MESS_GET_HOST_PLUGIN_LIST_RESP, &types.GetPluginResp{HostID: host.ID, Plugins: mydb.GetPlugins(host.ID)}), ) case types.MESS_POST_HOST_ALIVE: cfg := &types.Host{} err = cfg.Decode(data[1:]) if err != nil { lg.Error(err.Error()) sess.Close() } host := mydb.GetHost(cfg.ID) if host == nil { return } mydb.UpdateHost(host) default: lg.Error("Unknown Option: %v", mt) } }