/** get resource usage of application (pods) on node **/ func GetAppResource(namespace, name string) thoth.AppMetric { var summary_cpu float32 var memory_bundle []*docker.CgroupMemStat container_ids, pod_ips, err := GetContainerIDList(thoth.KubeApi, name, namespace) if err != nil { panic(err) } for _, container_id := range container_ids { // fmt.Println(container_id, pod_ips) // calculation percentage of cpu usage container_cpu, _ := DockerCPUPercent(container_id) summary_cpu += container_cpu // memory usage container_memory, _ := docker.CgroupMemDocker(container_id) memory_bundle = append(memory_bundle, container_memory) } podNum := len(pod_ips) /* */ //fmt.Println("rps: ", rps, ", rtime: ", rtime) // find the cpu avarage of application cpu usage average_cpu := summary_cpu / float32(len(container_ids)) // Cal Avg Mem usage var avgMem uint64 for i := 0; i < podNum; i++ { if memory_bundle[i] != nil { avgMem += memory_bundle[i].MemUsageInBytes } } avgMem = avgMem / uint64(podNum) avgMem = avgMem / uint64(1024*1024) // MB // Convert to percentage 100%=200MB avgMem = avgMem / 2 // create appliction object app_metric := thoth.AppMetric{ App: name, Cpu: average_cpu, Memory: int64(avgMem), /* Request: rps_int, Response: rtime_int, Response2xx: res2xx_int, Response4xx: res4xx_int, Response5xx: res5xx_int, */ } return app_metric }
func (s *systemPS) DockerStat() ([]*DockerContainerStat, error) { if s.dockerClient == nil { c, err := dc.NewClient("unix:///var/run/docker.sock") if err != nil { return nil, err } s.dockerClient = c } opts := dc.ListContainersOptions{} containers, err := s.dockerClient.ListContainers(opts) if err != nil { if _, ok := err.(*gonet.OpError); ok { return nil, nil } return nil, err } var stats []*DockerContainerStat for _, container := range containers { ctu, err := docker.CgroupCPUDocker(container.ID) if err != nil { return nil, err } mem, err := docker.CgroupMemDocker(container.ID) if err != nil { return nil, err } name := strings.Join(container.Names, " ") stats = append(stats, &DockerContainerStat{ Id: container.ID, Name: name, Command: container.Command, Labels: container.Labels, CPU: ctu, Mem: mem, }) } return stats, nil }
/** get resource usage of application (pods) on node **/ func GetAppResource(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) // get app Name appName := vars["appName"] namespace := vars["namespace"] var summary_cpu float32 var memory_bundle []*docker.CgroupMemStat container_ids, pod_ips, err := GetContainerIDList(thoth.KubeApi, appName, namespace) if err != nil { fmt.Println(err) } for _, container_id := range container_ids { fmt.Println(container_id, pod_ips) // calculation percentage of cpu usage container_cpu, _ := DockerCPUPercent(container_id) summary_cpu += container_cpu // memory usage container_memory, _ := docker.CgroupMemDocker(container_id) memory_bundle = append(memory_bundle, container_memory) } podNum := len(pod_ips) // find the request per sec from haproxy-frontend res_front, err := http.Get(thoth.VampApi + "/v1/stats/frontends") if err != nil { panic(err) } body_front, err := ioutil.ReadAll(res_front.Body) res_front.Body.Close() if err != nil { panic(err) } //var rps uint64 var object_front []map[string]interface{} err = json.Unmarshal([]byte(body_front), &object_front) rps := object_front[0]["req_rate"].(string) rps_int, _ := strconv.ParseInt(rps, 10, 64) if err == nil { } else { fmt.Println(err) } //find resonse time from haproxy-backends //var rtime uint64 res_back, err := http.Get(thoth.VampApi + "/v1/stats/backends") if err != nil { panic(err) } body_back, err := ioutil.ReadAll(res_back.Body) res_back.Body.Close() if err != nil { panic(err) } var object_back []map[string]interface{} err = json.Unmarshal([]byte(body_back), &object_back) rtime := object_back[0]["rtime"].(string) res_2xx := object_back[0]["hrsp_2xx"].(string) res_4xx := object_back[0]["hrsp_4xx"].(string) res_5xx := object_back[0]["hrsp_5xx"].(string) rtime_int, _ := strconv.ParseInt(rtime, 10, 64) res2xx_int, _ := strconv.ParseInt(res_2xx, 10, 64) res4xx_int, _ := strconv.ParseInt(res_4xx, 10, 64) res5xx_int, _ := strconv.ParseInt(res_5xx, 10, 64) if err == nil { } else { fmt.Println(err) } fmt.Println("rps: ", rps, ", rtime: ", rtime) // find the cpu avarage of application cpu usage average_cpu := summary_cpu / float32(len(container_ids)) fmt.Println("avg_cpu : ", average_cpu) fmt.Println("summary_cpu : ", summary_cpu) // Cal Avg Mem usage var avgMem uint64 for i := 0; i < podNum; i++ { avgMem += memory_bundle[i].MemUsageInBytes } avgMem = avgMem / uint64(podNum) avgMem = avgMem / uint64(1024*1024) // MB // create appliction object app_metric := thoth.AppMetric{ App: appName, Cpu: average_cpu, Memory: int64(avgMem), Request: rps_int, Response: rtime_int, Response2xx: res2xx_int, Response4xx: res4xx_int, Response5xx: res5xx_int, } app_json, err := json.MarshalIndent(app_metric, "", "\t") if err != nil { fmt.Println("error:", err) } fmt.Fprint(w, string(app_json)) }