add cache
This commit is contained in:
parent
e88bb581c6
commit
a92ed9fe8f
1 changed files with 178 additions and 129 deletions
121
go/main.go
121
go/main.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/api/types/image"
|
"github.com/docker/docker/api/types/image"
|
||||||
|
@ -18,6 +19,23 @@ import (
|
||||||
"github.com/regclient/regclient/types/ref"
|
"github.com/regclient/regclient/types/ref"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ImageStatus struct {
|
||||||
|
ContainerName, Image, Tag string
|
||||||
|
UpdateAvailable float64
|
||||||
|
LocalDigest, RemoteDigest string
|
||||||
|
}
|
||||||
|
|
||||||
|
type StatusCache struct {
|
||||||
|
sync.RWMutex
|
||||||
|
Data []ImageStatus
|
||||||
|
LastCheck time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
cache = &StatusCache{Data: []ImageStatus{}, LastCheck: time.Time{}}
|
||||||
|
interval = 6 * time.Hour
|
||||||
|
)
|
||||||
|
|
||||||
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)
|
||||||
|
@ -45,26 +63,7 @@ func extractDigest(s string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
type imageUpdateCollector struct {
|
func checkImageUpdates() {
|
||||||
metric *prometheus.Desc
|
|
||||||
}
|
|
||||||
|
|
||||||
func newImageUpdateCollector() *imageUpdateCollector {
|
|
||||||
return &imageUpdateCollector{
|
|
||||||
metric: prometheus.NewDesc(
|
|
||||||
"docker_image_update_available",
|
|
||||||
"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"},
|
|
||||||
nil,
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *imageUpdateCollector) Describe(ch chan<- *prometheus.Desc) {
|
|
||||||
ch <- c.metric
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *imageUpdateCollector) Collect(ch chan<- prometheus.Metric) {
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -74,7 +73,6 @@ 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})
|
containers, err := cli.ContainerList(ctx, container.ListOptions{All: false})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Fehler bei ContainerList: %v", err)
|
log.Printf("Fehler bei ContainerList: %v", err)
|
||||||
|
@ -99,18 +97,22 @@ func (c *imageUpdateCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
results := make([]ImageStatus, 0)
|
||||||
|
resultsLock := sync.Mutex{}
|
||||||
for _, ctr := range containers {
|
for _, ctr := range containers {
|
||||||
tag := ctr.Image
|
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)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
localDigest := imageTagToDigest[tag]
|
localDigest := imageTagToDigest[tag]
|
||||||
|
containerName := "unknown"
|
||||||
|
if len(ctr.Names) > 0 {
|
||||||
|
containerName = ctr.Names[0]
|
||||||
|
}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(ctr container.Summary, tag, localDigest, imageRef string) {
|
go func(containerName, tag, localDigest, imageRef string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
refObj, err := ref.New(imageRef)
|
refObj, err := ref.New(imageRef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -124,30 +126,77 @@ func (c *imageUpdateCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
}
|
}
|
||||||
remoteDigest := desc.GetDigest().String()
|
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)
|
update := 0.0
|
||||||
var update float64 = 0
|
|
||||||
if localDigest != remoteDigest {
|
if localDigest != remoteDigest {
|
||||||
fmt.Println(" -> Update verfügbar!")
|
update = 1.0
|
||||||
update = 1
|
fmt.Printf("Container: %s\n Image: %s\n Local Digest: %s\n Remote Digest: %s\n -> Update verfügbar!\n", containerName, tag, localDigest, remoteDigest)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(" -> Kein Update verfügbar.")
|
fmt.Printf("Container: %s\n Image: %s\n Local Digest: %s\n Remote Digest: %s\n -> Kein Update verfügbar.\n", containerName, tag, localDigest, remoteDigest)
|
||||||
}
|
}
|
||||||
// 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(
|
resultsLock.Lock()
|
||||||
c.metric, prometheus.GaugeValue,
|
results = append(results, ImageStatus{
|
||||||
update, ctr.Names[0], labelImg, labelTag,
|
ContainerName: containerName, Image: labelImg, Tag: labelTag,
|
||||||
)
|
UpdateAvailable: update, LocalDigest: localDigest, RemoteDigest: remoteDigest,
|
||||||
}(ctr, tag, localDigest, imageRef)
|
})
|
||||||
|
resultsLock.Unlock()
|
||||||
|
}(containerName, tag, localDigest, imageRef)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
|
// Im Cache speichern
|
||||||
|
cache.Lock()
|
||||||
|
cache.Data = results
|
||||||
|
cache.LastCheck = time.Now()
|
||||||
|
cache.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
type imageUpdateCollector struct {
|
||||||
|
metric *prometheus.Desc
|
||||||
|
}
|
||||||
|
|
||||||
|
func newImageUpdateCollector() *imageUpdateCollector {
|
||||||
|
return &imageUpdateCollector{
|
||||||
|
metric: prometheus.NewDesc(
|
||||||
|
"docker_image_update_available",
|
||||||
|
"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"},
|
||||||
|
nil,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *imageUpdateCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||||
|
ch <- c.metric
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *imageUpdateCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
|
cache.RLock()
|
||||||
|
for _, stat := range cache.Data {
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
c.metric, prometheus.GaugeValue,
|
||||||
|
stat.UpdateAvailable, stat.ContainerName, stat.Image, stat.Tag,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
cache.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Println("Starte Prometheus Exporter für laufende Container-Images (Port 9788)...")
|
log.Printf("Starte Docker-Image-Update-Exporter mit 6h-Intervall...")
|
||||||
|
|
||||||
|
// Hintergrund: alle 6h Update, zu Beginn auch direkt
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
checkImageUpdates()
|
||||||
|
time.Sleep(interval)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
checkImageUpdates() // Initiales Scrape
|
||||||
|
|
||||||
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