forked from Tomcat-Engineering/docker_swarm_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
229 lines (198 loc) · 6.31 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
/*
Basic Prometheus exporter for a Docker Swarm. Exposes a HTTP endpoint for
Prometheus to scrape which just has the basic info on what services are
running and how many tasks are in what state for each service.
*/
package main
import (
"context"
"net/http"
"os"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
)
// Setting up the logger
var log_output = zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
var logger = zerolog.New(log_output).With().Timestamp().Logger()
func main() {
// Set the envvar DEBUG=1 to enable debug logging.
if os.Getenv("DEBUG") == "1" {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
// Init the docker client, use the DOCKER_HOST envvar to override the OS-default.
dockerClient, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
logger.Fatal().Err(err).Msgf("Error while initialising Docker client.")
}
// Output some useful info
var docker_host = os.Getenv("DOCKER_HOST")
if docker_host == "" {
docker_host = client.DefaultDockerHost
}
logger.Info().Msgf("Docker client created using socket host: %s", docker_host)
// Setup the metrics collector
coll := DockerServices{Client: dockerClient}
if err := prometheus.Register(&coll); err != nil {
logger.Fatal().Err(err).Msgf("Error while registering metrics collector.")
}
// Test connectivity to the docker daemon
info, err := coll.Client.Info(context.Background())
if err != nil {
logger.Fatal().Err(err).Msgf("Error communicating with Docker Socket.")
}
logger.Info().Str("OS", info.OSType+" / "+info.OperatingSystem).Str("version", info.ServerVersion).Msgf("Connected to Docker Daemon")
// Get rid of the stupid golang metrics
prometheus.Unregister(collectors.NewGoCollector())
// Setup the HTTP routing
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/status", coll.healthCheckHandler)
// Start the HTTP server
logger.Info().Msgf("Starting HTTP Server on port TCP/9675")
err = http.ListenAndServe(":9675", nil)
if err != nil {
logger.Fatal().Err(err).Msgf("Error starting HTTP server.")
}
}
// DockerServices implements the Collector interface.
type DockerServices struct {
*client.Client
}
var _ prometheus.Collector = (*DockerServices)(nil)
var (
replicaCount = prometheus.NewDesc(
"swarm_service_desired_replicas",
"Number of replicas requested for this service",
[]string{"service_name"}, nil,
)
taskCount = prometheus.NewDesc(
"swarm_service_tasks",
"Number of docker tasks",
[]string{"service_name", "state"}, nil,
)
containerHealthStatus = prometheus.NewDesc(
"container_health_status",
"Container Health Status",
[]string{"container_health_status", "container_name"}, nil,
)
containerStatus = prometheus.NewDesc(
"container_status",
"Container status",
[]string{"container_status", "container_name"}, nil,
)
imageVersion = prometheus.NewDesc(
"swarm_service_info",
"Information about each service",
[]string{"service_name", "image"}, nil,
)
lastChangeTime = prometheus.NewDesc(
"swarm_service_change_time",
"Time when a task state last changed",
[]string{"service_name"}, nil,
)
)
func (c DockerServices) Describe(ch chan<- *prometheus.Desc) {
ch <- replicaCount
ch <- taskCount
ch <- imageVersion
ch <- lastChangeTime
}
// Collect scrapes the container information from Docker.
func (c DockerServices) Collect(ch chan<- prometheus.Metric) {
logger.Debug().Msgf("Received request for metrics.")
services, err := c.Client.ServiceList(context.Background(), types.ServiceListOptions{})
if err != nil {
logger.Fatal().Err(err).Msgf("Error listing Swarm Services.")
}
tasks, err := c.Client.TaskList(context.Background(), types.TaskListOptions{})
if err != nil {
logger.Fatal().Err(err).Msgf("Error listing Swarm Tasks.")
}
containers, err := c.Client.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
logger.Fatal().Err(err).Msgf("Error listing Docker Containers.")
}
for _, container := range containers {
container_json, err := c.Client.ContainerInspect(context.Background(), container.ID)
if err != nil {
logger.Fatal().Err(err).Msgf("Error inspecting Docker Container details.")
}
ch <- prometheus.MustNewConstMetric(
containerStatus,
prometheus.GaugeValue,
float64(1),
container_json.State.Status,
container_json.Name,
)
if container_json.State.Health != nil {
ch <- prometheus.MustNewConstMetric(
containerHealthStatus,
prometheus.GaugeValue,
float64(1),
container_json.State.Health.Status,
container_json.Name,
)
}
}
for _, service := range services {
if service.Spec.Mode.Replicated != nil {
ch <- prometheus.MustNewConstMetric(
replicaCount,
prometheus.GaugeValue,
float64(*service.Spec.Mode.Replicated.Replicas),
service.Spec.Annotations.Name,
)
}
taskStates := make(map[string]int)
var lastTaskStatusChange time.Time
for _, task := range tasks {
if task.ServiceID == service.ID {
taskStates[string(task.Status.State)] += 1
if task.Status.Timestamp.After(lastTaskStatusChange) {
lastTaskStatusChange = task.Status.Timestamp
}
}
}
for state, count := range taskStates {
ch <- prometheus.MustNewConstMetric(
taskCount,
prometheus.GaugeValue,
float64(count),
service.Spec.Annotations.Name,
string(state),
)
}
// See https://www.robustperception.io/exposing-the-software-version-to-prometheus
ch <- prometheus.MustNewConstMetric(
imageVersion,
prometheus.GaugeValue,
1,
service.Spec.Annotations.Name,
string(service.Spec.TaskTemplate.ContainerSpec.Image),
)
ch <- prometheus.MustNewConstMetric(
lastChangeTime,
prometheus.GaugeValue,
float64(lastTaskStatusChange.Unix()),
service.Spec.Annotations.Name,
)
}
}
func (c DockerServices) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
_, err := c.Client.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
logger.Error().Err(err).Msgf("Health check error.")
w.WriteHeader(http.StatusBadRequest)
} else {
w.WriteHeader(http.StatusOK)
}
}