prometheus-borg-exporter/main.go
2024-04-01 17:02:29 +02:00

92 lines
2.5 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const VERSION = "0.1.0-alpha.0"
const LISTEN_ADDR = ":9403"
const INTERVAL = 30 * time.Minute
var backupDir = flag.String("backup-dir", "/srv/backups", "Directory where the backups are located")
var version_flag = flag.Bool("version", false, "Shows the program version")
func main() {
flag.Parse()
if *version_flag {
fmt.Printf("%v\n", VERSION)
os.Exit(0)
}
reg := prometheus.NewRegistry()
log.Printf("Backup directory is: %v\n", *backupDir)
m := NewMetrics(reg)
go RecordMetrics(*m)
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
log.Printf("Listening on %v ...\n", LISTEN_ADDR)
log.Fatal(http.ListenAndServe(LISTEN_ADDR, nil))
}
func RecordMetrics(m Metrics) {
for {
entries, err := os.ReadDir(*backupDir)
if err != nil {
log.Println(err)
os.Exit(1)
}
for _, entry := range entries {
if !entry.IsDir() || strings.HasPrefix(entry.Name(), ".") {
log.Printf(">> Ignoring %v\n", entry.Name())
continue
}
path := filepath.Join(*backupDir, entry.Name())
info, err := GetInfo(path)
if err != nil {
log.Printf(">> Could not get info about %v: %v\n", path, err)
continue
}
list, err := GetList(path)
if err != nil {
log.Printf(">> Could not get archive list from %v: %v\n", path, err)
continue
}
stats := info.Cache.Stats
log.Printf("> Got info for: %v\n", path)
m.ArchiveCount.With(prometheus.Labels{"repo_name": entry.Name()}).Set(float64(len(list.Archives)))
m.LastArchiveTime.With(prometheus.Labels{"repo_name": entry.Name()}).Set(list.LastArchiveUnix())
m.LastModified.With(prometheus.Labels{"repo_name": entry.Name()}).Set(info.LastmodUnix())
m.TotalChunks.With(prometheus.Labels{"repo_name": entry.Name()}).Set(stats.Total_chunks)
m.TotalCsize.With(prometheus.Labels{"repo_name": entry.Name()}).Set(stats.Total_csize)
m.TotalSize.With(prometheus.Labels{"repo_name": entry.Name()}).Set(stats.Total_size)
m.TotalUniqueChunks.With(prometheus.Labels{"repo_name": entry.Name()}).Set(stats.Total_unique_chunks)
m.UniqueCsize.With(prometheus.Labels{"repo_name": entry.Name()}).Set(stats.Unique_csize)
m.UniqueSize.With(prometheus.Labels{"repo_name": entry.Name()}).Set(stats.Unique_size)
}
log.Printf("> Waiting %v\n", INTERVAL)
time.Sleep(INTERVAL)
}
}