diff --git a/server/conf/config.go b/server/conf/config.go index 360077245cdf6e8986baab5742e590f9d132202a..252ba9b672389ec9b924b07c0731704340d4ae78 100644 --- a/server/conf/config.go +++ b/server/conf/config.go @@ -6,7 +6,7 @@ import ( "os" "path" - "gitee.com/openeuler/PilotGo-plugin-topology-server/utils" + "gitee.com/openeuler/PilotGo-plugin-topology-server/global" "gitee.com/openeuler/PilotGo/sdk/logger" "github.com/pkg/errors" "gopkg.in/yaml.v2" @@ -42,7 +42,7 @@ func InitConfig() { } flag.Parse() - bytes, err := utils.FileReadBytes(ConfigFile()) + bytes, err := global.FileReadBytes(ConfigFile()) if err != nil { err = errors.Wrapf(err, "open file failed: %s, %s", ConfigFile(), err.Error()) // err top fmt.Printf("%+v\n", err) diff --git a/server/db/redismanager/redis.go b/server/db/redismanager/redis.go index 219a67db537c9782b903c640962ee1ad282972c5..29451a3789f19c23ba847c970f9d2aa6b6771fd7 100644 --- a/server/db/redismanager/redis.go +++ b/server/db/redismanager/redis.go @@ -12,7 +12,7 @@ import ( "gitee.com/openeuler/PilotGo-plugin-topology-server/conf" "gitee.com/openeuler/PilotGo-plugin-topology-server/errormanager" "gitee.com/openeuler/PilotGo-plugin-topology-server/pluginclient" - "gitee.com/openeuler/PilotGo-plugin-topology-server/utils" + "gitee.com/openeuler/PilotGo-plugin-topology-server/global" "gitee.com/openeuler/PilotGo/sdk/logger" "gitee.com/openeuler/PilotGo/sdk/utils/httputils" "github.com/go-redis/redis/v8" @@ -180,7 +180,7 @@ func (r *RedisClient) UpdateTopoRunningAgentList(uuids []string, updateonce bool return } - if ok, err := utils.IsIPandPORTValid(agentp.IP, agentmanager.Global_AgentManager.AgentPort); !ok { + if ok, err := global.IsIPandPORTValid(agentp.IP, agentmanager.Global_AgentManager.AgentPort); !ok { err := errors.Errorf("%s:%s is unreachable (%s) %s **warn**1", agentp.IP, agentmanager.Global_AgentManager.AgentPort, err.Error(), agentp.UUID) // err top errormanager.ErrorTransmit(pluginclient.GlobalContext, err, false) abort_reason = append(abort_reason, fmt.Sprintf("%s:ip||port不可达", agentvalue.UUID)) @@ -278,7 +278,7 @@ func (r *RedisClient) ActiveHeartbeatDetection(uuids []string) { go func(a *agentmanager.Agent) { defer wg.Done() - if ok, _ := utils.IsIPandPORTValid(a.IP, agentmanager.Global_AgentManager.AgentPort); !ok { + if ok, _ := global.IsIPandPORTValid(a.IP, agentmanager.Global_AgentManager.AgentPort); !ok { // err := errors.Errorf("%s:%s is unreachable (%s) %s **warn**1", a.IP, agentmanager.Topo.AgentPort, err.Error(), a.UUID) // err top // agentmanager.ErrorTransmit(agentmanager.Topo.Tctx, err, agentmanager.Topo.ErrCh, false) return @@ -304,7 +304,7 @@ func (r *RedisClient) ActiveHeartbeatDetection(uuids []string) { return } - if ok, _ := utils.IsIPandPORTValid(agent.IP, agentmanager.Global_AgentManager.AgentPort); !ok { + if ok, _ := global.IsIPandPORTValid(agent.IP, agentmanager.Global_AgentManager.AgentPort); !ok { // err := errors.Errorf("%s:%s is unreachable (%s) %s **warn**1", agent.IP, agentmanager.Topo.AgentPort, err.Error(), agent.UUID) // err top // agentmanager.ErrorTransmit(agentmanager.Topo.Tctx, err, agentmanager.Topo.ErrCh, false) return diff --git a/server/utils/IsIPandPORTValid.go b/server/global/IsIPandPORTValid.go similarity index 96% rename from server/utils/IsIPandPORTValid.go rename to server/global/IsIPandPORTValid.go index c3a5ae9e6186f0f959bf470aa59ef2638e9adc96..4b44da742c4bcc5685c3a9bed6012fd248ee02f3 100644 --- a/server/utils/IsIPandPORTValid.go +++ b/server/global/IsIPandPORTValid.go @@ -1,4 +1,4 @@ -package utils +package global import ( "fmt" diff --git a/server/utils/file.go b/server/global/file.go similarity index 98% rename from server/utils/file.go rename to server/global/file.go index c12f32292266dc38c91e5e621616c6211596ea55..d9123703d5f3ca629d80ca2433c8876e23947ac2 100755 --- a/server/utils/file.go +++ b/server/global/file.go @@ -1,4 +1,4 @@ -package utils +package global import ( "io" diff --git a/server/graph/meta.go b/server/graph/meta.go index d56574153d889e5c88982a0fef29787f5c6879fe..24fa78367bee781fa5449e61dec571837b2f8f1b 100755 --- a/server/graph/meta.go +++ b/server/graph/meta.go @@ -1,6 +1,10 @@ package graph import ( + "reflect" + "strconv" + "strings" + "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/disk" "github.com/shirou/gopsutil/net" @@ -43,7 +47,7 @@ type Process struct { Nice int32 `json:"nice"` IOnice int32 `json:"ionice"` - Connections []Netconnection `json:"connections"` + Connections []Netconnection `json:"connections"` NetIOCounters []net.IOCountersStat `json:"netiocounters"` IOCounters process.IOCountersStat `json:"iocounters"` @@ -109,3 +113,221 @@ type Cpu struct { Info cpu.InfoStat `json:"info"` Time cpu.TimesStat `json:"time"` } + +func StructToMap(obj interface{}) map[string]string { + objValue := reflect.ValueOf(obj) + if objValue.Kind() == reflect.Ptr { + objValue = objValue.Elem() + } + + if objValue.Kind() != reflect.Struct { + return nil + } + + objType := objValue.Type() + fieldCount := objType.NumField() + + m := make(map[string]string) + for i := 0; i < fieldCount; i++ { + field := objType.Field(i) + fieldValue := objValue.Field(i) + + switch fieldValue.Kind() { + case reflect.String: + m[field.Name] = fieldValue.Interface().(string) + case reflect.Uint64: + fieldvalue_uint64 := fieldValue.Interface().(uint64) + m[field.Name] = strconv.Itoa(int(fieldvalue_uint64)) + case reflect.Float64: + fieldvalue_float64 := fieldValue.Interface().(float64) + m[field.Name] = strconv.FormatFloat(fieldvalue_float64, 'f', -1, 64) + } + } + + return m +} + +func HostToMap(host *Host, a_i_map *map[string][]string) *map[string]string { + host_metrics := StructToMap(host) + + interfaces_string := []string{} + for key, value := range *a_i_map { + interfaces_string = append(interfaces_string, key+":"+strings.Join(value, " ")) + } + + if _, ok := host_metrics["interfaces"]; ok { + host_metrics["interfaces"] = strings.Join(interfaces_string, ";") + } + + return &host_metrics +} + +func ProcessToMap(process *Process) *map[string]string { + uids_string := []string{} + for _, u := range process.Uids { + uids_string = append(uids_string, strconv.Itoa(int(u))) + } + + gids_string := []string{} + for _, g := range process.Gids { + gids_string = append(gids_string, strconv.Itoa(int(g))) + } + + openfiles_string := []string{} + for _, of := range process.OpenFiles { + openfiles_string = append(openfiles_string, strconv.Itoa(int(of.Fd))+":"+of.Path) + } + + cpid_string := []string{} + for _, cid := range process.Cpid { + cpid_string = append(cpid_string, strconv.Itoa(int(cid))) + } + + return &map[string]string{ + "Pid": strconv.Itoa(int(process.Pid)), + "Ppid": strconv.Itoa(int(process.Ppid)), + "Cpid": strings.Join(cpid_string, " "), + "Uids": strings.Join(uids_string, " "), + "Gids": strings.Join(gids_string, " "), + "Status": process.Status, + "CreateTime": strconv.Itoa(int(process.CreateTime)), + "Cwd": process.Cwd, + "ExePath": process.ExePath, + "Cmdline": process.Cmdline, + "Nice": strconv.Itoa(int(process.Nice)), + "IOnice": strconv.Itoa(int(process.IOnice)), + "DISK-rc": strconv.Itoa(int(process.IOCounters.ReadCount)), + "DISK-rb": strconv.Itoa(int(process.IOCounters.ReadBytes)), + "DISK-wc": strconv.Itoa(int(process.IOCounters.WriteCount)), + "DISK-wb": strconv.Itoa(int(process.IOCounters.WriteBytes)), + "fd": strings.Join(openfiles_string, " "), + "NumCtxSwitches-v": strconv.Itoa(int(process.NumCtxSwitches.Voluntary)), + "NumCtxSwitches-inv": strconv.Itoa(int(process.NumCtxSwitches.Involuntary)), + "PageFaults-MinorFaults": strconv.Itoa(int(process.PageFaults.MinorFaults)), + "PageFaults-MajorFaults": strconv.Itoa(int(process.PageFaults.MajorFaults)), + "PageFaults-ChildMinorFaults": strconv.Itoa(int(process.PageFaults.ChildMinorFaults)), + "PageFaults-ChildMajorFaults": strconv.Itoa(int(process.PageFaults.ChildMajorFaults)), + "CPUPercent": strconv.FormatFloat(process.CPUPercent, 'f', -1, 64), + "MemoryPercent": strconv.FormatFloat(process.MemoryPercent, 'f', -1, 64), + "MemoryInfo": process.MemoryInfo.String(), + } +} + +func ThreadToMap(thread *Thread) *map[string]string { + return &map[string]string{ + "Tid": strconv.Itoa(int(thread.Tid)), + "Tgid": strconv.Itoa(int(thread.Tgid)), + "CPU": thread.CPU, + "User": strconv.FormatFloat(thread.User, 'f', -1, 64), + "System": strconv.FormatFloat(thread.System, 'f', -1, 64), + "Idle": strconv.FormatFloat(thread.Idle, 'f', -1, 64), + "Nice": strconv.FormatFloat(thread.Nice, 'f', -1, 64), + "Iowait": strconv.FormatFloat(thread.Iowait, 'f', -1, 64), + "Irq": strconv.FormatFloat(thread.Irq, 'f', -1, 64), + "Softirq": strconv.FormatFloat(thread.Softirq, 'f', -1, 64), + "Steal": strconv.FormatFloat(thread.Steal, 'f', -1, 64), + "Guest": strconv.FormatFloat(thread.Guest, 'f', -1, 64), + "GuestNice": strconv.FormatFloat(thread.GuestNice, 'f', -1, 64), + } +} + +// net节点的metrics字段 临时定义 +func NetToMap(net *Netconnection) *map[string]string { + uids_string := []string{} + for _, uid := range net.Uids { + uids_string = append(uids_string, strconv.Itoa(int(uid))) + } + + return &map[string]string{ + "Fd": strconv.Itoa(int(net.Fd)), + "Family": strconv.Itoa(int(net.Family)), + "Type": strconv.Itoa(int(net.Type)), + "Laddr": net.Laddr, + "Raddr": net.Raddr, + "Status": net.Status, + "Uids": strings.Join(uids_string, " "), + "Pid": strconv.Itoa(int(net.Pid)), + } +} + +// func NetToMap(net *net.IOCountersStat, a_i_map *map[string][]string) *map[string]string { +// addrs := []string{} +// for key, value := range *a_i_map { +// if net.Name == key { +// addrs = value +// } +// } + +// return &map[string]string{ +// "Name": net.Name, +// "addrs": addrs[0], +// "BytesSent": strconv.Itoa(int(net.BytesSent)), +// "BytesRecv": strconv.Itoa(int(net.BytesRecv)), +// "PacketsSent": strconv.Itoa(int(net.PacketsSent)), +// "PacketsRecv": strconv.Itoa(int(net.PacketsRecv)), +// "Errin": strconv.Itoa(int(net.Errin)), +// "Errout": strconv.Itoa(int(net.Errout)), +// "Dropin": strconv.Itoa(int(net.Dropin)), +// "Dropout": strconv.Itoa(int(net.Dropout)), +// "Fifoin": strconv.Itoa(int(net.Fifoin)), +// "Fifoout": strconv.Itoa(int(net.Fifoout)), +// } +// } + +func DiskToMap(disk *Disk) *map[string]string { + disk_map := make(map[string]string) + partition_map := StructToMap(disk.Partition) + iocounter_map := StructToMap(disk.IOcounter) + usage_map := StructToMap(disk.Usage) + + for k, v := range partition_map { + disk_map[k] = v + } + + for k, v := range iocounter_map { + if k != "Name" { + disk_map[k] = v + } + } + + for k, v := range usage_map { + if k != "Path" && k != "Fstype" { + disk_map[k] = v + } + } + + return &disk_map +} + +func CpuToMap(cpu *Cpu) *map[string]string { + cpu_map := make(map[string]string) + info_map := StructToMap(cpu.Info) + time_map := StructToMap(cpu.Time) + + for k, v := range info_map { + if k != "Flags" { + cpu_map[k] = v + } + } + + for k, v := range time_map { + if k != "CPU" { + cpu_map[k] = v + } + } + + return &cpu_map +} + +func InterfaceToMap(iface *NetIOcounter) *map[string]string { + iface_map := make(map[string]string) + old_map := StructToMap(iface) + + for k, v := range old_map { + if k != "Name" { + iface_map[k] = v + } + } + + return &iface_map +} diff --git a/server/service/init.go b/server/graph/treenode.go similarity index 79% rename from server/service/init.go rename to server/graph/treenode.go index c107accefa085a23ce755b0bd9306d987c96f070..274dc5d925718839b8dd040d84bcbdea4f4a4007 100755 --- a/server/service/init.go +++ b/server/graph/treenode.go @@ -1,14 +1,12 @@ -package service - -import "gitee.com/openeuler/PilotGo-plugin-topology-server/graph" +package graph type TreeTopoNode struct { ID string `json:"id"` - Node *graph.Node `json:"node"` + Node *Node `json:"node"` Children []*TreeTopoNode `json:"children"` } -func CreateTreeNode(node *graph.Node) *TreeTopoNode { +func CreateTreeNode(node *Node) *TreeTopoNode { return &TreeTopoNode{ ID: node.ID, Node: node, diff --git a/server/main.go b/server/main.go index 51bb244a749d770fcc8041cb0253873e3c9dfe73..e427e5cc2a4d1f818d294f2566e9869ca6d8aeab 100755 --- a/server/main.go +++ b/server/main.go @@ -12,7 +12,7 @@ import ( "gitee.com/openeuler/PilotGo-plugin-topology-server/logger" "gitee.com/openeuler/PilotGo-plugin-topology-server/pluginclient" "gitee.com/openeuler/PilotGo-plugin-topology-server/signal" - service "gitee.com/openeuler/PilotGo-plugin-topology-server/service/background" + "gitee.com/openeuler/PilotGo-plugin-topology-server/service" // "github.com/pyroscope-io/pyroscope/pkg/agent/profiler" ) diff --git a/server/processor/processor.go b/server/processor/processor.go index aa77f1081a11631c482ec5086a3b91f4cd1c4f0f..3ab4a85645b65e90c8c46dadd484aebfda0b17c9 100755 --- a/server/processor/processor.go +++ b/server/processor/processor.go @@ -16,7 +16,6 @@ import ( "gitee.com/openeuler/PilotGo-plugin-topology-server/global" "gitee.com/openeuler/PilotGo-plugin-topology-server/graph" "gitee.com/openeuler/PilotGo-plugin-topology-server/pluginclient" - "gitee.com/openeuler/PilotGo-plugin-topology-server/utils" "gitee.com/openeuler/PilotGo/sdk/logger" "github.com/pkg/errors" ) @@ -143,7 +142,7 @@ func (d *DataProcesser) CreateNodeEntities(agent *agentmanager.Agent, nodes *gra UUID: agent.UUID, LayoutAttr: global.INNER_LAYOUT_1, ComboId: agent.UUID, - Metrics: *utils.HostToMap(agent.Host_2, &agent.AddrInterfaceMap_2), + Metrics: *graph.HostToMap(agent.Host_2, &agent.AddrInterfaceMap_2), } nodes.Add(host_node) @@ -156,7 +155,7 @@ func (d *DataProcesser) CreateNodeEntities(agent *agentmanager.Agent, nodes *gra UUID: agent.UUID, LayoutAttr: global.INNER_LAYOUT_2, ComboId: agent.UUID, - Metrics: *utils.ProcessToMap(process), + Metrics: *graph.ProcessToMap(process), } nodes.Add(proc_node) @@ -169,7 +168,7 @@ func (d *DataProcesser) CreateNodeEntities(agent *agentmanager.Agent, nodes *gra UUID: agent.UUID, LayoutAttr: global.INNER_LAYOUT_3, ComboId: agent.UUID, - Metrics: *utils.ThreadToMap(&thread), + Metrics: *graph.ThreadToMap(&thread), } nodes.Add(thre_node) @@ -198,7 +197,7 @@ func (d *DataProcesser) CreateNodeEntities(agent *agentmanager.Agent, nodes *gra UUID: agent.UUID, LayoutAttr: global.INNER_LAYOUT_5, ComboId: agent.UUID, - Metrics: *utils.NetToMap(net), + Metrics: *graph.NetToMap(net), } nodes.Add(net_node) @@ -216,7 +215,7 @@ func (d *DataProcesser) CreateNodeEntities(agent *agentmanager.Agent, nodes *gra UUID: agent.UUID, LayoutAttr: global.INNER_LAYOUT_4, ComboId: agent.UUID, - Metrics: *utils.DiskToMap(disk), + Metrics: *graph.DiskToMap(disk), } nodes.Add(disk_node) @@ -230,7 +229,7 @@ func (d *DataProcesser) CreateNodeEntities(agent *agentmanager.Agent, nodes *gra UUID: agent.UUID, LayoutAttr: global.INNER_LAYOUT_4, ComboId: agent.UUID, - Metrics: *utils.CpuToMap(cpu), + Metrics: *graph.CpuToMap(cpu), } nodes.Add(cpu_node) @@ -244,7 +243,7 @@ func (d *DataProcesser) CreateNodeEntities(agent *agentmanager.Agent, nodes *gra UUID: agent.UUID, LayoutAttr: global.INNER_LAYOUT_4, ComboId: agent.UUID, - Metrics: *utils.InterfaceToMap(ifaceio), + Metrics: *graph.InterfaceToMap(ifaceio), } nodes.Add(iface_node) @@ -269,7 +268,7 @@ func (d *DataProcesser) CustomCreateNodeEntities(agent *agentmanager.Agent, node LayoutAttr: global.INNER_LAYOUT_1, ComboId: agent.UUID, Network: allconnections, - Metrics: *utils.HostToMap(agent.Host_2, &agent.AddrInterfaceMap_2), + Metrics: *graph.HostToMap(agent.Host_2, &agent.AddrInterfaceMap_2), } host_node.Tags = append(host_node.Tags, host_node.UUID, host_node.Type) @@ -315,7 +314,7 @@ func (d *DataProcesser) CustomCreateNodeEntities(agent *agentmanager.Agent, node LayoutAttr: global.INNER_LAYOUT_2, ComboId: agent.UUID, Network: process.Connections, - Metrics: *utils.ProcessToMap(process), + Metrics: *graph.ProcessToMap(process), } proc_node.Tags = append(proc_node.Tags, proc_node.UUID, proc_node.Type) @@ -343,7 +342,7 @@ func (d *DataProcesser) CustomCreateNodeEntities(agent *agentmanager.Agent, node LayoutAttr: global.INNER_LAYOUT_2, ComboId: agent.UUID, Network: process.Connections, - Metrics: *utils.ProcessToMap(process), + Metrics: *graph.ProcessToMap(process), } proc_node.Tags = append(proc_node.Tags, proc_node.UUID, proc_node.Type) @@ -366,7 +365,7 @@ func (d *DataProcesser) CustomCreateNodeEntities(agent *agentmanager.Agent, node UUID: agent.UUID, LayoutAttr: global.INNER_LAYOUT_4, ComboId: agent.UUID, - Metrics: *utils.DiskToMap(disk), + Metrics: *graph.DiskToMap(disk), } disk_node.Tags = append(disk_node.Tags, disk_node.UUID, disk_node.Type) @@ -386,7 +385,7 @@ func (d *DataProcesser) CustomCreateNodeEntities(agent *agentmanager.Agent, node UUID: agent.UUID, LayoutAttr: global.INNER_LAYOUT_4, ComboId: agent.UUID, - Metrics: *utils.CpuToMap(cpu), + Metrics: *graph.CpuToMap(cpu), } cpu_node.Tags = append(cpu_node.Tags, cpu_node.UUID, cpu_node.Type) @@ -406,7 +405,7 @@ func (d *DataProcesser) CustomCreateNodeEntities(agent *agentmanager.Agent, node UUID: agent.UUID, LayoutAttr: global.INNER_LAYOUT_4, ComboId: agent.UUID, - Metrics: *utils.InterfaceToMap(ifaceio), + Metrics: *graph.InterfaceToMap(ifaceio), } iface_node.Tags = append(iface_node.Tags, iface_node.UUID, iface_node.Type) diff --git a/server/service/customTopo.go b/server/service/customTopo.go index 9dd79ffc3b63521395dd3f0d7e37b369352a2575..ede37e397ee640247ab841fc0970d720d7a94753 100755 --- a/server/service/customTopo.go +++ b/server/service/customTopo.go @@ -10,7 +10,6 @@ import ( "gitee.com/openeuler/PilotGo-plugin-topology-server/db/redismanager" "gitee.com/openeuler/PilotGo-plugin-topology-server/graph" "gitee.com/openeuler/PilotGo-plugin-topology-server/pluginclient" - back "gitee.com/openeuler/PilotGo-plugin-topology-server/service/background" "gitee.com/openeuler/PilotGo/sdk/response" "github.com/pkg/errors" ) @@ -64,7 +63,7 @@ func RunCustomTopoService(tcid uint) ([]*graph.Node, []*graph.Edge, []map[string } unixtime_now := time.Now().Unix() - nodes, edges, combos, err := back.DataProcessWorking(unixtime_now, running_agent_num, graphmanager.Global_GraphDB, tc.TagRules, tc.NodeRules) + nodes, edges, combos, err := DataProcessWorking(unixtime_now, running_agent_num, graphmanager.Global_GraphDB, tc.TagRules, tc.NodeRules) if err != nil { return nil, nil, nil, errors.Wrap(err, "**2") } @@ -153,7 +152,7 @@ func DeleteCustomTopoService(ids []uint) error { if mysqlmanager.Global_Mysql == nil { return errors.New("global_mysql is nil **errstackfatal**1") } - + for _, tcid := range ids { if err := mysqlmanager.Global_Mysql.DeleteTopoConfiguration(tcid); err != nil { return errors.Wrap(err, "**errstack**2") diff --git a/server/service/multihost.go b/server/service/multihost.go index 0a88e892d15a1466eca153dd393ecdf80b18b7b9..c2782b32626738dff40eec01243b0acbee222395 100755 --- a/server/service/multihost.go +++ b/server/service/multihost.go @@ -4,16 +4,16 @@ import ( "fmt" "gitee.com/openeuler/PilotGo-plugin-topology-server/db/graphmanager" - "gitee.com/openeuler/PilotGo-plugin-topology-server/graph" "gitee.com/openeuler/PilotGo-plugin-topology-server/global" + "gitee.com/openeuler/PilotGo-plugin-topology-server/graph" "github.com/pkg/errors" ) func MultiHostService() ([]*graph.Node, []*graph.Edge, []map[string]string, error) { var latest string - nodes := make([]*graph.Node, 0) + var nodes []*graph.Node + var edges []*graph.Edge nodes_map := make(map[int64]*graph.Node) - edges := make([]*graph.Edge, 0) edges_map := make(map[int64]*graph.Edge) hostids := make([]int64, 0) multi_nodes_map := make(map[int64]*graph.Node) diff --git a/server/service/background/periodcollect.go b/server/service/periodcollect.go similarity index 88% rename from server/service/background/periodcollect.go rename to server/service/periodcollect.go index e207d92ea9e544946b8a9256523a7641e368f276..798e7f16f7473bccbb16286918916f96007e3258 100644 --- a/server/service/background/periodcollect.go +++ b/server/service/periodcollect.go @@ -16,7 +16,6 @@ import ( "gitee.com/openeuler/PilotGo-plugin-topology-server/graph" "gitee.com/openeuler/PilotGo-plugin-topology-server/pluginclient" "gitee.com/openeuler/PilotGo-plugin-topology-server/processor" - "gitee.com/openeuler/PilotGo-plugin-topology-server/utils" "gitee.com/openeuler/PilotGo/sdk/logger" "github.com/pkg/errors" ) @@ -116,7 +115,7 @@ func DataProcessWorking(unixtime int64, agentnum int, graphdb graphmanager.Graph defer nodeUuidWg.Done() // TODO: 根据插件运行状态agent的数目拆分nodes - splitnodes := utils.SplitNodesByBreakpoint(_nodesbyuuid, agentnum) + splitnodes := SplitNodesByBreakpoint(_nodesbyuuid, agentnum) if splitnodes != nil { for _, _nodes := range splitnodes { __nodes := _nodes @@ -150,7 +149,7 @@ func DataProcessWorking(unixtime int64, agentnum int, graphdb graphmanager.Graph } nodeUuidWg.Wait() - splitedges := utils.SplitEdgesByBreakpoint(edges.Edges, agentnum) + splitedges := SplitEdgesByBreakpoint(edges.Edges, agentnum) if splitedges != nil { for _, _edges := range splitedges { __edges := _edges @@ -176,3 +175,49 @@ func DataProcessWorking(unixtime int64, agentnum int, graphdb graphmanager.Graph return nil, nil, nil, nil } + +func SplitEdgesByBreakpoint(arr []*graph.Edge, n int) [][]*graph.Edge { + length := len(arr) + if length == 0 { + return nil + } + + size := length / n + result := make([][]*graph.Edge, n) + + for i := 0; i < n; i++ { + start := i * size + end := (i + 1) * size + + if end > length { + end = length + } + + result = append(result, arr[start:end]) + } + + return result +} + +func SplitNodesByBreakpoint(arr []*graph.Node, n int) [][]*graph.Node { + length := len(arr) + if length == 0 { + return nil + } + + size := length / n + result := make([][]*graph.Node, n) + + for i := 0; i < n; i++ { + start := i * size + end := (i + 1) * size + + if end > length { + end = length + } + + result = append(result, arr[start:end]) + } + + return result +} diff --git a/server/service/singlehosttree.go b/server/service/singlehosttree.go index 64106175b329d32483c649a7e2d784c0fa861af6..2b4a731de8f974fecc2fd5f90f46333cabe5207c 100755 --- a/server/service/singlehosttree.go +++ b/server/service/singlehosttree.go @@ -2,18 +2,18 @@ package service import ( "gitee.com/openeuler/PilotGo-plugin-topology-server/db/graphmanager" - "gitee.com/openeuler/PilotGo-plugin-topology-server/graph" "gitee.com/openeuler/PilotGo-plugin-topology-server/global" + "gitee.com/openeuler/PilotGo-plugin-topology-server/graph" "github.com/pkg/errors" ) -func SingleHostTreeService(uuid string) (*TreeTopoNode, error) { +func SingleHostTreeService(uuid string) (*graph.TreeTopoNode, error) { var latest string - var treerootnode *TreeTopoNode - single_nodes := make([]*graph.Node, 0) + var treerootnode *graph.TreeTopoNode + var single_nodes []*graph.Node single_nodes_map := make(map[int64]*graph.Node) - treenodes_process := make([]*TreeTopoNode, 0) - treenodes_net := make([]*TreeTopoNode, 0) + treenodes_process := make([]*graph.TreeTopoNode, 0) + treenodes_net := make([]*graph.TreeTopoNode, 0) nodes_type_map := make(map[string][]*graph.Node) if graphmanager.Global_GraphDB == nil { @@ -53,7 +53,7 @@ func SingleHostTreeService(uuid string) (*TreeTopoNode, error) { for _, node := range single_nodes { nodes_type_map[node.Type] = append(nodes_type_map[node.Type], node) if node.Type == "host" { - treerootnode = CreateTreeNode(node) + treerootnode = graph.CreateTreeNode(node) } } @@ -63,23 +63,23 @@ func SingleHostTreeService(uuid string) (*TreeTopoNode, error) { } for _, node := range nodes_type_map[global.NODE_RESOURCE] { - childnode := CreateTreeNode(node) + childnode := graph.CreateTreeNode(node) treerootnode.Children = append(treerootnode.Children, childnode) } for _, node := range nodes_type_map[global.NODE_PROCESS] { - treenode := CreateTreeNode(node) + treenode := graph.CreateTreeNode(node) treenodes_process = append(treenodes_process, treenode) } for _, node := range nodes_type_map[global.NODE_NET] { - treenode := CreateTreeNode(node) + treenode := graph.CreateTreeNode(node) treenodes_net = append(treenodes_net, treenode) } for _, node := range treenodes_process { if node.Node.Metrics["Pid"] == "1" { - node.Children = SliceToTree(treenodes_process, treenodes_net, "1") + node.Children = graph.SliceToTree(treenodes_process, treenodes_net, "1") treerootnode.Children = append(treerootnode.Children, node) break diff --git a/server/utils/gopsutilMeta2TopoMeta.go b/server/utils/gopsutilMeta2TopoMeta.go deleted file mode 100644 index 45e4900dea13c6e6092e5083656d0321e2017b1a..0000000000000000000000000000000000000000 --- a/server/utils/gopsutilMeta2TopoMeta.go +++ /dev/null @@ -1,33 +0,0 @@ -package utils - -import ( - "strconv" - - "gitee.com/openeuler/PilotGo-plugin-topology-server/graph" - "github.com/shirou/gopsutil/net" -) - -func GopsutilNetMeta2TopoNetMeta(gopsnets []net.ConnectionStat) []*graph.Netconnection { - toponets := []*graph.Netconnection{} - - for _, c := range gopsnets { - if c.Status == "NONE" { - continue - } - if c.Laddr.Port == 22 || c.Raddr.Port == 22 { - continue - } - c1 := &graph.Netconnection{} - c1.Fd = c.Fd - c1.Family = c.Family - c1.Type = c.Type - c1.Laddr = c.Laddr.IP + ":" + strconv.Itoa(int(c.Laddr.Port)) - c1.Raddr = c.Raddr.IP + ":" + strconv.Itoa(int(c.Raddr.Port)) - c1.Status = c.Status - c1.Uids = c.Uids - c1.Pid = c.Pid - toponets = append(toponets, c1) - } - - return toponets -} diff --git a/server/utils/init.go b/server/utils/init.go deleted file mode 100755 index 5b55d239aca3bc68b9185863950c6c5085439c55..0000000000000000000000000000000000000000 --- a/server/utils/init.go +++ /dev/null @@ -1 +0,0 @@ -package utils \ No newline at end of file diff --git a/server/utils/splitNodesAndEdgesArray.go b/server/utils/splitNodesAndEdgesArray.go deleted file mode 100644 index 03a4b63ccb5bbfbd174b452c4533058a22a00157..0000000000000000000000000000000000000000 --- a/server/utils/splitNodesAndEdgesArray.go +++ /dev/null @@ -1,49 +0,0 @@ -package utils - -import "gitee.com/openeuler/PilotGo-plugin-topology-server/graph" - -func SplitEdgesByBreakpoint(arr []*graph.Edge, n int) [][]*graph.Edge { - length := len(arr) - if length == 0 { - return nil - } - - size := length / n - result := make([][]*graph.Edge, n) - - for i := 0; i < n; i++ { - start := i * size - end := (i + 1) * size - - if end > length { - end = length - } - - result = append(result, arr[start:end]) - } - - return result -} - -func SplitNodesByBreakpoint(arr []*graph.Node, n int) [][]*graph.Node { - length := len(arr) - if length == 0 { - return nil - } - - size := length / n - result := make([][]*graph.Node, n) - - for i := 0; i < n; i++ { - start := i * size - end := (i + 1) * size - - if end > length { - end = length - } - - result = append(result, arr[start:end]) - } - - return result -} diff --git a/server/utils/struct2Map.go b/server/utils/struct2Map.go deleted file mode 100755 index 0bc46ff0358f264d6a0b5df2329b813365142b2e..0000000000000000000000000000000000000000 --- a/server/utils/struct2Map.go +++ /dev/null @@ -1,227 +0,0 @@ -package utils - -import ( - "reflect" - "strconv" - "strings" - - "gitee.com/openeuler/PilotGo-plugin-topology-server/graph" -) - -func StructToMap(obj interface{}) map[string]string { - objValue := reflect.ValueOf(obj) - if objValue.Kind() == reflect.Ptr { - objValue = objValue.Elem() - } - - if objValue.Kind() != reflect.Struct { - return nil - } - - objType := objValue.Type() - fieldCount := objType.NumField() - - m := make(map[string]string) - for i := 0; i < fieldCount; i++ { - field := objType.Field(i) - fieldValue := objValue.Field(i) - - switch fieldValue.Kind() { - case reflect.String: - m[field.Name] = fieldValue.Interface().(string) - case reflect.Uint64: - fieldvalue_uint64 := fieldValue.Interface().(uint64) - m[field.Name] = strconv.Itoa(int(fieldvalue_uint64)) - case reflect.Float64: - fieldvalue_float64 := fieldValue.Interface().(float64) - m[field.Name] = strconv.FormatFloat(fieldvalue_float64, 'f', -1, 64) - } - } - - return m -} - -func HostToMap(host *graph.Host, a_i_map *map[string][]string) *map[string]string { - host_metrics := StructToMap(host) - - interfaces_string := []string{} - for key, value := range *a_i_map { - interfaces_string = append(interfaces_string, key+":"+strings.Join(value, " ")) - } - - if _, ok := host_metrics["interfaces"]; ok { - host_metrics["interfaces"] = strings.Join(interfaces_string, ";") - } - - return &host_metrics -} - -func ProcessToMap(process *graph.Process) *map[string]string { - uids_string := []string{} - for _, u := range process.Uids { - uids_string = append(uids_string, strconv.Itoa(int(u))) - } - - gids_string := []string{} - for _, g := range process.Gids { - gids_string = append(gids_string, strconv.Itoa(int(g))) - } - - openfiles_string := []string{} - for _, of := range process.OpenFiles { - openfiles_string = append(openfiles_string, strconv.Itoa(int(of.Fd))+":"+of.Path) - } - - cpid_string := []string{} - for _, cid := range process.Cpid { - cpid_string = append(cpid_string, strconv.Itoa(int(cid))) - } - - return &map[string]string{ - "Pid": strconv.Itoa(int(process.Pid)), - "Ppid": strconv.Itoa(int(process.Ppid)), - "Cpid": strings.Join(cpid_string, " "), - "Uids": strings.Join(uids_string, " "), - "Gids": strings.Join(gids_string, " "), - "Status": process.Status, - "CreateTime": strconv.Itoa(int(process.CreateTime)), - "Cwd": process.Cwd, - "ExePath": process.ExePath, - "Cmdline": process.Cmdline, - "Nice": strconv.Itoa(int(process.Nice)), - "IOnice": strconv.Itoa(int(process.IOnice)), - "DISK-rc": strconv.Itoa(int(process.IOCounters.ReadCount)), - "DISK-rb": strconv.Itoa(int(process.IOCounters.ReadBytes)), - "DISK-wc": strconv.Itoa(int(process.IOCounters.WriteCount)), - "DISK-wb": strconv.Itoa(int(process.IOCounters.WriteBytes)), - "fd": strings.Join(openfiles_string, " "), - "NumCtxSwitches-v": strconv.Itoa(int(process.NumCtxSwitches.Voluntary)), - "NumCtxSwitches-inv": strconv.Itoa(int(process.NumCtxSwitches.Involuntary)), - "PageFaults-MinorFaults": strconv.Itoa(int(process.PageFaults.MinorFaults)), - "PageFaults-MajorFaults": strconv.Itoa(int(process.PageFaults.MajorFaults)), - "PageFaults-ChildMinorFaults": strconv.Itoa(int(process.PageFaults.ChildMinorFaults)), - "PageFaults-ChildMajorFaults": strconv.Itoa(int(process.PageFaults.ChildMajorFaults)), - "CPUPercent": strconv.FormatFloat(process.CPUPercent, 'f', -1, 64), - "MemoryPercent": strconv.FormatFloat(process.MemoryPercent, 'f', -1, 64), - "MemoryInfo": process.MemoryInfo.String(), - } -} - -func ThreadToMap(thread *graph.Thread) *map[string]string { - return &map[string]string{ - "Tid": strconv.Itoa(int(thread.Tid)), - "Tgid": strconv.Itoa(int(thread.Tgid)), - "CPU": thread.CPU, - "User": strconv.FormatFloat(thread.User, 'f', -1, 64), - "System": strconv.FormatFloat(thread.System, 'f', -1, 64), - "Idle": strconv.FormatFloat(thread.Idle, 'f', -1, 64), - "Nice": strconv.FormatFloat(thread.Nice, 'f', -1, 64), - "Iowait": strconv.FormatFloat(thread.Iowait, 'f', -1, 64), - "Irq": strconv.FormatFloat(thread.Irq, 'f', -1, 64), - "Softirq": strconv.FormatFloat(thread.Softirq, 'f', -1, 64), - "Steal": strconv.FormatFloat(thread.Steal, 'f', -1, 64), - "Guest": strconv.FormatFloat(thread.Guest, 'f', -1, 64), - "GuestNice": strconv.FormatFloat(thread.GuestNice, 'f', -1, 64), - } -} - -// net节点的metrics字段 临时定义 -func NetToMap(net *graph.Netconnection) *map[string]string { - uids_string := []string{} - for _, uid := range net.Uids { - uids_string = append(uids_string, strconv.Itoa(int(uid))) - } - - return &map[string]string{ - "Fd": strconv.Itoa(int(net.Fd)), - "Family": strconv.Itoa(int(net.Family)), - "Type": strconv.Itoa(int(net.Type)), - "Laddr": net.Laddr, - "Raddr": net.Raddr, - "Status": net.Status, - "Uids": strings.Join(uids_string, " "), - "Pid": strconv.Itoa(int(net.Pid)), - } -} - -// func NetToMap(net *net.IOCountersStat, a_i_map *map[string][]string) *map[string]string { -// addrs := []string{} -// for key, value := range *a_i_map { -// if net.Name == key { -// addrs = value -// } -// } - -// return &map[string]string{ -// "Name": net.Name, -// "addrs": addrs[0], -// "BytesSent": strconv.Itoa(int(net.BytesSent)), -// "BytesRecv": strconv.Itoa(int(net.BytesRecv)), -// "PacketsSent": strconv.Itoa(int(net.PacketsSent)), -// "PacketsRecv": strconv.Itoa(int(net.PacketsRecv)), -// "Errin": strconv.Itoa(int(net.Errin)), -// "Errout": strconv.Itoa(int(net.Errout)), -// "Dropin": strconv.Itoa(int(net.Dropin)), -// "Dropout": strconv.Itoa(int(net.Dropout)), -// "Fifoin": strconv.Itoa(int(net.Fifoin)), -// "Fifoout": strconv.Itoa(int(net.Fifoout)), -// } -// } - -func DiskToMap(disk *graph.Disk) *map[string]string { - disk_map := make(map[string]string) - partition_map := StructToMap(disk.Partition) - iocounter_map := StructToMap(disk.IOcounter) - usage_map := StructToMap(disk.Usage) - - for k, v := range partition_map { - disk_map[k] = v - } - - for k, v := range iocounter_map { - if k != "Name" { - disk_map[k] = v - } - } - - for k, v := range usage_map { - if k != "Path" && k != "Fstype" { - disk_map[k] = v - } - } - - return &disk_map -} - -func CpuToMap(cpu *graph.Cpu) *map[string]string { - cpu_map := make(map[string]string) - info_map := StructToMap(cpu.Info) - time_map := StructToMap(cpu.Time) - - for k, v := range info_map { - if k != "Flags" { - cpu_map[k] = v - } - } - - for k, v := range time_map { - if k != "CPU" { - cpu_map[k] = v - } - } - - return &cpu_map -} - -func InterfaceToMap(iface *graph.NetIOcounter) *map[string]string { - iface_map := make(map[string]string) - old_map := StructToMap(iface) - - for k, v := range old_map { - if k != "Name" { - iface_map[k] = v - } - } - - return &iface_map -}