-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-beat_func.go
76 lines (60 loc) · 2.04 KB
/
docker-beat_func.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
package main
import (
"encoding/json"
"github.com/dmportella/docker-beat/logging"
"github.com/dmportella/docker-beat/plugin"
"github.com/fsouza/go-dockerclient"
)
func newDockerBeat(config configuration) (*dockerBeat, error) {
dockerbeat := &dockerBeat{
dockerEvents: make(chan *docker.APIEvents),
config: config,
}
client, err := docker.NewClient(dockerbeat.config.DockerEndpoint)
if err != nil {
logging.Error.Printf(err.Error())
return nil, err
}
dockerbeat.dockerClient = client
return dockerbeat, nil
}
func (dockerbeat *dockerBeat) Start() {
go dockerbeat.listContainers()
go dockerbeat.dockerEventListener()
}
func (dockerbeat *dockerBeat) listContainers() {
containers, _ := dockerbeat.dockerClient.ListContainers(docker.ListContainersOptions{All: true})
for _, containerEntry := range containers {
if container, _ := dockerbeat.dockerClient.InspectContainer(containerEntry.ID); container != nil {
logging.Info.Printf("Container '%s' with ID '%s' %s.", container.Name, container.ID, container.State.Status)
}
}
}
func (dockerbeat *dockerBeat) indentJSON(event plugin.DockerEvent) (data []byte) {
if dockerbeat.config.IndentJSON {
data, _ = json.MarshalIndent(event, "", " ")
} else {
data, _ = json.Marshal(event)
}
return data
}
func (dockerbeat *dockerBeat) dockerEventListener() {
err := dockerbeat.dockerClient.AddEventListener(dockerbeat.dockerEvents)
if err != nil {
logging.Error.Printf(err.Error())
panic(err)
}
for event := range dockerbeat.dockerEvents {
logging.Info.Printf("Type: '%s' Action: '%s' Status: '%s' Time: '%d' Id: '%s'", event.Type, event.Action, event.Status, event.Time, event.ID)
if dockerbeat.config.Consumer != "console" {
eventWrapper := plugin.DockerEvent{APIEvents: event}
consumer := plugin.GetConsumer(dockerbeat.config.Consumer)
if consumer != nil {
eventData := dockerbeat.indentJSON(eventWrapper)
go consumer.OnEvent(eventWrapper, eventData)
} else {
logging.Error.Printf("Consumer '%s' is not available.", dockerbeat.config.Consumer)
}
}
}
}