示例#1
0
//syncRedisBaseInfo 同步redis性能信息
func syncRedisBaseInfo(res *sys.Redises, conf *config.Config) {
	// sync redis info
	go func(conf *config.Config) {
		rs := res.GetReidsInfo(conf)
		returnServer["info"] = rs
		returnServer["type"] = "redisinfo"
		info, err := json.Marshal(returnServer)
		if err != nil {
			log.Println("syncRedisBaseInfo 获取redis信息执行错误", err)
		} else {
			server.SendInfo(conf.Collector.CollectUrl, conf.Collector.AcceptType, conf.Collector.ConnTimeout, string(info))
		}
	}(conf)
}
示例#2
0
func (s *SocketServer) handlerConn(conn net.Conn, r *sys.Redises) {
	defer func() {
		if err := recover(); err != nil {
			log.Println("[server/msocket.go:handlerConn] error", err)
			fmt.Println("[msocket.go:handlerConn] recover error: :", err)
		}
	}()
	//conn.SetDeadline(time.Now().Add(5 * time.Second))
	//conn.SetReadDeadline(time.Now().Add(2 * time.Second))
	//conn.SetWriteDeadline(time.Now().Add(2 * time.Second))
	defer conn.Close()
	cmdbytes := make([]byte, 100)
	cmdstr := ""
RL:
	for {
		readLen, err := conn.Read(cmdbytes)
		if err != nil {
			fmt.Println("[msocket.go:handlerConn]receive redis cmd error: :", string(cmdbytes), " error:", err)
			break
		}
		if readLen == 0 {
			break // connection already closed by client
		}
		//####################需要特殊处理字符串,找到每一个字符,然后累加。####################
		for i := 0; i < len(cmdbytes); i++ {
			if cmdbytes[i] == 0 {
				break
			} else {
				cmdstr += string(cmdbytes[i])
			}
		}
		//判断结尾字符串
		var cstemp []string
		if strings.Contains(cmdstr, "\r\n") {
			cstemp = strings.Split(cmdstr, "\r\n")
		}
		if len(cstemp) == 0 {
			break
		} else if len(cstemp[0]) < 1 {
			break
		}
		switch cstemp[0][0] {
		case 'T':
			//发送给goroutine 后台执行迁移操作,如有错发错误邮件
			sys.CommandChan <- cstemp[0]
			conn.Write([]byte(`{"success":"done"}`))
			conn.Close()
		case 'Q':
			//查询redis nodes 信息
			cdetail := strings.Split(cstemp[0], "@")
			if len(cdetail) < 2 || strings.Count(cdetail[1], ":") != 1 {
				break RL
			}
			nodesInfoMap := r.GetClusterNodes(cdetail[1])
			rd, er := json.Marshal(nodesInfoMap)
			if er != nil {
				log.Println("转化json 错误---->", nodesInfoMap)
			} else {
				log.Println("====================>", string(rd))
				conn.Write(rd)
			}
			conn.Close()
		case 'G':
			cdetail := strings.Split(cstemp[0], "@")
			if len(cdetail) < 2 || utf8.RuneCountInString(cdetail[1]) < 1 {
				break RL
			}
			ipp := strings.Split(cdetail[1], "|")
			if len(ipp) != 2 {
				break RL
			}
			rs := r.ExecRedisCmd(ipp[0], "get", ipp[1])
			if strings.Contains(rs[0], "MOVED") {
				ipport := findip.FindString(rs[0])
				rs = r.ExecRedisCmd(ipport, "get", ipp[1])
			}
			result := make(map[string]string)
			result[ipp[1]] = rs[0]
			rd, er := json.Marshal(result)
			if er != nil {
				log.Println("====================>", string(rd))
			} else {
				conn.Write(rd)
				conn.Close()
			}
		default:
			conn.Close()
		}
	}
}