-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
104 lines (85 loc) · 2.84 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
package main
import (
"context"
"encoding/json"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
)
const (
namespace = "docker_exporter"
)
var (
cpuUsageDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "cpu_usage_percent"),
"Container CPU Usage Percentage",
[]string{"container_id"}, nil,
)
memoryUsageDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "memory_usage_bytes"),
"Container Memory Usage in bytes",
[]string{"container_id"}, nil,
)
)
type dockerCollector struct {
dockerClient *client.Client
}
func newDockerCollector() (*dockerCollector, error) {
cli, err := client.NewClientWithOpts(client.WithVersion("1.41")) // Use the appropriate Docker API version
if err != nil {
return nil, err
}
return &dockerCollector{
dockerClient: cli,
}, nil
}
func (dc *dockerCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- cpuUsageDesc
ch <- memoryUsageDesc
}
func (dc *dockerCollector) Collect(ch chan<- prometheus.Metric) {
containers, err := dc.dockerClient.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
log.Println("Failed to list containers:", err)
return
}
for _, container := range containers {
cpuUsagePercent, memoryUsageBytes, err := dc.getContainerMetrics(container.ID)
if err != nil {
log.Println("Failed to get metrics for container", container.ID, ":", err)
continue
}
ch <- prometheus.MustNewConstMetric(cpuUsageDesc, prometheus.GaugeValue, cpuUsagePercent, container.ID)
ch <- prometheus.MustNewConstMetric(memoryUsageDesc, prometheus.GaugeValue, float64(memoryUsageBytes), container.ID)
}
}
func (dc *dockerCollector) getContainerMetrics(containerID string) (float64, uint64, error) {
stats, err := dc.dockerClient.ContainerStats(context.Background(), containerID, false)
if err != nil {
return 0, 0, err
}
defer stats.Body.Close()
var statData types.StatsJSON
if err := json.NewDecoder(stats.Body).Decode(&statData); err != nil {
return 0, 0, err
}
// Calculate CPU usage percentage
cpuDelta := float64(statData.CPUStats.CPUUsage.TotalUsage - statData.PreCPUStats.CPUUsage.TotalUsage)
systemDelta := float64(statData.CPUStats.SystemUsage - statData.PreCPUStats.SystemUsage)
cpuUsagePercent := (cpuDelta / systemDelta) * float64(len(statData.CPUStats.CPUUsage.PercpuUsage)) * 100.0
// Memory usage in bytes
memoryUsageBytes := statData.MemoryStats.Usage - statData.MemoryStats.Stats["cache"]
return cpuUsagePercent, memoryUsageBytes, nil
}
func main() {
dc, err := newDockerCollector()
if err != nil {
log.Fatal("Error creating Docker collector:", err)
}
prometheus.MustRegister(dc)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":924", nil))
}