1// Copyright 2015 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package stdout
16
17import (
18	"bytes"
19	"fmt"
20
21	info "github.com/google/cadvisor/info/v1"
22	"github.com/google/cadvisor/storage"
23)
24
25func init() {
26	storage.RegisterStorageDriver("stdout", new)
27}
28
29type stdoutStorage struct {
30	Namespace string
31}
32
33const (
34	colCpuCumulativeUsage = "cpu_cumulative_usage"
35	// Memory Usage
36	colMemoryUsage = "memory_usage"
37	// Working set size
38	colMemoryWorkingSet = "memory_working_set"
39	// Cumulative count of bytes received.
40	colRxBytes = "rx_bytes"
41	// Cumulative count of receive errors encountered.
42	colRxErrors = "rx_errors"
43	// Cumulative count of bytes transmitted.
44	colTxBytes = "tx_bytes"
45	// Cumulative count of transmit errors encountered.
46	colTxErrors = "tx_errors"
47	// Filesystem summary
48	colFsSummary = "fs_summary"
49	// Filesystem limit.
50	colFsLimit = "fs_limit"
51	// Filesystem usage.
52	colFsUsage = "fs_usage"
53)
54
55func new() (storage.StorageDriver, error) {
56	return newStorage(*storage.ArgDbHost)
57}
58
59func (driver *stdoutStorage) containerStatsToValues(stats *info.ContainerStats) (series map[string]uint64) {
60	series = make(map[string]uint64)
61
62	// Cumulative Cpu Usage
63	series[colCpuCumulativeUsage] = stats.Cpu.Usage.Total
64
65	// Memory Usage
66	series[colMemoryUsage] = stats.Memory.Usage
67
68	// Working set size
69	series[colMemoryWorkingSet] = stats.Memory.WorkingSet
70
71	// Network stats.
72	series[colRxBytes] = stats.Network.RxBytes
73	series[colRxErrors] = stats.Network.RxErrors
74	series[colTxBytes] = stats.Network.TxBytes
75	series[colTxErrors] = stats.Network.TxErrors
76
77	return series
78}
79
80func (driver *stdoutStorage) containerFsStatsToValues(series *map[string]uint64, stats *info.ContainerStats) {
81	for _, fsStat := range stats.Filesystem {
82		// Summary stats.
83		(*series)[colFsSummary+"."+colFsLimit] += fsStat.Limit
84		(*series)[colFsSummary+"."+colFsUsage] += fsStat.Usage
85
86		// Per device stats.
87		(*series)[fsStat.Device+"."+colFsLimit] = fsStat.Limit
88		(*series)[fsStat.Device+"."+colFsUsage] = fsStat.Usage
89	}
90}
91
92func (driver *stdoutStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
93	if stats == nil {
94		return nil
95	}
96
97	containerName := ref.Name
98	if len(ref.Aliases) > 0 {
99		containerName = ref.Aliases[0]
100	}
101
102	var buffer bytes.Buffer
103	buffer.WriteString(fmt.Sprintf("cName=%s host=%s", containerName, driver.Namespace))
104
105	series := driver.containerStatsToValues(stats)
106	driver.containerFsStatsToValues(&series, stats)
107	for key, value := range series {
108		buffer.WriteString(fmt.Sprintf(" %s=%v", key, value))
109	}
110
111	_, err := fmt.Println(buffer.String())
112
113	return err
114}
115
116func (driver *stdoutStorage) Close() error {
117	return nil
118}
119
120func newStorage(namespace string) (*stdoutStorage, error) {
121	stdoutStorage := &stdoutStorage{
122		Namespace: namespace,
123	}
124	return stdoutStorage, nil
125}
126