Skip to content

Commit 5919075

Browse files
committed
Use config.yml for configuration
Signed-off-by: Christian König <[email protected]>
1 parent 363454f commit 5919075

13 files changed

+201
-155
lines changed

docker-compose.yml

+1-20
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
1-
version: '2.4'
2-
31
services:
42
docker-event-monitor:
53
container_name: docker-event-monitor
64
image: ghcr.io/yubiuser/docker-event-monitor:latest
75
volumes:
86
- /var/run/docker.sock:/var/run/docker.sock:ro
97
- /etc/localtime:/etc/localtime:ro
8+
- ./config.yml:/config.yml:ro
109
restart: unless-stopped
11-
environment:
12-
PUSHOVER: false
13-
PUSHOVER_USER: 'USER'
14-
PUSHOVER_APITOKEN: 'TOKEN'
15-
GOTIFY: false
16-
GOTIFY_URL: 'URL'
17-
GOTIFY_TOKEN: 'TOKEN'
18-
MAIL: false
19-
MAIL_FROM: '[email protected]'
20-
MAIL_TO: '[email protected]'
21-
MAIL_USER: 'SMTP USER'
22-
MAIL_PASSWORD: 'PASSWORD'
23-
MAIL_PORT: 587
24-
MAIL_HOST: '[email protected]'
25-
FILTER: 'event=start,event=stop,type=container'
26-
DELAY: '500ms'
27-
LOG_LEVEL: 'info'
28-
SERVER_TAG: ''

src/config.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
options:
3+
delay: 500ms
4+
filter_strings: ["type=container"]
5+
exclude_strings: ["Action=exec_start", "Action=exec_die", "Action=exec_create"]
6+
log_level: debug
7+
server_tag: My Server
8+
9+
reporter:
10+
pushover:
11+
enabled: true
12+
api_token: xyz
13+
user_key: abc
14+
gotify:
15+
enabled: true
16+
url: http://gotify.lan
17+
token: xyz
18+
mail:
19+
enabled: true
20+
21+
22+
user: SMTP Username
23+
password: SMTP Password
24+
port: 587
25+
host: SMTP Host
26+
mattermost:
27+
enabled: true
28+
url: http://mattermost.lan
29+
channel: Docker Event Channel
30+
user: Docker Event Bot
31+

