forked from Ingensi/dockbeat
/
dockerbeat.go
209 lines (177 loc) · 5.14 KB
/
dockerbeat.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"time"
"github.com/elastic/libbeat/beat"
"github.com/elastic/libbeat/cfgfile"
"github.com/elastic/libbeat/logp"
"github.com/elastic/libbeat/publisher"
"github.com/fsouza/go-dockerclient"
"github.com/elastic/libbeat/common"
)
type Dockerbeat struct {
isAlive bool
period time.Duration
socket string
TbConfig ConfigSettings
dockerClient *docker.Client
dockerStats map[string]chan *docker.Stats
events publisher.Client
}
func (d *Dockerbeat) Config(b *beat.Beat) error {
err := cfgfile.Read(&d.TbConfig, "")
if err != nil {
logp.Err("Error reading configuration file: %v", err)
return err
}
if d.TbConfig.Input.Period != nil {
d.period = time.Duration(*d.TbConfig.Input.Period) * time.Second
} else {
d.period = 1 * time.Second
}
if d.TbConfig.Input.Socket != nil {
d.socket = *d.TbConfig.Input.Socket
} else {
d.socket = "unix:///var/run/docker.sock" // default docker socket location
}
logp.Debug("dockerbeat", "Init dockerbeat")
logp.Debug("dockerbeat", "Follow socker %q\n", d.socket)
logp.Debug("dockerbeat", "Period %v\n", d.period)
return nil
}
func (d *Dockerbeat) Setup(b *beat.Beat) error {
d.events = b.Events
d.dockerClient, _ = docker.NewClient(d.socket)
return nil
}
func (d *Dockerbeat) Run(b *beat.Beat) error {
d.isAlive = true
var err error
for d.isAlive {
containers, err := d.dockerClient.ListContainers(docker.ListContainersOptions{})
if err == nil {
for _, container := range containers {
logp.Info("3")
d.exportContainerStats(container)
logp.Info("4")
}
logp.Info("running! %s", d.socket)
} else {
logp.Err("Cannot get container list: %d", err)
}
time.Sleep(d.period)
}
return err
}
func (d *Dockerbeat) Cleanup(b *beat.Beat) error {
return nil
}
func (d *Dockerbeat) Stop() {
d.isAlive = false
}
func (d *Dockerbeat) exportContainerStats(container docker.APIContainers) error {
statsC := make(chan *docker.Stats)
done := make(chan bool)
errC := make(chan error, 1)
statsOptions := docker.StatsOptions{container.ID, statsC, false, done, -1}
go func() {
logp.Info("1")
errC <- d.dockerClient.Stats(statsOptions)
logp.Info("2")
close(errC)
}()
go func() {
stats := <-statsC
events := []common.MapStr{
d.getContainerEvent(&container, stats),
d.getCpuEvent(&container, stats),
d.getMemoryEvent(&container, stats),
d.getNetworkEvent(&container, stats),
}
d.events.PublishEvents(events)
}()
return nil
}
func (d *Dockerbeat) getContainerEvent(container *docker.APIContainers, stats *docker.Stats) common.MapStr {
event := common.MapStr{
// "timestamp": common.Time(time.Now()),
"timestamp": common.Time(stats.Read),
"type": "container",
"containerID": container.ID,
"containerNames": container.Names,
"container": common.MapStr{
"id": container.ID,
"command": container.Command,
"created": time.Unix(container.Created, 0),
"image": container.Image,
"labels": container.Labels,
"names": container.Names,
"ports": d.convertContainerPorts(&container.Ports),
"sizeRootFs": container.SizeRootFs,
"sizeRw": container.SizeRw,
"status": container.Status,
},
}
return event
}
func (d *Dockerbeat) getCpuEvent(container *docker.APIContainers, stats *docker.Stats) common.MapStr {
event := common.MapStr{
"timestamp": common.Time(stats.Read),
"type": "cpu",
"containerID": container.ID,
"containerNames": container.Names,
"cpu": common.MapStr{
"percpuUsage": stats.CPUStats.CPUUsage.PercpuUsage,
"totalUsage": stats.CPUStats.CPUUsage.TotalUsage,
"usageInKernelmode": stats.CPUStats.CPUUsage.UsageInKernelmode,
"usageInUsermode": stats.CPUStats.CPUUsage.UsageInUsermode,
},
}
return event
}
func (d *Dockerbeat) getNetworkEvent(container *docker.APIContainers, stats *docker.Stats) common.MapStr {
event := common.MapStr{
"timestamp": common.Time(stats.Read),
"type": "net",
"containerID": container.ID,
"containerNames": container.Names,
"net": common.MapStr{
"rxBytes": stats.Network.RxBytes,
"rxDropped": stats.Network.RxDropped,
"rxErrors": stats.Network.RxErrors,
"rxPackets": stats.Network.RxPackets,
"txBytes": stats.Network.TxBytes,
"txDropped": stats.Network.TxDropped,
"txErrors": stats.Network.TxErrors,
"txPackets": stats.Network.TxPackets,
},
}
return event
}
func (d *Dockerbeat) getMemoryEvent(container *docker.APIContainers, stats *docker.Stats) common.MapStr {
event := common.MapStr{
"timestamp": common.Time(stats.Read),
"type": "memory",
"containerID": container.ID,
"containerNames": container.Names,
"memory": common.MapStr{
"failcnt": stats.MemoryStats.Failcnt,
"limit": stats.MemoryStats.Limit,
"maxUsage": stats.MemoryStats.MaxUsage,
"usage": stats.MemoryStats.Usage,
},
}
return event
}
func (d *Dockerbeat) convertContainerPorts(ports *[]docker.APIPort) []map[string]interface{} {
var outputPorts []map[string]interface{}
for _, port := range *ports {
outputPort := common.MapStr{
"ip": port.IP,
"privatePort": port.PrivatePort,
"publicPort": port.PublicPort,
"type": port.Type,
}
outputPorts = append(outputPorts, outputPort)
}
return outputPorts
}