hcloud-pricing-exporter/main.go

73 lines
2 KiB
Go
Raw Normal View History

2021-03-05 18:47:23 +00:00
package main
import (
"flag"
"fmt"
"net/http"
"os"
"time"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/jangraefen/hcloud-pricing-exporter/fetcher"
"github.com/jtaczanowski/go-scheduler"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
defaultPort = 8080
defaultFetchInterval = 1 * time.Minute
)
2021-03-05 19:28:59 +00:00
var (
hcloudAPIToken string
port uint
fetchInterval time.Duration
)
2021-03-05 18:47:23 +00:00
2021-03-05 19:28:59 +00:00
func handleFlags() {
2021-03-05 18:47:23 +00:00
flag.StringVar(&hcloudAPIToken, "hcloud-token", "", "the token to authenticate against the HCloud API")
flag.UintVar(&port, "port", defaultPort, "the port that the exporter exposes its data on")
flag.DurationVar(&fetchInterval, "fetch-interval", defaultFetchInterval, "the interval between data fetching cycles")
flag.Parse()
if hcloudAPIToken == "" {
if envHCloudAPIToken, present := os.LookupEnv("HCLOUD_TOKEN"); present {
hcloudAPIToken = envHCloudAPIToken
}
}
if hcloudAPIToken == "" {
panic(fmt.Errorf("no API token for HCloud specified, but required"))
}
2021-03-05 19:28:59 +00:00
}
func main() {
handleFlags()
2021-03-05 18:47:23 +00:00
client := hcloud.NewClient(hcloud.WithToken(hcloudAPIToken))
2021-03-05 19:28:17 +00:00
priceRepository := &fetcher.PriceProvider{Client: client}
fetchers := fetcher.Fetchers{
fetcher.NewFloatingIP(priceRepository),
fetcher.NewLoadbalancer(priceRepository),
fetcher.NewLoadbalancerTraffic(priceRepository),
fetcher.NewServer(priceRepository),
fetcher.NewServerBackup(priceRepository),
fetcher.NewServerTraffic(priceRepository),
fetcher.NewSnapshot(priceRepository),
fetcher.NewVolume(priceRepository),
}
2021-03-05 18:47:23 +00:00
fetchers.MustRun(client)
scheduler.RunTaskAtInterval(func() { fetchers.MustRun(client) }, fetchInterval, 0)
2021-03-05 19:28:17 +00:00
scheduler.RunTaskAtInterval(priceRepository.Sync, 10*fetchInterval, 10*fetchInterval)
2021-03-05 18:47:23 +00:00
registry := prometheus.NewRegistry()
fetchers.RegisterCollectors(registry)
2021-03-05 18:47:23 +00:00
http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
panic(err)
}
}