Exemplo n.º 1
0
// Creates a cAdvisor and exports its API on the specified port if port > 0.
func New(port uint) (Interface, error) {
	sysFs, err := sysfs.NewRealSysFs()
	if err != nil {
		return nil, err
	}

	// Create and start the cAdvisor container manager.
	m, err := manager.New(memory.New(statsToCache, nil), sysFs)
	if err != nil {
		return nil, err
	}
	err = m.Start()
	if err != nil {
		return nil, err
	}

	cadvisorClient := &cadvisorClient{
		Manager: m,
	}

	// Export the HTTP endpoint if a port was specified.
	if port > 0 {
		err = cadvisorClient.exportHTTP(port)
		if err != nil {
			return nil, err
		}
	}

	return cadvisorClient, nil
}
Exemplo n.º 2
0
func NewStorageDriver(driverName string) (storage.StorageDriver, error) {
	var storageDriver storage.StorageDriver
	var err error
	// TODO(vmarmol): We shouldn't need the housekeeping interval here and it shouldn't be public.
	samplesToCache := int(*argDbBufferDuration / *manager.HousekeepingInterval)
	if samplesToCache < statsRequestedByUI {
		// The UI requests the most recent 60 stats by default.
		samplesToCache = statsRequestedByUI
	}
	switch driverName {
	case "":
		// empty string by default is the in memory store
		fallthrough
	case "memory":
		storageDriver = memory.New(*argSampleSize, int(*argDbBufferDuration))
		return storageDriver, nil
	case "influxdb":
		var hostname string
		hostname, err = os.Hostname()
		if err != nil {
			return nil, err
		}

		storageDriver, err = influxdb.New(
			hostname,
			"stats",
			*argDbName,
			*argDbUsername,
			*argDbPassword,
			*argDbHost,
			*argDbIsSecure,
			*argDbBufferDuration,
			// TODO(monnand): One hour? Or user-defined?
			1*time.Hour,
		)
		glog.V(2).Infof("Caching %d recent stats in memory\n", samplesToCache)
		storageDriver = cache.MemoryCache(samplesToCache, samplesToCache, storageDriver)
	case "bigquery":
		var hostname string
		hostname, err = os.Hostname()
		if err != nil {
			return nil, err
		}
		storageDriver, err = bigquery.New(
			hostname,
			"cadvisor",
			*argDbName,
			1*time.Hour,
		)
		glog.V(2).Infof("Caching %d recent stats in memory\n", samplesToCache)
		storageDriver = cache.MemoryCache(samplesToCache, samplesToCache, storageDriver)

	default:
		err = fmt.Errorf("Unknown database driver: %v", *argDbDriver)
	}
	if err != nil {
		return nil, err
	}
	return storageDriver, nil
}
Exemplo n.º 3
0
func TestPercentiles(t *testing.T) {
	N := 100
	testDriver := &cacheTestStorageDriver{}
	backend := memory.New(N*2, N*2)
	testDriver.base = MemoryCache(N, N, backend)
	test.StorageDriverTestPercentiles(testDriver, t)
}
Exemplo n.º 4
0
func main() {
	flag.Parse()

	storage := memory.New(*argSampleSize, *argHistoryDuration)
	// TODO(monnand): Add stats writer for manager
	containerManager, err := manager.New(storage)
	if err != nil {
		log.Fatalf("Failed to create a Container Manager: %s", err)
	}

	if err := lmctfy.Register("/"); err != nil {
		log.Printf("lmctfy registration failed: %v.", err)
		log.Print("Running in docker only mode.")
		if err := docker.Register(containerManager, "/"); err != nil {
			log.Printf("Docker registration failed: %v.", err)
			log.Fatalf("Unable to continue without docker or lmctfy.")
		}
	}

	if err := docker.Register(containerManager, "/docker"); err != nil {
		// Ignore this error because we should work with lmctfy only
		log.Printf("Docker registration failed: %v.", err)
		log.Print("Running in lmctfy only mode.")
	}

	// Handler for static content.
	http.HandleFunc(static.StaticResource, func(w http.ResponseWriter, r *http.Request) {
		err := static.HandleRequest(w, r.URL)
		if err != nil {
			fmt.Fprintf(w, "%s", err)
		}
	})

	// Handler for the API.
	http.HandleFunc(api.ApiResource, func(w http.ResponseWriter, r *http.Request) {
		err := api.HandleRequest(containerManager, w, r.URL)
		if err != nil {
			fmt.Fprintf(w, "%s", err)
		}
	})

	// Redirect / to containers page.
	http.Handle("/", http.RedirectHandler(pages.ContainersPage, http.StatusTemporaryRedirect))

	// Register the handler for the containers page.
	http.HandleFunc(pages.ContainersPage, func(w http.ResponseWriter, r *http.Request) {
		err := pages.ServerContainersPage(containerManager, w, r.URL)
		if err != nil {
			fmt.Fprintf(w, "%s", err)
		}
	})

	go containerManager.Start()

	log.Printf("Starting cAdvisor version: %q", info.VERSION)
	log.Print("About to serve on port ", *argPort)

	addr := fmt.Sprintf(":%v", *argPort)
	log.Fatal(http.ListenAndServe(addr, nil))
}
Exemplo n.º 5
0
func TestPercentiles(t *testing.T) {
	N := 100
	backend := memory.New(N*2, N*2)
	driver := MemoryCache(N, N, backend)
	testDriver := test.TestStorageDriver{Driver: driver, StatsEq: test.DefaultStatsEq}
	test.StorageDriverTestPercentiles(testDriver, t)
}
Exemplo n.º 6
0
// TODO(vishh): Cache all samples for a given duration and do not cap the maximum number of samples. This is useful if we happen to change the housekeeping duration.
func MemoryCache(maxNumSamplesInCache, maxNumStatsInCache int, driver storage.StorageDriver) storage.StorageDriver {
	return &cachedStorageDriver{
		maxNumStatsInCache:   maxNumStatsInCache,
		maxNumSamplesInCache: maxNumSamplesInCache,
		cache:                memory.New(maxNumSamplesInCache, maxNumStatsInCache),
		backend:              driver,
	}
}
Exemplo n.º 7
0
func NewStorageDriver(driverName string) (*memory.InMemoryStorage, error) {
	var storageDriver *memory.InMemoryStorage
	var backendStorage storage.StorageDriver
	var err error
	// TODO(vmarmol): We shouldn't need the housekeeping interval here and it shouldn't be public.
	samplesToCache := int(*argDbBufferDuration / *manager.HousekeepingInterval)
	if samplesToCache < statsRequestedByUI {
		// The UI requests the most recent 60 stats by default.
		samplesToCache = statsRequestedByUI
	}
	switch driverName {
	case "":
		backendStorage = nil
	case "influxdb":
		var hostname string
		hostname, err = os.Hostname()
		if err != nil {
			return nil, err
		}

		backendStorage, err = influxdb.New(
			hostname,
			"stats",
			*argDbName,
			*argDbUsername,
			*argDbPassword,
			*argDbHost,
			*argDbIsSecure,
			*argDbBufferDuration,
			// TODO(monnand): One hour? Or user-defined?
			1*time.Hour,
		)
	case "bigquery":
		var hostname string
		hostname, err = os.Hostname()
		if err != nil {
			return nil, err
		}
		backendStorage, err = bigquery.New(
			hostname,
			"cadvisor",
			*argDbName,
			1*time.Hour,
		)

	default:
		err = fmt.Errorf("Unknown database driver: %v", *argDbDriver)
	}
	if err != nil {
		return nil, err
	}
	glog.Infof("Caching %d recent stats in memory; using \"%v\" storage driver\n", samplesToCache, driverName)
	storageDriver = memory.New(samplesToCache, samplesToCache, backendStorage)
	return storageDriver, nil
}
Exemplo n.º 8
0
func runStorageTest(f func(storage.StorageDriver, *testing.T), t *testing.T) {
	maxSize := 200

	var driver storage.StorageDriver
	for N := 10; N < maxSize; N += 10 {
		backend := memory.New(N*2, N*2)
		driver = MemoryCache(N, N, backend)
		f(driver, t)
	}

}
Exemplo n.º 9
0
func runStorageTest(f func(test.TestStorageDriver, *testing.T), t *testing.T) {
	maxSize := 200

	for N := 10; N < maxSize; N += 10 {
		testDriver := &cacheTestStorageDriver{}
		backend := memory.New(N*2, N*2)
		testDriver.base = MemoryCache(N, N, backend)
		f(testDriver, t)
	}

}
Exemplo n.º 10
0
// Create a containerData instance for a test.
func setupContainerData(t *testing.T, spec info.ContainerSpec) (*containerData, *container.MockContainerHandler, *memory.InMemoryStorage) {
	mockHandler := container.NewMockContainerHandler(containerName)
	mockHandler.On("GetSpec").Return(
		spec,
		nil,
	)
	memoryStorage := memory.New(60, nil)
	ret, err := newContainerData(containerName, memoryStorage, mockHandler, nil, false)
	if err != nil {
		t.Fatal(err)
	}
	return ret, mockHandler, memoryStorage
}
Exemplo n.º 11
0
func runStorageTest(f func(test.TestStorageDriver, *testing.T), t *testing.T) {
	maxSize := 200

	var driver storage.StorageDriver
	var testDriver test.TestStorageDriver
	testDriver.StatsEq = test.DefaultStatsEq
	for N := 10; N < maxSize; N += 10 {
		backend := memory.New(N*2, N*2)
		driver = MemoryCache(N, N, backend)
		testDriver.Driver = driver
		f(testDriver, t)
	}

}
Exemplo n.º 12
0
func initContainerManager() {
	sysFs, err := sysfs.NewRealSysFs()
	if err != nil {
		log.Infof("stats: failed to create a system interface (%v)", err)
		return
	}
	// TODO: support influxdb or other backend storage
	cmgr, err = manager.New(memory.New(storageDuration, nil), sysFs)
	if err != nil {
		log.Infof("stats: failed to create a container Manager (%v)", err)
		return
	}
	if err := cmgr.Start(); err != nil {
		log.Infof("stats: failed to start container manager (%v)", err)
		return
	}
}
Exemplo n.º 13
0
// Expect a manager with the specified containers and query. Returns the manager, map of ContainerInfo objects,
// and map of MockContainerHandler objects.}
func expectManagerWithContainers(containers []string, query *info.ContainerInfoRequest, t *testing.T) (*manager, map[string]*info.ContainerInfo, map[string]*container.MockContainerHandler) {
	infosMap := make(map[string]*info.ContainerInfo, len(containers))
	handlerMap := make(map[string]*container.MockContainerHandler, len(containers))

	for _, container := range containers {
		infosMap[container] = itest.GenerateRandomContainerInfo(container, 4, query, 1*time.Second)
	}

	memoryStorage := memory.New(query.NumStats, nil)
	sysfs := &fakesysfs.FakeSysFs{}
	m := createManagerAndAddContainers(
		memoryStorage,
		sysfs,
		containers,
		func(h *container.MockContainerHandler) {
			cinfo := infosMap[h.Name]
			ref, err := h.ContainerReference()
			if err != nil {
				t.Error(err)
			}
			for _, stat := range cinfo.Stats {
				err = memoryStorage.AddStats(ref, stat)
				if err != nil {
					t.Error(err)
				}
			}
			spec := cinfo.Spec

			h.On("ListContainers", container.ListSelf).Return(
				[]info.ContainerReference(nil),
				nil,
			)
			h.On("GetSpec").Return(
				spec,
				nil,
			)
			handlerMap[h.Name] = h
		},
		t,
	)

	return m, infosMap, handlerMap
}
Exemplo n.º 14
0
func NewStorageDriver(driverName string) (storage.StorageDriver, error) {
	var storageDriver storage.StorageDriver
	var err error
	switch driverName {
	case "":
		// empty string by default is the in memory store
		fallthrough
	case "memory":
		storageDriver = memory.New(*argSampleSize, *argHistoryDuration)
		return storageDriver, nil
	case "influxdb":
		var hostname string
		hostname, err = os.Hostname()
		if err != nil {
			return nil, err
		}

		storageDriver, err = influxdb.New(
			hostname,
			"cadvisorTable",
			*argDbName,
			*argDbUsername,
			*argDbPassword,
			*argDbHost,
			*argDbIsSecure,
			// TODO(monnand): One hour? Or user-defined?
			1*time.Hour,
		)
		storageDriver = cache.MemoryCache(*argHistoryDuration, *argHistoryDuration, storageDriver)
	default:
		err = fmt.Errorf("Unknown database driver: %v", *argDbDriver)
	}
	if err != nil {
		return nil, err
	}
	return storageDriver, nil
}
Exemplo n.º 15
0
func TestPercentiles(t *testing.T) {
	N := 100
	backend := memory.New(N*2, N*2)
	driver := MemoryCache(N, N, backend)
	test.StorageDriverTestPercentiles(driver, t)
}