From 96a7710a455f26802f53b3e41dd8a2a4b8c20247 Mon Sep 17 00:00:00 2001 From: "Jose I. Monreal Bailey" Date: Tue, 11 Aug 2015 15:39:23 -0300 Subject: [PATCH 1/3] Adding new storage driver stdout --- storage/stdout/stdout.go | 117 +++++++++++++++++++++++++++++++++++++++ storagedriver.go | 5 ++ 2 files changed, 122 insertions(+) create mode 100644 storage/stdout/stdout.go diff --git a/storage/stdout/stdout.go b/storage/stdout/stdout.go new file mode 100644 index 00000000..4fec4c6f --- /dev/null +++ b/storage/stdout/stdout.go @@ -0,0 +1,117 @@ +// Copyright 2015 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package stdout + +import ( + "bytes" + "fmt" + info "github.com/google/cadvisor/info/v1" +) + +type stdoutStorage struct { + Namespace string +} + +const ( + colCpuCumulativeUsage string = "cpu_cumulative_usage" + // Memory Usage + colMemoryUsage string = "memory_usage" + // Working set size + colMemoryWorkingSet string = "memory_working_set" + // Cumulative count of bytes received. + colRxBytes string = "rx_bytes" + // Cumulative count of receive errors encountered. + colRxErrors string = "rx_errors" + // Cumulative count of bytes transmitted. + colTxBytes string = "tx_bytes" + // Cumulative count of transmit errors encountered. + colTxErrors string = "tx_errors" + // Filesystem summary + colFsSummary = "fs_summary" + // Filesystem limit. + colFsLimit = "fs_limit" + // Filesystem usage. + colFsUsage = "fs_usage" +) + +func (driver *stdoutStorage) containerStatsToValues(stats *info.ContainerStats) (series map[string]uint64) { + series = make(map[string]uint64) + + // Cumulative Cpu Usage + series[colCpuCumulativeUsage] = stats.Cpu.Usage.Total + + // Memory Usage + series[colMemoryUsage] = stats.Memory.Usage + + // Working set size + series[colMemoryWorkingSet] = stats.Memory.WorkingSet + + // Network stats. + series[colRxBytes] = stats.Network.RxBytes + series[colRxErrors] = stats.Network.RxErrors + series[colTxBytes] = stats.Network.TxBytes + series[colTxErrors] = stats.Network.TxErrors + + return series +} + +func (driver *stdoutStorage) containerFsStatsToValues(series *map[string]uint64, stats *info.ContainerStats) { + for _, fsStat := range stats.Filesystem { + // Summary stats. + (*series)[colFsSummary+"."+colFsLimit] += fsStat.Limit + (*series)[colFsSummary+"."+colFsUsage] += fsStat.Usage + + // Per device stats. + (*series)[fsStat.Device+"."+colFsLimit] = fsStat.Limit + (*series)[fsStat.Device+"."+colFsUsage] = fsStat.Usage + } +} + +func (driver *stdoutStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error { + if stats == nil { + return nil + } + + var containerName string + if len(ref.Aliases) > 0 { + containerName = ref.Aliases[0] + } else { + containerName = ref.Name + } + + var buffer bytes.Buffer + buffer.WriteString(fmt.Sprintf("cName=%s host=%s", containerName, driver.Namespace)) + + series := driver.containerStatsToValues(stats) + driver.containerFsStatsToValues(&series, stats) + for key, value := range series { + buffer.WriteString(fmt.Sprintf(" %s=%v", key, value)) + } + + fmt.Println(buffer.String()) + + return nil +} + +func (driver *stdoutStorage) Close() error { + return nil +} + +func New(namespace string) (*stdoutStorage, error) { + stdoutStorage := &stdoutStorage{ + Namespace: namespace, + } + return stdoutStorage, nil +} diff --git a/storagedriver.go b/storagedriver.go index c42cda9b..7f71a93f 100644 --- a/storagedriver.go +++ b/storagedriver.go @@ -27,6 +27,7 @@ import ( "github.com/google/cadvisor/storage/influxdb" "github.com/google/cadvisor/storage/redis" "github.com/google/cadvisor/storage/statsd" + "github.com/google/cadvisor/storage/stdout" ) var argDbUsername = flag.String("storage_driver_user", "root", "database username") @@ -94,6 +95,10 @@ func NewMemoryStorage(backendStorageName string) (*memory.InMemoryCache, error) *argDbName, *argDbHost, ) + case "stdout": + backendStorage, err = stdout.New( + *argDbHost, + ) default: err = fmt.Errorf("unknown backend storage driver: %v", *argDbDriver) } From 6573abf96dabeca29140ffe889744d80a8be2096 Mon Sep 17 00:00:00 2001 From: "Jose I. Monreal Bailey" Date: Fri, 14 Aug 2015 12:12:44 -0300 Subject: [PATCH 2/3] Fixing assignment of containerName and adding space between external imports and golibs --- storage/stdout/stdout.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/storage/stdout/stdout.go b/storage/stdout/stdout.go index 4fec4c6f..17229588 100644 --- a/storage/stdout/stdout.go +++ b/storage/stdout/stdout.go @@ -17,6 +17,7 @@ package stdout import ( "bytes" "fmt" + info "github.com/google/cadvisor/info/v1" ) @@ -84,11 +85,9 @@ func (driver *stdoutStorage) AddStats(ref info.ContainerReference, stats *info.C return nil } - var containerName string + containerName := ref.Name if len(ref.Aliases) > 0 { containerName = ref.Aliases[0] - } else { - containerName = ref.Name } var buffer bytes.Buffer From 3f54f77f9e283bd08772e5213d8911f6c7f70ecc Mon Sep 17 00:00:00 2001 From: "Jose I. Monreal Bailey" Date: Fri, 14 Aug 2015 14:19:23 -0300 Subject: [PATCH 3/3] Removing constant typed string. Handle fmt.Println error --- storage/stdout/stdout.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/storage/stdout/stdout.go b/storage/stdout/stdout.go index 17229588..af7b58cc 100644 --- a/storage/stdout/stdout.go +++ b/storage/stdout/stdout.go @@ -26,19 +26,19 @@ type stdoutStorage struct { } const ( - colCpuCumulativeUsage string = "cpu_cumulative_usage" + colCpuCumulativeUsage = "cpu_cumulative_usage" // Memory Usage - colMemoryUsage string = "memory_usage" + colMemoryUsage = "memory_usage" // Working set size - colMemoryWorkingSet string = "memory_working_set" + colMemoryWorkingSet = "memory_working_set" // Cumulative count of bytes received. - colRxBytes string = "rx_bytes" + colRxBytes = "rx_bytes" // Cumulative count of receive errors encountered. - colRxErrors string = "rx_errors" + colRxErrors = "rx_errors" // Cumulative count of bytes transmitted. - colTxBytes string = "tx_bytes" + colTxBytes = "tx_bytes" // Cumulative count of transmit errors encountered. - colTxErrors string = "tx_errors" + colTxErrors = "tx_errors" // Filesystem summary colFsSummary = "fs_summary" // Filesystem limit. @@ -99,9 +99,9 @@ func (driver *stdoutStorage) AddStats(ref info.ContainerReference, stats *info.C buffer.WriteString(fmt.Sprintf(" %s=%v", key, value)) } - fmt.Println(buffer.String()) + _, err := fmt.Println(buffer.String()) - return nil + return err } func (driver *stdoutStorage) Close() error {