only check running containers
This commit is contained in:
parent
8a9d194984
commit
be5c17c097
1 changed files with 63 additions and 55 deletions
58
main.go
58
main.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/api/types/image"
|
"github.com/docker/docker/api/types/image"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
@ -17,7 +18,6 @@ import (
|
||||||
"github.com/regclient/regclient/types/ref"
|
"github.com/regclient/regclient/types/ref"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Hilfsfunktion: registry/repo:tag für regclient
|
|
||||||
func toRegistryImage(imageTag string) (string, error) {
|
func toRegistryImage(imageTag string) (string, error) {
|
||||||
r := regexp.MustCompile(`^(?:(?P<registry>[^/]+)/)?(?P<repo>[^:]+)(?::(?P<tag>.+))?$`)
|
r := regexp.MustCompile(`^(?:(?P<registry>[^/]+)/)?(?P<repo>[^:]+)(?::(?P<tag>.+))?$`)
|
||||||
match := r.FindStringSubmatch(imageTag)
|
match := r.FindStringSubmatch(imageTag)
|
||||||
|
@ -36,7 +36,6 @@ func toRegistryImage(imageTag string) (string, error) {
|
||||||
return fmt.Sprintf("%s/%s:%s", registry, repo, tag), nil
|
return fmt.Sprintf("%s/%s:%s", registry, repo, tag), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extrahiere nur den sha256:digest
|
|
||||||
func extractDigest(s string) string {
|
func extractDigest(s string) string {
|
||||||
for _, part := range strings.Split(s, "@") {
|
for _, part := range strings.Split(s, "@") {
|
||||||
if strings.HasPrefix(part, "sha256:") {
|
if strings.HasPrefix(part, "sha256:") {
|
||||||
|
@ -54,8 +53,8 @@ func newImageUpdateCollector() *imageUpdateCollector {
|
||||||
return &imageUpdateCollector{
|
return &imageUpdateCollector{
|
||||||
metric: prometheus.NewDesc(
|
metric: prometheus.NewDesc(
|
||||||
"docker_image_update_available",
|
"docker_image_update_available",
|
||||||
"Ob Update für das lokale Docker-Image für das Tag im Registry verfügbar ist (1=Update, 0=aktuell)",
|
"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{"image", "tag"},
|
[]string{"container_name", "image", "tag"},
|
||||||
nil,
|
nil,
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
@ -75,33 +74,44 @@ func (c *imageUpdateCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
rc := regclient.New()
|
rc := regclient.New()
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
images, err := cli.ImageList(ctx, image.ListOptions{All: true})
|
images, err := cli.ImageList(ctx, image.ListOptions{All: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Fehler bei ImageList: %v", err)
|
log.Printf("Fehler bei ImageList: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
imageTagToDigest := make(map[string]string)
|
||||||
for _, img := range images {
|
for _, img := range images {
|
||||||
for _, tag := range img.RepoTags {
|
for _, tag := range img.RepoTags {
|
||||||
wg.Add(1)
|
if len(img.RepoDigests) > 0 {
|
||||||
go func(tag string, img image.Summary) {
|
imageTagToDigest[tag] = extractDigest(img.RepoDigests[0])
|
||||||
defer wg.Done()
|
} else {
|
||||||
|
imageTagToDigest[tag] = img.ID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for _, ctr := range containers {
|
||||||
|
tag := ctr.Image
|
||||||
|
|
||||||
imageRef, err := toRegistryImage(tag)
|
imageRef, err := toRegistryImage(tag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ImageRef-Fehler bei %s: %v", tag, err)
|
log.Printf("ImageRef-Fehler bei %s: %v", tag, err)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
|
localDigest := imageTagToDigest[tag]
|
||||||
|
|
||||||
// Lokalen Digest holen
|
wg.Add(1)
|
||||||
var localDigest string
|
go func(ctr container.Summary, tag, localDigest, imageRef string) {
|
||||||
if len(img.RepoDigests) > 0 {
|
defer wg.Done()
|
||||||
localDigest = extractDigest(img.RepoDigests[0])
|
|
||||||
} else {
|
|
||||||
localDigest = img.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remote Digest holen
|
|
||||||
refObj, err := ref.New(imageRef)
|
refObj, err := ref.New(imageRef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ImageRef (regclient) ungültig (%s): %v", tag, err)
|
log.Printf("ImageRef (regclient) ungültig (%s): %v", tag, err)
|
||||||
|
@ -114,8 +124,7 @@ func (c *imageUpdateCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
}
|
}
|
||||||
remoteDigest := desc.GetDigest().String()
|
remoteDigest := desc.GetDigest().String()
|
||||||
|
|
||||||
// Logging
|
fmt.Printf("Container: %s\n Image: %s\n Local Digest: %s\n Remote Digest: %s\n", ctr.Names[0], tag, localDigest, remoteDigest)
|
||||||
fmt.Printf("Image: %s\n Local Digest: %s\n Remote Digest: %s\n", tag, localDigest, remoteDigest)
|
|
||||||
var update float64 = 0
|
var update float64 = 0
|
||||||
if localDigest != remoteDigest {
|
if localDigest != remoteDigest {
|
||||||
fmt.Println(" -> Update verfügbar!")
|
fmt.Println(" -> Update verfügbar!")
|
||||||
|
@ -123,23 +132,22 @@ func (c *imageUpdateCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(" -> Kein Update verfügbar.")
|
fmt.Println(" -> Kein Update verfügbar.")
|
||||||
}
|
}
|
||||||
|
// Labels container_name, image, tag
|
||||||
labelImg, labelTag := tag, "latest"
|
labelImg, labelTag := tag, "latest"
|
||||||
if cp := strings.Split(tag, ":"); len(cp) == 2 {
|
if cp := strings.Split(tag, ":"); len(cp) == 2 {
|
||||||
labelImg, labelTag = cp[0], cp[1]
|
labelImg, labelTag = cp[0], cp[1]
|
||||||
}
|
}
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.metric, prometheus.GaugeValue,
|
c.metric, prometheus.GaugeValue,
|
||||||
update, labelImg, labelTag,
|
update, ctr.Names[0], labelImg, labelTag,
|
||||||
)
|
)
|
||||||
}(tag, img)
|
}(ctr, tag, localDigest, imageRef)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Println("Starte Docker-Image-Update Prometheus Exporter (Port 9788)...")
|
log.Println("Starte Prometheus Exporter für laufende Container-Images (Port 9788)...")
|
||||||
exporter := newImageUpdateCollector()
|
exporter := newImageUpdateCollector()
|
||||||
prometheus.MustRegister(exporter)
|
prometheus.MustRegister(exporter)
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
|
|
Loading…
Add table
Reference in a new issue