app-exporter/app_exporter.go

73 lines
1.4 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"recycled.cloud/app-exporter/apps"
)
// var so it can be overridden at build time
var VERSION = "dev"
const LISTEN_ADDR = ":9404"
const INTERVAL = 24 * time.Hour
var version_flag = flag.Bool("version", false, "Shows the program version")
var logfile = flag.String("logfile", "-", "Where to write the logs")
func main() {
flag.Parse()
if *version_flag {
fmt.Printf("%v\n", VERSION)
os.Exit(0)
}
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)
}
reg := prometheus.NewRegistry()
log.Printf("app-exporter version %v\n", VERSION)
m := apps.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 apps.Metrics) {
for {
m.Reset()
apps.Cubebackup(m)
apps.Forgejo(m)
apps.ForgejoRunner(m)
apps.Grafana(m)
apps.Nextcloud(m)
apps.Odoo(m)
apps.Postgres(m)
apps.Vaultwarden(m)
log.Printf("> Waiting %v\n", INTERVAL)
time.Sleep(INTERVAL)
}
}