// New takes a driver and returns a new manager. func New(driver storage.StorageDriver, sysfs sysfs.SysFs) (Manager, error) { if driver == nil { return nil, fmt.Errorf("nil storage driver!") } // Detect the container we are running on. selfContainer, err := cgroups.GetThisCgroupDir("cpu") if err != nil { return nil, err } glog.Infof("cAdvisor running in container: %q", selfContainer) newManager := &manager{ containers: make(map[namespacedContainerName]*containerData), quitChannels: make([]chan error, 0, 2), storageDriver: driver, cadvisorContainer: selfContainer, } machineInfo, err := getMachineInfo(sysfs) if err != nil { return nil, err } newManager.machineInfo = *machineInfo glog.Infof("Machine: %+v", newManager.machineInfo) versionInfo, err := getVersionInfo() if err != nil { return nil, err } newManager.versionInfo = *versionInfo glog.Infof("Version: %+v", newManager.versionInfo) return newManager, nil }
// New takes a memory storage and returns a new manager. func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingInterval time.Duration, allowDynamicHousekeeping bool) (Manager, error) { if memoryCache == nil { return nil, fmt.Errorf("manager requires memory storage") } // Detect the container we are running on. selfContainer, err := cgroups.GetThisCgroupDir("cpu") if err != nil { return nil, err } glog.Infof("cAdvisor running in container: %q", selfContainer) dockerInfo, err := docker.DockerInfo() if err != nil { glog.Warningf("Unable to connect to Docker: %v", err) } context := fs.Context{DockerRoot: docker.RootDir(), DockerInfo: dockerInfo} fsInfo, err := fs.NewFsInfo(context) if err != nil { return nil, err } // If cAdvisor was started with host's rootfs mounted, assume that its running // in its own namespaces. inHostNamespace := false if _, err := os.Stat("/rootfs/proc"); os.IsNotExist(err) { inHostNamespace = true } newManager := &manager{ containers: make(map[namespacedContainerName]*containerData), quitChannels: make([]chan error, 0, 2), memoryCache: memoryCache, fsInfo: fsInfo, cadvisorContainer: selfContainer, inHostNamespace: inHostNamespace, startupTime: time.Now(), maxHousekeepingInterval: maxHousekeepingInterval, allowDynamicHousekeeping: allowDynamicHousekeeping, } machineInfo, err := getMachineInfo(sysfs, fsInfo) if err != nil { return nil, err } newManager.machineInfo = *machineInfo glog.Infof("Machine: %+v", newManager.machineInfo) versionInfo, err := getVersionInfo() if err != nil { return nil, err } glog.Infof("Version: %+v", *versionInfo) newManager.eventHandler = events.NewEventManager(parseEventsStoragePolicy()) return newManager, nil }
// New takes a memory storage and returns a new manager. func New(memoryStorage *memory.InMemoryStorage, sysfs sysfs.SysFs) (Manager, error) { if memoryStorage == nil { return nil, fmt.Errorf("manager requires memory storage") } // Detect the container we are running on. selfContainer, err := cgroups.GetThisCgroupDir("cpu") if err != nil { return nil, err } glog.Infof("cAdvisor running in container: %q", selfContainer) context := fs.Context{DockerRoot: docker.RootDir()} fsInfo, err := fs.NewFsInfo(context) if err != nil { return nil, err } newManager := &manager{ containers: make(map[namespacedContainerName]*containerData), quitChannels: make([]chan error, 0, 2), memoryStorage: memoryStorage, fsInfo: fsInfo, cadvisorContainer: selfContainer, startupTime: time.Now(), } machineInfo, err := getMachineInfo(sysfs, fsInfo) if err != nil { return nil, err } newManager.machineInfo = *machineInfo glog.Infof("Machine: %+v", newManager.machineInfo) versionInfo, err := getVersionInfo() if err != nil { return nil, err } newManager.versionInfo = *versionInfo glog.Infof("Version: %+v", newManager.versionInfo) newManager.eventHandler = events.NewEventManager() // Register Docker container factory. err = docker.Register(newManager, fsInfo) if err != nil { glog.Errorf("Docker container factory registration failed: %v.", err) } // Register the raw driver. err = raw.Register(newManager, fsInfo) if err != nil { glog.Errorf("Registration of the raw container factory failed: %v", err) } return newManager, nil }
func findCgroupRootAndDir(subsystem string) (string, string, error) { cgroupRoot, err := cgroups.FindCgroupMountpoint(subsystem) if err != nil { return "", "", err } cgroupDir, err := cgroups.GetThisCgroupDir(subsystem) if err != nil { return "", "", err } return cgroupRoot, cgroupDir, nil }
func (d *driver) GetPidsForContainer(id string) ([]int, error) { pids := []int{} // cpu is chosen because it is the only non optional subsystem in cgroups subsystem := "cpu" cgroupRoot, err := cgroups.FindCgroupMountpoint(subsystem) if err != nil { return pids, err } cgroupDir, err := cgroups.GetThisCgroupDir(subsystem) if err != nil { return pids, err } filename := filepath.Join(cgroupRoot, cgroupDir, id, "tasks") if _, err := os.Stat(filename); os.IsNotExist(err) { // With more recent lxc versions use, cgroup will be in lxc/ filename = filepath.Join(cgroupRoot, cgroupDir, "lxc", id, "tasks") } output, err := ioutil.ReadFile(filename) if err != nil { return pids, err } for _, p := range strings.Split(string(output), "\n") { if len(p) == 0 { continue } pid, err := strconv.Atoi(p) if err != nil { return pids, fmt.Errorf("Invalid pid '%s': %s", p, err) } pids = append(pids, pid) } return pids, nil }
func detectDockerContainer() (string, error) { return cgroups.GetThisCgroupDir("cpu") }