示例#1
0
// Update node information by CAdvisor
func (core *Core) updateNodesByCAdvisor() {
	for _, node := range core.GetAllNodes() {
		if node.Host != "" {
			client, err := client.NewClient("http://" + node.Host + ":" + core.GetAgentLisenPort())
			if err != nil {
				log.Errorf("Cannot connect to cadvisor agent: %v", err)
				node.DockerDaemonHealth = registry.DockerDaemonDown
				continue
			}

			node.DockerDaemonHealth = registry.DockerDaemonUp
			// Fetch software versions and hardware information
			// One node fetches just one time
			if !node.MachineInfoFetched {
				attributes, err := client.Attributes()
				if err != nil {
					log.Errorf("Fetch machine info failed: %v", err)
					continue
				}
				node.NumCores = attributes.NumCores
				node.KernelVersion = attributes.KernelVersion
				node.CPUFrequency = attributes.CpuFrequency
				node.ContainerOsVersion = attributes.ContainerOsVersion
				node.DockerVersion = attributes.DockerVersion
				node.MemoryCapacity = attributes.MemoryCapacity
				node.MachineInfoFetched = true
			}

			request := info.RequestOptions{
				IdType:    "docker",
				Count:     5,
				Recursive: true,
			}
			containerInfo, err := client.Stats("", &request)
			if err != nil {
				log.Errorf("Fetch container info failed: %v", err)
			}

			node.Containers = make([]string, 0, len(containerInfo))
			for container, info := range containerInfo {
				node.Containers = append(node.Containers, container)
				if len(info.Stats) > 1 {
					lastStats := info.Stats[len(info.Stats)-2]
					currentStats := info.Stats[len(info.Stats)-1]

					// ms -> ns.
					timeInterval := float64((currentStats.Timestamp.Unix() - lastStats.Timestamp.Unix()) * 1000000)
					node.CPUUsage = float64(currentStats.Cpu.Usage.Total-lastStats.Cpu.Usage.Total) / timeInterval
					node.MemoryUsage = currentStats.Memory.Usage
					node.LastUpdateTime = time.Now().Unix()
				}
			}
		}
	}
}
示例#2
0
func (core *Core) updateTasksByCAdvisor() {
	for _, task := range core.GetAllTasks() {
		if task.State == "TASK_RUNNING" && task.DockerID != "" && task.SlaveID != "" {
			node, _ := core.GetNode(task.SlaveID)
			client, err := client.NewClient("http://" + node.Host + ":" + core.GetAgentLisenPort())
			if err != nil {
				log.Errorf("Cannot connect to cadvisor agent: %v", err)
				continue
			}

			request := info.RequestOptions{
				IdType: "docker",
				Count:  15,
			}
			containerInfo, err := client.Stats(task.DockerID, &request)
			if err != nil {
				log.Errorf("Fetch container info failed: %v", err)
			}

			var cpuStats []*registry.Usage
			var memoryStats []*registry.Usage
			for _, containerInfo := range containerInfo {
				for _, containerStats := range containerInfo.Stats {
					cpuStats = append(cpuStats, &registry.Usage{
						Total:     containerStats.Cpu.Usage.Total,
						Timestamp: containerStats.Timestamp,
					})
					memoryStats = append(memoryStats, &registry.Usage{
						Total:     containerStats.Memory.Usage,
						Timestamp: containerStats.Timestamp,
					})
				}
			}
			task.CPUUsage = cpuStats
			task.MemoryUsage = memoryStats
		}
		task.LastUpdateTime = time.Now().Unix()
	}
}