2021-11-13 19:36:28 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-04 13:34:10 +01:00
|
|
|
"errors"
|
2021-11-13 19:36:28 +01:00
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Tomy2e/livebox-api-client"
|
2023-03-04 13:34:10 +01:00
|
|
|
"github.com/Tomy2e/livebox-exporter/internal/poller"
|
2021-11-13 19:36:28 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2023-03-04 13:34:10 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
2021-11-13 19:36:28 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultPollingFrequency = 30
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
pollingFrequency := flag.Uint("polling-frequency", defaultPollingFrequency, "Polling frequency")
|
|
|
|
listen := flag.String("listen", ":8080", "Listening address")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
adminPassword := os.Getenv("ADMIN_PASSWORD")
|
|
|
|
if adminPassword == "" {
|
|
|
|
log.Fatal("ADMIN_PASSWORD environment variable must be set")
|
|
|
|
}
|
|
|
|
|
2023-03-04 13:34:10 +01:00
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
registry = prometheus.NewRegistry()
|
|
|
|
client = livebox.NewClient(adminPassword)
|
|
|
|
pollers = poller.Pollers{
|
|
|
|
poller.NewDevicesTotal(client),
|
|
|
|
poller.NewInterfaceMbits(client),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
registry.MustRegister(
|
|
|
|
append(
|
|
|
|
pollers.Collectors(),
|
|
|
|
collectors.NewGoCollector(),
|
|
|
|
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
|
|
|
)...,
|
|
|
|
)
|
2021-11-13 19:36:28 +01:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2023-03-04 13:34:10 +01:00
|
|
|
if err := pollers.Poll(ctx); err != nil {
|
|
|
|
if errors.Is(err, livebox.ErrInvalidPassword) {
|
|
|
|
log.Fatal(err)
|
2021-11-13 19:36:28 +01:00
|
|
|
}
|
|
|
|
|
2023-03-04 13:34:10 +01:00
|
|
|
log.Printf("WARN: polling failed: %s\n", err)
|
2021-11-13 19:36:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Duration(*pollingFrequency) * time.Second)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-04 13:34:10 +01:00
|
|
|
http.Handle("/metrics", promhttp.InstrumentMetricHandler(
|
|
|
|
registry, promhttp.HandlerFor(registry, promhttp.HandlerOpts{}),
|
|
|
|
))
|
2021-11-13 19:36:28 +01:00
|
|
|
log.Printf("Listening on %s\n", *listen)
|
|
|
|
log.Fatal(http.ListenAndServe(*listen, nil))
|
|
|
|
}
|