-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
238 lines (202 loc) · 6.61 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"sync"
"syscall"
"time"
"github.com/docker/docker/api/types"
tcontainer "github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
// cachePeriod indicates the period of time the collector will reuse the results of docker inspect.
cachePeriod = 1 * time.Second
)
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"}
)
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)
}
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.collectContainer()
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_]")
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))
}
}
func (c *dockerHealthCollector) collectContainer() {
containers, err := c.containerClient.ContainerList(context.Background(), types.ContainerListOptions{All: true})
errCheck(err)
c.containerInfoCache = []types.ContainerJSON{}
for _, container := range containers {
info, err := c.containerClient.ContainerInspect(context.Background(), container.ID)
errCheck(err)
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 (
address = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
)
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(prometheus.NewBuildInfoCollector())
}
func main() {
flag.Parse()
client, err := client.NewEnvClient()
errCheck(err)
defer client.Close()
_, err = client.Ping(context.Background())
errCheck(err)
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}))
normalLogger.Log("message", "Server listening...", "address", address)
server := &http.Server{Addr: *address, Handler: nil}
go func() {
err = server.ListenAndServe()
if err != http.ErrServerClosed {
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: %v", err))
}
normalLogger.Log("message", "Server shutdown")
}