Commit 648368d2 authored by jingbo.wang's avatar jingbo.wang

节点信息报表

parent 1d199fdc
......@@ -56,7 +56,6 @@ func (n *Node) queryKey(key string) (int, int, int) {
`);`
sql := fmt.Sprintf(sqlFormat, key, n.serviceName, n.address, n.startTime.UnixNano(), n.endTime.UnixNano())
glog.Debug(sql)
resp, err := query(sql)
if err != nil {
glog.Error("node query key:", err)
......
......@@ -53,7 +53,6 @@ func (p *Path) initCount() {
sql := fmt.Sprintf(sqlGetCount, p.serviceName, p.path, p.startTime.UnixNano(), p.endTime.UnixNano())
count, ok := queryOneAndToInt(sql)
if !ok {
glog.Warn("init count query one:", sql)
return
}
p.count = count
......@@ -66,7 +65,6 @@ func (p *Path) initSumDuration() {
sql := fmt.Sprintf(sqlGetAverageDuration, p.serviceName, p.path, p.startTime.UnixNano(), p.endTime.UnixNano())
sum, ok := queryOneAndToInt(sql)
if !ok {
glog.Warn("init average query one:", sql)
return
}
......@@ -90,7 +88,6 @@ func (p *Path) initMedianDuration() {
sql := fmt.Sprintf(sqlGetMedianDuration, p.serviceName, p.path, p.startTime.UnixNano(), p.endTime.UnixNano())
median, ok := queryOneAndToInt(sql)
if !ok {
glog.Warn("init median query one:", sql)
return
}
p.medianDuration = time.Duration(median * 1e6)
......@@ -108,17 +105,14 @@ func (p *Path) initMaxDurationTracePoint() {
return
}
if len(req.Results) != 1 {
glog.Warn("init max duration req:", sql)
return
}
resp := req.Results[0]
if len(resp.Series) != 1 {
glog.Warn("init max duration resp:", sql)
return
}
row := resp.Series[0]
if len(row.Values) != 1 {
glog.Warn("init max duration row:", sql)
return
}
idxTime, ok1 := getModRowKeyIdx("time", row)
......
......@@ -119,6 +119,12 @@ func (t *Table) AddRecord(values ...interface{}) error {
}
func Run(n int) string {
const (
cpuMin = 5
memMin = 10
diskMin = 20
)
//求前n天的数据,按天取整
now := time.Now()
startTime := now.Add(-1 * time.Duration(now.Hour()) * time.Hour)
......@@ -171,5 +177,41 @@ func Run(n int) string {
rtn.WriteString(maxTable.ToString())
rtn.WriteString("\n")
//平均cpu使用率排行
cpuTable := NewTable("平均cpu使用率排行", "cpu", "sys_name", "count", "average_duration")
cpuList := sm.GetAverageCpuServiceList()
for _, s := range cpuList {
if s.GetAverageCpu() < cpuMin {
break
}
_ = cpuTable.AddRecord(s.GetAverageCpu(), s.Name(), s.GetCount(), s.GetAverageDuration())
}
rtn.WriteString(cpuTable.ToString())
rtn.WriteString("\n")
//平均内存使用率排行
memTable := NewTable("平均内存用率排行", "mem", "sys_name", "count", "average_duration")
memList := sm.GetAverageMemServiceList()
for _, s := range memList {
if s.GetAverageMem() < memMin {
break
}
_ = memTable.AddRecord(s.Name(), s.GetAverageMem(), s.GetCount(), s.GetAverageDuration())
}
rtn.WriteString(memTable.ToString())
rtn.WriteString("\n")
//平均硬盘使用率排行
diskTable := NewTable("平均硬盘使用率排行", "disk", "sys_name", "count", "average_duration")
diskList := sm.GetAverageDiskServiceList()
for _, s := range diskList {
if s.GetAverageDisk() < diskMin {
break
}
_ = diskTable.AddRecord(s.GetAverageDisk(), s.Name(), s.GetCount(), s.GetAverageDuration())
}
rtn.WriteString(diskTable.ToString())
rtn.WriteString("\n")
return rtn.String()
}
......@@ -12,9 +12,10 @@ const (
)
type Service struct {
name string //服务名称
startTime time.Time //开始时间
endTime time.Time //结束时间
name string //服务名称
startTime time.Time //开始时间
endTime time.Time //结束时间
//path相关
pathMap map[string]*Path //path列表
sumDuration time.Duration //总响应时间
count int //总访问量
......@@ -22,6 +23,8 @@ type Service struct {
maxMedianPath *Path //最大中位响应时间path
maxAveragePath *Path //最大平均响应时间path
maxDurationTracePoint *TracePoint //最大响应时间的tracePoint
//node相关
nodeMap map[string]*Node //节点列表
}
var IgnorePathMap = map[string]struct{}{
......@@ -41,6 +44,7 @@ func NewService(name string, startTime time.Time, endTime time.Time) *Service {
startTime: startTime,
endTime: endTime,
pathMap: make(map[string]*Path),
nodeMap: make(map[string]*Node),
}
//初始化path
......@@ -64,15 +68,61 @@ func NewService(name string, startTime time.Time, endTime time.Time) *Service {
//初始化访问时间相关的参数
rtn.initDuration()
//初始化node
nodeList := rtn.getNodeList()
for _, node := range nodeList {
rtn.nodeMap[node] = NewNode(rtn.name, node, rtn.startTime, rtn.endTime)
}
return &rtn
}
//获取服务名称
func (s *Service) Name() string {
return s.name
}
//获取节点列表
func (s *Service) getNodeList() []string {
const sqlGetNodeList = `SHOW TAG VALUES FROM machine_info WITH key = "host" WHERE sys_name = '%s';`
sql := fmt.Sprintf(sqlGetNodeList, s.name)
resp, err := query(sql)
if err != nil {
glog.Error("query sql:", sql, err)
}
if len(resp.Results) != 1 {
return []string{}
}
res := resp.Results[0]
if len(res.Series) != 1 {
return []string{}
}
row := res.Series[0]
idx, ok := getModRowKeyIdx("value", row)
if !ok {
return []string{}
}
rtn := make([]string, 0)
for _, v := range row.Values {
pathInterface := v[idx]
path, ok := pathInterface.(string)
if !ok {
continue
}
rtn = append(rtn, path)
}
return rtn
}
func (s *Service) getPathList() []string {
const sqlGetPathList = `SHOW TAG VALUES FROM trace_info WITH key = "path" WHERE sys_name = '%s';`
sql := fmt.Sprintf(sqlGetPathList, s.name)
resp, err := query(sql)
if err != nil {
glog.Error("query sql:", err)
glog.Error("query sql:", sql, err)
return []string{}
}
......@@ -170,3 +220,89 @@ func (s *Service) GetMaxAverageDurationPath() (*Path, bool) {
func (s *Service) GetMaxDurationTracePoint() (*TracePoint, bool) {
return s.maxDurationTracePoint, s.maxDurationTracePoint != nil
}
//求平均值
func (s *Service) getNodeMapValue(f func(node *Node) int) int {
if len(s.nodeMap) == 0 {
return 0
}
sum := 0
for _, node := range s.nodeMap {
sum += f(node)
}
return sum / len(s.nodeMap)
}
func (s *Service) GetAverageCpu() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetAverageCpu()
})
}
func (s *Service) GetMedianCpu() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMedianCpu()
})
}
func (s *Service) GetMaxCpu() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMaxCpu()
})
}
func (s *Service) GetAverageMem() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetAverageMem()
})
}
func (s *Service) GetMedianMem() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMedianMem()
})
}
func (s *Service) GetMaxMem() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMaxMem()
})
}
func (s *Service) GetAverageDisk() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetAverageDisk()
})
}
func (s *Service) GetMedianDisk() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMedianDisk()
})
}
func (s *Service) GetMaxDisk() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMaxDisk()
})
}
func (s *Service) GetAverageThread() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetAverageThread()
})
}
func (s *Service) GetMedianThread() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMedianThread()
})
}
func (s *Service) GetMaxThread() int {
return s.getNodeMapValue(func(node *Node) int {
return node.GetMaxThread()
})
}
......@@ -56,6 +56,56 @@ func (sm *ServiceMap) getServiceList() []string {
return rtn
}
//获取平均cpu使用率列表
func (sm *ServiceMap) GetAverageCpuServiceList() []*Service {
rtn := make([]*Service, 0)
for _, s := range sm.serviceMap {
rtn = append(rtn, s)
}
sort.Slice(rtn, func(i, j int) bool {
if rtn[i].GetAverageCpu() > rtn[j].GetAverageCpu() {
return true
}
return false
})
return rtn
}
//获取峰值内存使用率列表
func (sm *ServiceMap) GetAverageMemServiceList() []*Service {
rtn := make([]*Service, 0)
for _, s := range sm.serviceMap {
rtn = append(rtn, s)
}
sort.Slice(rtn, func(i, j int) bool {
if rtn[i].GetAverageMem() > rtn[j].GetAverageMem() {
return true
}
return false
})
return rtn
}
//获取峰值磁盘使用率列表
func (sm *ServiceMap) GetAverageDiskServiceList() []*Service {
rtn := make([]*Service, 0)
for _, s := range sm.serviceMap {
rtn = append(rtn, s)
}
sort.Slice(rtn, func(i, j int) bool {
if rtn[i].GetAverageDisk() > rtn[j].GetAverageDisk() {
return true
}
return false
})
return rtn
}
//获取最大中位响应时间列表
func (sm *ServiceMap) GetMedianDurationPathList() []*Path {
rtn := make([]*Path, 0)
......
......@@ -13,20 +13,32 @@ func TestNewServiceMap(t *testing.T) {
t.Log("总平均访问时间:", sm.GetAverageDuration())
t.Log("------------------median-------------------")
medianList := sm.GetMedianDurationPathList()
t.Log("服务数量:", len(medianList))
for _, m := range medianList {
t.Log(m.GetMedianDuration(), m.GetServiceName(), m.GetPath(), m.GetCount())
}
t.Log("------------------average------------------")
averageList := sm.GetAverageDurationPathList()
t.Log("服务数量:", len(averageList))
for _, m := range averageList {
t.Log(m.GetAverageDuration(), m.GetServiceName(), m.GetPath(), m.GetCount())
}
t.Log("------------------max----------------------")
maxList := sm.GetMaxDurationTracePointList()
t.Log("服务数量:", len(maxList))
for _, m := range maxList {
t.Log(m.Duration, m.ServiceName, m.Path, m.Timestamp, m.TraceId)
}
t.Log("------------------cpu----------------------")
cpuList := sm.GetAverageCpuServiceList()
for _, s := range cpuList {
t.Log(s.Name(), s.GetAverageCpu())
}
t.Log("------------------mem----------------------")
memList := sm.GetAverageMemServiceList()
for _, s := range memList {
t.Log(s.Name(), s.GetAverageMem())
}
t.Log("------------------disk---------------------")
diskList := sm.GetAverageDiskServiceList()
for _, s := range diskList {
t.Log(s.Name(), s.GetAverageDisk())
}
}
......@@ -23,4 +23,9 @@ func TestNewService(t *testing.T) {
if ok {
t.Log("average_max_path:", average.GetPath())
}
t.Log("cpu:", s.GetMedianCpu(), s.GetAverageCpu(), s.GetMaxCpu())
t.Log("mem:", s.GetMedianMem(), s.GetAverageMem(), s.GetMaxMem())
t.Log("disk:", s.GetMedianDisk(), s.GetAverageDisk(), s.GetMaxDisk())
t.Log("trace", s.GetMedianThread(), s.GetAverageThread(), s.GetMaxThread())
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment