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
|
|
|
|
2025-07-17 03:18:04 +02:00
|
|
|
"github.com/docker/docker/api/types/container"
|
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: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: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:18:04 +02:00
|
|
|
"Ob Update für das lokale Docker-Image des laufenden Containers für das Tag im Registry verfügbar ist (1=Update, 0=aktuell)",
|
|
|
|
[]string{"container_name", "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:18:04 +02:00
|
|
|
// Hole nur aktive (laufende) Container
|
|
|
|
containers, err := cli.ContainerList(ctx, container.ListOptions{All: false})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Fehler bei ContainerList: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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:18:04 +02:00
|
|
|
imageTagToDigest := make(map[string]string)
|
2025-07-17 03:04:35 +02:00
|
|
|
for _, img := range images {
|
|
|
|
for _, tag := range img.RepoTags {
|
2025-07-17 03:18:04 +02:00
|
|
|
if len(img.RepoDigests) > 0 {
|
|
|
|
imageTagToDigest[tag] = extractDigest(img.RepoDigests[0])
|
|
|
|
} else {
|
|
|
|
imageTagToDigest[tag] = img.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, ctr := range containers {
|
|
|
|
tag := ctr.Image
|
|
|
|
|
|
|
|
imageRef, err := toRegistryImage(tag)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ImageRef-Fehler bei %s: %v", tag, err)
|
|
|
|
continue
|
2025-07-17 02:35:36 +02:00
|
|
|
}
|
2025-07-17 03:18:04 +02:00
|
|
|
localDigest := imageTagToDigest[tag]
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(ctr container.Summary, tag, localDigest, imageRef string) {
|
|
|
|
defer wg.Done()
|
|
|
|
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()
|
|
|
|
|
|
|
|
fmt.Printf("Container: %s\n Image: %s\n Local Digest: %s\n Remote Digest: %s\n", ctr.Names[0], tag, localDigest, remoteDigest)
|
|
|
|
var update float64 = 0
|
|
|
|
if localDigest != remoteDigest {
|
|
|
|
fmt.Println(" -> Update verfügbar!")
|
|
|
|
update = 1
|
|
|
|
} else {
|
|
|
|
fmt.Println(" -> Kein Update verfügbar.")
|
|
|
|
}
|
|
|
|
// Labels container_name, image, tag
|
|
|
|
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, ctr.Names[0], labelImg, labelTag,
|
|
|
|
)
|
|
|
|
}(ctr, tag, localDigest, imageRef)
|
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:18:04 +02:00
|
|
|
log.Println("Starte Prometheus Exporter für laufende Container-Images (Port 9788)...")
|
2025-07-17 03:15:14 +02:00
|
|
|
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
|
|
|
}
|