Example #1
0
func TestGetUsage(t *testing.T) {
	// Need Root permission
	/* err := exec.Command(LXC_START_COMMAND, "-d", "--name", "test_cpu_container", "/usr/bin/sleep 10").Run() */
	/* if err != nil { */
	/*   t.Fatal("Please install", LXC_START_COMMAND, err) */
	/* } */

	containers, err := lxc.ListContainers()
	if err != nil {
		t.Fatal(err)
	}
	if len(containers) == 0 {
		t.Log("There isn't any existing container, please create one")
		t.Log("Example : ")
		t.Log("\t# lxc-start -d --name test_cpu_container /usr/bin/sleep 10")
		t.FailNow()
	}

	go Monitor()

	time.Sleep(2 * time.Second)

	cpuUsage := GetUsage(containers[0])
	if cpuUsage < 0 || cpuUsage > 100 {
		t.Fatal("CPU usage out of [0,100]")
	}
}
Example #2
0
func Monitor() {
	tick := time.NewTicker(REFRESH_TIME * time.Second)
	for {
		<-tick.C
		for k, v := range cpuUsages {
			previousCpuUsages[k] = v
		}

		containers, err := lxc.ListContainers()
		if err != nil {
			log.Println(err)
			continue
		}

		for _, container := range containers {
			n, err := cpuacctUsage(container)
			if err != nil {
				log.Println(err)
			}
			cpuUsages[container] = n
		}
	}
}