Example #1
0
func main() {

	if len(os.Args) < 2 {
		log.Fatalf("commad must has one parameters!\n")
		return
	}
	var testingProject = os.Args[1] //"docker"  or  "rkt"
	if testingProject != "docker" && testingProject != "rkt" {
		log.Fatalf("commad is %v %v, is not corrected!\n", os.Args[0], os.Args[1])
		return
	}

	var containers []string
	client, err := client.NewClient("http://localhost:8080/")
	if err != nil {
		log.Fatalf("tried to make client and got error %v\n", err)
		return
	}

	switch testingProject {
	case "docker":
		containers, err = adaptor.GetDockerContainers()
	case "rkt":
		containers, err = adaptor.GetRktContainers()
	default:
		return
	}
	if err != nil {
		log.Fatalf("getContainerName fail, error: %v\n", err)
		return
	}
	//cpuusageinfo := new(CpuUsageInfo)
	mInfo, err := client.MachineInfo()
	var jsonString []byte
	for _, container := range containers {
		fmt.Printf("container %v's cpu info: \n", container)
		cInfo, err := getContainerInfo(client, container)
		if err != nil {
			fmt.Printf("getContainerInfo fail and got error %v\n", err)
			return
		}
		cpuUsageInfo := new(CpuUsageInfo)
		cpuUsageInfo.ContainerID = cInfo.Name
		//fmt.Println(cpuUsageInfo.ContainerID)
		//var usageInfo CpuUsageInfo
		err = getCpu(cInfo, mInfo, cpuUsageInfo)
		fmt.Println(cpuUsageInfo)

		//fmt.Println(usageInfo.ContainerID)
		jsonString, err = json.Marshal(cpuUsageInfo)
		if err != nil {
			log.Fatalf("convert to json err, error:  %v\n", err)
			return
		}

	}
	err = ioutil.WriteFile("./"+testingProject+"_cpu.json", []byte(jsonString), 0666)

}
Example #2
0
func main() {

	if len(os.Args) < 2 {
		log.Fatalf("commad must has one parameters!\n")
		return
	}
	var testingProject = os.Args[1] //"docker"  or  "rkt"
	if testingProject != "docker" && testingProject != "rkt" {
		log.Fatalf("commad is %v %v, is not corrected!\n", os.Args[0], os.Args[1])
		return
	}

	var containers []string
	client, err := client.NewClient("http://localhost:8080/")
	if err != nil {
		log.Fatalf("tried to make client and got error %v\n", err)
		return
	}

	switch testingProject {
	case "docker":
		containers, err = adaptor.GetDockerContainers()
	case "rkt":
		containers, err = adaptor.GetRktContainers()
	default:
		return
	}
	if err != nil {
		log.Fatalf("getContainerName fail, error: %v\n", err)
		return
	}

	mInfo, err := client.MachineInfo()
	var jsonString []byte
	for _, container := range containers {
		//Get container info struct from cadvisor client
		cInfo, err := getContainerInfo(client, container)
		if err != nil {
			log.Fatalf("getContainerInfo fail and got error %v\n", err)
			return
		}
		var cpuArray []CpuUsageInfo
		cpuArray = []CpuUsageInfo{}
		cpuUsageInfo := new(CpuUsageInfo)
		cpuUsageInfo.Usage.PerCoreUsage = make(map[string]float64)
		cpuUsageInfo.ContainerID = cInfo.Name

		// Get cpu usage and store  them to result(cpuArray)
		err, result := getCpu(cInfo, mInfo, cpuUsageInfo, cpuArray)
		if err != nil {
			log.Fatalf("Get cpuusage err, error:  %v\n", err)
			return
		}

		//Conver to json
		jsonString, err = json.Marshal(result)
		if err != nil {
			log.Fatalf("convert to json err, error:  %v\n", err)
			return
		}

	}

	//Output to docker_cpu.json file
	err = ioutil.WriteFile("./"+testingProject+"_cpu.json", []byte(jsonString), 0666)

}