mirror of
https://github.com/jangraefen/hcloud-pricing-exporter
synced 2024-11-10 05:54:15 +00:00
44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
package fetcher
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/hetznercloud/hcloud-go/hcloud"
|
|
)
|
|
|
|
var _ Fetcher = &serverTraffic{}
|
|
|
|
// NewServerTraffic creates a new fetcher that will collect pricing information on server traffic.
|
|
func NewServerTraffic(pricing *PriceProvider) Fetcher {
|
|
return &serverTraffic{newBase(pricing, "server_traffic", "location", "type")}
|
|
}
|
|
|
|
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 := int(s.OutgoingTraffic) - int(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
|
|
}
|
|
|
|
monthlyPrice := math.Ceil(float64(additionalTraffic)/sizeTB) * serverTraffic.pricing.Traffic()
|
|
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
|
|
}
|