Exemplo n.º 1
0
func toReadable(node *topo.Node, state string) *RNode {
	if node == nil {
		return nil
	}

	var id string
	if context.Display == "simple" {
		id = node.Id[:6]
	} else {
		id = node.Id
	}
	n := &RNode{
		State:    state,
		Id:       id,
		ParentId: node.ParentId,
		Tag:      node.Tag,
		Role:     "S",
		Fail:     "OK",
		Mode:     "--",
		Addr:     fmt.Sprintf("%s:%d", node.Ip, node.Port),
		Keys:     node.SummaryInfo.Keys,
		Link:     node.SummaryInfo.MasterLinkStatus,
		QPS:      node.SummaryInfo.InstantaneousOpsPerSec,
	}

	if node.Role == "master" {
		n.Role = "M"
	}
	if node.Fail {
		n.Fail = "Fail"
	}
	if node.Readable && node.Writable {
		n.Mode = "rw"
	}
	if node.Readable && !node.Writable {
		n.Mode = "r-"
	}
	if !node.Readable && node.Writable {
		n.Mode = "-w"
	}
	if node.IsMaster() {
		n.Link = "-"
	}
	n.UsedMemory = fmt.Sprintf("%0.2fG", float64(node.SummaryInfo.UsedMemory)/1024.0/1024.0/1024.0)
	n.NetIn = fmt.Sprintf("%.2fKbps", node.SummaryInfo.InstantaneousInputKbps)
	n.NetOut = fmt.Sprintf("%.2fKbps", node.SummaryInfo.InstantaneousOutputKbps)
	n.Repl = fmt.Sprintf("%d", node.ReplOffset)
	return n
}
Exemplo n.º 2
0
func (self *Inspector) initClusterTopo(seed *topo.Node) (*topo.Cluster, error) {
	resp, err := redis.ClusterNodesInRegion(seed.Addr(), self.LocalRegion)
	if err != nil && strings.HasPrefix(err.Error(), "ERR Wrong CLUSTER subcommand or number of arguments") {
		//server version do not support 'cluster nodes extra [region]'
		resp, err = redis.ClusterNodes(seed.Addr())
	}
	if err != nil {
		return nil, err
	}

	cluster := topo.NewCluster(self.LocalRegion)

	var summary topo.SummaryInfo
	var nodeidx *topo.Node
	var cnt int

	lines := strings.Split(resp, "\n")
	cnt = 0
	for _, line := range lines {
		if strings.HasPrefix(line, "# ") {
			summary.ReadLine(line)
			continue
		}
		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}
		node, myself, err := self.buildNode(line)
		if err == ErrNodeInHandShake || err == ErrNodeNoAddr {
			continue
		}
		// Fix 'cluster nodes extra' & 'cluster nodes extra region' compatiable
		if node.Region != self.LocalRegion {
			continue
		}
		if err != nil {
			return nil, err
		}
		if node.Ip == "127.0.0.1" {
			node.Ip = seed.Ip
		}
		// 遇到myself,读取该节点的ClusterInfo
		if myself {
			info, err := redis.FetchClusterInfo(node.Addr())
			if err != nil {
				return nil, err
			}
			node.ClusterInfo = info
			node.SummaryInfo = summary
		}
		cluster.AddNode(node)
		nodeidx = node
		cnt++
	}
	if cnt == 1 {
		if nodeidx.IsMaster() && len(nodeidx.Ranges) == 0 {
			glog.Infof("Node %s is free node", nodeidx.Addr())
			nodeidx.SetFree(true)
		}
	}

	return cluster, nil
}