friendica-exporter/main.go

80 lines
1.9 KiB
Go
Raw Normal View History

2024-10-24 20:26:41 +02:00
package main
import (
"fmt"
"friendica-exporter/internal/client"
"friendica-exporter/internal/config"
"friendica-exporter/internal/metrics"
"friendica-exporter/serverinfo"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"os"
"github.com/sirupsen/logrus"
)
var (
// Version contains the version as set during build.
Version = ""
// GitCommit contains the git commit hash set during the build.
GitCommit = ""
log = &logrus.Logger{
Out: os.Stderr,
Formatter: &logrus.TextFormatter{
DisableTimestamp: true,
},
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
ExitFunc: os.Exit,
ReportCaller: false,
}
)
func main() {
cfg, err := config.Get()
if err != nil {
log.Fatalf("Error loading configuration: %s", err)
}
if cfg.RunMode == config.RunModeHelp {
return
}
if cfg.RunMode == config.RunModeVersion {
fmt.Println(Version)
return
}
log.Infof("friendica-exporter %s", Version)
userAgent := fmt.Sprintf("friendica-exporter/%s", Version)
if err := cfg.Validate(); err != nil {
log.Fatalf("Error validating configuration: %s", err)
}
log.Infof("Friendica server: %s Authentication using token.", cfg.ServerURL)
statsUrl := serverinfo.StatsURL(cfg.ServerURL, cfg.StatsToken)
if cfg.TLSSkipVerify {
log.Warn("HTTPS certificate verification is disabled.")
}
statsClient := client.New(statsUrl, cfg.Timeout, userAgent, cfg.TLSSkipVerify)
if err := metrics.RegisterCollector(log, statsClient); err != nil {
log.Fatalf("Failed to register collector: %s", err)
}
if err := metrics.RegisterStatsMetrics(Version, GitCommit); err != nil {
log.Fatalf("Failed to register info metrics: %s", err)
}
http.Handle("/metrics", promhttp.Handler())
http.Handle("/", http.RedirectHandler("/metrics", http.StatusFound))
log.Infof("Listen on %s...", cfg.ListenAddr)
log.Fatal(http.ListenAndServe(cfg.ListenAddr, nil))
}