Пример #1
0
func (this *NetCollect) SendConfig2CFC() {
	for {
		if this.cfc.IsClosed() {
			goto sleep
		}
		for _, s := range this.switchs {
			if s.Hostname == "" || s.Hostname == "Unknown" {
				continue
			}
			h := &types.Host{
				ID:           s.ID,
				IP:           s.IP,
				Hostname:     s.Hostname,
				AgentVersion: AgentVersion,
			}
			//config
			this.cfc.Send(
				types.Pack(
					types.MESS_POST_HOST_CONFIG,
					h,
				))
			//heartbeat
			this.cfc.Send(
				types.Pack(
					types.MESS_POST_HOST_ALIVE,
					h,
				))
		}
	sleep:
		time.Sleep(time.Minute * 1)
	}
}
Пример #2
0
func (this *Agent) SendTSD2Repeater() {
	go func() {
		var (
			//tsd types.TimeSeriesData
			pk string
		)
		tags := map[string]string{"uuid": this.ID(), "hostname": this.Hostname()}
		for {
			if this.repeater.IsClosed() {
				time.Sleep(time.Second * 5)
				continue
			}
			select {
			case tsd, ok := <-this.SendChan:
				pk = tsd.PK()
				curr := tsd.Value
				history, ok := this.tsdHistory[pk]
				if ok {
					switch tsd.DataType {
					case "GAUGE", "gauge":
					case "COUNTER", "counter":
						tsd.Value = (tsd.Value - history) / float64(tsd.Cycle)
					case "DRIVER", "driver":
						tsd.Value = tsd.Value - history
						if tsd.Value < 0 {
							continue
						}
					default:
						lg.Error("%s DataType is illegal", tsd)
						continue
					}
					//如果运算后的值小于0,则取当前值
					if tsd.Value < 0 {
						tsd.Value = curr
					}
					if tsd.Tags == nil {
						tsd.Tags = tags
					} else {
						for k, v := range tags {
							tsd.Tags[k] = v
						}
					}
					this.repeater.Send(
						types.Pack(types.MESS_POST_TSD,
							&tsd,
						))
					lg.Info("sender to repeater %s", tsd)
				} else {
					this.cfc.Send(
						types.Pack(types.MESS_POST_METRIC,
							&types.MetricConfig{this.ID(), tsd},
						))
					lg.Info("sender to cfc %s", tsd)
				}
				this.tsdHistory[pk] = curr
			}
		}
	}()
}
Пример #3
0
func (this *Agent) SendHostAlive2CFC() {
	go func() {
		for {
			if this.cfc.IsClosed() {
				time.Sleep(time.Second * 10)
				continue
			}
			this.cfc.Send(types.Pack(types.MESS_POST_HOST_ALIVE, NewHostConfig()))
			time.Sleep(time.Minute * 1)
		}
	}()
}
Пример #4
0
func (this *NetCollect) SendMetri2CFC() {
	time.Sleep(time.Second * time.Duration(GlobalConfig.COLLECT_INTERVAL))
	for {
		if this.cfc.IsClosed() {
			time.Sleep(time.Second * 30)
			continue
		}
		select {
		case tsd, ok := <-this.metricBuffer:
			if ok {
				this.cfc.Send(types.Pack(types.MESS_POST_METRIC, tsd))
				lg.Info("sender to cfc %s", tsd)
			}
		}
	}
}
Пример #5
0
func (this *repeaterBackend) Write(data *types.TimeSeriesData) error {
	if this.session.IsClosed() {
		return errors.New("backend session is closed.")
	}
	head := make([]byte, 4)
	var buf bytes.Buffer
	b := types.Pack(types.MESS_POST_TSD, data)
	binary.BigEndian.PutUint32(head, uint32(len(b)))
	binary.Write(&buf, binary.BigEndian, head)
	binary.Write(&buf, binary.BigEndian, b)
	if _, err := this.session.Write(buf.Bytes()); err != nil {
		this.session.Close()
		return err
	}
	return nil
}
Пример #6
0
func (this *Agent) GetPluginList() {
	go func() {
		time.Sleep(time.Second)
		for {
			if agent.cfc.IsClosed() {
				goto sleep
			}
			agent.cfc.Send(
				types.Pack(
					types.MESS_GET_HOST_PLUGIN_LIST,
					NewHostConfig()))
		sleep:
			time.Sleep(time.Minute * GetHostPluginListInterval)
		}
	}()
}
Пример #7
0
func (this *Agent) SendConfig2CFC() {
	go func() {
		time.Sleep(time.Second)
		for {
			if this.cfc.IsClosed() {
				goto sleep
			}
			this.cfc.Send(
				types.Pack(types.MESS_POST_HOST_CONFIG,
					NewHostConfig(),
				))
		sleep:
			time.Sleep(time.Minute * SendHostConfigInterval)
		}
	}()
}
Пример #8
0
func (this *NetCollect) SendTSD2Repeater() {
	go func() {
		for {
			if this.repeater.IsClosed() {
				time.Sleep(time.Second * 5)
				continue
			}
			select {
			case tsd, ok := <-this.tsdBuffer:
				if ok {
					this.repeater.Send(
						types.Pack(types.MESS_POST_TSD,
							tsd,
						))
					lg.Info("sender to repeater %s", tsd)
				}
			}
		}
	}()
}
Пример #9
0
//数据包逻辑处理
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)
	}
}
Пример #10
0
//数据包逻辑处理
//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)
	}
}