-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
123 lines (105 loc) · 2.71 KB
/
Copy pathmain.go
File metadata and controls
123 lines (105 loc) · 2.71 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"github.com/DataDog/datadog-go/statsd"
"github.com/fsouza/go-dockerclient"
)
// config represents a config file that controls what events and actions to
// track.
type config struct {
// Attributes defines any global attributes to include across all events
// and actions.
Attributes map[string]bool `json:"attributes"`
// Events configures the events that should be tracked.
Events map[string]struct {
// Actions configures the actions that should be tracked.
Actions map[string]struct {
// Attributes configures the attributes in the action
// that should be included.
Attributes map[string]bool `json:"attributes"`
} `json:"actions"`
} `json:"events"`
}
// attributes returns a map of the attributes that should be included for a
// given action.
func (c *config) attributes(event, action string) map[string]bool {
attributes := make(map[string]bool)
for k, v := range c.Attributes {
attributes[k] = v
}
if e, ok := c.Events[event]; ok {
if a, ok := e.Actions[action]; ok {
for k, v := range a.Attributes {
attributes[k] = v
}
}
}
return attributes
}
// loadConfig parses the given json config file in r and returns a parsed
// config.
func loadConfig(r io.Reader) (*config, error) {
var c config
err := json.NewDecoder(r).Decode(&c)
return &c, err
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
var (
statsdAddr = flag.String("statsd", "localhost:8126", "Address of statsd")
)
flag.Parse()
args := flag.Args()
var r io.Reader = os.Stdin
if len(args) > 0 {
f, err := os.Open(args[0])
if err != nil {
return err
}
defer f.Close()
r = f
}
config, err := loadConfig(r)
if err != nil {
return fmt.Errorf("error loading config: %v", err)
}
s, err := statsd.New(*statsdAddr)
if err != nil {
return fmt.Errorf("could not connect to statsd: %v", err)
}
defer s.Close()
d, err := docker.NewClientFromEnv()
if err != nil {
return fmt.Errorf("could not connect to Docker daemon: %v", err)
}
return watch(config, d, s)
}
func watch(config *config, c *docker.Client, s *statsd.Client) error {
events := make(chan *docker.APIEvents)
if err := c.AddEventListener(events); err != nil {
return fmt.Errorf("could not subscribe event listener: %v", err)
}
for event := range events {
if _, ok := config.Events[event.Type]; !ok {
continue
}
enabledAttributes := config.attributes(event.Type, event.Action)
var tags []string
for k, v := range event.Actor.Attributes {
if enabledAttributes[k] {
tags = append(tags, fmt.Sprintf("%s:%s", k, v))
}
}
s.Count(fmt.Sprintf("docker.events.%s.%s", event.Type, event.Action), 1, tags, 1)
}
return nil
}