forked from thecubed/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputdockerstats.go
More file actions
170 lines (150 loc) · 4.6 KB
/
inputdockerstats.go
File metadata and controls
170 lines (150 loc) · 4.6 KB
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
package inputdockerstats
import (
"context"
"os"
"regexp"
"time"
"github.com/fsouza/go-dockerclient"
"github.com/tsaikd/KDGoLib/errutil"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/logevent"
"github.com/tsaikd/gogstash/input/dockerlog/dockertool"
"golang.org/x/sync/errgroup"
)
// ModuleName is the name used in config file
const ModuleName = "dockerstats"
// InputConfig holds the configuration json fields and internal objects
type InputConfig struct {
config.InputConfig
DockerURL string `json:"dockerurl"`
IncludePatterns []string `json:"include_patterns"`
ExcludePatterns []string `json:"exclude_patterns"`
StatInterval int `json:"stat_interval"`
ConnectionRetryInterval int `json:"connection_retry_interval,omitempty"`
LogMode Mode `json:"log_mode,omitempty"`
sincemap
containerExist dockertool.StringExist
includes []*regexp.Regexp
excludes []*regexp.Regexp
hostname string
client *docker.Client
}
// DefaultInputConfig returns an InputConfig struct with default values
func DefaultInputConfig() InputConfig {
return InputConfig{
InputConfig: config.InputConfig{
CommonConfig: config.CommonConfig{
Type: ModuleName,
},
},
DockerURL: "unix:///var/run/docker.sock",
StatInterval: 15,
ConnectionRetryInterval: 10,
LogMode: ModeFull,
sincemap: newSinceMap(),
containerExist: dockertool.NewStringExist(),
}
}
// errors
var (
ErrorPingFailed = errutil.NewFactory("ping docker server failed")
ErrorListContainerFailed = errutil.NewFactory("list docker container failed")
ErrorListenDockerEventFailed = errutil.NewFactory("listen docker event failed")
ErrorContainerLoopRunning1 = errutil.NewFactory("container log loop running: %s")
ErrorGetContainerInfoFailed = errutil.NewFactory("get container info failed")
)
// InitHandler initialize the input plugin
func InitHandler(ctx context.Context, raw *config.ConfigRaw) (config.TypeInputConfig, error) {
conf := DefaultInputConfig()
err := config.ReflectConfig(raw, &conf)
if err != nil {
return nil, err
}
for _, pattern := range conf.IncludePatterns {
conf.includes = append(conf.includes, regexp.MustCompile(pattern))
}
for _, pattern := range conf.ExcludePatterns {
conf.excludes = append(conf.excludes, regexp.MustCompile(pattern))
}
if conf.hostname, err = os.Hostname(); err != nil {
return nil, err
}
if conf.client, err = docker.NewClient(conf.DockerURL); err != nil {
return nil, err
}
if err = conf.client.Ping(); err != nil {
return nil, ErrorPingFailed.New(err)
}
return &conf, nil
}
// Start wraps the actual function starting the plugin
func (t *InputConfig) Start(ctx context.Context, msgChan chan<- logevent.LogEvent) error {
eg, ctx := errgroup.WithContext(ctx)
// listen running containers
eg.Go(func() error {
containers, err := t.client.ListContainers(docker.ListContainersOptions{})
if err != nil {
return ErrorListContainerFailed.New(err)
}
for _, container := range containers {
if !t.isValidContainer(container.Names) {
continue
}
since := t.sincemap.ensure(container.ID)
func(container interface{}, since *time.Time) {
eg.Go(func() error {
return t.containerLogLoop(ctx, container, since, msgChan)
})
}(container, since)
}
return nil
})
// listen for running in future containers
eg.Go(func() error {
dockerEventChan := make(chan *docker.APIEvents)
if err := t.client.AddEventListener(dockerEventChan); err != nil {
return ErrorListenDockerEventFailed.New(err)
}
for {
select {
case <-ctx.Done():
return nil
case dockerEvent := <-dockerEventChan:
if dockerEvent.Status == "start" {
container, err := t.client.InspectContainer(dockerEvent.ID)
if err != nil {
return errutil.New("inspect container failed", err)
}
if !t.isValidContainer([]string{container.Name}) {
return errutil.New("invalid container name " + container.Name)
}
since := t.sincemap.ensure(container.ID)
func(container interface{}, since *time.Time) {
eg.Go(func() error {
return t.containerLogLoop(ctx, container, since, msgChan)
})
}(container, since)
}
}
}
})
return eg.Wait()
}
func (t *InputConfig) isValidContainer(names []string) bool {
for _, name := range names {
for _, re := range t.excludes {
if re.MatchString(name) {
return false
}
}
for _, re := range t.includes {
if re.MatchString(name) {
return true
}
}
}
if len(t.includes) > 0 {
return false
}
return true
}