forked from skytap/heapster
/
cadvisor.go
146 lines (133 loc) · 4.21 KB
/
cadvisor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2014 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.
// This file is dedicated to heapster running outside of kubernetes. Heapster
// will poll a file to get the hosts that it needs to monitor and will collect
// stats from cadvisor running on those hosts.
package sources
import (
"flag"
"fmt"
"strconv"
"sync"
"time"
"github.com/golang/glog"
"github.com/skytap/heapster/sources/api"
"github.com/skytap/heapster/sources/datasource"
"github.com/skytap/heapster/sources/nodes"
)
var (
argCadvisorPort = flag.Int("cadvisor_port", 8080, "The port on which cadvisor binds to on all nodes.")
argCoreOSMode = flag.Bool("coreos", false, "When true, heapster looks will connect with fleet servers to get the list of nodes to monitor. It is expected that cadvisor will be running on all the nodes at the port specified using flag '--cadvisor_port'. Use flag '--fleet_endpoints' to manage fleet endpoints to watch.")
)
type cadvisorSource struct {
cadvisorPort string
cadvisorApi datasource.Cadvisor
pollDuration time.Duration
nodesApi nodes.NodesApi
lastQuery time.Time
}
func (self *cadvisorSource) GetInfo() (api.AggregateData, error) {
var (
lock sync.Mutex
wg sync.WaitGroup
)
nodeList, err := self.nodesApi.List()
if err != nil {
return api.AggregateData{}, err
}
result := api.AggregateData{}
for hostname, info := range nodeList.Items {
wg.Add(1)
go func(hostname string, info nodes.Info) {
defer wg.Done()
host := datasource.Host{
IP: info.InternalIP,
Port: self.cadvisorPort,
}
rawSubcontainers, node, err := self.cadvisorApi.GetAllContainers(host, self.numStatsToFetch())
if err != nil {
glog.Error(err)
return
}
subcontainers := []api.Container{}
for _, cont := range rawSubcontainers {
if cont != nil {
cont.Hostname = hostname
subcontainers = append(subcontainers, *cont)
}
}
lock.Lock()
defer lock.Unlock()
result.Containers = append(result.Containers, subcontainers...)
if node != nil {
node.Hostname = hostname
result.Machine = append(result.Machine, *node)
}
}(string(hostname), info)
}
wg.Wait()
self.lastQuery = time.Now()
return result, nil
}
func (self *cadvisorSource) numStatsToFetch() int {
numStats := int(self.pollDuration / time.Second)
if time.Since(self.lastQuery) > self.pollDuration {
numStats = int(time.Since(self.lastQuery) / time.Second)
}
return numStats
}
func (self *cadvisorSource) DebugInfo() string {
desc := "Source type: Cadvisor\n"
// TODO(rjnagal): Cache config?
nodeList, err := self.nodesApi.List()
if err != nil {
desc += fmt.Sprintf("\tFailed to read host config: %s", err)
}
desc += fmt.Sprintf("\tNodeList: %+v\n", *nodeList)
desc += fmt.Sprintf("\t%s\n", self.nodesApi.DebugInfo())
desc += "\n"
return desc
}
func newExternalCadvisorSource(pollDuration time.Duration) (Source, error) {
if *argCadvisorPort <= 0 {
return nil, fmt.Errorf("invalid cadvisor port - %d", *argCadvisorPort)
}
nodesApi, err := nodes.NewExternalNodes()
if err != nil {
return nil, err
}
return &cadvisorSource{
pollDuration: pollDuration,
cadvisorApi: datasource.NewCadvisor(),
nodesApi: nodesApi,
cadvisorPort: strconv.Itoa(*argCadvisorPort),
lastQuery: time.Now(),
}, nil
}
func newCoreOSCadvisorSource(pollDuration time.Duration) (Source, error) {
if *argCadvisorPort <= 0 {
return nil, fmt.Errorf("invalid cadvisor port - %d", *argCadvisorPort)
}
nodesApi, err := nodes.NewCoreOSNodes()
if err != nil {
return nil, err
}
return &cadvisorSource{
pollDuration: pollDuration,
cadvisorApi: datasource.NewCadvisor(),
nodesApi: nodesApi,
cadvisorPort: strconv.Itoa(*argCadvisorPort),
lastQuery: time.Now(),
}, nil
}