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 03:15:14 +02:00
|
|
|
"sync"
|
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:15:14 +02:00
|
|
|
// Hilfsfunktion: registry/repo:tag für regclient
|
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:15:14 +02:00
|
|
|
// Extrahiere 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:15:14 +02:00
|
|
|
type imageUpdateCollector struct {
|
|
|
|
metric *prometheus.Desc
|
2025-07-17 03:12:27 +02:00
|
|
|
}
|
|
|
|
|
2025-07-17 03:15:14 +02:00
|
|
|
func newImageUpdateCollector() *imageUpdateCollector {
|
|
|
|
return &imageUpdateCollector{
|
|
|
|
metric: prometheus.NewDesc(
|
2025-07-17 03:12:27 +02:00
|
|
|
"docker_image_update_available",
|
2025-07-17 03:15:14 +02:00
|
|
|
"Ob Update für das lokale Docker-Image für das Tag im Registry verfügbar ist (1=Update, 0=aktuell)",
|
|
|
|
[]string{"image", "tag"},
|
2025-07-17 03:12:27 +02:00
|
|
|
nil,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-17 03:15:14 +02:00
|
|
|
func (c *imageUpdateCollector) Describe(ch chan<- *prometheus.Desc) {
|
|
|
|
ch <- c.metric
|
2025-07-17 03:12:27 +02:00
|
|
|
}
|
|
|
|
|
2025-07-17 03:15:14 +02:00
|
|
|
func (c *imageUpdateCollector) 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:15:14 +02:00
|
|
|
log.Printf("Fehler bei Docker-Client: %v", err)
|
2025-07-17 03:12:27 +02:00
|
|
|
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:15:14 +02:00
|
|
|
log.Printf("Fehler bei ImageList: %v", err)
|
2025-07-17 03:12:27 +02:00
|
|
|
return
|
2025-07-17 02:53:36 +02:00
|
|
|
}
|
2025-07-17 02:35:36 +02:00
|
|
|
|
2025-07-17 03:15:14 +02:00
|
|
|
var wg sync.WaitGroup
|
2025-07-17 03:04:35 +02:00
|
|
|
for _, img := range images {
|
|
|
|
for _, tag := range img.RepoTags {
|
2025-07-17 03:15:14 +02:00
|
|
|
wg.Add(1)
|
|
|
|
go func(tag string, img image.Summary) {
|
|
|
|
defer wg.Done()
|
|
|
|
imageRef, err := toRegistryImage(tag)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ImageRef-Fehler bei %s: %v", tag, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lokalen Digest holen
|
|
|
|
var localDigest string
|
|
|
|
if len(img.RepoDigests) > 0 {
|
|
|
|
localDigest = extractDigest(img.RepoDigests[0])
|
|
|
|
} else {
|
|
|
|
localDigest = img.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remote Digest holen
|
|
|
|
refObj, err := ref.New(imageRef)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ImageRef (regclient) ungültig (%s): %v", tag, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
desc, err := rc.ManifestHead(ctx, refObj)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Remote-Manifest nicht gefunden (%s): %v", tag, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
remoteDigest := desc.GetDigest().String()
|
|
|
|
|
|
|
|
// Logging
|
|
|
|
fmt.Printf("Image: %s\n Local Digest: %s\n Remote Digest: %s\n", tag, localDigest, remoteDigest)
|
|
|
|
var update float64 = 0
|
|
|
|
if localDigest != remoteDigest {
|
|
|
|
fmt.Println(" -> Update verfügbar!")
|
|
|
|
update = 1
|
|
|
|
} else {
|
|
|
|
fmt.Println(" -> Kein Update verfügbar.")
|
|
|
|
}
|
|
|
|
|
|
|
|
labelImg, labelTag := tag, "latest"
|
|
|
|
if cp := strings.Split(tag, ":"); len(cp) == 2 {
|
|
|
|
labelImg, labelTag = cp[0], cp[1]
|
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.metric, prometheus.GaugeValue,
|
|
|
|
update, labelImg, labelTag,
|
|
|
|
)
|
|
|
|
}(tag, img)
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
|
|
|
}
|
2025-07-17 03:15:14 +02:00
|
|
|
wg.Wait()
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
2025-07-17 03:12:27 +02:00
|
|
|
|
|
|
|
func main() {
|
2025-07-17 03:15:14 +02:00
|
|
|
log.Println("Starte Docker-Image-Update Prometheus Exporter (Port 9788)...")
|
|
|
|
exporter := newImageUpdateCollector()
|
|
|
|
prometheus.MustRegister(exporter)
|
2025-07-17 03:12:27 +02:00
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
2025-07-17 03:15:14 +02:00
|
|
|
log.Fatal(http.ListenAndServe(":9788", nil))
|
2025-07-17 03:12:27 +02:00
|
|
|
}
|