forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
executable file
·138 lines (118 loc) · 3.01 KB
/
stats.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
package stats
import (
"bufio"
"io"
"net/url"
"time"
log "github.com/Sirupsen/logrus"
"github.com/rancher/agent/service/hostapi/events"
"github.com/rancher/websocket-proxy/backend"
"github.com/rancher/websocket-proxy/common"
"golang.org/x/net/context"
)
type Handler struct {
}
func (s *Handler) Handle(key string, initialMessage string, incomingMessages <-chan string, response chan<- common.Message) {
defer backend.SignalHandlerClosed(key, response)
requestURL, err := url.Parse(initialMessage)
if err != nil {
log.WithFields(log.Fields{"error": err, "message": initialMessage}).Error("Couldn't parse url from message.")
return
}
id := ""
parts := pathParts(requestURL.Path)
if len(parts) == 3 {
id = parts[2]
}
dclient, err := events.NewDockerClient()
if err != nil {
log.Errorf("Can not get docker client. err: %v", err)
return
}
reader, writer := io.Pipe()
go func(w *io.PipeWriter) {
for {
_, ok := <-incomingMessages
if !ok {
w.Close()
return
}
}
}(writer)
go func(r *io.PipeReader) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
text := scanner.Text()
message := common.Message{
Key: key,
Type: common.Body,
Body: text,
}
response <- message
}
if err := scanner.Err(); err != nil {
log.WithFields(log.Fields{"error": err}).Error("Error with the container stat scanner.")
}
}(reader)
count := 1
memLimit, err := getMemCapcity()
if err != nil {
log.WithFields(log.Fields{"error": err, "id": id}).Error("Error getting memory capacity.")
return
}
if id == "" {
for {
infos := []containerInfo{}
cInfo, err := getRootContainerInfo(count)
if err != nil {
return
}
infos = append(infos, cInfo)
for i := range infos {
if len(infos[i].Stats) > 0 {
infos[i].Stats[0].Timestamp = time.Now()
}
}
err = writeAggregatedStats("", nil, "host", infos, uint64(memLimit), writer)
if err != nil {
return
}
time.Sleep(1 * time.Second)
count = 1
}
} else {
inspect, err := dclient.ContainerInspect(context.Background(), id)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Can not inspect containers")
return
}
statsReader, err := dclient.ContainerStats(context.Background(), id, true)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Can not get stats reader from docker")
return
}
defer statsReader.Body.Close()
pid := inspect.State.Pid
bufioReader := bufio.NewReader(statsReader.Body)
for {
infos := []containerInfo{}
cInfo, err := getContainerStats(bufioReader, count, id, pid)
if err != nil {
log.WithFields(log.Fields{"error": err, "id": id}).Error("Error getting container info.")
return
}
infos = append(infos, cInfo)
for i := range infos {
if len(infos[i].Stats) > 0 {
infos[i].Stats[0].Timestamp = time.Now()
}
}
err = writeAggregatedStats(id, nil, "container", infos, uint64(memLimit), writer)
if err != nil {
return
}
time.Sleep(1 * time.Second)
count = 1
}
}
}