2024-04-01 17:02:29 +02:00
|
|
|
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")
|
2024-04-01 17:20:34 +02:00
|
|
|
var logfile = flag.String("logfile", "-", "Where to write the logs")
|
2024-04-01 17:02:29 +02:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2024-04-01 17:20:34 +02:00
|
|
|
if *logfile != "-" {
|
|
|
|
f, err := os.OpenFile(*logfile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0664)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not open logfile: %v\n", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
log.SetOutput(f)
|
|
|
|
}
|
|
|
|
|
2024-04-01 17:02:29 +02:00
|
|
|
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 {
|
2024-04-01 17:20:34 +02:00
|
|
|
log.Fatalln(err)
|
2024-04-01 17:02:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|