// 上报docker事件 func reportEvents() error { job := Eng.Job("events", "DockerAgent") // 从当前到3214080000(100年)后,^-^100后我都不在了,还需要考虑超时吗 job.Setenv("since", fmt.Sprint(time.Now().Unix())) job.Setenv("until", fmt.Sprint(time.Now().Unix()+3214080000)) reader, err := job.Stdout.AddPipe() if err != nil { log.Fatalf("Create event receive pipe error:%s", err) } // 从管道读取事件数据并广播给所有controller go func() { dec := json.NewDecoder(reader) for { m := &dockerUtils.JSONMessage{} if err := dec.Decode(m); err != nil { log.Printf("Error streaming events: %s\n", err) break } if b, err := json.Marshal(m); err == nil { // 广播 log.Println("Event:", string(b)) content := utils.PacketByes(append(b, " docker_event"...)) ClusterSwitcher.Broadcast(content) } } }() return job.Run() }
// 上报docker主机状态 func reportStatus() { log.Println("Report status start") tick := time.Tick(time.Duration(5) * time.Second) for { select { case <-tick: systemInfo, err := utils.GetSystemInfo() if err != nil { log.Println("Get system info error:", err) } // 包含cpu使用率,内存,交换区和负载信息 systemInfoBytes, err := json.Marshal(systemInfo) if err != nil { log.Println("Encode system info error:", err) } log.Println("Report status...") data := utils.PacketByes(append(systemInfoBytes, " docker_status"...)) ClusterSwitcher.Broadcast(data) } } log.Println("Report status finish") }
// controller连接到docker的连接断开,广播给其他controller func ControllerDisconnection(c *utils.Connection, data []byte) { address := string(data) log.Println("controller:", address, "is offline.") delete(config.Controllers, address) cluster.ClusterSwitcher.Broadcast(utils.PacketByes(append(data, " controller_offline"...))) }