hcloud-pricing-exporter/fetcher/server_traffic.go

45 lines
1.3 KiB
Go
Raw Normal View History

2021-03-05 18:47:23 +00:00
package fetcher
import (
"math"
"github.com/hetznercloud/hcloud-go/hcloud"
)
var _ Fetcher = &serverTraffic{}
2021-03-05 18:53:37 +00:00
// NewServerTraffic creates a new fetcher that will collect pricing information on server traffic.
2021-03-05 19:28:17 +00:00
func NewServerTraffic(pricing *PriceProvider) Fetcher {
return &serverTraffic{newBase(pricing, "server_traffic", "location", "type")}
2021-03-05 18:47:23 +00:00
}
type serverTraffic struct {
*baseFetcher
}
func (serverTraffic serverTraffic) 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
additionalTraffic := s.OutgoingTraffic - s.IncludedTraffic
if additionalTraffic < 0 {
serverTraffic.hourly.WithLabelValues(s.Name, location.Name, s.ServerType.Name).Set(0)
serverTraffic.monthly.WithLabelValues(s.Name, location.Name, s.ServerType.Name).Set(0)
break
}
2021-03-05 19:28:17 +00:00
monthlyPrice := math.Ceil(float64(additionalTraffic)/sizeTB) * serverTraffic.pricing.Traffic()
2021-03-05 18:47:23 +00:00
hourlyPrice := pricingPerHour(monthlyPrice)
serverTraffic.hourly.WithLabelValues(s.Name, location.Name, s.ServerType.Name).Set(hourlyPrice)
serverTraffic.monthly.WithLabelValues(s.Name, location.Name, s.ServerType.Name).Set(monthlyPrice)
}
return nil
}