forked from AdaptiveConsulting/docker_state_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
267 lines (230 loc) · 7.97 KB
/
main.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"sync"
"syscall"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/docker/docker/api/types"
tcontainer "github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/exporter-toolkit/web"
"github.com/prometheus/exporter-toolkit/web/kingpinflag"
)
var (
addContainerLabels *bool
cachePeriod *time.Duration
)
type dockerHealthCollector struct {
mu sync.Mutex
containerClient *client.Client
containerInfoCache []types.ContainerJSON
lastseen time.Time
}
type descSource struct {
name string
help string
}
func (desc *descSource) Desc(labels prometheus.Labels) *prometheus.Desc {
return prometheus.NewDesc(desc.name, desc.help, nil, labels)
}
var (
namespace = "container_state_"
healthStatusDesc = descSource{
namespace + "health_status",
"Container health status."}
statusDesc = descSource{
namespace + "status",
"Container status."}
oomkilledDesc = descSource{
namespace + "oomkilled",
"Container was killed by OOMKiller."}
startedatDesc = descSource{
namespace + "startedat",
"Time when the Container started."}
finishedatDesc = descSource{
namespace + "finishedat",
"Time when the Container finished."}
restartcountDesc = descSource{
"container_restartcount",
"Number of times the container has been restarted"}
lastseenDesc = descSource{
namespace + "lastseen",
"Time when the Container was last seen."}
// Key is the container name; value is the last time the container was seen
lastseenContainers map[string]time.Time = map[string]time.Time{}
)
func (c *dockerHealthCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- healthStatusDesc.Desc(nil)
ch <- statusDesc.Desc(nil)
ch <- oomkilledDesc.Desc(nil)
ch <- startedatDesc.Desc(nil)
ch <- finishedatDesc.Desc(nil)
ch <- restartcountDesc.Desc(nil)
ch <- lastseenDesc.Desc(nil)
}
func (c *dockerHealthCollector) Collect(ch chan<- prometheus.Metric) {
c.mu.Lock()
defer c.mu.Unlock()
now := time.Now()
if now.Sub(c.lastseen) >= *cachePeriod {
c.collectContainers()
c.lastseen = now
}
c.collectMetrics(ch)
}
func (c *dockerHealthCollector) collectMetrics(ch chan<- prometheus.Metric) {
for _, info := range c.containerInfoCache {
var labels = map[string]string{}
rep := regexp.MustCompile("[^a-zA-Z0-9_]")
if *addContainerLabels {
for k, v := range info.Config.Labels {
label := strings.ToLower("container_label_" + k)
labels[rep.ReplaceAllLiteralString(label, "_")] = v
}
}
labels["id"] = "/docker/" + info.ID
labels["image"] = info.Config.Image
labels["name"] = strings.TrimPrefix(info.Name, "/")
b2f := func(b bool) float64 {
if b {
return 1
}
return 0
}
mapcopy := func(src map[string]string) prometheus.Labels {
dst := map[string]string{}
for k, v := range labels {
dst[k] = v
}
return dst
}
for _, lv := range []string{"none", "starting", "healthy", "unhealthy"} {
tmpLabels := mapcopy(labels)
tmpLabels["status"] = lv
ch <- prometheus.MustNewConstMetric(healthStatusDesc.Desc(tmpLabels), prometheus.GaugeValue, b2f(info.State.Health.Status == lv))
}
for _, lv := range []string{"paused", "restarting", "running", "removing", "dead", "created", "exited"} {
tmpLabels := mapcopy(labels)
tmpLabels["status"] = lv
ch <- prometheus.MustNewConstMetric(statusDesc.Desc(tmpLabels), prometheus.GaugeValue, b2f(info.State.Status == lv))
}
ch <- prometheus.MustNewConstMetric(oomkilledDesc.Desc(labels), prometheus.GaugeValue, b2f(info.State.OOMKilled))
startedat, err := time.Parse(time.RFC3339Nano, info.State.StartedAt)
errCheck(err)
finishedat, err := time.Parse(time.RFC3339Nano, info.State.FinishedAt)
errCheck(err)
ch <- prometheus.MustNewConstMetric(startedatDesc.Desc(labels), prometheus.GaugeValue, float64(startedat.Unix()))
ch <- prometheus.MustNewConstMetric(finishedatDesc.Desc(labels), prometheus.GaugeValue, float64(finishedat.Unix()))
ch <- prometheus.MustNewConstMetric(restartcountDesc.Desc(labels), prometheus.GaugeValue, float64(info.RestartCount))
}
// Loop over the last seen containers map and create metrics for each container
for name, lastseen := range lastseenContainers {
var labels = map[string]string{"name": name}
ch <- prometheus.MustNewConstMetric(lastseenDesc.Desc(labels), prometheus.GaugeValue, float64(lastseen.Unix()))
}
}
func (c *dockerHealthCollector) collectContainers() {
// Get list of containers that currently exist in the docker daemon
containers, err := c.containerClient.ContainerList(context.Background(), types.ContainerListOptions{All: true})
errCheck(err)
c.containerInfoCache = []types.ContainerJSON{}
for _, container := range containers {
// Collect metrics for each container
info, err := c.containerClient.ContainerInspect(context.Background(), container.ID)
if err != nil {
errorLogger.Log("message", err)
continue
}
// Append container name to known list of container names
containerName := strings.TrimPrefix(info.Name, "/")
lastseenContainers[containerName] = time.Now()
c.containerInfoCache = append(c.containerInfoCache, info)
if info.Config == nil {
info.Config = &tcontainer.Config{Labels: map[string]string{}}
}
if info.State.Health == nil {
info.State.Health = &types.Health{Status: "none"}
}
}
}
type loggerWrapper struct {
Logger *log.Logger
}
func (l *loggerWrapper) Println(v ...interface{}) {
(*l.Logger).Log("messages", v)
}
// Define loggers.
var (
normalLogger = log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
errorLogger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
)
func errCheck(err error) {
if err != nil {
errorLogger.Log("message", err)
os.Exit(1)
}
}
// Define flags.
var (
toolkitFlags = kingpinflag.AddFlags(kingpin.CommandLine, ":9491")
)
func init() {
normalLogger = log.With(normalLogger, "timestamp", log.DefaultTimestampUTC)
normalLogger = log.With(normalLogger, "severity", "info")
errorLogger = log.With(errorLogger, "timestamp", log.DefaultTimestampUTC)
errorLogger = log.With(errorLogger, "severity", "error")
prometheus.MustRegister(collectors.NewBuildInfoCollector())
}
func main() {
kingpin.CommandLine.UsageWriter(os.Stdout)
kingpin.HelpFlag.Short('h')
cachePeriod = kingpin.Flag("cache-period", "The period of time the collector will reuse the results of docker inspect before polling again").Default("5s").Duration()
addContainerLabels = kingpin.Flag("add-container-labels", "Add labels from docker containers as metric labels").Default("false").Bool()
kingpin.Parse()
client, err := client.NewClientWithOpts()
errCheck(err)
defer client.Close()
_, err = client.Ping(context.Background())
errCheck(err)
normalLogger.Log("message", fmt.Sprintf("Cache period is set to %v", *cachePeriod))
prometheus.MustRegister(&dockerHealthCollector{
containerClient: client,
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>docker state exporter</h1>")
})
http.HandleFunc("/-/healthy", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "up")
})
http.Handle("/metrics", promhttp.HandlerFor(
prometheus.DefaultGatherer,
promhttp.HandlerOpts{ErrorLog: &loggerWrapper{Logger: &errorLogger}, EnableOpenMetrics: true}))
server := &http.Server{}
go func() {
err = web.ListenAndServe(server, toolkitFlags, normalLogger)
if err != nil {
errCheck(err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
<-quit
normalLogger.Log("message", "Server shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
errorLogger.Log("message", fmt.Sprintf("Failed to gracefully shutdown: %d", err))
}
normalLogger.Log("message", "Server shutdown")
}