2021-03-05 18:47:23 +00:00
|
|
|
package fetcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-01-17 16:49:34 +00:00
|
|
|
"github.com/hetznercloud/hcloud-go/v2/hcloud"
|
2021-03-05 18:47:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ Fetcher = &server{}
|
|
|
|
|
2021-03-05 18:53:37 +00:00
|
|
|
// NewServer creates a new fetcher that will collect pricing information on servers.
|
2023-01-25 19:24:33 +00:00
|
|
|
func NewServer(pricing *PriceProvider, additionalLabels ...string) Fetcher {
|
2023-01-25 20:23:54 +00:00
|
|
|
return &server{newBase(pricing, "server", []string{"location", "type"}, additionalLabels...)}
|
2021-03-05 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
*baseFetcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server server) Run(client *hcloud.Client) error {
|
|
|
|
servers, _, err := client.Server.List(ctx, hcloud.ServerListOpts{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range servers {
|
|
|
|
location := s.Datacenter.Location
|
|
|
|
|
2023-01-25 19:24:33 +00:00
|
|
|
labels := append([]string{
|
|
|
|
s.Name,
|
|
|
|
location.Name,
|
|
|
|
s.ServerType.Name,
|
|
|
|
},
|
|
|
|
parseAdditionalLabels(server.additionalLabels, s.Labels)...,
|
|
|
|
)
|
|
|
|
|
2021-03-05 18:47:23 +00:00
|
|
|
pricing, err := findServerPricing(location, s.ServerType.Pricings)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-25 19:24:33 +00:00
|
|
|
parseToGauge(server.hourly.WithLabelValues(labels...), pricing.Hourly.Gross)
|
|
|
|
parseToGauge(server.monthly.WithLabelValues(labels...), pricing.Monthly.Gross)
|
2021-03-05 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findServerPricing(location *hcloud.Location, pricings []hcloud.ServerTypeLocationPricing) (*hcloud.ServerTypeLocationPricing, error) {
|
|
|
|
for _, pricing := range pricings {
|
2021-03-06 11:20:10 +00:00
|
|
|
if pricing.Location.Name == location.Name {
|
2021-03-05 18:47:23 +00:00
|
|
|
return &pricing, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 11:10:22 +00:00
|
|
|
return nil, fmt.Errorf("no server pricing found for location %s", location.Name)
|
2021-03-05 18:47:23 +00:00
|
|
|
}
|