-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_exporter.go
113 lines (95 loc) · 2.7 KB
/
docker_exporter.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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
containerUptime = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "container_uptime_seconds",
Help: "Time since container started in seconds.",
},
[]string{"container_name", "container_id"},
)
containerStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "container_status",
Help: "Status of the container (1 for running, 0 for not running).",
},
[]string{"container_name", "container_id", "status"},
)
containerImage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "container_image",
Help: "Image of the container.",
},
[]string{"container_name", "container_id", "image"},
)
port = flag.String("port", "3003", "Define a porta em que o servidor deve escutar")
)
func init() {
prometheus.MustRegister(containerUptime)
prometheus.MustRegister(containerStatus)
prometheus.MustRegister(containerImage)
}
func recordMetrics() {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if err != nil {
panic(err)
}
for _, container := range containers {
json, err := cli.ContainerInspect(context.Background(), container.ID)
if err != nil {
panic(err)
}
layout := "2006-01-02T15:04:05.999999999Z"
startTime, err := time.Parse(layout, json.State.StartedAt)
if err != nil {
panic(err)
}
uptime := time.Since(startTime).Seconds()
containerUptime.WithLabelValues(container.Names[0], container.ID).Set(uptime)
status := 0
if json.State.Running {
status = 1
}
containerStatus.WithLabelValues(container.Names[0], container.ID, json.State.Status).Set(float64(status))
containerImage.WithLabelValues(container.Names[0], container.ID, container.Image).Set(1)
}
}
func main() {
flag.Parse()
addr := fmt.Sprintf("0.0.0.0:%s", *port)
fmt.Printf("Starting server on http://%s/metrics\n", addr)
go recordMetrics()
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
srv := &http.Server{Addr: addr, Handler: mux}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Error starting HTTP server: %v", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Println("Shutting down server...")
if err := srv.Shutdown(context.Background()); err != nil {
log.Fatalf("Server forced to shutdown: %v", err)
}
}