2025-07-17 02:35:36 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2025-07-17 02:53:36 +02:00
|
|
|
"log"
|
2025-07-17 03:12:27 +02:00
|
|
|
"net/http"
|
2025-07-17 02:35:36 +02:00
|
|
|
"regexp"
|
2025-07-17 03:04:35 +02:00
|
|
|
"strings"
|
2025-07-17 02:35:36 +02:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/image"
|
|
|
|
"github.com/docker/docker/client"
|
2025-07-17 03:12:27 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2025-07-17 02:53:36 +02:00
|
|
|
"github.com/regclient/regclient"
|
|
|
|
"github.com/regclient/regclient/types/ref"
|
2025-07-17 02:35:36 +02:00
|
|
|
)
|
|
|
|
|
2025-07-17 03:12:27 +02:00
|
|
|
// Hilfsfunktion: registrykompatibles Format aus repo:tag erzeugen
|
2025-07-17 03:04:35 +02:00
|
|
|
func toRegistryImage(imageTag string) (string, error) {
|
2025-07-17 02:35:36 +02:00
|
|
|
r := regexp.MustCompile(`^(?:(?P<registry>[^/]+)/)?(?P<repo>[^:]+)(?::(?P<tag>.+))?$`)
|
2025-07-17 03:04:35 +02:00
|
|
|
match := r.FindStringSubmatch(imageTag)
|
2025-07-17 02:35:36 +02:00
|
|
|
if len(match) == 0 {
|
2025-07-17 03:04:35 +02:00
|
|
|
return "", fmt.Errorf("Image-Tag nicht erkannt: %s", imageTag)
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
2025-07-17 02:53:36 +02:00
|
|
|
registry := match[r.SubexpIndex("registry")]
|
|
|
|
repo := match[r.SubexpIndex("repo")]
|
|
|
|
tag := match[r.SubexpIndex("tag")]
|
|
|
|
if registry == "" {
|
|
|
|
registry = "registry-1.docker.io"
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
2025-07-17 02:53:36 +02:00
|
|
|
if tag == "" {
|
|
|
|
tag = "latest"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s/%s:%s", registry, repo, tag), nil
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
|
|
|
|
2025-07-17 03:12:27 +02:00
|
|
|
// Extrahiert nur den sha256:... Digest
|
2025-07-17 03:04:35 +02:00
|
|
|
func extractDigest(s string) string {
|
|
|
|
for _, part := range strings.Split(s, "@") {
|
|
|
|
if strings.HasPrefix(part, "sha256:") {
|
|
|
|
return part
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
|
|
|
}
|
2025-07-17 03:12:27 +02:00
|
|
|
return s
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
|
|
|
|
2025-07-17 03:12:27 +02:00
|
|
|
// Prometheus Collector-Struktur
|
|
|
|
type dockerImageUpdateCollector struct {
|
|
|
|
desc *prometheus.Desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDockerImageUpdateCollector() prometheus.Collector {
|
|
|
|
return &dockerImageUpdateCollector{
|
|
|
|
desc: prometheus.NewDesc(
|
|
|
|
"docker_image_update_available",
|
|
|
|
"Whether a new image digest is available for this local image tag (1=update, 0=current)",
|
|
|
|
[]string{"image"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dockerImageUpdateCollector) Describe(ch chan<- *prometheus.Desc) {
|
|
|
|
ch <- c.desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dockerImageUpdateCollector) Collect(ch chan<- prometheus.Metric) {
|
2025-07-17 02:53:36 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2025-07-17 02:35:36 +02:00
|
|
|
if err != nil {
|
2025-07-17 03:12:27 +02:00
|
|
|
log.Println("Docker-Client-Fehler:", err)
|
|
|
|
return
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
2025-07-17 02:53:36 +02:00
|
|
|
defer cli.Close()
|
|
|
|
rc := regclient.New()
|
|
|
|
|
2025-07-17 03:04:35 +02:00
|
|
|
images, err := cli.ImageList(ctx, image.ListOptions{All: true})
|
2025-07-17 02:35:36 +02:00
|
|
|
if err != nil {
|
2025-07-17 03:12:27 +02:00
|
|
|
log.Println("Docker-ImageList-Fehler:", err)
|
|
|
|
return
|
2025-07-17 02:53:36 +02:00
|
|
|
}
|
2025-07-17 02:35:36 +02:00
|
|
|
|
2025-07-17 03:04:35 +02:00
|
|
|
for _, img := range images {
|
|
|
|
for _, tag := range img.RepoTags {
|
|
|
|
imageRef, err := toRegistryImage(tag)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var localDigest string
|
|
|
|
if len(img.RepoDigests) > 0 {
|
|
|
|
localDigest = extractDigest(img.RepoDigests[0])
|
|
|
|
} else {
|
|
|
|
localDigest = img.ID
|
|
|
|
}
|
|
|
|
refObj, err := ref.New(imageRef)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
desc, err := rc.ManifestHead(ctx, refObj)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
remoteDigest := desc.GetDigest().String()
|
2025-07-17 03:12:27 +02:00
|
|
|
var value float64 = 0
|
|
|
|
if localDigest != remoteDigest {
|
|
|
|
value = 1
|
2025-07-17 03:04:35 +02:00
|
|
|
}
|
2025-07-17 03:12:27 +02:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.desc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
value,
|
|
|
|
tag,
|
|
|
|
)
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-07-17 03:12:27 +02:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
prometheus.MustRegister(newDockerImageUpdateCollector())
|
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
|
|
log.Println("Prometheus Exporter läuft auf :9888/metrics")
|
|
|
|
log.Fatal(http.ListenAndServe(":9888", nil))
|
|
|
|
}
|