src/events.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func processEvent(event events.Message) {
2121
// Sometimes events are pushed through the event channel really quickly, but they arrive on the notification clients in
2222
// wrong order (probably due to message delivery time), e.g. Pushover is susceptible for this.
2323
// Finishing this function not before a certain time before draining the next event from the event channel in main() solves the issue
24-
timer := time.NewTimer(glb_arguments.Delay)
24+
timer := time.NewTimer(glb_arguments.Options.Delay)
2525

2626
ActorID = getActorID(event)
2727
ActorImage = getActorImage(event)

src/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ require (
77
github.com/docker/docker v26.1.0+incompatible
88
github.com/rs/zerolog v1.32.0
99
golang.org/x/text v0.14.0
10+
gopkg.in/yaml.v3 v3.0.1
1011
)
1112

1213
require (
1314
github.com/Microsoft/go-winio v0.6.1 // indirect
14-
github.com/alexflint/go-scalar v1.2.0 // indirect
1515
github.com/containerd/log v0.1.0 // indirect
1616
github.com/distribution/reference v0.5.0 // indirect
1717
github.com/docker/go-connections v0.5.0 // indirect

src/go.sum

+1-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOEl
22
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
33
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
44
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
5-
github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/uo=
6-
github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA=
7-
github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
8-
github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw=
9-
github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
105
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
116
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
127
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
138
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
149
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
15-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1610
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1711
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1812
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
@@ -66,9 +60,6 @@ github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
6660
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
6761
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
6862
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
69-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
70-
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
71-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
7263
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
7364
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7465
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -139,8 +130,8 @@ google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU=
139130
google.golang.org/grpc v1.60.1/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM=
140131
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
141132
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
133+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
142134
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
143-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
144135
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
145136
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
146137
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=

src/gotify.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func sendGotify(message string, title string, errCh chan ReporterError) {
3030
return
3131
}
3232

33-
err = sendhttpMessage("Gotify", glb_arguments.GotifyURL+"/message?token="+glb_arguments.GotifyToken, messageJSON)
33+
err = sendhttpMessage("Gotify", glb_arguments.Reporter.Gotify.URL+"/message?token="+glb_arguments.Reporter.Gotify.Token, messageJSON)
3434
if err != nil {
3535
e.Error = err
3636
errCh <- e

src/mail.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ func sendMail(timestamp time.Time, message string, title string, errCh chan Repo
2626
Reporter: "Mail",
2727
}
2828

29-
from := glb_arguments.MailFrom
30-
to := []string{glb_arguments.MailTo}
31-
username := glb_arguments.MailUser
32-
password := glb_arguments.MailPassword
29+
from := glb_arguments.Reporter.Mail.From
30+
to := []string{glb_arguments.Reporter.Mail.To}
31+
username := glb_arguments.Reporter.Mail.User
32+
password := glb_arguments.Reporter.Mail.Password
3333

34-
host := glb_arguments.MailHost
35-
port := strconv.Itoa(glb_arguments.MailPort)
34+
host := glb_arguments.Reporter.Mail.Host
35+
port := strconv.Itoa(glb_arguments.Reporter.Mail.Port)
3636
address := host + ":" + port
3737

3838
subject := title

src/main.go

+57-67
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,23 @@ package main
33
import (
44
"context"
55
"os"
6+
"path/filepath"
67
"strings"
78
"time"
89

9-
"github.com/alexflint/go-arg"
1010
"github.com/docker/docker/api/types"
1111
"github.com/docker/docker/api/types/filters"
1212
"github.com/docker/docker/client"
13+
"gopkg.in/yaml.v3"
1314

1415
"github.com/rs/zerolog"
1516
)
1617

17-
type args struct {
18-
Pushover bool `arg:"env:PUSHOVER" default:"false" help:"Enable/Disable Pushover Notification (True/False)"`
19-
PushoverAPIToken string `arg:"env:PUSHOVER_APITOKEN" help:"Pushover's API Token/Key"`
20-
PushoverUserKey string `arg:"env:PUSHOVER_USER" help:"Pushover's User Key"`
21-
Gotify bool `arg:"env:GOTIFY" default:"false" help:"Enable/Disable Gotify Notification (True/False)"`
22-
GotifyURL string `arg:"env:GOTIFY_URL" help:"URL of your Gotify server"`
23-
GotifyToken string `arg:"env:GOTIFY_TOKEN" help:"Gotify's App Token"`
24-
Mail bool `arg:"env:MAIL" default:"false" help:"Enable/Disable Mail (SMTP) Notification (True/False)"`
25-
MailFrom string `arg:"env:MAIL_FROM" help:"[email protected]"`
26-
MailTo string `arg:"env:MAIL_TO" help:"[email protected]"`
27-
MailUser string `arg:"env:MAIL_USER" help:"SMTP Username"`
28-
MailPassword string `arg:"env:MAIL_PASSWORD" help:"SMTP Password"`
29-
MailPort int `arg:"env:MAIL_PORT" default:"587" help:"SMTP Port"`
30-
MailHost string `arg:"env:MAIL_HOST" help:"SMTP Host"`
31-
Mattermost bool `arg:"env:MATTERMOST" default:"false" help:"Enable/Disable Mattermost Notification (True/False)"`
32-
MattermostURL string `arg:"env:MATTERMOST_URL" help:"URL of your Mattermost incoming webhook"`
33-
MattermostChannel string `arg:"env:MATTERMOST_CHANNEL" help:"Mattermost channel to post in"`
34-
MattermostUser string `arg:"env:MATTERMOST_USER" default:"Docker Event Monitor" help:"Mattermost user to post as"`
35-
Reporters []string `arg:"-"`
36-
Delay time.Duration `arg:"env:DELAY" default:"500ms" help:"Delay before next message is send"`
37-
FilterStrings []string `arg:"env:FILTER,--filter,separate" help:"Filter docker events using Docker syntax."`
38-
Filter map[string][]string `arg:"-"`
39-
ExcludeStrings []string `arg:"env:EXCLUDE,--exclude,separate" help:"Exclude docker events using Docker syntax."`
40-
Exclude map[string][]string `arg:"-"`
41-
LogLevel string `arg:"env:LOG_LEVEL" default:"info" help:"Set log level. Use debug for more logging."`
42-
ServerTag string `arg:"env:SERVER_TAG" help:"Prefix to include in the title of notifications. Useful when running docker-event-monitors on multiple machines."`
43-
Version bool `arg:"-v" help:"Print version information."`
44-
}
45-
46-
// Creating a global logger
18+
// Create global logger
4719
var logger zerolog.Logger
4820

4921
// hold the supplied run-time arguments globally
50-
var glb_arguments args
22+
var glb_arguments config
5123

5224
// version information, are injected during build process
5325
var (
@@ -59,45 +31,46 @@ var (
5931
)
6032

6133
func init() {
34+
loadConfig()
6235
parseArgs()
63-
configureLogger(glb_arguments.LogLevel)
36+
configureLogger(glb_arguments.Options.LogLevel)
6437

65-
if glb_arguments.Pushover {
66-
if len(glb_arguments.PushoverAPIToken) == 0 {
67-
logger.Fatal().Msg("Pushover enabled. Pushover API token required!")
38+
if glb_arguments.Reporter.Pushover.Enabled {
39+
if len(glb_arguments.Reporter.Pushover.APIToken) == 0 {
40+
logger.Fatal().Msg("Pushover Enabled. Pushover API token required!")
6841
}
69-
if len(glb_arguments.PushoverUserKey) == 0 {
70-
logger.Fatal().Msg("Pushover enabled. Pushover user key required!")
42+
if len(glb_arguments.Reporter.Pushover.UserKey) == 0 {
43+
logger.Fatal().Msg("Pushover Enabled. Pushover user key required!")
7144
}
7245
}
73-
if glb_arguments.Gotify {
74-
if len(glb_arguments.GotifyURL) == 0 {
75-
logger.Fatal().Msg("Gotify enabled. Gotify URL required!")
46+
if glb_arguments.Reporter.Gotify.Enabled {
47+
if len(glb_arguments.Reporter.Gotify.URL) == 0 {
48+
logger.Fatal().Msg("Gotify Enabled. Gotify URL required!")
7649
}
77-
if len(glb_arguments.GotifyToken) == 0 {
78-
logger.Fatal().Msg("Gotify enabled. Gotify APP token required!")
50+
if len(glb_arguments.Reporter.Gotify.Token) == 0 {
51+
logger.Fatal().Msg("Gotify Enabled. Gotify APP token required!")
7952
}
8053
}
81-
if glb_arguments.Mail {
82-
if len(glb_arguments.MailUser) == 0 {
83-
logger.Fatal().Msg("E-Mail notification enabled. SMTP username required!")
54+
if glb_arguments.Reporter.Mail.Enabled {
55+
if len(glb_arguments.Reporter.Mail.User) == 0 {
56+
logger.Fatal().Msg("E-Mail notification Enabled. SMTP username required!")
8457
}
85-
if len(glb_arguments.MailTo) == 0 {
86-
logger.Fatal().Msg("E-Mail notification enabled. Recipient address required!")
58+
if len(glb_arguments.Reporter.Mail.To) == 0 {
59+
logger.Fatal().Msg("E-Mail notification Enabled. Recipient address required!")
8760
}
88-
if len(glb_arguments.MailFrom) == 0 {
89-
glb_arguments.MailFrom = glb_arguments.MailUser
61+
if len(glb_arguments.Reporter.Mail.From) == 0 {
62+
glb_arguments.Reporter.Mail.From = glb_arguments.Reporter.Mail.User
9063
}
91-
if len(glb_arguments.MailPassword) == 0 {
92-
logger.Fatal().Msg("E-Mail notification enabled. SMTP Password required!")
64+
if len(glb_arguments.Reporter.Mail.Password) == 0 {
65+
logger.Fatal().Msg("E-Mail notification Enabled. SMTP Password required!")
9366
}
94-
if len(glb_arguments.MailHost) == 0 {
95-
logger.Fatal().Msg("E-Mail notification enabled. SMTP host address required!")
67+
if len(glb_arguments.Reporter.Mail.Host) == 0 {
68+
logger.Fatal().Msg("E-Mail notification Enabled. SMTP host address required!")
9669
}
9770
}
98-
if glb_arguments.Mattermost {
99-
if len(glb_arguments.MattermostURL) == 0 {
100-
logger.Fatal().Msg("Mattermost enabled. Mattermost URL required!")
71+
if glb_arguments.Reporter.Mattermost.Enabled {
72+
if len(glb_arguments.Reporter.Mattermost.URL) == 0 {
73+
logger.Fatal().Msg("Mattermost Enabled. Mattermost URL required!")
10174
}
10275
}
10376
}
@@ -152,16 +125,33 @@ func main() {
152125
}
153126
}
154127

128+
func loadConfig() {
129+
configFile, err := filepath.Abs("./config.yml")
130+
131+
if err != nil {
132+
logger.Fatal().Err(err).Msg("Failed to set config file path")
133+
}
134+
135+
buf, err := os.ReadFile(configFile)
136+
if err != nil {
137+
logger.Fatal().Err(err).Msg("Failed to read config file")
138+
}
139+
140+
err = yaml.Unmarshal(buf, &glb_arguments)
141+
if err != nil {
142+
logger.Fatal().Err(err).Msg("Failed to parse config file")
143+
}
144+
}
145+
155146
func parseArgs() {
156-
parser := arg.MustParse(&glb_arguments)
157147

158148
// Parse (include) filters
159149
glb_arguments.Filter = make(map[string][]string)
160150

161-
for _, filter := range glb_arguments.FilterStrings {
151+
for _, filter := range glb_arguments.Options.FilterStrings {
162152
pos := strings.Index(filter, "=")
163153
if pos == -1 {
164-
parser.Fail("each filter should be of the form key=value")
154+
logger.Fatal().Msg("each filter should be of the form key=value")
165155
}
166156
key := filter[:pos]
167157
val := filter[pos+1:]
@@ -171,29 +161,29 @@ func parseArgs() {
171161
// Parse exclude filters
172162
glb_arguments.Exclude = make(map[string][]string)
173163

174-
for _, exclude := range glb_arguments.ExcludeStrings {
164+
for _, exclude := range glb_arguments.Options.ExcludeStrings {
175165
pos := strings.Index(exclude, "=")
176166
if pos == -1 {
177-
parser.Fail("each filter should be of the form key=value")
167+
logger.Fatal().Msg("each filter should be of the form key=value")
178168
}
179169
//trim whitespaces
180170
key := strings.TrimSpace(exclude[:pos])
181171
val := exclude[pos+1:]
182172
glb_arguments.Exclude[key] = append(glb_arguments.Exclude[key], val)
183173
}
184174

185-
//Parse enabled reportes
175+
//Parse Enabled reportes
186176

187-
if glb_arguments.Gotify {
177+
if glb_arguments.Reporter.Gotify.Enabled {
188178
glb_arguments.Reporters = append(glb_arguments.Reporters, "Gotify")
189179
}
190-
if glb_arguments.Mattermost {
180+
if glb_arguments.Reporter.Mattermost.Enabled {
191181
glb_arguments.Reporters = append(glb_arguments.Reporters, "Mattermost")
192182
}
193-
if glb_arguments.Pushover {
183+
if glb_arguments.Reporter.Pushover.Enabled {
194184
glb_arguments.Reporters = append(glb_arguments.Reporters, "Pushover")
195185
}
196-
if glb_arguments.Mail {
186+
if glb_arguments.Reporter.Mail.Enabled {
197187
glb_arguments.Reporters = append(glb_arguments.Reporters, "Mail")
198188
}
199189

src/mattermost.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ type MattermostMessage struct {
1818
func sendMattermost(message string, title string, errCh chan ReporterError) {
1919

2020
m := MattermostMessage{
21-
Username: glb_arguments.MattermostUser,
22-
Channel: glb_arguments.MattermostChannel,
21+
Username: glb_arguments.Reporter.Mattermost.User,
22+
Channel: glb_arguments.Reporter.Mattermost.Channel,
2323
Text: "##### " + title + "\n" + message,
2424
}
2525

@@ -35,7 +35,7 @@ func sendMattermost(message string, title string, errCh chan ReporterError) {
3535
return
3636
}
3737

38-
err = sendhttpMessage("Mattermost", glb_arguments.MattermostURL, messageJSON)
38+
err = sendhttpMessage("Mattermost", glb_arguments.Reporter.Mattermost.URL, messageJSON)
3939
if err != nil {
4040
e.Error = err
4141
errCh <- e

src/notifications.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func sendNotifications(timestamp time.Time, message string, title string, report
2929
errCh := make(chan ReporterError, len(reporters))
3030

3131
// If there is a server tag, add it to the title
32-
if len(glb_arguments.ServerTag) > 0 {
33-
title = "[" + glb_arguments.ServerTag + "] " + title
32+
if len(glb_arguments.Options.ServerTag) > 0 {
33+
title = "[" + glb_arguments.Options.ServerTag + "] " + title
3434
}
3535

3636
if slices.Contains(reporters, "Pushover") {

src/pushover.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func sendPushover(timestamp time.Time, message string, title string, errCh chan
1919
// Send a message to Pushover
2020

2121
m := PushoverMessage{
22-
Token: glb_arguments.PushoverAPIToken,
23-
User: glb_arguments.PushoverUserKey,
22+
Token: glb_arguments.Reporter.Pushover.APIToken,
23+
User: glb_arguments.Reporter.Pushover.UserKey,
2424
Title: title,
2525
Message: message,
2626
Timestamp: strconv.FormatInt(timestamp.Unix(), 10),

0 commit comments

Comments
 (0)